1 /*
2  * Copyright (C) 2018 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.wifi;
18 
19 import android.Manifest;
20 import android.app.AppOpsManager;
21 import android.content.Context;
22 
23 import com.android.internal.util.ArrayUtils;
24 import com.android.settings.applications.AppStateAppOpsBridge;
25 import com.android.settingslib.applications.ApplicationsState;
26 import com.android.settingslib.applications.ApplicationsState.AppEntry;
27 import com.android.settingslib.applications.ApplicationsState.AppFilter;
28 
29 import java.util.List;
30 /*
31  * Connects info of apps that change wifi state to the ApplicationsState. Wraps around the generic
32  * AppStateAppOpsBridge class to tailor to the semantics of CHANGE_WIFI_STATE. Also provides app
33  * filters that can use the info.
34  */
35 public class AppStateChangeWifiStateBridge extends AppStateAppOpsBridge {
36 
37     private static final String TAG = "AppStateChangeWifiStateBridge";
38     private static final int APP_OPS_OP_CODE = AppOpsManager.OP_CHANGE_WIFI_STATE;
39     private static final String PM_CHANGE_WIFI_STATE = Manifest.permission.CHANGE_WIFI_STATE;
40     private static final String PM_NETWORK_SETTINGS = Manifest.permission.NETWORK_SETTINGS;
41 
42     private static final String[] PM_PERMISSIONS = {
43             PM_CHANGE_WIFI_STATE
44     };
45 
AppStateChangeWifiStateBridge(Context context, ApplicationsState appState, Callback callback)46     public AppStateChangeWifiStateBridge(Context context, ApplicationsState appState, Callback
47             callback) {
48         super(context, appState, callback, APP_OPS_OP_CODE, PM_PERMISSIONS);
49     }
50 
51     @Override
updateExtraInfo(AppEntry app, String pkg, int uid)52     protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
53         app.extraInfo = getWifiSettingsInfo(pkg, uid);
54     }
55 
56     @Override
loadAllExtraInfo()57     protected void loadAllExtraInfo() {
58         final List<AppEntry> allApps = mAppSession.getAllApps();
59         for (AppEntry entry : allApps) {
60             updateExtraInfo(entry, entry.info.packageName, entry.info.uid);
61         }
62     }
63 
getWifiSettingsInfo(String pkg, int uid)64     public WifiSettingsState getWifiSettingsInfo(String pkg, int uid) {
65         PermissionState permissionState = super.getPermissionInfo(pkg, uid);
66         return new WifiSettingsState(permissionState);
67     }
68 
69     public static class WifiSettingsState extends AppStateAppOpsBridge.PermissionState {
WifiSettingsState(PermissionState permissionState)70         public WifiSettingsState(PermissionState permissionState) {
71             super(permissionState.packageName, permissionState.userHandle);
72             this.packageInfo = permissionState.packageInfo;
73             this.appOpMode = permissionState.appOpMode;
74             this.permissionDeclared = permissionState.permissionDeclared;
75             this.staticPermissionGranted = permissionState.staticPermissionGranted;
76         }
77     }
78 
79     public static final AppFilter FILTER_CHANGE_WIFI_STATE = new AppFilter() {
80         @Override
81         public void init() {
82         }
83 
84         @Override
85         public boolean filterApp(AppEntry info) {
86             if (info == null || info.extraInfo == null) {
87                 return false;
88             }
89             WifiSettingsState wifiSettingsState = (WifiSettingsState) info.extraInfo;
90             if (wifiSettingsState.packageInfo != null) {
91                 final String[] requestedPermissions
92                         = wifiSettingsState.packageInfo.requestedPermissions;
93                 if (ArrayUtils.contains(requestedPermissions, PM_NETWORK_SETTINGS)) {
94                     /*
95                      * NETWORK_SETTINGS permission trumps CHANGE_WIFI_CONFIG, so remove this from
96                      * the list.
97                     */
98                     return false;
99                 }
100             }
101             return wifiSettingsState.permissionDeclared;
102         }
103     };
104 }
105