blob: 26f5bf4143e6c2d9f33a7f29531988ce879dcae5 [file] [log] [blame]
Christopher Tatee012a232015-04-01 17:18:50 -07001/*
2 * Copyright (C) 2015 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.server.backup;
18
19import android.app.AppGlobals;
Christopher Tatef7cb8a02015-04-20 16:09:48 -070020import android.app.backup.BlobBackupHelper;
Christopher Tatee012a232015-04-01 17:18:50 -070021import android.content.pm.IPackageManager;
Christopher Tatee012a232015-04-01 17:18:50 -070022import android.os.UserHandle;
23import android.util.Slog;
Christopher Tatee012a232015-04-01 17:18:50 -070024
Christopher Tatef7cb8a02015-04-20 16:09:48 -070025public class PreferredActivityBackupHelper extends BlobBackupHelper {
Christopher Tatee012a232015-04-01 17:18:50 -070026 private static final String TAG = "PreferredBackup";
Christopher Tatef7cb8a02015-04-20 16:09:48 -070027 private static final boolean DEBUG = false;
Christopher Tatee012a232015-04-01 17:18:50 -070028
29 // current schema of the backup state blob
Christopher Tatef7cb8a02015-04-20 16:09:48 -070030 private static final int STATE_VERSION = 2;
Christopher Tatee012a232015-04-01 17:18:50 -070031
32 // key under which the preferred-activity state blob is committed to backup
33 private static final String KEY_PREFERRED = "preferred-activity";
34
Christopher Tatef7cb8a02015-04-20 16:09:48 -070035 public PreferredActivityBackupHelper() {
36 super(STATE_VERSION, KEY_PREFERRED);
Christopher Tatee012a232015-04-01 17:18:50 -070037 }
38
39 @Override
Christopher Tatef7cb8a02015-04-20 16:09:48 -070040 protected byte[] getBackupPayload(String key) {
41 if (KEY_PREFERRED.equals(key)) {
Christopher Tatee012a232015-04-01 17:18:50 -070042 if (DEBUG) {
Christopher Tatef7cb8a02015-04-20 16:09:48 -070043 Slog.v(TAG, "Checking whether to back up");
Christopher Tatee012a232015-04-01 17:18:50 -070044 }
Christopher Tatef7cb8a02015-04-20 16:09:48 -070045 IPackageManager pm = AppGlobals.getPackageManager();
46 try {
47 return pm.getPreferredActivityBackup(UserHandle.USER_OWNER);
48 } catch (Exception e) {
49 Slog.e(TAG, "Unable to store backup payload", e);
50 // fall through to report null state
51 }
52 } else {
53 Slog.w(TAG, "Unexpected backup key " + key);
Christopher Tatee012a232015-04-01 17:18:50 -070054 }
Christopher Tatef7cb8a02015-04-20 16:09:48 -070055 return null;
Christopher Tatee012a232015-04-01 17:18:50 -070056 }
57
58 @Override
Christopher Tatef7cb8a02015-04-20 16:09:48 -070059 protected void applyRestoredPayload(String key, byte[] payload) {
60 if (KEY_PREFERRED.equals(key)) {
61 if (DEBUG) {
62 Slog.v(TAG, "Restoring");
63 }
64 IPackageManager pm = AppGlobals.getPackageManager();
65 try {
66 pm.restorePreferredActivities(payload, UserHandle.USER_OWNER);
67 } catch (Exception e) {
68 Slog.e(TAG, "Unable to restore", e);
69 }
70 } else {
71 Slog.w(TAG, "Unexpected restore key " + key);
72 }
Christopher Tatee012a232015-04-01 17:18:50 -070073 }
Christopher Tatee012a232015-04-01 17:18:50 -070074}