blob: ad7e3143d192bed1bdb87b81e5296689b2b655b4 [file] [log] [blame]
Sunny Goyalae788a12019-04-24 17:11:24 -07001/**
2 * Copyright (C) 2019 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.settings.backup;
18
Allen Su00c40d82023-10-05 09:28:37 +000019import static com.android.settings.localepicker.LocaleNotificationDataManager.LOCALE_NOTIFICATION;
20
Sunny Goyalae788a12019-04-24 17:11:24 -070021import android.app.backup.BackupAgentHelper;
22import android.app.backup.BackupDataInputStream;
23import android.app.backup.BackupDataOutput;
24import android.app.backup.BackupHelper;
Allen Su00c40d82023-10-05 09:28:37 +000025import android.app.backup.SharedPreferencesBackupHelper;
Sunny Goyalae788a12019-04-24 17:11:24 -070026import android.os.ParcelFileDescriptor;
27
ykhung96e96852021-08-13 15:36:53 +080028import com.android.settings.fuelgauge.BatteryBackupHelper;
Sunny Goyalae788a12019-04-24 17:11:24 -070029import com.android.settings.shortcut.CreateShortcutPreferenceController;
30
31import java.io.FileInputStream;
32import java.io.FileOutputStream;
33import java.io.IOException;
34
35/**
36 * Backup agent for Settings APK
37 */
38public class SettingsBackupHelper extends BackupAgentHelper {
Allen Su00c40d82023-10-05 09:28:37 +000039 private static final String PREF_LOCALE_NOTIFICATION = "localeNotificationSharedPref";
Sunny Goyalae788a12019-04-24 17:11:24 -070040
41 @Override
42 public void onCreate() {
43 super.onCreate();
44 addHelper("no-op", new NoOpHelper());
ykhung96e96852021-08-13 15:36:53 +080045 addHelper(BatteryBackupHelper.TAG, new BatteryBackupHelper(this));
Allen Su00c40d82023-10-05 09:28:37 +000046 addHelper(PREF_LOCALE_NOTIFICATION,
47 new SharedPreferencesBackupHelper(this, LOCALE_NOTIFICATION));
Sunny Goyalae788a12019-04-24 17:11:24 -070048 }
49
50 @Override
51 public void onRestoreFinished() {
52 super.onRestoreFinished();
53 CreateShortcutPreferenceController.updateRestoredShortcuts(this);
54 }
55
56 /**
57 * Backup helper which does not do anything. Having at least one helper ensures that the
58 * transport is not empty and onRestoreFinished is called eventually.
59 */
60 private static class NoOpHelper implements BackupHelper {
61
62 private final int VERSION_CODE = 1;
63
64 @Override
65 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
66 ParcelFileDescriptor newState) {
67
68 try (FileOutputStream out = new FileOutputStream(newState.getFileDescriptor())) {
69 if (getVersionCode(oldState) != VERSION_CODE) {
Edgar Wangc2e45132020-08-04 16:58:02 +080070 data.writeEntityHeader("placeholder", 1);
Sunny Goyalae788a12019-04-24 17:11:24 -070071 data.writeEntityData(new byte[1], 1);
72 }
73
74 // Write new version code
75 out.write(VERSION_CODE);
76 out.flush();
77 } catch (IOException e) { }
78 }
79
80 @Override
81 public void restoreEntity(BackupDataInputStream data) { }
82
83 @Override
84 public void writeNewStateDescription(ParcelFileDescriptor newState) { }
85
86 private int getVersionCode(ParcelFileDescriptor state) {
87 if (state == null) {
88 return 0;
89 }
90 try (FileInputStream in = new FileInputStream(state.getFileDescriptor())) {
91 return in.read();
92 } catch (IOException e) {
93 return 0;
94 }
95 }
96 }
97}