Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.provider;
     17 
     18 import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
     19 
     20 import android.annotation.NonNull;
     21 import android.annotation.Nullable;
     22 import android.content.ContentProvider;
     23 import android.content.ContentValues;
     24 import android.database.Cursor;
     25 import android.database.MatrixCursor;
     26 import android.graphics.Typeface;
     27 import android.net.Uri;
     28 import android.os.ParcelFileDescriptor;
     29 
     30 import java.io.File;
     31 import java.io.IOException;
     32 
     33 /**
     34  * Provides a test Content Provider implementing {@link FontsContract}.
     35  */
     36 public class TestFontsProvider extends ContentProvider {
     37     static final String AUTHORITY = "android.provider.TestFontsProvider";
     38     static final int TTC_INDEX = 2;
     39     static final String VARIATION_SETTINGS = "'wdth' 1";
     40     static final int NORMAL_WEIGHT = 400;
     41     static final boolean ITALIC = false;
     42 
     43     private ParcelFileDescriptor mPfd;
     44     private boolean mReturnAllFields = true;
     45     private int mResultCode = FontsContract.Columns.RESULT_CODE_OK;
     46     private MatrixCursor mCustomCursor = null;
     47 
     48     /**
     49      * Used by tests to modify the result code that should be returned.
     50      */
     51     void setResultCode(int resultCode) {
     52         mResultCode = resultCode;
     53     }
     54 
     55     /**
     56      * Used by tests to switch whether all fields should be returned or not.
     57      */
     58     void setReturnAllFields(boolean returnAllFields) {
     59         mReturnAllFields = returnAllFields;
     60     }
     61 
     62     /**
     63      * Used by tests to control what values are returned.
     64      */
     65     void setCustomCursor(MatrixCursor cursor) {
     66         mCustomCursor = cursor;
     67     }
     68 
     69     @Override
     70     public boolean onCreate() {
     71         mPfd = createFontFile();
     72         return true;
     73     }
     74 
     75     @Override
     76     public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
     77             @Nullable String[] selectionArgs, @Nullable String sortOrder) {
     78         if (mCustomCursor != null) {
     79             return mCustomCursor;
     80         }
     81         MatrixCursor cursor;
     82         if (mReturnAllFields) {
     83             cursor = new MatrixCursor(new String[] { FontsContract.Columns._ID,
     84                     FontsContract.Columns.TTC_INDEX, FontsContract.Columns.VARIATION_SETTINGS,
     85                     FontsContract.Columns.WEIGHT, FontsContract.Columns.ITALIC,
     86                     FontsContract.Columns.RESULT_CODE });
     87             cursor.addRow(new Object[] { 1, TTC_INDEX, VARIATION_SETTINGS, 400, 0, mResultCode });
     88         } else {
     89             cursor = new MatrixCursor(new String[] { FontsContract.Columns._ID });
     90             cursor.addRow(new Object[] { 1 });
     91         }
     92         return cursor;
     93     }
     94 
     95     @Override
     96     public ParcelFileDescriptor openFile(Uri uri, String mode) {
     97         try {
     98             return mPfd.dup();
     99         } catch (IOException e) {
    100             e.printStackTrace();
    101         }
    102         return null;
    103     }
    104 
    105     @Override
    106     public String getType(@NonNull Uri uri) {
    107         return "application/x-font-ttf";
    108     }
    109 
    110     @Override
    111     public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
    112         return null;
    113     }
    114 
    115     @Override
    116     public int delete(@NonNull Uri uri, @Nullable String selection,
    117             @Nullable String[] selectionArgs) {
    118         return 0;
    119     }
    120 
    121     @Override
    122     public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
    123             @Nullable String[] selectionArgs) {
    124         return 0;
    125     }
    126 
    127     private ParcelFileDescriptor createFontFile() {
    128         try {
    129             final File file = new File(getContext().getCacheDir(), "font.ttf");
    130             file.getParentFile().mkdirs();
    131             file.createNewFile();
    132             return ParcelFileDescriptor.open(file, MODE_READ_ONLY);
    133         } catch (IOException e) {
    134             e.printStackTrace();
    135         }
    136         return null;
    137     }
    138 }
    139