• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 
25 import java.io.File;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 
29 /**
30  * Full Backup agent that tries to cast its {@link android.app.Application} object during backup.
31  *
32  * <p>This is used to check whether the app is in restricted mode during backup since casting the
33  * application will throw an exception and fail the backup when in restricted mode.
34  */
35 public class ApplicationCastingFullBackupAgent extends BackupAgent {
36     @Override
onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)37     public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
38             ParcelFileDescriptor newState) throws IOException {
39         throw new IllegalStateException("unexpected onBackup");
40     }
41 
42     @Override
onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)43     public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
44             throws IOException {
45         throw new IllegalStateException("unexpected onRestore");
46     }
47 
48     @Override
onFullBackup(FullBackupDataOutput data)49     public void onFullBackup(FullBackupDataOutput data) throws IOException {
50         CustomApplication customApplication = (CustomApplication) getApplicationContext();
51 
52         // Write a file so that the backup doesn't get rejected for being empty.
53         try (FileWriter fileWriter = new FileWriter(new File(getFilesDir(), "testfile"))) {
54             fileWriter.write("meow");
55         }
56 
57         super.onFullBackup(data);
58     }
59 }
60