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 
     17 package android.inputmethodservice.cts.provider;
     18 
     19 import static android.inputmethodservice.cts.common.EventProviderConstants.AUTHORITY;
     20 
     21 import android.content.ContentProvider;
     22 import android.content.ContentUris;
     23 import android.content.ContentValues;
     24 import android.database.Cursor;
     25 import android.inputmethodservice.cts.common.EventProviderConstants.EventTableConstants;
     26 import android.inputmethodservice.cts.db.Database;
     27 import android.inputmethodservice.cts.db.Table;
     28 import android.inputmethodservice.cts.DeviceEvent;
     29 import android.net.Uri;
     30 import androidx.annotation.NonNull;
     31 import androidx.annotation.Nullable;
     32 import android.util.Log;
     33 
     34 import java.util.Arrays;
     35 import java.util.Collections;
     36 import java.util.List;
     37 
     38 /**
     39  * IME event content provider.
     40  */
     41 public final class EventProvider extends ContentProvider {
     42 
     43     private static final String TAG = EventProvider.class.getSimpleName();
     44     private static final boolean DEBUG = false;
     45 
     46     private static final String DB_NAME = "database";
     47     private static final int DB_VERSION = 1;
     48 
     49     private UriHelper.Factory mUriFactory;
     50     private Database mDatabase;
     51 
     52     @Override
     53     public boolean onCreate() {
     54         mUriFactory = UriHelper.Factory.builder()
     55                 .addUri(AUTHORITY, EventTableConstants.NAME, EventTableConstants.TYPE_DIR)
     56                 .addUri(AUTHORITY, EventTableConstants.NAME + "/#", EventTableConstants.TYPE_ITEM)
     57                 .build();
     58         mDatabase = new Database(getContext(), DB_NAME, DB_VERSION) {
     59             @Override
     60             @NonNull
     61             protected List<Table> getTables() {
     62                 return Collections.singletonList(DeviceEvent.TABLE);
     63             }
     64         };
     65         return true;
     66     }
     67 
     68     @Override
     69     public Cursor query(@NonNull final Uri uri, @Nullable final String[] projection,
     70             final @Nullable String selection, @Nullable final String[] selectionArgs,
     71             @Nullable final String orderBy) {
     72         final UriHelper uriHelper = mUriFactory.newInstance(uri);
     73         if (DEBUG) {
     74             Log.d(TAG, "query:"
     75                     + " uri=" + uri
     76                     + " projection=" + Arrays.toString(projection)
     77                     + " selection=" + uriHelper.buildSelection(selection)
     78                     + " selectionArgs=" + Arrays.toString(
     79                             uriHelper.buildSelectionArgs(selectionArgs))
     80                     + " orderBy=" + orderBy);
     81         }
     82         final Cursor cursor = mDatabase.query(
     83                 uriHelper.table, projection, uriHelper.buildSelection(selection),
     84                 uriHelper.buildSelectionArgs(selectionArgs), orderBy);
     85         if (DEBUG) {
     86             Log.d(TAG, "  query.count=" + cursor.getCount());
     87         }
     88         cursor.setNotificationUri(getContext().getContentResolver(), uri);
     89         return cursor;
     90     }
     91 
     92     @Override
     93     public Uri insert(@NonNull final Uri uri, @Nullable final ContentValues values) {
     94         final UriHelper uriHelper = mUriFactory.newInstance(uri);
     95         if (DEBUG) {
     96             Log.d(TAG, "insert: uri=" + uri + " values={" + values + "}");
     97         }
     98         final long rowId = mDatabase.insert(uriHelper.table, values);
     99         final Uri insertedUri = ContentUris.withAppendedId(uri, rowId);
    100         if (DEBUG) {
    101             Log.d(TAG, "  insert.uri=" + insertedUri);
    102         }
    103         getContext().getContentResolver().notifyChange(insertedUri, null);
    104         return insertedUri;
    105     }
    106 
    107     @Override
    108     public int delete(@NonNull final Uri uri, @Nullable final String selection,
    109             @Nullable final String[] selectionArgs) {
    110         final UriHelper uriHelper = mUriFactory.newInstance(uri);
    111         if (DEBUG) {
    112             Log.d(TAG, "delete:"
    113                     + " uri=" + uri
    114                     + " selection=" + uriHelper.buildSelection(selection)
    115                     + " selectionArgs=" + Arrays.toString(
    116                             uriHelper.buildSelectionArgs(selectionArgs)));
    117         }
    118         final int count = mDatabase.delete(uriHelper.table, uriHelper.buildSelection(selection),
    119                 uriHelper.buildSelectionArgs(selectionArgs));
    120         if (DEBUG) {
    121             Log.d(TAG, "  delete.count=" + count);
    122         }
    123         getContext().getContentResolver().notifyChange(uri, null);
    124         return count;
    125     }
    126 
    127     @Override
    128     public int update(@NonNull final Uri uri, @Nullable final ContentValues values,
    129             final @Nullable String selection, @Nullable final String[] selectionArgs) {
    130         final UriHelper uriHelper = mUriFactory.newInstance(uri);
    131         if (DEBUG) {
    132             Log.d(TAG, "update:"
    133                     + " uri=" + uri
    134                     + " values={" + values + "}"
    135                     + " selection=" + uriHelper.buildSelection(selection)
    136                     + " selectionArgs=" + Arrays.toString(
    137                             uriHelper.buildSelectionArgs(selectionArgs)));
    138         }
    139         final int count = mDatabase.update(uriHelper.table, values,
    140                 uriHelper.buildSelection(selection), uriHelper.buildSelectionArgs(selectionArgs));
    141         if (DEBUG) {
    142             Log.d(TAG, "  update.count=" + count);
    143         }
    144         getContext().getContentResolver().notifyChange(uri, null);
    145         return count;
    146     }
    147 
    148     @Override
    149     @Nullable
    150     public String getType(@NonNull final Uri uri) {
    151         return mUriFactory.getTypeOf(uri);
    152     }
    153 
    154     @Override
    155     public void shutdown() {
    156         super.shutdown();
    157         mDatabase.close();
    158         mDatabase = null;
    159         mUriFactory = null;
    160     }
    161 }
    162