1 /* 2 * Copyright (C) 2023 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.BackupManager; 23 import android.app.backup.BackupRestoreEventLogger; 24 import android.app.backup.FullBackupDataOutput; 25 import android.os.ParcelFileDescriptor; 26 27 import java.io.IOException; 28 29 public class LoggingFullBackupAgent extends BackupAgent { 30 private static final String DATA_TYPE = "data_type"; 31 private static final String ERROR = "error"; 32 private static final String METADATA = "metadata"; 33 private static final int SUCCESS_COUNT = 1; 34 private static final int FAIL_COUNT = 2; 35 36 private BackupRestoreEventLogger mBackupRestoreEventLogger; 37 38 @Override onCreate()39 public void onCreate() { 40 BackupManager backupManager = new BackupManager(getApplicationContext()); 41 mBackupRestoreEventLogger = backupManager.getBackupRestoreEventLogger( 42 /* backupAgent */ this); 43 } 44 45 @Override onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)46 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 47 ParcelFileDescriptor newState) throws IOException { 48 } 49 50 @Override onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)51 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) 52 throws IOException { 53 } 54 55 @Override onFullBackup(FullBackupDataOutput data)56 public void onFullBackup(FullBackupDataOutput data) throws IOException { 57 super.onFullBackup(data); 58 59 mBackupRestoreEventLogger.logItemsBackedUp(DATA_TYPE, SUCCESS_COUNT); 60 mBackupRestoreEventLogger.logBackupMetadata(DATA_TYPE, METADATA); 61 mBackupRestoreEventLogger.logItemsBackupFailed(DATA_TYPE, FAIL_COUNT, ERROR); 62 } 63 64 @Override onRestoreFinished()65 public void onRestoreFinished() { 66 super.onRestoreFinished(); 67 68 mBackupRestoreEventLogger.logItemsRestored(DATA_TYPE, SUCCESS_COUNT); 69 mBackupRestoreEventLogger.logRestoreMetadata(DATA_TYPE, METADATA); 70 mBackupRestoreEventLogger.logItemsRestoreFailed(DATA_TYPE, FAIL_COUNT, ERROR); 71 } 72 } 73