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.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 import com.android.settingslib.applications.ApplicationsState.AppEntry;
25 import com.android.settingslib.applications.ApplicationsState.AppFilter;
26 
27 import java.util.List;
28 
29 /**
30  * Retrieves information from {@link AppOpsManager} and {@link android.content.pm.PackageManager}
31  * regarding {@link AppOpsManager#OP_MANAGE_MEDIA} and
32  * {@link Manifest.permission#MANAGE_MEDIA}.
33  */
34 public class AppStateMediaManagementAppsBridge extends AppStateAppOpsBridge {
35 
36     private final AppOpsManager mAppOpsManager;
37 
AppStateMediaManagementAppsBridge(Context context, ApplicationsState appState, Callback callback)38     public AppStateMediaManagementAppsBridge(Context context, ApplicationsState appState,
39             Callback callback) {
40         super(context, appState, callback,
41                 AppOpsManager.strOpToOp(AppOpsManager.OPSTR_MANAGE_MEDIA),
42                 new String[]{Manifest.permission.MANAGE_MEDIA});
43 
44         mAppOpsManager = context.getSystemService(AppOpsManager.class);
45     }
46 
47     @Override
updateExtraInfo(AppEntry app, String pkg, int uid)48     protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
49         app.extraInfo = createPermissionState(pkg, uid);
50     }
51 
52     @Override
loadAllExtraInfo()53     protected void loadAllExtraInfo() {
54         super.loadAllExtraInfo();
55         final List<AppEntry> allApps = mAppSession.getAllApps();
56         final int appCount = allApps.size();
57         for (int i = 0; i < appCount; i++) {
58             final AppEntry appEntry = allApps.get(i);
59             if (appEntry.extraInfo instanceof PermissionState) {
60                 updateExtraInfo(appEntry, appEntry.info.packageName, appEntry.info.uid);
61             }
62         }
63     }
64 
65     /**
66      * Returns information regarding {@link Manifest.permission#MANAGE_MEDIA} for the given
67      * package and uid.
68      */
createPermissionState(String packageName, int uid)69     public PermissionState createPermissionState(String packageName, int uid) {
70         final PermissionState permissionState = getPermissionInfo(packageName, uid);
71         permissionState.appOpMode = mAppOpsManager.unsafeCheckOpNoThrow(
72                 AppOpsManager.OPSTR_MANAGE_MEDIA, uid, packageName);
73         return permissionState;
74     }
75 
76     /**
77      * Used by {@link com.android.settings.applications.manageapplications.AppFilterRegistry} to
78      * determine which apps get to appear on the Special App Access list.
79      */
80     public static final AppFilter FILTER_MEDIA_MANAGEMENT_APPS = new AppFilter() {
81         @Override
82         public void init() {
83         }
84 
85         @Override
86         public boolean filterApp(AppEntry info) {
87             return info.extraInfo != null;
88         }
89     };
90 }
91