1 /*
2  * Copyright (C) 2021 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.server.statusbar;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.Uri;
25 import android.os.UserHandle;
26 import android.util.ArraySet;
27 import android.util.IndentingPrintWriter;
28 import android.util.SparseArrayMap;
29 
30 import com.android.internal.annotations.GuardedBy;
31 import com.android.internal.annotations.VisibleForTesting;
32 
33 import java.io.FileDescriptor;
34 
35 /**
36  * Tracks user denials of requests from {@link StatusBarManagerService#requestAddTile}.
37  *
38  * After a certain number of denials for a particular pair (user,ComponentName), requests will be
39  * auto-denied without showing a dialog to the user.
40  */
41 public class TileRequestTracker {
42 
43     @VisibleForTesting
44     static final int MAX_NUM_DENIALS = 3;
45 
46     private final Context mContext;
47     private final Object mLock = new Object();
48 
49     @GuardedBy("mLock")
50     private final SparseArrayMap<ComponentName, Integer> mTrackingMap = new SparseArrayMap<>();
51     @GuardedBy("mLock")
52     private final ArraySet<ComponentName> mComponentsToRemove = new ArraySet<>();
53 
54     private final BroadcastReceiver mUninstallReceiver = new BroadcastReceiver() {
55 
56         @Override
57         public void onReceive(Context context, Intent intent) {
58             if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
59                 return;
60             }
61 
62             Uri data = intent.getData();
63             String packageName = data.getEncodedSchemeSpecificPart();
64 
65             if (!intent.hasExtra(Intent.EXTRA_UID)) {
66                 return;
67             }
68             int userId = UserHandle.getUserId(intent.getIntExtra(Intent.EXTRA_UID, -1));
69             synchronized (mLock) {
70                 mComponentsToRemove.clear();
71                 final int elementsForUser = mTrackingMap.numElementsForKey(userId);
72                 final int userKeyIndex = mTrackingMap.indexOfKey(userId);
73                 for (int compKeyIndex = 0; compKeyIndex < elementsForUser; compKeyIndex++) {
74                     ComponentName c = mTrackingMap.keyAt(userKeyIndex, compKeyIndex);
75                     if (c.getPackageName().equals(packageName)) {
76                         mComponentsToRemove.add(c);
77                     }
78                 }
79                 final int compsToRemoveNum = mComponentsToRemove.size();
80                 for (int i = 0; i < compsToRemoveNum; i++) {
81                     ComponentName c = mComponentsToRemove.valueAt(i);
82                     mTrackingMap.delete(userId, c);
83                 }
84             }
85         }
86     };
87 
TileRequestTracker(Context context)88     TileRequestTracker(Context context) {
89         mContext = context;
90 
91         IntentFilter intentFilter = new IntentFilter();
92         intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
93         intentFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
94         intentFilter.addDataScheme("package");
95         mContext.registerReceiverAsUser(mUninstallReceiver, UserHandle.ALL, intentFilter, null,
96                 null);
97     }
98 
99     /**
100      * Return whether this combination of {@code userId} and {@link ComponentName} should be
101      * auto-denied.
102      */
shouldBeDenied(int userId, ComponentName componentName)103     boolean shouldBeDenied(int userId, ComponentName componentName) {
104         synchronized (mLock) {
105             return mTrackingMap.getOrDefault(userId, componentName, 0) >= MAX_NUM_DENIALS;
106         }
107     }
108 
109     /**
110      * Add a new denial instance for a given {@code userId} and {@link ComponentName}.
111      */
addDenial(int userId, ComponentName componentName)112     void addDenial(int userId, ComponentName componentName) {
113         synchronized (mLock) {
114             int current = mTrackingMap.getOrDefault(userId, componentName, 0);
115             mTrackingMap.add(userId, componentName, current + 1);
116         }
117     }
118 
119     /**
120      * Reset the number of denied request for a given {@code userId} and {@link ComponentName}.
121      */
resetRequests(int userId, ComponentName componentName)122     void resetRequests(int userId, ComponentName componentName) {
123         synchronized (mLock) {
124             mTrackingMap.delete(userId, componentName);
125         }
126     }
127 
dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args)128     void dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args) {
129         pw.println("TileRequestTracker:");
130         pw.increaseIndent();
131         synchronized (mLock) {
132             mTrackingMap.forEach((user, componentName, value) -> {
133                 pw.println("user=" + user + ", " + componentName.toShortString() + ": " + value);
134             });
135         }
136         pw.decreaseIndent();
137     }
138 }
139