Home | History | Annotate | Download | only in applications
      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 import java.util.ArrayList;
     27 import java.util.List;
     28 
     29 /*
     30  * Connects info of apps that draw overlay to the ApplicationsState. Wraps around the generic
     31  * AppStateAppOpsBridge class to tailor to the semantics of SYSTEM_ALERT_WINDOW. Also provides app
     32  * filters that can use the info.
     33  */
     34 public class AppStateOverlayBridge extends AppStateAppOpsBridge {
     35 
     36     private static final String TAG = "AppStateOverlayBridge";
     37     private static final int APP_OPS_OP_CODE = AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
     38     private static final String PM_SYSTEM_ALERT_WINDOW = Manifest.permission.SYSTEM_ALERT_WINDOW;
     39     private static final String[] PM_PERMISSION = {
     40             PM_SYSTEM_ALERT_WINDOW
     41     };
     42 
     43     public AppStateOverlayBridge(Context context, ApplicationsState appState, Callback callback) {
     44         super(context, appState, callback, APP_OPS_OP_CODE, PM_PERMISSION);
     45     }
     46 
     47     @Override
     48     protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
     49         app.extraInfo = getOverlayInfo(pkg, uid);
     50     }
     51 
     52     public OverlayState getOverlayInfo(String pkg, int uid) {
     53         PermissionState permissionState = super.getPermissionInfo(pkg, uid);
     54         return new OverlayState(permissionState);
     55     }
     56 
     57     // TODO: figure out how to filter out system apps for this method
     58     public int getNumberOfPackagesWithPermission() {
     59         return super.getNumPackagesDeclaredPermission();
     60     }
     61 
     62     // TODO: figure out how to filter out system apps for this method
     63     public int getNumberOfPackagesCanDrawOverlay() {
     64         return super.getNumPackagesAllowedByAppOps();
     65     }
     66 
     67     public static class OverlayState extends AppStateAppOpsBridge.PermissionState {
     68         public final boolean controlEnabled;
     69 
     70         private static final List<String> DISABLE_PACKAGE_LIST = new ArrayList<>();
     71 
     72         static {
     73             DISABLE_PACKAGE_LIST.add("com.android.systemui");
     74         }
     75 
     76         public OverlayState(PermissionState permissionState) {
     77             super(permissionState.packageName, permissionState.userHandle);
     78             this.packageInfo = permissionState.packageInfo;
     79             this.appOpMode = permissionState.appOpMode;
     80             this.permissionDeclared = permissionState.permissionDeclared;
     81             this.staticPermissionGranted = permissionState.staticPermissionGranted;
     82             controlEnabled = !DISABLE_PACKAGE_LIST.contains(permissionState.packageName);
     83         }
     84     }
     85 
     86     public static final AppFilter FILTER_SYSTEM_ALERT_WINDOW = new AppFilter() {
     87         @Override
     88         public void init() {
     89         }
     90 
     91         @Override
     92         public boolean filterApp(AppEntry info) {
     93             return info.extraInfo != null;
     94         }
     95     };
     96 }
     97