summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/userdictionary/UserDictionaryProvider.java
blob: e65304799ed7da6ad7ab4755113d7e0f406515b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.userdictionary;

import java.util.List;

import android.app.backup.BackupManager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.Binder;
import android.os.Process;
import android.provider.UserDictionary;
import android.provider.UserDictionary.Words;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.textservice.SpellCheckerInfo;
import android.view.textservice.TextServicesManager;

/**
 * Provides access to a database of user defined words. Each item has a word and a frequency.
 */
public class UserDictionaryProvider extends ContentProvider {

    /**
     * DB versions are as follow:
     *
     * Version 1:
     *   Up to IceCreamSandwich 4.0.3 - API version 15
     *   Contient ID (INTEGER PRIMARY KEY), WORD (TEXT), FREQUENCY (INTEGER),
     *   LOCALE (TEXT), APP_ID (INTEGER).
     *
     * Version 2:
     *   From IceCreamSandwich, 4.1 - API version 16
     *   Adds SHORTCUT (TEXT).
     */

    private static final String AUTHORITY = UserDictionary.AUTHORITY;

    private static final String TAG = "UserDictionaryProvider";

    private static final String DATABASE_NAME = "user_dict.db";
    private static final int DATABASE_VERSION = 2;

    private static final String USERDICT_TABLE_NAME = "words";

    private static ArrayMap<String, String> sDictProjectionMap;

    private static final UriMatcher sUriMatcher;

    private static final int WORDS = 1;

