blob: 04935a7bf0b32cea7a8f28f8c6b5fb9503eb0857 [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;
XingHaiLu49cb0002023-11-14 14:17:30 +080029import com.android.settings.onboarding.OnboardingFeatureProvider;
30import com.android.settings.overlay.FeatureFactory;
Sunny Goyalae788a12019-04-24 17:11:24 -070031import com.android.settings.shortcut.CreateShortcutPreferenceController;
32
33import java.io.FileInputStream;
34import java.io.FileOutputStream;
35import java.io.IOException;
XingHaiLu49cb0002023-11-14 14:17:30 +080036import com.android.settings.flags.Flags;
Sunny Goyalae788a12019-04-24 17:11:24 -070037
38/**
39 * Backup agent for Settings APK
40 */
41public class SettingsBackupHelper extends BackupAgentHelper {
Allen Su00c40d82023-10-05 09:28:37 +000042 private static final String PREF_LOCALE_NOTIFICATION = "localeNotificationSharedPref";
XingHaiLu49cb0002023-11-14 14:17:30 +080043 public static final String SOUND_BACKUP_HELPER = "SoundSettingsBackup";
Sunny Goyalae788a12019-04-24 17:11:24 -070044
45 @Override
46 public void onCreate() {
47 super.onCreate();
48 addHelper("no-op", new NoOpHelper());
ykhung96e96852021-08-13 15:36:53 +080049 addHelper(BatteryBackupHelper.TAG, new BatteryBackupHelper(this));
Allen Su00c40d82023-10-05 09:28:37 +000050 addHelper(PREF_LOCALE_NOTIFICATION,
51 new SharedPreferencesBackupHelper(this, LOCALE_NOTIFICATION));
XingHaiLu49cb0002023-11-14 14:17:30 +080052 if (Flags.enableSoundBackup()) {
53 OnboardingFeatureProvider onboardingFeatureProvider =
54 FeatureFactory.getFeatureFactory().getOnboardingFeatureProvider();
55 if (onboardingFeatureProvider != null) {
56 addHelper(SOUND_BACKUP_HELPER, onboardingFeatureProvider.
57 getSoundBackupHelper(this, this.getBackupRestoreEventLogger()));
58 }
59 }
Sunny Goyalae788a12019-04-24 17:11:24 -070060 }
61
62 @Override
63 public void onRestoreFinished() {
64 super.onRestoreFinished();
65 CreateShortcutPreferenceController.updateRestoredShortcuts(this);
66 }
67
68 /**
69 * Backup helper which does not do anything. Having at least one helper ensures that the
70 * transport is not empty and onRestoreFinished is called eventually.
71 */
72 private static class NoOpHelper implements BackupHelper {
73
74 private final int VERSION_CODE = 1;
75
76 @Override
77 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
78 ParcelFileDescriptor newState) {
79
80 try (FileOutputStream out = new FileOutputStream(newState.getFileDescriptor())) {
81 if (getVersionCode(oldState) != VERSION_CODE) {
Edgar Wangc2e45132020-08-04 16:58:02 +080082 data.writeEntityHeader("placeholder", 1);
Sunny Goyalae788a12019-04-24 17:11:24 -070083 data.writeEntityData(new byte[1], 1);
84 }
85
86 // Write new version code
87 out.write(VERSION_CODE);
88 out.flush();
89 } catch (IOException e) { }
90 }
91
92 @Override
93 public void restoreEntity(BackupDataInputStream data) { }
94
95 @Override
96 public void writeNewStateDescription(ParcelFileDescriptor newState) { }
97
98 private int getVersionCode(ParcelFileDescriptor state) {
99 if (state == null) {
100 return 0;
101 }
102 try (FileInputStream in = new FileInputStream(state.getFileDescriptor())) {
103 return in.read();
104 } catch (IOException e) {
105 return 0;
106 }
107 }
108 }
109}