1 /* 2 * Copyright (C) 2020 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.settings.applications; 18 19 import android.Manifest; 20 import android.app.AppOpsManager; 21 import android.content.Context; 22 23 import com.android.settingslib.applications.ApplicationsState; 24 25 import java.util.List; 26 27 /** 28 * Retrieves information from {@link AppOpsManager} and {@link android.content.pm.PackageManager} 29 * regarding {@link AppOpsManager#OP_MANAGE_EXTERNAL_STORAGE} and 30 * {@link Manifest.permission#MANAGE_EXTERNAL_STORAGE}. 31 */ 32 public class AppStateManageExternalStorageBridge extends AppStateAppOpsBridge { 33 private static final String APP_OP_STR = AppOpsManager.OPSTR_MANAGE_EXTERNAL_STORAGE; 34 private static final String[] PERMISSIONS = { 35 Manifest.permission.MANAGE_EXTERNAL_STORAGE 36 }; 37 38 private final AppOpsManager mAppOpsManager; 39 AppStateManageExternalStorageBridge(Context context, ApplicationsState appState, Callback callback)40 public AppStateManageExternalStorageBridge(Context context, ApplicationsState appState, 41 Callback callback) { 42 super(context, appState, callback, AppOpsManager.strOpToOp(APP_OP_STR), PERMISSIONS); 43 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 44 } 45 46 @Override updateExtraInfo(ApplicationsState.AppEntry app, String pkg, int uid)47 protected void updateExtraInfo(ApplicationsState.AppEntry app, String pkg, int uid) { 48 app.extraInfo = getManageExternalStoragePermState(pkg, uid); 49 } 50 51 @Override loadAllExtraInfo()52 protected void loadAllExtraInfo() { 53 super.loadAllExtraInfo(); 54 List<ApplicationsState.AppEntry> apps = mAppSession.getAllApps(); 55 for (ApplicationsState.AppEntry app : apps) { 56 if (app.extraInfo instanceof PermissionState) { 57 ((PermissionState) app.extraInfo).appOpMode = mAppOpsManager.unsafeCheckOpNoThrow( 58 APP_OP_STR, app.info.uid, app.info.packageName); 59 } 60 } 61 } 62 63 @Override getPermissionInfo(String pkg, int uid)64 public PermissionState getPermissionInfo(String pkg, int uid) { 65 PermissionState ps = super.getPermissionInfo(pkg, uid); 66 ps.appOpMode = mAppOpsManager.unsafeCheckOpNoThrow(APP_OP_STR, uid, pkg); 67 return ps; 68 } 69 70 /** 71 * Returns the MANAGE_EXTERNAL_STORAGE {@link AppStateAppOpsBridge.PermissionState} object 72 * associated with the given package and UID. 73 */ getManageExternalStoragePermState(String pkg, int uid)74 public PermissionState getManageExternalStoragePermState(String pkg, int uid) { 75 return getPermissionInfo(pkg, uid); 76 } 77 78 /** 79 * Used by {@link com.android.settings.applications.manageapplications.AppFilterRegistry} to 80 * determine which apps get to appear on the Special App Access list. 81 */ 82 public static final ApplicationsState.AppFilter FILTER_MANAGE_EXTERNAL_STORAGE = 83 new ApplicationsState.AppFilter() { 84 @Override 85 public void init() { 86 } 87 88 @Override 89 public boolean filterApp(ApplicationsState.AppEntry info) { 90 // If extraInfo != null, it means that the app has declared 91 // Manifest.permission.MANAGE_EXTERNAL_STORAGE and therefore it should appear on our 92 // list 93 return info.extraInfo != null; 94 } 95 }; 96 } 97