1/*
2 * Copyright (C) 2008 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
17package com.android.inputmethod.latin;
18
19import android.app.backup.BackupAgentHelper;
20import android.app.backup.BackupDataInput;
21import android.app.backup.SharedPreferencesBackupHelper;
22import android.content.SharedPreferences;
23import android.os.ParcelFileDescriptor;
24
25import com.android.inputmethod.latin.settings.LocalSettingsConstants;
26
27import java.io.IOException;
28
29/**
30 * Backup/restore agent for LatinIME.
31 * Currently it backs up the default shared preferences.
32 */
33public final class BackupAgent extends BackupAgentHelper {
34    private static final String PREF_SUFFIX = "_preferences";
35
36    @Override
37    public void onCreate() {
38        addHelper("shared_pref", new SharedPreferencesBackupHelper(this,
39                getPackageName() + PREF_SUFFIX));
40    }
41
42    @Override
43    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
44            throws IOException {
45        // Let the restore operation go through
46        super.onRestore(data, appVersionCode, newState);
47
48        // Remove the preferences that we don't want restored.
49        final SharedPreferences.Editor prefEditor = getSharedPreferences(
50                getPackageName() + PREF_SUFFIX, MODE_PRIVATE).edit();
51        for (final String key : LocalSettingsConstants.PREFS_TO_SKIP_RESTORING) {
52            prefEditor.remove(key);
53        }
54        // Flush the changes to disk.
55        prefEditor.commit();
56    }
57}
58