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