1 /**
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 
17 package com.android.settings.backup;
18 
19 import android.app.backup.BackupAgentHelper;
20 import android.app.backup.BackupDataInputStream;
21 import android.app.backup.BackupDataOutput;
22 import android.app.backup.BackupHelper;
23 import android.os.ParcelFileDescriptor;
24 
25 import com.android.settings.fuelgauge.BatteryBackupHelper;
26 import com.android.settings.shortcut.CreateShortcutPreferenceController;
27 
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 
32 /**
33  * Backup agent for Settings APK
34  */
35 public class SettingsBackupHelper extends BackupAgentHelper {
36 
37     @Override
onCreate()38     public void onCreate() {
39         super.onCreate();
40         addHelper("no-op", new NoOpHelper());
41         addHelper(BatteryBackupHelper.TAG, new BatteryBackupHelper(this));
42     }
43 
44     @Override
onRestoreFinished()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
performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)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) {
64                     data.writeEntityHeader("placeholder", 1);
65                     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
restoreEntity(BackupDataInputStream data)75         public void restoreEntity(BackupDataInputStream data) { }
76 
77         @Override
writeNewStateDescription(ParcelFileDescriptor newState)78         public void writeNewStateDescription(ParcelFileDescriptor newState) { }
79 
getVersionCode(ParcelFileDescriptor state)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 }
92