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.car.builtin.util;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.annotation.UserIdInt;
22 import android.app.usage.Flags;
23 import android.app.usage.UsageStatsManager;
24 import android.os.PersistableBundle;
25 import android.util.Log;
26 
27 import java.util.Objects;
28 
29 /**
30  * Wrapper for UsageStatsManager.
31  *
32  * @hide
33  */
34 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
35 public final class UsageStatsManagerHelper {
36 
37     private static final String TAG = UsageStatsManagerHelper.class.getSimpleName();
38     private static final boolean DEBUG = false;
39 
UsageStatsManagerHelper()40     private UsageStatsManagerHelper() {
41         throw new UnsupportedOperationException();
42     }
43 
44     /**
45      * Reports user interaction with a given package in the given user with {@code extras}.
46      * Check {@link UsageStatsManager#reportUserInteraction(String, int, PersistableBundle)}.
47      * If {@code Flags.userInteractionTypeApi()} is {@code false}, fall back to
48      * {@link UsageStatsManager#reportUserInteraction(String, int)}.
49      *
50      * <p>
51      * Note: The structure of {@code extras} is a {@link PersistableBundle} with the
52      * category {@link UsageStatsManager#EXTRA_EVENT_CATEGORY} and the action
53      * {@link UsageStatsManager#EXTRA_EVENT_ACTION}.
54      * Category provides additional detail about the user interaction, the value
55      * is defined in namespace based. Example: android.app.notification could be used to
56      * indicate that the reported user interaction is related to notification. Action
57      * indicates the general action that performed.
58      * </p>
59      *
60      * @param usageStatsManager {@link UsageStatsManager} instance used to report the user
61      *                          interaction
62      * @param packageName The package name of the app
63      * @param userId The user id who triggers the user interaction
64      * @param extras The {@link PersistableBundle} that will be used to specify the
65      *               extra details for the user interaction event. The {@link PersistableBundle}
66      *               must contain the extras {@link UsageStatsManager#EXTRA_EVENT_CATEGORY},
67      *               {@link UsageStatsManager#EXTRA_EVENT_ACTION}. Cannot be empty.
68      */
reportUserInteraction(@onNull UsageStatsManager usageStatsManager, @NonNull String packageName, @UserIdInt int userId, @NonNull PersistableBundle extras)69     public static void reportUserInteraction(@NonNull UsageStatsManager usageStatsManager,
70             @NonNull String packageName, @UserIdInt int userId, @NonNull PersistableBundle extras) {
71         if (DEBUG) {
72             Log.d(TAG, "usageStatsManager#reportUserInteraction package=" + packageName
73                     + " userId=" + userId + " extras=" + extras);
74         }
75         Objects.requireNonNull(usageStatsManager);
76         Objects.requireNonNull(packageName);
77         if (Flags.userInteractionTypeApi()) {
78             Objects.requireNonNull(extras);
79             usageStatsManager.reportUserInteraction(packageName, userId, extras);
80         } else {
81             usageStatsManager.reportUserInteraction(packageName, userId);
82         }
83     }
84 
85 }
86