1e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate/* 2e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * Copyright (C) 2015 The Android Open Source Project 3e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * 4e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * Licensed under the Apache License, Version 2.0 (the "License"); 5e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * you may not use this file except in compliance with the License. 6e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * You may obtain a copy of the License at 7e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * 8e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * http://www.apache.org/licenses/LICENSE-2.0 9e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * 10e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * Unless required by applicable law or agreed to in writing, software 11e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * distributed under the License is distributed on an "AS IS" BASIS, 12e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * See the License for the specific language governing permissions and 14e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate * limitations under the License. 15e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate */ 16e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 17e012a235569fe307d165dfd0784ae847d0b13739Christopher Tatepackage com.android.server.backup; 18e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 19e012a235569fe307d165dfd0784ae847d0b13739Christopher Tateimport android.app.AppGlobals; 20f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tateimport android.app.backup.BlobBackupHelper; 21e012a235569fe307d165dfd0784ae847d0b13739Christopher Tateimport android.content.pm.IPackageManager; 22e012a235569fe307d165dfd0784ae847d0b13739Christopher Tateimport android.os.UserHandle; 23e012a235569fe307d165dfd0784ae847d0b13739Christopher Tateimport android.util.Slog; 24e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 25f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tatepublic class PreferredActivityBackupHelper extends BlobBackupHelper { 26e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate private static final String TAG = "PreferredBackup"; 27f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tate private static final boolean DEBUG = false; 28e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 29e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate // current schema of the backup state blob 306038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate private static final int STATE_VERSION = 3; 31e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 32e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate // key under which the preferred-activity state blob is committed to backup 33e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate private static final String KEY_PREFERRED = "preferred-activity"; 34e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 356038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate // key for default-browser [etc] state 366038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate private static final String KEY_DEFAULT_APPS = "default-apps"; 376038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate 386038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate // intent-filter verification state 396038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate private static final String KEY_INTENT_VERIFICATION = "intent-verification"; 406038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate 41f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tate public PreferredActivityBackupHelper() { 426038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate super(STATE_VERSION, 436038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate KEY_PREFERRED, 446038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate KEY_DEFAULT_APPS, 456038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate KEY_INTENT_VERIFICATION); 46e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate } 47e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 48e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate @Override 49f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tate protected byte[] getBackupPayload(String key) { 506038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate IPackageManager pm = AppGlobals.getPackageManager(); 516038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate if (DEBUG) { 526038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate Slog.d(TAG, "Handling backup of " + key); 536038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate } 546038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate try { 556038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate switch (key) { 566038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate case KEY_PREFERRED: 576038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate return pm.getPreferredActivityBackup(UserHandle.USER_OWNER); 586038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate case KEY_DEFAULT_APPS: 596038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate return pm.getDefaultAppsBackup(UserHandle.USER_OWNER); 606038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate case KEY_INTENT_VERIFICATION: 616038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate return pm.getIntentFilterVerificationBackup(UserHandle.USER_OWNER); 626038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate default: 636038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate Slog.w(TAG, "Unexpected backup key " + key); 64e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate } 656038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate } catch (Exception e) { 666038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate Slog.e(TAG, "Unable to store payload " + key); 67e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate } 68f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tate return null; 69e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate } 70e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate 71e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate @Override 72f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tate protected void applyRestoredPayload(String key, byte[] payload) { 736038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate IPackageManager pm = AppGlobals.getPackageManager(); 746038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate if (DEBUG) { 756038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate Slog.d(TAG, "Handling restore of " + key); 766038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate } 776038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate try { 786038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate switch (key) { 796038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate case KEY_PREFERRED: 806038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate pm.restorePreferredActivities(payload, UserHandle.USER_OWNER); 816038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate break; 826038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate case KEY_DEFAULT_APPS: 836038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate pm.restoreDefaultApps(payload, UserHandle.USER_OWNER); 846038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate break; 856038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate case KEY_INTENT_VERIFICATION: 866038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate pm.restoreIntentFilterVerification(payload, UserHandle.USER_OWNER); 876038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate break; 886038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate default: 896038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate Slog.w(TAG, "Unexpected restore key " + key); 90f7cb8a0b50968f51994279b0ae3fede0a586ba45Christopher Tate } 916038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate } catch (Exception e) { 926038d15cbc7f4648ceaadf5f15d1928c4899f98eChristopher Tate Slog.w(TAG, "Unable to restore key " + key); 93e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate } 94e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate } 95e012a235569fe307d165dfd0784ae847d0b13739Christopher Tate} 96