1865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount/*
2865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * Copyright (C) 2008 The Android Open Source Project
3865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount *
4865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * Licensed under the Apache License, Version 2.0 (the "License");
5865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * you may not use this file except in compliance with the License.
6865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * You may obtain a copy of the License at
7865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount *
8865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount *      http://www.apache.org/licenses/LICENSE-2.0
9865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount *
10865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * Unless required by applicable law or agreed to in writing, software
11865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * distributed under the License is distributed on an "AS IS" BASIS,
12865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * See the License for the specific language governing permissions and
14865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * limitations under the License.
15865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount */
16865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
17865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountpackage com.android.providers.userdictionary;
18865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
19865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
20c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasaniimport java.util.HashMap;
21c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani
229d0d421e99b46318170660a14c794f6c9b5e59d6Christopher Tateimport android.app.backup.BackupManager;
23865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.content.ContentProvider;
24865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.content.ContentUris;
25865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.content.ContentValues;
26865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.content.Context;
27865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.content.UriMatcher;
28865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.database.Cursor;
29865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.database.SQLException;
30865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.database.sqlite.SQLiteDatabase;
31865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.database.sqlite.SQLiteOpenHelper;
32865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.database.sqlite.SQLiteQueryBuilder;
33865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.net.Uri;
34865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.provider.UserDictionary;
35865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.provider.UserDictionary.Words;
36865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.text.TextUtils;
37865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountimport android.util.Log;
38865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
39865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount/**
40865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount * Provides access to a database of user defined words. Each item has a word and a frequency.
41865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount */
42865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccountpublic class UserDictionaryProvider extends ContentProvider {
43865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
44bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard    /**
45bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     * DB versions are as follow:
46bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     *
47bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     * Version 1:
48bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     *   Up to IceCreamSandwich 4.0.3 - API version 15
49bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     *   Contient ID (INTEGER PRIMARY KEY), WORD (TEXT), FREQUENCY (INTEGER),
50bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     *   LOCALE (TEXT), APP_ID (INTEGER).
51bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     *
52bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     * Version 2:
53bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     *   From IceCreamSandwich, 4.1 - API version 16
54bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     *   Adds SHORTCUT (TEXT).
55bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard     */
56bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard
57865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final String AUTHORITY = UserDictionary.AUTHORITY;
58865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
59865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final String TAG = "UserDictionaryProvider";
60865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
61865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final Uri CONTENT_URI = UserDictionary.CONTENT_URI;
62865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
63865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final String DATABASE_NAME = "user_dict.db";
64bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard    private static final int DATABASE_VERSION = 2;
65865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
66865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final String USERDICT_TABLE_NAME = "words";
67865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
68865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static HashMap<String, String> sDictProjectionMap;
69865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
70865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final UriMatcher sUriMatcher;
71865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
72865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final int WORDS = 1;
73865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
74865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static final int WORD_ID = 2;
75c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani
76c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani    private BackupManager mBackupManager;
77c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani
78865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    /**
79865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount     * This class helps open, create, and upgrade the database file.
80865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount     */
81865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private static class DatabaseHelper extends SQLiteOpenHelper {
82865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
83865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        DatabaseHelper(Context context) {
84865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            super(context, DATABASE_NAME, null, DATABASE_VERSION);
85865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
86865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
87865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        @Override
88865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        public void onCreate(SQLiteDatabase db) {
89865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            db.execSQL("CREATE TABLE " + USERDICT_TABLE_NAME + " ("
90865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount                    + Words._ID + " INTEGER PRIMARY KEY,"
91865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount                    + Words.WORD + " TEXT,"
92865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount                    + Words.FREQUENCY + " INTEGER,"
93865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount                    + Words.LOCALE + " TEXT,"
94ccfedce270d66beb36c685cb496245c699917832Fabrice Di Meglio                    + Words.APP_ID + " INTEGER,"
95ccfedce270d66beb36c685cb496245c699917832Fabrice Di Meglio                    + Words.SHORTCUT + " TEXT"
96865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount                    + ");");
97865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
98865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
99865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        @Override
100865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
101bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard            if (oldVersion == 1 && newVersion == 2) {
102bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                Log.i(TAG, "Upgrading database from version " + oldVersion
103bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                        + " to version 2: adding " + Words.SHORTCUT + " column");
104bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                db.execSQL("ALTER TABLE " + USERDICT_TABLE_NAME
105bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                        + " ADD " + Words.SHORTCUT + " TEXT;");
106bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard            } else {
107bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
108bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                        + newVersion + ", which will destroy all old data");
109bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                db.execSQL("DROP TABLE IF EXISTS " + USERDICT_TABLE_NAME);
110bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard                onCreate(db);
111bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard            }
112865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
113865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
114865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
115865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    private DatabaseHelper mOpenHelper;
116865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
117865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    @Override
118865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    public boolean onCreate() {
119865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        mOpenHelper = new DatabaseHelper(getContext());
120c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani        mBackupManager = new BackupManager(getContext());
121865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        return true;
122865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
123865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
124865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    @Override
125865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
126865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            String sortOrder) {
127865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
128865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
129865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        switch (sUriMatcher.match(uri)) {
130865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORDS:
131865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            qb.setTables(USERDICT_TABLE_NAME);
132865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            qb.setProjectionMap(sDictProjectionMap);
133865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            break;
134865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
135865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORD_ID:
136865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            qb.setTables(USERDICT_TABLE_NAME);
137865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            qb.setProjectionMap(sDictProjectionMap);
138865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            qb.appendWhere("_id" + "=" + uri.getPathSegments().get(1));
139865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            break;
140865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
141865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        default:
142865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            throw new IllegalArgumentException("Unknown URI " + uri);
143865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
144865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
145865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        // If no sort order is specified use the default
146865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        String orderBy;
147865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        if (TextUtils.isEmpty(sortOrder)) {
148865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            orderBy = Words.DEFAULT_SORT_ORDER;
149865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        } else {
150865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            orderBy = sortOrder;
151865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
152865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
153865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        // Get the database and run the query
154865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
155865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
156865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
157865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        // Tell the cursor what uri to watch, so it knows when its source data changes
158865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        c.setNotificationUri(getContext().getContentResolver(), uri);
159865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        return c;
160865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
161865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
162865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    @Override
163865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    public String getType(Uri uri) {
164865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        switch (sUriMatcher.match(uri)) {
165865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORDS:
166865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            return Words.CONTENT_TYPE;
167865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
168865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORD_ID:
169865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            return Words.CONTENT_ITEM_TYPE;
170865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
171865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        default:
172865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            throw new IllegalArgumentException("Unknown URI " + uri);
173865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
174865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
175865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
176865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    @Override
177865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    public Uri insert(Uri uri, ContentValues initialValues) {
178865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        // Validate the requested uri
179865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        if (sUriMatcher.match(uri) != WORDS) {
180865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            throw new IllegalArgumentException("Unknown URI " + uri);
181865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
182865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
183865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        ContentValues values;
184865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        if (initialValues != null) {
185865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            values = new ContentValues(initialValues);
186865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        } else {
187865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            values = new ContentValues();
188865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
189865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
190865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        if (values.containsKey(Words.WORD) == false) {
191865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            throw new SQLException("Word must be specified");
192865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
193865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
194865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        if (values.containsKey(Words.FREQUENCY) == false) {
195865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            values.put(Words.FREQUENCY, "1");
196865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
197865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
198865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        if (values.containsKey(Words.LOCALE) == false) {
199865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            values.put(Words.LOCALE, (String) null);
200865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
201bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard
202bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard        if (values.containsKey(Words.SHORTCUT) == false) {
203bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard            values.put(Words.SHORTCUT, (String) null);
204bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard        }
205bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard
206865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        values.put(Words.APP_ID, 0);
207865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
208865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
209865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        long rowId = db.insert(USERDICT_TABLE_NAME, Words.WORD, values);
210865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        if (rowId > 0) {
211865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            Uri wordUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI, rowId);
212865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            getContext().getContentResolver().notifyChange(wordUri, null);
213c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani            mBackupManager.dataChanged();
214865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            return wordUri;
215865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
216865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
217865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        throw new SQLException("Failed to insert row into " + uri);
218865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
219865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
220865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    @Override
221865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    public int delete(Uri uri, String where, String[] whereArgs) {
222865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
223865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        int count;
224865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        switch (sUriMatcher.match(uri)) {
225865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORDS:
226865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            count = db.delete(USERDICT_TABLE_NAME, where, whereArgs);
227865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            break;
228865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
229865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORD_ID:
230865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            String wordId = uri.getPathSegments().get(1);
231865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            count = db.delete(USERDICT_TABLE_NAME, Words._ID + "=" + wordId
232865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
233865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            break;
234865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
235865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        default:
236865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            throw new IllegalArgumentException("Unknown URI " + uri);
237865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
238865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
239865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        getContext().getContentResolver().notifyChange(uri, null);
240c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani        mBackupManager.dataChanged();
241865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        return count;
242865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
243865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
244865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    @Override
245865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
246865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
247865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        int count;
248865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        switch (sUriMatcher.match(uri)) {
249865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORDS:
250865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            count = db.update(USERDICT_TABLE_NAME, values, where, whereArgs);
251865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            break;
252865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
253865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        case WORD_ID:
254865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            String wordId = uri.getPathSegments().get(1);
255865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            count = db.update(USERDICT_TABLE_NAME, values, Words._ID + "=" + wordId
256865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
257865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            break;
258865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
259865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        default:
260865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount            throw new IllegalArgumentException("Unknown URI " + uri);
261865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        }
262865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
263865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        getContext().getContentResolver().notifyChange(uri, null);
264c5885c466b80dfda79a364e5623348469f8eb8dbAmith Yamasani        mBackupManager.dataChanged();
265865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        return count;
266865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
267865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
268865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    static {
269865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
270865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sUriMatcher.addURI(AUTHORITY, "words", WORDS);
271865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sUriMatcher.addURI(AUTHORITY, "words/#", WORD_ID);
272865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount
273865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sDictProjectionMap = new HashMap<String, String>();
274865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sDictProjectionMap.put(Words._ID, Words._ID);
275865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sDictProjectionMap.put(Words.WORD, Words.WORD);
276865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sDictProjectionMap.put(Words.FREQUENCY, Words.FREQUENCY);
277865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sDictProjectionMap.put(Words.LOCALE, Words.LOCALE);
278865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount        sDictProjectionMap.put(Words.APP_ID, Words.APP_ID);
279bf063f25421b08f118524890914fbdbfa63dbdafJean Chalard        sDictProjectionMap.put(Words.SHORTCUT, Words.SHORTCUT);
280865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount    }
281865b14b5faf8df8c090efa181ee5c5641f362fcandroid-build SharedAccount}
282