blob: a682df8b88b6cada0d824be1b9e2f138931dcc85 [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
19import android.app.backup.BackupAgentHelper;
20import android.app.backup.BackupDataInputStream;
21import android.app.backup.BackupDataOutput;
22import android.app.backup.BackupHelper;
23import android.os.ParcelFileDescriptor;
24
ykhung96e96852021-08-13 15:36:53 +080025import com.android.settings.fuelgauge.BatteryBackupHelper;
Sunny Goyalae788a12019-04-24 17:11:24 -070026import com.android.settings.shortcut.CreateShortcutPreferenceController;
27
28import java.io.FileInputStream;
29import java.io.FileOutputStream;
30import java.io.IOException;
31
32/**
33 * Backup agent for Settings APK
34 */
35public class SettingsBackupHelper extends BackupAgentHelper {
36
37 @Override
38 public void onCreate() {
39 super.onCreate();
40 addHelper("no-op", new NoOpHelper());
ykhung96e96852021-08-13 15:36:53 +080041 addHelper(BatteryBackupHelper.TAG, new BatteryBackupHelper(this));
Sunny Goyalae788a12019-04-24 17:11:24 -070042 }
43
44 @Override
45 public void onRestoreFinished() {
46 super.onRestoreFinished();
47 CreateShortcutPreferenceController.updateRestoredShortcuts(this);
48 }
49
50 /**
51 * Backup helper which does not do anything. Having at least one helper ensures that the
52 * transport is not empty and onRestoreFinished is called eventually.
53 */
54 private static class NoOpHelper implements BackupHelper {
55
56 private final int VERSION_CODE = 1;
57
58 @Override
59 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
60 ParcelFileDescriptor newState) {
61
62 try (FileOutputStream out = new FileOutputStream(newState.getFileDescriptor())) {
63 if (getVersionCode(oldState) != VERSION_CODE) {
Edgar Wangc2e45132020-08-04 16:58:02 +080064 data.writeEntityHeader("placeholder", 1);
Sunny Goyalae788a12019-04-24 17:11:24 -070065 data.writeEntityData(new byte[1], 1);
66 }
67
68 // Write new version code
69 out.write(VERSION_CODE);
70 out.flush();
71 } catch (IOException e) { }
72 }
73
74 @Override
75 public void restoreEntity(BackupDataInputStream data) { }
76
77 @Override
78 public void writeNewStateDescription(ParcelFileDescriptor newState) { }
79
80 private int getVersionCode(ParcelFileDescriptor state) {
81 if (state == null) {
82 return 0;
83 }
84 try (FileInputStream in = new FileInputStream(state.getFileDescriptor())) {
85 return in.read();
86 } catch (IOException e) {
87 return 0;
88 }
89 }
90 }
91}