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