1 /*
2  * Copyright (C) 2016 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 android.backup.app;
18 
19 import android.app.backup.BackupAgent;
20 import android.app.backup.BackupDataInput;
21 import android.app.backup.BackupDataOutput;
22 import android.app.backup.FullBackupDataOutput;
23 import android.os.ParcelFileDescriptor;
24 import android.util.Log;
25 
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 
30 /*
31  * Key Value Backup agent for Backup CTS App.
32  *
33  * Logs callbacks into logcat.
34  */
35 public class KeyValueBackupAgent extends BackupAgent {
36 
37     @Override
onCreate()38     public void onCreate() {
39         Log.d(MainActivity.TAG, "onCreate");
40     }
41 
42     @Override
onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)43     public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
44                          ParcelFileDescriptor newState) throws IOException {
45         Log.d(MainActivity.TAG, "Backup requested, quota is " + data.getQuota());
46 
47         // Always backup the entire file
48         File testFile = new File(getFilesDir(), MainActivity.FILE_NAME);
49         Log.d(MainActivity.TAG, "Writing " + testFile.length());
50 
51         data.writeEntityHeader(MainActivity.FILE_NAME, (int) testFile.length());
52         byte[] buffer = new byte[4096];
53         try (FileInputStream input = new FileInputStream(testFile)) {
54             int read;
55             while ((read = input.read(buffer)) >= 0) {
56                 data.writeEntityData(buffer, read);
57             }
58         }
59     }
60 
61     @Override
onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)62     public void onRestore(BackupDataInput data, int appVersionCode,
63                           ParcelFileDescriptor newState) throws IOException {
64         Log.d(MainActivity.TAG, "Restore requested");
65     }
66 
67     @Override
onRestoreFile(ParcelFileDescriptor data, long size, File destination, int type, long mode, long mtime)68     public void onRestoreFile(ParcelFileDescriptor data, long size,
69             File destination, int type, long mode, long mtime) throws IOException {
70         throw new IllegalStateException("unexpected onRestoreFile");
71     }
72 
73     @Override
onFullBackup(FullBackupDataOutput data)74     public void onFullBackup(FullBackupDataOutput data) throws IOException {
75         throw new IllegalStateException("unexpected onFullBackup");
76     }
77 
78     @Override
onQuotaExceeded(long backupDataBytes, long quotaBytes)79     public void onQuotaExceeded(long backupDataBytes, long quotaBytes) {
80         Log.d(MainActivity.TAG, "Quota exceeded!");
81     }
82 
83     @Override
onRestoreFinished()84     public void onRestoreFinished() {
85         Log.d(MainActivity.TAG, "onRestoreFinished");
86     }
87 
88     @Override
onDestroy()89     public void onDestroy() {
90         Log.d(MainActivity.TAG, "onDestroy");
91     }
92 
93 }
94