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.DeviceEvent;
26 import android.inputmethodservice.cts.common.EventProviderConstants.EventTableConstants;
27 import android.inputmethodservice.cts.db.Database;
28 import android.inputmethodservice.cts.db.Table;
29 import android.net.Uri;
30 import android.util.Log;
31 
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 
39 /**
40  * IME event content provider.
41  */
42 public final class EventProvider extends ContentProvider {
43 
44     private static final String TAG = EventProvider.class.getSimpleName();
45     private static final boolean DEBUG = false;
46 
47     private static final String DB_NAME = "database";
48     private static final int DB_VERSION = 1;
49 
50     private UriHelper.Factory mUriFactory;
51     private Database mDatabase;
52 
53     @Override
onCreate()54     public boolean onCreate() {
55         mUriFactory = UriHelper.Factory.builder()
56                 .addUri(AUTHORITY, EventTableConstants.NAME, EventTableConstants.TYPE_DIR)
57                 .addUri(AUTHORITY, EventTableConstants.NAME + "/#", EventTableConstants.TYPE_ITEM)
58                 .build();
59         mDatabase = new Database(getContext(), DB_NAME, DB_VERSION) {
60             @Override
61             @NonNull
62             protected List<Table> getTables() {
63                 return Collections.singletonList(DeviceEvent.TABLE);
64             }
65         };
66         return true;
67     }
68 
69     @Override
query(@onNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String orderBy)70     public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
71             @Nullable String[] selectionArgs, @Nullable 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.mTable, 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
insert(@onNull Uri uri, @Nullable ContentValues values)93     public Uri insert(@NonNull Uri uri, @Nullable 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.mTable, 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
delete(@onNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs)108     public int delete(@NonNull Uri uri, @Nullable String selection,
109             @Nullable 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.mTable, 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
update(@onNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs)128     public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
129             @Nullable 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.mTable, 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
getType(@onNull Uri uri)150     public String getType(@NonNull Uri uri) {
151         return mUriFactory.getTypeOf(uri);
152     }
153 
154     @Override
shutdown()155     public void shutdown() {
156         super.shutdown();
157         mDatabase.close();
158         mDatabase = null;
159         mUriFactory = null;
160     }
161 }
162