1 /*
2  * Copyright (C) 2015 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 package com.android.settings.applications;
17 
18 import android.Manifest;
19 import android.app.AppOpsManager;
20 import android.content.Context;
21 
22 import com.android.settingslib.applications.ApplicationsState;
23 import com.android.settingslib.applications.ApplicationsState.AppEntry;
24 import com.android.settingslib.applications.ApplicationsState.AppFilter;
25 
26 /*
27  * Connects info of apps that draw overlay to the ApplicationsState. Wraps around the generic
28  * AppStateAppOpsBridge class to tailor to the semantics of SYSTEM_ALERT_WINDOW. Also provides app
29  * filters that can use the info.
30  */
31 public class AppStateOverlayBridge extends AppStateAppOpsBridge {
32 
33     private static final String TAG = "AppStateOverlayBridge";
34     private static final int APP_OPS_OP_CODE = AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
35     private static final String PM_SYSTEM_ALERT_WINDOW = Manifest.permission.SYSTEM_ALERT_WINDOW;
36     private static final String[] PM_PERMISSION = {
37             PM_SYSTEM_ALERT_WINDOW
38     };
39 
AppStateOverlayBridge(Context context, ApplicationsState appState, Callback callback)40     public AppStateOverlayBridge(Context context, ApplicationsState appState, Callback callback) {
41         super(context, appState, callback, APP_OPS_OP_CODE, PM_PERMISSION);
42     }
43 
44     @Override
updateExtraInfo(AppEntry app, String pkg, int uid)45     protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
46         app.extraInfo = getOverlayInfo(pkg, uid);
47     }
48 
getOverlayInfo(String pkg, int uid)49     public OverlayState getOverlayInfo(String pkg, int uid) {
50         PermissionState permissionState = super.getPermissionInfo(pkg, uid);
51         return new OverlayState(permissionState);
52     }
53 
54     // TODO: figure out how to filter out system apps for this method
getNumberOfPackagesWithPermission()55     public int getNumberOfPackagesWithPermission() {
56         return super.getNumPackagesDeclaredPermission();
57     }
58 
59     // TODO: figure out how to filter out system apps for this method
getNumberOfPackagesCanDrawOverlay()60     public int getNumberOfPackagesCanDrawOverlay() {
61         return super.getNumPackagesAllowedByAppOps();
62     }
63 
64     public static class OverlayState extends AppStateAppOpsBridge.PermissionState {
65 
OverlayState(PermissionState permissionState)66         public OverlayState(PermissionState permissionState) {
67             super(permissionState.packageName, permissionState.userHandle);
68             this.packageInfo = permissionState.packageInfo;
69             this.appOpMode = permissionState.appOpMode;
70             this.permissionDeclared = permissionState.permissionDeclared;
71             this.staticPermissionGranted = permissionState.staticPermissionGranted;
72         }
73     }
74 
75     public static final AppFilter FILTER_SYSTEM_ALERT_WINDOW = new AppFilter() {
76         @Override
77         public void init() {
78         }
79 
80         @Override
81         public boolean filterApp(AppEntry info) {
82             return info.extraInfo != null;
83         }
84     };
85 }
86