summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmith Yamasani <yamasani@google.com>2009-08-05 18:55:23 -0700
committerAmith Yamasani <yamasani@google.com>2009-08-06 15:00:38 -0700
commitc5885c466b80dfda79a364e5623348469f8eb8db (patch)
treea9343ae16dea7289a7acd83d5cae06119d4efc7a
parentab34718b21f64ae279177dc1424d74d5e8fc3d2f (diff)
downloadandroid_packages_providers_UserDictionaryProvider-c5885c466b80dfda79a364e5623348469f8eb8db.tar.gz
android_packages_providers_UserDictionaryProvider-c5885c466b80dfda79a364e5623348469f8eb8db.tar.bz2
android_packages_providers_UserDictionaryProvider-c5885c466b80dfda79a364e5623348469f8eb8db.zip
Backup user dictionary.
-rw-r--r--AndroidManifest.xml4
-rw-r--r--src/com/android/providers/userdictionary/DictionaryBackupAgent.java246
-rw-r--r--src/com/android/providers/userdictionary/UserDictionaryProvider.java13
3 files changed, 259 insertions, 4 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 9585043..a53e4f2 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -18,7 +18,9 @@
package="com.android.providers.userdictionary"
android:sharedUserId="android.uid.shared">
- <application android:process="android.process.acore">
+ <application android:process="android.process.acore"
+ android:backupAgent="DictionaryBackupAgent"
+ >
<provider android:name="UserDictionaryProvider" android:authorities="user_dictionary"
android:syncable="false" android:multiprocess="false"
android:readPermission="android.permission.READ_USER_DICTIONARY"
diff --git a/src/com/android/providers/userdictionary/DictionaryBackupAgent.java b/src/com/android/providers/userdictionary/DictionaryBackupAgent.java
new file mode 100644
index 0000000..5eed461
--- /dev/null
+++ b/src/com/android/providers/userdictionary/DictionaryBackupAgent.java
@@ -0,0 +1,246 @@
+/*
+ * Copyright (C) 2009 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.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.EOFException;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.StringTokenizer;
+import java.util.zip.CRC32;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+import android.backup.BackupDataInput;
+import android.backup.BackupDataOutput;
+import android.backup.BackupHelperAgent;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.ParcelFileDescriptor;
+import android.provider.UserDictionary.Words;
+import android.text.TextUtils;
+import android.util.Log;
+
+/**
+ * Performs backup and restore of the User Dictionary.
+ */
+public class DictionaryBackupAgent extends BackupHelperAgent {
+
+ private static final String KEY_DICTIONARY = "userdictionary";
+
+ private static final int STATE_DICTIONARY = 0;
+ private static final int STATE_SIZE = 1;
+
+ private static final String SEPARATOR = "|";
+
+ private static final byte[] EMPTY_DATA = new byte[0];
+
+ private static final String TAG = "DictionaryBackupAgent";
+
+ private static final int COLUMN_WORD = 1;
+ private static final int COLUMN_FREQUENCY = 2;
+ private static final int COLUMN_LOCALE = 3;
+ private static final int COLUMN_APPID = 4;
+
+ private static final String[] PROJECTION = {
+ Words._ID,
+ Words.WORD,
+ Words.FREQUENCY,
+ Words.LOCALE,
+ Words.APP_ID
+ };
+
+ @Override
+ public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
+ ParcelFileDescriptor newState) throws IOException {
+
+ byte[] userDictionaryData = getDictionary();
+
+ long[] stateChecksums = readOldChecksums(oldState);
+
+ stateChecksums[STATE_DICTIONARY] =
+ writeIfChanged(stateChecksums[STATE_DICTIONARY], KEY_DICTIONARY,
+ userDictionaryData, data);
+
+ writeNewChecksums(stateChecksums, newState);
+ }
+
+ @Override
+ public void onRestore(BackupDataInput data, int appVersionCode,
+ ParcelFileDescriptor newState) throws IOException {
+
+ while (data.readNextHeader()) {
+ final String key = data.getKey();
+ final int size = data.getDataSize();
+ if (KEY_DICTIONARY.equals(key)) {
+ restoreDictionary(data, Words.CONTENT_URI);
+ } else {
+ data.skipEntityData();
+ }
+ }
+ }
+
+ private long[] readOldChecksums(ParcelFileDescriptor oldState) throws IOException {
+ long[] stateChecksums = new long[STATE_SIZE];
+
+ DataInputStream dataInput = new DataInputStream(
+ new FileInputStream(oldState.getFileDescriptor()));
+ for (int i = 0; i < STATE_SIZE; i++) {
+ try {
+ stateChecksums[i] = dataInput.readLong();
+ } catch (EOFException eof) {
+ break;
+ }
+ }
+ dataInput.close();
+ return stateChecksums;
+ }
+
+ private void writeNewChecksums(long[] checksums, ParcelFileDescriptor newState)
+ throws IOException {
+ DataOutputStream dataOutput = new DataOutputStream(
+ new FileOutputStream(newState.getFileDescriptor()));
+ for (int i = 0; i < STATE_SIZE; i++) {
+ dataOutput.writeLong(checksums[i]);
+ }
+ dataOutput.close();
+ }
+
+ private long writeIfChanged(long oldChecksum, String key, byte[] data,
+ BackupDataOutput output) {
+ CRC32 checkSummer = new CRC32();
+ checkSummer.update(data);
+ long newChecksum = checkSummer.getValue();
+ if (oldChecksum == newChecksum) {
+ return oldChecksum;
+ }
+ try {
+ output.writeEntityHeader(key, data.length);
+ output.writeEntityData(data, data.length);
+ } catch (IOException ioe) {
+ // Bail
+ }
+ return newChecksum;
+ }
+
+ private byte[] getDictionary() {
+ Cursor cursor = getContentResolver().query(Words.CONTENT_URI, PROJECTION,
+ null, null, Words.WORD);
+ if (!cursor.moveToFirst()) {
+ Log.e(TAG, "Couldn't read from the cursor");
+ return EMPTY_DATA;
+ }
+ byte[] sizeBytes = new byte[4];
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(cursor.getCount() * 10);
+ try {
+ GZIPOutputStream gzip = new GZIPOutputStream(baos);
+ while (!cursor.isAfterLast()) {
+ String name = cursor.getString(COLUMN_WORD);
+ int frequency = cursor.getInt(COLUMN_FREQUENCY);
+ String locale = cursor.getString(COLUMN_LOCALE);
+ int appId = cursor.getInt(COLUMN_APPID);
+ String out = name + "|" + frequency + "|" + locale + "|" + appId;
+ byte[] line = out.getBytes();
+ writeInt(sizeBytes, 0, line.length);
+ gzip.write(sizeBytes);
+ gzip.write(line);
+ cursor.moveToNext();
+ }
+ cursor.close();
+ gzip.finish();
+ } catch (IOException ioe) {
+ Log.e(TAG, "Couldn't compress the dictionary:\n" + ioe);
+ return EMPTY_DATA;
+ }
+ return baos.toByteArray();
+ }
+
+ private void restoreDictionary(BackupDataInput data, Uri contentUri) {
+ ContentValues cv = new ContentValues(2);
+ byte[] dictCompressed = new byte[data.getDataSize()];
+ byte[] dictionary = null;
+ try {
+ data.readEntityData(dictCompressed, 0, dictCompressed.length);
+ GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(dictCompressed));
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] tempData = new byte[1024];
+ int got;
+ while ((got = gzip.read(tempData)) > 0) {
+ baos.write(tempData, 0, got);
+ }
+ gzip.close();
+ dictionary = baos.toByteArray();
+ } catch (IOException ioe) {
+ Log.e(TAG, "Couldn't read and uncompress entity data:\n" + ioe);
+ return;
+ }
+ int pos = 0;
+ while (pos < dictionary.length) {
+ int length = readInt(dictionary, pos);
+ pos += 4;
+ String line = new String(dictionary, pos, length);
+ pos += length;
+ StringTokenizer st = new StringTokenizer(line, SEPARATOR);
+ String word = st.nextToken();
+ String frequency = st.nextToken();
+ String locale = null;
+ String appid = null;
+ if (st.hasMoreTokens()) locale = st.nextToken();
+ if (st.hasMoreTokens()) appid = st.nextToken();
+ int frequencyInt = Integer.parseInt(frequency);
+ int appidInt = appid != null? Integer.parseInt(appid) : 0;
+
+ if (!TextUtils.isEmpty(frequency)) {
+ cv.clear();
+ cv.put(Words.WORD, word);
+ cv.put(Words.FREQUENCY, frequencyInt);
+ cv.put(Words.LOCALE, locale);
+ cv.put(Words.APP_ID, appidInt);
+ getContentResolver().insert(contentUri, cv);
+ }
+ }
+ }
+
+ /**
+ * Write an int in BigEndian into the byte array.
+ * @param out byte array
+ * @param pos current pos in array
+ * @param value integer to write
+ * @return the index after adding the size of an int (4)
+ */
+ private int writeInt(byte[] out, int pos, int value) {
+ out[pos + 0] = (byte) ((value >> 24) & 0xFF);
+ out[pos + 1] = (byte) ((value >> 16) & 0xFF);
+ out[pos + 2] = (byte) ((value >> 8) & 0xFF);
+ out[pos + 3] = (byte) ((value >> 0) & 0xFF);
+ return pos + 4;
+ }
+
+ private int readInt(byte[] in, int pos) {
+ int result =
+ ((in[pos ] & 0xFF) << 24) |
+ ((in[pos + 1] & 0xFF) << 16) |
+ ((in[pos + 2] & 0xFF) << 8) |
+ ((in[pos + 3] & 0xFF) << 0);
+ return result;
+ }
+}
diff --git a/src/com/android/providers/userdictionary/UserDictionaryProvider.java b/src/com/android/providers/userdictionary/UserDictionaryProvider.java
index c1928c2..59a0c24 100644
--- a/src/com/android/providers/userdictionary/UserDictionaryProvider.java
+++ b/src/com/android/providers/userdictionary/UserDictionaryProvider.java
@@ -17,6 +17,9 @@
package com.android.providers.userdictionary;
+import java.util.HashMap;
+
+import android.backup.BackupManager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
@@ -33,8 +36,6 @@ import android.provider.UserDictionary.Words;
import android.text.TextUtils;
import android.util.Log;
-import java.util.HashMap;
-
/**
* Provides access to a database of user defined words. Each item has a word and a frequency.
*/
@@ -58,7 +59,9 @@ public class UserDictionaryProvider extends ContentProvider {
private static final int WORDS = 1;
private static final int WORD_ID = 2;
-
+
+ private BackupManager mBackupManager;
+
/**
* This class helps open, create, and upgrade the database file.
*/
@@ -93,6 +96,7 @@ public class UserDictionaryProvider extends ContentProvider {
@Override
public boolean onCreate() {
mOpenHelper = new DatabaseHelper(getContext());
+ mBackupManager = new BackupManager(getContext());
return true;
}
@@ -181,6 +185,7 @@ public class UserDictionaryProvider extends ContentProvider {
if (rowId > 0) {
Uri wordUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(wordUri, null);
+ mBackupManager.dataChanged();
return wordUri;
}
@@ -207,6 +212,7 @@ public class UserDictionaryProvider extends ContentProvider {
}
getContext().getContentResolver().notifyChange(uri, null);
+ mBackupManager.dataChanged();
return count;
}
@@ -230,6 +236,7 @@ public class UserDictionaryProvider extends ContentProvider {
}
getContext().getContentResolver().notifyChange(uri, null);
+ mBackupManager.dataChanged();
return count;
}