| Sunny Goyal | ae788a1 | 2019-04-24 17:11:24 -0700 | [diff] [blame] | 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.shortcut.CreateShortcutPreferenceController; |
| 26 | |
| 27 | import java.io.FileInputStream; |
| 28 | import java.io.FileOutputStream; |
| 29 | import java.io.IOException; |
| 30 | |
| 31 | /** |
| 32 | * Backup agent for Settings APK |
| 33 | */ |
| 34 | public 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 Wang | c2e4513 | 2020-08-04 16:58:02 +0800 | [diff] [blame^] | 62 | data.writeEntityHeader("placeholder", 1); |
| Sunny Goyal | ae788a1 | 2019-04-24 17:11:24 -0700 | [diff] [blame] | 63 | 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 | } |