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.providers.media.photopicker.metrics;
18 
19 import com.android.internal.logging.InstanceId;
20 import com.android.internal.logging.InstanceIdSequence;
21 import com.android.internal.logging.UiEvent;
22 import com.android.internal.logging.UiEventLogger;
23 import com.android.providers.media.metrics.MPUiEventLoggerImpl;
24 
25 /**
26  * Logger for the Non UI Events triggered indirectly by some UI event(s).
27  */
28 public class NonUiEventLogger {
29     enum NonUiEvent implements UiEventLogger.UiEventEnum {
30         @UiEvent(doc = "User changed the active Photo picker cloud provider")
31         PHOTO_PICKER_CLOUD_PROVIDER_CHANGED(1135),
32         @UiEvent(doc = "Photo Picker uri is queried with an unknown column")
33         PHOTO_PICKER_QUERY_UNKNOWN_COLUMN(1227),
34         @UiEvent(doc = "Triggered a full sync in photo picker")
35         PHOTO_PICKER_FULL_SYNC_START(1442),
36         @UiEvent(doc = "Triggered an incremental sync in photo picker")
37         PHOTO_PICKER_INCREMENTAL_SYNC_START(1443),
38         @UiEvent(doc = "Triggered an album media sync in photo picker")
39         PHOTO_PICKER_ALBUM_MEDIA_SYNC_START(1444),
40         @UiEvent(doc = "Triggered get media collection info in photo picker")
41         PHOTO_PICKER_GET_MEDIA_COLLECTION_INFO_START(1448),
42         @UiEvent(doc = "Triggered get albums in photo picker")
43         PHOTO_PICKER_GET_ALBUMS_START(1449),
44         @UiEvent(doc = "Ended an add media sync in photo picker")
45         PHOTO_PICKER_ADD_MEDIA_SYNC_END(1445),
46         @UiEvent(doc = "Ended a remove media sync in photo picker")
47         PHOTO_PICKER_REMOVE_MEDIA_SYNC_END(1446),
48         @UiEvent(doc = "Ended an add album media sync in photo picker")
49         PHOTO_PICKER_ADD_ALBUM_MEDIA_SYNC_END(1447),
50         @UiEvent(doc = "Ended get media collection info in photo picker")
51         PHOTO_PICKER_GET_MEDIA_COLLECTION_INFO_END(1450),
52         @UiEvent(doc = "Ended get albums in photo picker")
53         PHOTO_PICKER_GET_ALBUMS_END(1451),
54         @UiEvent(doc = "Read grants added count.")
55         PHOTO_PICKER_GRANTS_ADDED_COUNT(1528),
56         @UiEvent(doc = "Read grants revoked count.")
57         PHOTO_PICKER_GRANTS_REVOKED_COUNT(1529),
58         @UiEvent(doc = "Total initial grants count.")
59         PHOTO_PICKER_INIT_GRANTS_COUNT(1530);
60 
61         private final int mId;
62 
NonUiEvent(int id)63         NonUiEvent(int id) {
64             mId = id;
65         }
66 
67         @Override
getId()68         public int getId() {
69             return mId;
70         }
71     }
72 
73     private static final int INSTANCE_ID_MAX = 1 << 15;
74     private static final InstanceIdSequence INSTANCE_ID_SEQUENCE =
75             new InstanceIdSequence(INSTANCE_ID_MAX);
76     private static final UiEventLogger LOGGER = new MPUiEventLoggerImpl();
77 
78     /**
79      * Generate and {@return} a new unique instance id to group some events for aggregated metrics
80      */
generateInstanceId()81     public static InstanceId generateInstanceId() {
82         return INSTANCE_ID_SEQUENCE.newInstanceId();
83     }
84 
85     /**
86      * Log metrics to notify that the user has changed the active cloud provider
87      * @param cloudProviderUid     new active cloud provider uid
88      * @param cloudProviderPackage new active cloud provider package name
89      */
logPickerCloudProviderChanged(int cloudProviderUid, String cloudProviderPackage)90     public static void logPickerCloudProviderChanged(int cloudProviderUid,
91             String cloudProviderPackage) {
92         LOGGER.log(NonUiEvent.PHOTO_PICKER_CLOUD_PROVIDER_CHANGED, cloudProviderUid,
93                 cloudProviderPackage);
94     }
95 
96     /**
97      * Log metrics to notify that a picker uri was queried for an unknown column (that is not
98      * supported yet)
99      * @param callingUid              the uid of the app initiating the picker query
100      * @param callingPackageAndColumn the package name of the app initiating the picker query,
101      *                                followed by the unknown column name, separated by a ':'
102      */
logPickerQueriedWithUnknownColumn(int callingUid, String callingPackageAndColumn)103     public static void logPickerQueriedWithUnknownColumn(int callingUid,
104             String callingPackageAndColumn) {
105         LOGGER.log(NonUiEvent.PHOTO_PICKER_QUERY_UNKNOWN_COLUMN, callingUid,
106                 callingPackageAndColumn);
107     }
108 
109     /**
110      * Log metrics to notify that a full sync started
111      * @param instanceId an identifier for the current sync
112      * @param uid        the uid of the MediaProvider logging this metric
113      * @param authority  the authority of the provider syncing with
114      */
logPickerFullSyncStart(InstanceId instanceId, int uid, String authority)115     public static void logPickerFullSyncStart(InstanceId instanceId, int uid, String authority) {
116         LOGGER.logWithInstanceId(NonUiEvent.PHOTO_PICKER_FULL_SYNC_START, uid, authority,
117                 instanceId);
118     }
119 
120     /**
121      * Log metrics to notify that an incremental sync started
122      * @param instanceId an identifier for the current sync
123      * @param uid        the uid of the MediaProvider logging this metric
124      * @param authority  the authority of the provider syncing with
125      */
logPickerIncrementalSyncStart(InstanceId instanceId, int uid, String authority)126     public static void logPickerIncrementalSyncStart(InstanceId instanceId, int uid,
127             String authority) {
128         LOGGER.logWithInstanceId(NonUiEvent.PHOTO_PICKER_INCREMENTAL_SYNC_START, uid, authority,
129                 instanceId);
130     }
131 
132     /**
133      * Log metrics to notify that an album media sync started
134      * @param instanceId an identifier for the current sync
135      * @param uid        the uid of the MediaProvider logging this metric
136      * @param authority  the authority of the provider syncing with
137      */
logPickerAlbumMediaSyncStart(InstanceId instanceId, int uid, String authority)138     public static void logPickerAlbumMediaSyncStart(InstanceId instanceId, int uid,
139             String authority) {
140         LOGGER.logWithInstanceId(NonUiEvent.PHOTO_PICKER_ALBUM_MEDIA_SYNC_START, uid, authority,
141                 instanceId);
142     }
143 
144     /**
145      * Log metrics to notify get media collection info triggered
146      * @param instanceId an identifier for the current query session
147      * @param uid        the uid of the MediaProvider logging this metric
148      * @param authority  the authority of the provider
149      */
logPickerGetMediaCollectionInfoStart(InstanceId instanceId, int uid, String authority)150     public static void logPickerGetMediaCollectionInfoStart(InstanceId instanceId, int uid,
151             String authority) {
152         LOGGER.logWithInstanceId(NonUiEvent.PHOTO_PICKER_GET_MEDIA_COLLECTION_INFO_START, uid,
153                 authority, instanceId);
154     }
155 
156     /**
157      * Log metrics to notify get albums triggered
158      * @param instanceId an identifier for the current query session
159      * @param uid        the uid of the MediaProvider logging this metric
160      * @param authority  the authority of the provider
161      */
logPickerGetAlbumsStart(InstanceId instanceId, int uid, String authority)162     public static void logPickerGetAlbumsStart(InstanceId instanceId, int uid, String authority) {
163         LOGGER.logWithInstanceId(NonUiEvent.PHOTO_PICKER_GET_ALBUMS_START, uid, authority,
164                 instanceId);
165     }
166 
167     /**
168      * Log metrics to notify that an add media sync ended
169      * @param instanceId an identifier for the current sync
170      * @param uid        the uid of the MediaProvider logging this metric
171      * @param authority  the authority of the provider syncing with
172      * @param count      the number of items synced
173      */
logPickerAddMediaSyncCompletion(InstanceId instanceId, int uid, String authority, int count)174     public static void logPickerAddMediaSyncCompletion(InstanceId instanceId, int uid,
175             String authority, int count) {
176         LOGGER.logWithInstanceIdAndPosition(NonUiEvent.PHOTO_PICKER_ADD_MEDIA_SYNC_END, uid,
177                 authority, instanceId, count);
178     }
179 
180     /**
181      * Log metrics to notify that a remove media sync ended
182      * @param instanceId an identifier for the current sync
183      * @param uid        the uid of the MediaProvider logging this metric
184      * @param authority  the authority of the provider syncing with
185      * @param count      the number of items synced
186      */
logPickerRemoveMediaSyncCompletion(InstanceId instanceId, int uid, String authority, int count)187     public static void logPickerRemoveMediaSyncCompletion(InstanceId instanceId, int uid,
188             String authority, int count) {
189         LOGGER.logWithInstanceIdAndPosition(NonUiEvent.PHOTO_PICKER_REMOVE_MEDIA_SYNC_END, uid,
190                 authority, instanceId, count);
191     }
192 
193     /**
194      * Log metrics to notify that an add album media sync ended
195      * @param instanceId an identifier for the current sync
196      * @param uid        the uid of the MediaProvider logging this metric
197      * @param authority  the authority of the provider syncing with
198      * @param count      the number of items synced
199      */
logPickerAddAlbumMediaSyncCompletion(InstanceId instanceId, int uid, String authority, int count)200     public static void logPickerAddAlbumMediaSyncCompletion(InstanceId instanceId, int uid,
201             String authority, int count) {
202         LOGGER.logWithInstanceIdAndPosition(NonUiEvent.PHOTO_PICKER_ADD_ALBUM_MEDIA_SYNC_END, uid,
203                 authority, instanceId, count);
204     }
205 
206     /**
207      * Log metrics to notify get media collection info ended
208      * @param instanceId an identifier for the current query session
209      * @param uid        the uid of the MediaProvider logging this metric
210      * @param authority  the authority of the provider
211      */
logPickerGetMediaCollectionInfoEnd(InstanceId instanceId, int uid, String authority)212     public static void logPickerGetMediaCollectionInfoEnd(InstanceId instanceId, int uid,
213             String authority) {
214         LOGGER.logWithInstanceId(NonUiEvent.PHOTO_PICKER_GET_MEDIA_COLLECTION_INFO_END, uid,
215                 authority, instanceId);
216     }
217 
218     /**
219      * Log metrics to notify get albums ended
220      * @param instanceId an identifier for the current query session
221      * @param uid        the uid of the MediaProvider logging this metric
222      * @param authority  the authority of the provider
223      * @param count      the number of albums fetched
224      */
logPickerGetAlbumsEnd(InstanceId instanceId, int uid, String authority, int count)225     public static void logPickerGetAlbumsEnd(InstanceId instanceId, int uid, String authority,
226             int count) {
227         LOGGER.logWithInstanceIdAndPosition(NonUiEvent.PHOTO_PICKER_GET_ALBUMS_END, uid, authority,
228                 instanceId, count);
229     }
230 
231     /**
232      * Log metrics for count of grants added for a package.
233      * @param instanceId   an identifier for the current session
234      * @param uid          the uid of the MediaProvider logging this metric
235      * @param packageName  the package name receiving the grant.
236      * @param count        the number of items for which the grants have been added.
237      */
logPickerChoiceGrantsAdditionCount(InstanceId instanceId, int uid, String packageName, int count)238     public static void logPickerChoiceGrantsAdditionCount(InstanceId instanceId, int uid,
239             String packageName, int count) {
240         LOGGER.logWithInstanceIdAndPosition(NonUiEvent.PHOTO_PICKER_GRANTS_ADDED_COUNT, uid,
241                 packageName, instanceId, count);
242     }
243 
244     /**
245      * Log metrics for count of grants revoked for a package.
246      * @param instanceId   an identifier for the current session
247      * @param uid          the uid of the MediaProvider logging this metric
248      * @param packageName  the package name for which the grants are being revoked.
249      * @param count        the number of items for which the grants have been revoked.
250      */
logPickerChoiceGrantsRemovedCount(InstanceId instanceId, int uid, String packageName, int count)251     public static void logPickerChoiceGrantsRemovedCount(InstanceId instanceId, int uid,
252             String packageName, int count) {
253         LOGGER.logWithInstanceIdAndPosition(NonUiEvent.PHOTO_PICKER_GRANTS_REVOKED_COUNT, uid,
254                 packageName, instanceId, count);
255     }
256 
257     /**
258      * Log metrics for total count of grants previously added for the package.
259      * @param instanceId   an identifier for the current session
260      * @param uid          the uid of the MediaProvider logging this metric
261      * @param packageName  the package name for which the grants are being initialized.
262      * @param count        the number of items for which the grants have been initialized.
263      */
logPickerChoiceInitGrantsCount(InstanceId instanceId, int uid, String packageName, int count)264     public static void logPickerChoiceInitGrantsCount(InstanceId instanceId, int uid,
265             String packageName, int count) {
266         LOGGER.logWithInstanceIdAndPosition(NonUiEvent.PHOTO_PICKER_INIT_GRANTS_COUNT, uid,
267                 packageName, instanceId, count);
268     }
269 
270 }
271