    private static final int WORD_ID = 2;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, "words", WORDS);
        sUriMatcher.addURI(AUTHORITY, "words/#", WORD_ID);

        sDictProjectionMap = new ArrayMap<>();
        sDictProjectionMap.put(Words._ID, Words._ID);
        sDictProjectionMap.put(Words.WORD, Words.WORD);
        sDictProjectionMap.put(Words.FREQUENCY, Words.FREQUENCY);
        sDictProjectionMap.put(Words.LOCALE, Words.LOCALE);
        sDictProjectionMap.put(Words.APP_ID, Words.APP_ID);
        sDictProjectionMap.put(Words.SHORTCUT, Words.SHORTCUT);
    }

    private BackupManager mBackupManager;
    private InputMethodManager mImeManager;
    private TextServicesManager mTextServiceManager;

    /**
     * This class helps open, create, and upgrade the database file.
     */
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + USERDICT_TABLE_NAME + " ("
                    + Words._ID + " INTEGER PRIMARY KEY,"
                    + Words.WORD + " TEXT,"
                    + Words.FREQUENCY + " INTEGER,"
                    + Words.LOCALE + " TEXT,"
                    + Words.APP_ID + " INTEGER,"
                    + Words.SHORTCUT + " TEXT"
                    + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (oldVersion == 1 && newVersion == 2) {
                Log.i(TAG, "Upgrading database from version " + oldVersion
                        + " to version 2: adding " + Words.SHORTCUT + " column");
                db.execSQL("ALTER TABLE " + USERDICT_TABLE_NAME
                        + " ADD " + Words.SHORTCUT + " TEXT;");
            } else {
                Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS " + USERDICT_TABLE_NAME);
                onCreate(db);
            }
        }
    }

    private DatabaseHelper mOpenHelper;

    @Override
    public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext());
        mBackupManager = new BackupManager(getContext());
        mImeManager = getContext().getSystemService(InputMethodManager.class);
        mTextServiceManager = getContext().getSystemService(TextServicesManager.class);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        switch (sUriMatcher.match(uri)) {
            case WORDS:
                qb.setTables(USERDICT_TABLE_NAME);
                qb.setProjectionMap(sDictProjectionMap);
                break;

            case WORD_ID:
                qb.setTables(USERDICT_TABLE_NAME);
                qb.setProjectionMap(sDictProjectionMap);
                qb.appendWhere("_id" + "=" + uri.getPathSegments().get(1));
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // Only the enabled IMEs and spell checkers can access this provider.
        if (!canCallerAccessUserDictionary()) {
            return getEmptyCursorOrThrow(projection);
        }

        // If no sort order is specified use the default
        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = Words.DEFAULT_SORT_ORDER;
        } else {
            orderBy = sortOrder;
        }

        // Get the database and run the query
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

        // Tell the cursor what uri to watch, so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
            case WORDS:
                return Words.CONTENT_TYPE;

            case WORD_ID:
                return Words.CONTENT_ITEM_TYPE;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        // Validate the requested uri
        if (sUriMatcher.match(uri) != WORDS) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // Only the enabled IMEs and spell checkers can access this provider.
        if (!canCallerAccessUserDictionary()) {
            return null;
        }

        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }

        if (!values.containsKey(Words.WORD)) {
            throw new SQLException("Word must be specified");
        }

        if (!values.containsKey(Words.FREQUENCY)) {
            values.put(Words.FREQUENCY, "1");
        }

        if (!values.containsKey(Words.LOCALE)) {
            values.put(Words.LOCALE, (String) null);
        }

        if (!values.containsKey(Words.SHORTCUT)) {
            values.put(Words.SHORTCUT, (String) null);
        }

        values.put(Words.APP_ID, 0);

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(USERDICT_TABLE_NAME, Words.WORD, values);
        if (rowId > 0) {
            Uri wordUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(wordUri, null);
            mBackupManager.dataChanged();
            return wordUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
            case WORDS:
                count = db.delete(USERDICT_TABLE_NAME, where, whereArgs);
                break;

            case WORD_ID:
                String wordId = uri.getPathSegments().get(1);
                count = db.delete(USERDICT_TABLE_NAME, Words._ID + "=" + wordId
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // Only the enabled IMEs and spell checkers can access this provider.
        if (!canCallerAccessUserDictionary()) {
            return 0;
        }

        getContext().getContentResolver().notifyChange(uri, null);
        mBackupManager.dataChanged();
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
            case WORDS:
                count = db.update(USERDICT_TABLE_NAME, values, where, whereArgs);
                break;

            case WORD_ID:
                String wordId = uri.getPathSegments().get(1);
                count = db.update(USERDICT_TABLE_NAME, values, Words._ID + "=" + wordId
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // Only the enabled IMEs and spell checkers can access this provider.
        if (!canCallerAccessUserDictionary()) {
            return 0;
        }

        getContext().getContentResolver().notifyChange(uri, null);
        mBackupManager.dataChanged();
        return count;
    }

    private boolean canCallerAccessUserDictionary() {
        final int callingUid = Binder.getCallingUid();

        if (callingUid == Process.SYSTEM_UID
                || callingUid == Process.ROOT_UID
                || callingUid == Process.myUid()) {
            return true;
        }

        String callingPackage = getCallingPackage();

        List<InputMethodInfo> imeInfos = mImeManager.getEnabledInputMethodList();
        if (imeInfos != null) {
            final int imeInfoCount = imeInfos.size();
            for (int i = 0; i < imeInfoCount; i++) {
                InputMethodInfo imeInfo = imeInfos.get(i);
                if (imeInfo.getServiceInfo().applicationInfo.uid == callingUid
                        && imeInfo.getPackageName().equals(callingPackage)) {
                    return true;
                }
            }
        }

        SpellCheckerInfo[] scInfos = mTextServiceManager.getEnabledSpellCheckers();
        if (scInfos != null) {
            for (SpellCheckerInfo scInfo : scInfos) {
                if (scInfo.getServiceInfo().applicationInfo.uid == callingUid
                        && scInfo.getPackageName().equals(callingPackage)) {
                    return true;
                }
            }
        }

        return false;
    }

    private static Cursor getEmptyCursorOrThrow(String[] projection) {
        if (projection != null) {
            for (String column : projection) {
                if (sDictProjectionMap.get(column) == null) {
                    throw new IllegalArgumentException("Unknown column: " + column);
                }
            }
        } else {
            final int columnCount = sDictProjectionMap.size();
            projection = new String[columnCount];
            for (int i = 0; i < columnCount; i++) {
                projection[i] = sDictProjectionMap.keyAt(i);
            }
        }

        return new MatrixCursor(projection, 0);
    }
}