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 com.android.wallpaperbackup; 18 19 import android.annotation.Nullable; 20 import android.app.WallpaperInfo; 21 import android.app.backup.BackupManager; 22 import android.app.backup.BackupRestoreEventLogger; 23 import android.app.backup.BackupRestoreEventLogger.BackupRestoreDataType; 24 import android.app.backup.BackupRestoreEventLogger.BackupRestoreError; 25 import android.content.ComponentName; 26 27 import com.android.internal.annotations.VisibleForTesting; 28 29 import java.util.HashSet; 30 import java.util.Set; 31 32 /** 33 * Log backup / restore related events using {@link BackupRestoreEventLogger}. 34 */ 35 public class WallpaperEventLogger { 36 /* Static image used as system (or home) screen wallpaper */ 37 @BackupRestoreDataType 38 @VisibleForTesting 39 static final String WALLPAPER_IMG_SYSTEM = "wlp_img_system"; 40 41 /* Static image used as lock screen wallpaper */ 42 @BackupRestoreDataType 43 @VisibleForTesting 44 static final String WALLPAPER_IMG_LOCK = "wlp_img_lock"; 45 46 /* Live component used as system (or home) screen wallpaper */ 47 @BackupRestoreDataType 48 @VisibleForTesting 49 static final String WALLPAPER_LIVE_SYSTEM = "wlp_live_system"; 50 51 /* Live component used as lock screen wallpaper */ 52 @BackupRestoreDataType 53 @VisibleForTesting 54 static final String WALLPAPER_LIVE_LOCK = "wlp_live_lock"; 55 56 @BackupRestoreError 57 static final String ERROR_INELIGIBLE = "ineligible"; 58 @BackupRestoreError 59 static final String ERROR_NO_METADATA = "no_metadata"; 60 @BackupRestoreError 61 static final String ERROR_NO_WALLPAPER = "no_wallpaper"; 62 @BackupRestoreError 63 static final String ERROR_QUOTA_EXCEEDED = "quota_exceeded"; 64 @BackupRestoreError 65 static final String ERROR_SET_COMPONENT_EXCEPTION = "exception_in_set_component"; 66 @BackupRestoreError 67 static final String ERROR_LIVE_PACKAGE_NOT_INSTALLED = "live_pkg_not_installed_in_restore"; 68 69 private final BackupRestoreEventLogger mLogger; 70 71 private final Set<String> mProcessedDataTypes = new HashSet<>(); 72 WallpaperEventLogger(BackupManager backupManager, WallpaperBackupAgent wallpaperAgent)73 WallpaperEventLogger(BackupManager backupManager, WallpaperBackupAgent wallpaperAgent) { 74 mLogger = backupManager.getBackupRestoreEventLogger(/* backupAgent */ wallpaperAgent); 75 } 76 WallpaperEventLogger(BackupRestoreEventLogger logger)77 WallpaperEventLogger(BackupRestoreEventLogger logger) { 78 mLogger = logger; 79 } 80 getBackupRestoreLogger()81 BackupRestoreEventLogger getBackupRestoreLogger() { 82 return mLogger; 83 } 84 onSystemImageWallpaperBackedUp()85 void onSystemImageWallpaperBackedUp() { 86 logBackupSuccessInternal(WALLPAPER_IMG_SYSTEM, /* liveComponentWallpaperInfo */ null); 87 } 88 onLockImageWallpaperBackedUp()89 void onLockImageWallpaperBackedUp() { 90 logBackupSuccessInternal(WALLPAPER_IMG_LOCK, /* liveComponentWallpaperInfo */ null); 91 } 92 onSystemLiveWallpaperBackedUp(WallpaperInfo wallpaperInfo)93 void onSystemLiveWallpaperBackedUp(WallpaperInfo wallpaperInfo) { 94 logBackupSuccessInternal(WALLPAPER_LIVE_SYSTEM, wallpaperInfo); 95 } 96 onLockLiveWallpaperBackedUp(WallpaperInfo wallpaperInfo)97 void onLockLiveWallpaperBackedUp(WallpaperInfo wallpaperInfo) { 98 logBackupSuccessInternal(WALLPAPER_LIVE_LOCK, wallpaperInfo); 99 } 100 onSystemImageWallpaperBackupFailed(@ackupRestoreError String error)101 void onSystemImageWallpaperBackupFailed(@BackupRestoreError String error) { 102 logBackupFailureInternal(WALLPAPER_IMG_SYSTEM, error); 103 } 104 onLockImageWallpaperBackupFailed(@ackupRestoreError String error)105 void onLockImageWallpaperBackupFailed(@BackupRestoreError String error) { 106 logBackupFailureInternal(WALLPAPER_IMG_LOCK, error); 107 } 108 onSystemLiveWallpaperBackupFailed(@ackupRestoreError String error)109 void onSystemLiveWallpaperBackupFailed(@BackupRestoreError String error) { 110 logBackupFailureInternal(WALLPAPER_LIVE_SYSTEM, error); 111 } 112 onLockLiveWallpaperBackupFailed(@ackupRestoreError String error)113 void onLockLiveWallpaperBackupFailed(@BackupRestoreError String error) { 114 logBackupFailureInternal(WALLPAPER_LIVE_LOCK, error); 115 } 116 onSystemImageWallpaperRestored()117 void onSystemImageWallpaperRestored() { 118 logRestoreSuccessInternal(WALLPAPER_IMG_SYSTEM, /* liveComponentWallpaperInfo */ null); 119 } 120 onLockImageWallpaperRestored()121 void onLockImageWallpaperRestored() { 122 logRestoreSuccessInternal(WALLPAPER_IMG_LOCK, /* liveComponentWallpaperInfo */ null); 123 } 124 onSystemLiveWallpaperRestored(ComponentName wpService)125 void onSystemLiveWallpaperRestored(ComponentName wpService) { 126 logRestoreSuccessInternal(WALLPAPER_LIVE_SYSTEM, wpService); 127 } 128 onLockLiveWallpaperRestored(ComponentName wpService)129 void onLockLiveWallpaperRestored(ComponentName wpService) { 130 logRestoreSuccessInternal(WALLPAPER_LIVE_LOCK, wpService); 131 } 132 onSystemImageWallpaperRestoreFailed(@ackupRestoreError String error)133 void onSystemImageWallpaperRestoreFailed(@BackupRestoreError String error) { 134 logRestoreFailureInternal(WALLPAPER_IMG_SYSTEM, error); 135 } 136 onLockImageWallpaperRestoreFailed(@ackupRestoreError String error)137 void onLockImageWallpaperRestoreFailed(@BackupRestoreError String error) { 138 logRestoreFailureInternal(WALLPAPER_IMG_LOCK, error); 139 } 140 onSystemLiveWallpaperRestoreFailed(@ackupRestoreError String error)141 void onSystemLiveWallpaperRestoreFailed(@BackupRestoreError String error) { 142 logRestoreFailureInternal(WALLPAPER_LIVE_SYSTEM, error); 143 } 144 onLockLiveWallpaperRestoreFailed(@ackupRestoreError String error)145 void onLockLiveWallpaperRestoreFailed(@BackupRestoreError String error) { 146 logRestoreFailureInternal(WALLPAPER_LIVE_LOCK, error); 147 } 148 149 150 151 /** 152 * Called when the whole backup flow is interrupted by an exception. 153 */ onBackupException(Exception exception)154 void onBackupException(Exception exception) { 155 String error = exception.getClass().getName(); 156 if (!mProcessedDataTypes.contains(WALLPAPER_IMG_SYSTEM) && !mProcessedDataTypes.contains( 157 WALLPAPER_LIVE_SYSTEM)) { 158 mLogger.logItemsBackupFailed(WALLPAPER_IMG_SYSTEM, /* count */ 1, error); 159 } 160 if (!mProcessedDataTypes.contains(WALLPAPER_IMG_LOCK) && !mProcessedDataTypes.contains( 161 WALLPAPER_LIVE_LOCK)) { 162 mLogger.logItemsBackupFailed(WALLPAPER_IMG_LOCK, /* count */ 1, error); 163 } 164 } 165 166 /** 167 * Called when the whole restore flow is interrupted by an exception. 168 */ onRestoreException(Exception exception)169 void onRestoreException(Exception exception) { 170 String error = exception.getClass().getName(); 171 if (!mProcessedDataTypes.contains(WALLPAPER_IMG_SYSTEM) && !mProcessedDataTypes.contains( 172 WALLPAPER_LIVE_SYSTEM)) { 173 mLogger.logItemsRestoreFailed(WALLPAPER_IMG_SYSTEM, /* count */ 1, error); 174 } 175 if (!mProcessedDataTypes.contains(WALLPAPER_IMG_LOCK) && !mProcessedDataTypes.contains( 176 WALLPAPER_LIVE_LOCK)) { 177 mLogger.logItemsRestoreFailed(WALLPAPER_IMG_LOCK, /* count */ 1, error); 178 } 179 } logBackupSuccessInternal(@ackupRestoreDataType String which, @Nullable WallpaperInfo liveComponentWallpaperInfo)180 private void logBackupSuccessInternal(@BackupRestoreDataType String which, 181 @Nullable WallpaperInfo liveComponentWallpaperInfo) { 182 mLogger.logItemsBackedUp(which, /* count */ 1); 183 logLiveWallpaperNameIfPresent(which, liveComponentWallpaperInfo); 184 mProcessedDataTypes.add(which); 185 } 186 logBackupFailureInternal(@ackupRestoreDataType String which, @BackupRestoreError String error)187 private void logBackupFailureInternal(@BackupRestoreDataType String which, 188 @BackupRestoreError String error) { 189 mLogger.logItemsBackupFailed(which, /* count */ 1, error); 190 mProcessedDataTypes.add(which); 191 } 192 logRestoreSuccessInternal(@ackupRestoreDataType String which, @Nullable ComponentName liveComponentWallpaperInfo)193 private void logRestoreSuccessInternal(@BackupRestoreDataType String which, 194 @Nullable ComponentName liveComponentWallpaperInfo) { 195 mLogger.logItemsRestored(which, /* count */ 1); 196 logRestoredLiveWallpaperNameIfPresent(which, liveComponentWallpaperInfo); 197 mProcessedDataTypes.add(which); 198 } 199 logRestoreFailureInternal(@ackupRestoreDataType String which, @BackupRestoreError String error)200 private void logRestoreFailureInternal(@BackupRestoreDataType String which, 201 @BackupRestoreError String error) { 202 mLogger.logItemsRestoreFailed(which, /* count */ 1, error); 203 mProcessedDataTypes.add(which); 204 } 205 logLiveWallpaperNameIfPresent(@ackupRestoreDataType String wallpaperType, WallpaperInfo wallpaperInfo)206 private void logLiveWallpaperNameIfPresent(@BackupRestoreDataType String wallpaperType, 207 WallpaperInfo wallpaperInfo) { 208 if (wallpaperInfo != null) { 209 mLogger.logBackupMetadata(wallpaperType, wallpaperInfo.getComponent().getClassName()); 210 } 211 } 212 logRestoredLiveWallpaperNameIfPresent(@ackupRestoreDataType String wallpaperType, ComponentName wpService)213 private void logRestoredLiveWallpaperNameIfPresent(@BackupRestoreDataType String wallpaperType, 214 ComponentName wpService) { 215 if (wpService != null) { 216 mLogger.logRestoreMetadata(wallpaperType, wpService.getClassName()); 217 } 218 } 219 } 220