1 /*
2  * Copyright (C) 2023 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  * Connects app op info to the ApplicationsState. Extends {@link AppStateAppOpsBridge} to tailor
31  * to the semantics of {@link Manifest.permission#TURN_SCREEN_ON}.
32  * Also provides app filters that can use the info.
33  */
34 public class AppStateTurnScreenOnBridge extends AppStateAppOpsBridge {
35     private static final String APP_OP_STR = AppOpsManager.OPSTR_TURN_SCREEN_ON;
36     private static final String[] PERMISSIONS = {
37             Manifest.permission.TURN_SCREEN_ON
38     };
39 
40     private AppOpsManager mAppOpsManager;
41 
AppStateTurnScreenOnBridge(Context context, ApplicationsState appState, Callback callback)42     public AppStateTurnScreenOnBridge(Context context, ApplicationsState appState,
43             Callback callback) {
44         super(context, appState, callback, AppOpsManager.strOpToOp(APP_OP_STR), PERMISSIONS);
45         mAppOpsManager = context.getSystemService(AppOpsManager.class);
46     }
47 
48     @Override
updateExtraInfo(AppEntry app, String pkg, int uid)49     protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
50         app.extraInfo = getPermissionInfo(pkg, uid);
51     }
52 
53     @Override
loadAllExtraInfo()54     protected void loadAllExtraInfo() {
55         super.loadAllExtraInfo();
56         final List<AppEntry> apps = mAppSession.getAllApps();
57         for (AppEntry app : apps) {
58             if (app.extraInfo instanceof PermissionState) {
59                 ((PermissionState) app.extraInfo).appOpMode = mAppOpsManager.unsafeCheckOpNoThrow(
60                         APP_OP_STR, app.info.uid, app.info.packageName);
61             }
62         }
63     }
64 
65     @Override
getPermissionInfo(String pkg, int uid)66     public PermissionState getPermissionInfo(String pkg, int uid) {
67         PermissionState ps = super.getPermissionInfo(pkg, uid);
68         ps.appOpMode = mAppOpsManager.unsafeCheckOpNoThrow(APP_OP_STR, uid, pkg);
69         return ps;
70     }
71 
72     public static final AppFilter FILTER_TURN_SCREEN_ON_APPS = new AppFilter() {
73 
74         @Override
75         public void init() {
76         }
77 
78         @Override
79         public boolean filterApp(AppEntry info) {
80             // If extraInfo != null, it means that the app has declared
81             // Manifest.permission.TURN_SCREEN_ON and therefore it should appear on our
82             // list
83             return info.extraInfo != null;
84         }
85     };
86 }
87