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