Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.backup; |
| 18 | |
| 19 | import android.app.AppGlobals; |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 20 | import android.app.backup.BlobBackupHelper; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 21 | import android.content.pm.IPackageManager; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 22 | import android.os.UserHandle; |
| 23 | import android.util.Slog; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 24 | |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 25 | public class PreferredActivityBackupHelper extends BlobBackupHelper { |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 26 | private static final String TAG = "PreferredBackup"; |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 27 | private static final boolean DEBUG = false; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 28 | |
| 29 | // current schema of the backup state blob |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 30 | private static final int STATE_VERSION = 2; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 31 | |
| 32 | // key under which the preferred-activity state blob is committed to backup |
| 33 | private static final String KEY_PREFERRED = "preferred-activity"; |
| 34 | |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 35 | public PreferredActivityBackupHelper() { |
| 36 | super(STATE_VERSION, KEY_PREFERRED); |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Override |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 40 | protected byte[] getBackupPayload(String key) { |
| 41 | if (KEY_PREFERRED.equals(key)) { |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 42 | if (DEBUG) { |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 43 | Slog.v(TAG, "Checking whether to back up"); |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 44 | } |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 45 | 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 Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 54 | } |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 55 | return null; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 59 | 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 Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 73 | } |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 74 | } |