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.app.AppOpsManager; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.os.Bundle; 23 24 import androidx.appcompat.app.AlertDialog; 25 import androidx.preference.Preference; 26 import androidx.preference.Preference.OnPreferenceChangeListener; 27 import androidx.preference.SwitchPreference; 28 29 import com.android.settings.R; 30 import com.android.settings.applications.AppInfoWithHeader; 31 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState; 32 import com.android.settings.overlay.FeatureFactory; 33 import com.android.settings.wifi.AppStateChangeWifiStateBridge.WifiSettingsState; 34 import com.android.settingslib.applications.ApplicationsState.AppEntry; 35 36 public class ChangeWifiStateDetails extends AppInfoWithHeader 37 implements OnPreferenceChangeListener { 38 39 private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch"; 40 private static final String LOG_TAG = "ChangeWifiStateDetails"; 41 42 private AppStateChangeWifiStateBridge mAppBridge; 43 private AppOpsManager mAppOpsManager; 44 private SwitchPreference mSwitchPref; 45 private WifiSettingsState mWifiSettingsState; 46 47 @Override onCreate(Bundle savedInstanceState)48 public void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 final Context context = getActivity(); 51 mAppBridge = new AppStateChangeWifiStateBridge(context, mState, null); 52 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 53 54 // find preferences 55 addPreferencesFromResource(R.xml.change_wifi_state_details); 56 mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH); 57 58 // set title/summary for all of them 59 mSwitchPref.setTitle(R.string.change_wifi_state_app_detail_switch); 60 61 // install event listeners 62 mSwitchPref.setOnPreferenceChangeListener(this); 63 } 64 65 @Override createDialog(int id, int errorCode)66 protected AlertDialog createDialog(int id, int errorCode) { 67 return null; 68 } 69 70 @Override getMetricsCategory()71 public int getMetricsCategory() { 72 return SettingsEnums.CONFIGURE_WIFI; 73 } 74 75 @Override onPreferenceChange(Preference preference, Object newValue)76 public boolean onPreferenceChange(Preference preference, Object newValue) { 77 if (preference == mSwitchPref) { 78 if (mWifiSettingsState != null && (Boolean) newValue 79 != mWifiSettingsState.isPermissible()) { 80 setCanChangeWifiState(!mWifiSettingsState.isPermissible()); 81 refreshUi(); 82 } 83 return true; 84 } 85 return false; 86 } 87 setCanChangeWifiState(boolean newState)88 private void setCanChangeWifiState(boolean newState) { 89 logSpecialPermissionChange(newState, mPackageName); 90 mAppOpsManager.setMode(AppOpsManager.OP_CHANGE_WIFI_STATE, 91 mPackageInfo.applicationInfo.uid, mPackageName, newState 92 ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); 93 } 94 logSpecialPermissionChange(boolean newState, String packageName)95 protected void logSpecialPermissionChange(boolean newState, String packageName) { 96 int logCategory = newState ? SettingsEnums.APP_SPECIAL_PERMISSION_SETTINGS_CHANGE_ALLOW 97 : SettingsEnums.APP_SPECIAL_PERMISSION_SETTINGS_CHANGE_DENY; 98 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider().action(getContext(), 99 logCategory, packageName); 100 } 101 102 @Override refreshUi()103 protected boolean refreshUi() { 104 if (mPackageInfo == null || mPackageInfo.applicationInfo == null) { 105 return false; 106 } 107 mWifiSettingsState = mAppBridge.getWifiSettingsInfo(mPackageName, 108 mPackageInfo.applicationInfo.uid); 109 110 boolean canChange = mWifiSettingsState.isPermissible(); 111 mSwitchPref.setChecked(canChange); 112 // you can't ask a user for a permission you didn't even declare! 113 mSwitchPref.setEnabled(mWifiSettingsState.permissionDeclared); 114 return true; 115 } 116 getSummary(Context context, AppEntry entry)117 public static CharSequence getSummary(Context context, AppEntry entry) { 118 WifiSettingsState state; 119 if (entry.extraInfo instanceof WifiSettingsState) { 120 state = (WifiSettingsState) entry.extraInfo; 121 } else if (entry.extraInfo instanceof PermissionState) { 122 state = new WifiSettingsState((PermissionState) entry.extraInfo); 123 } else { 124 state = new AppStateChangeWifiStateBridge(context, null, null).getWifiSettingsInfo( 125 entry.info.packageName, entry.info.uid); 126 } 127 return getSummary(context, state); 128 } 129 getSummary(Context context, WifiSettingsState wifiSettingsState)130 public static CharSequence getSummary(Context context, WifiSettingsState wifiSettingsState) { 131 return context.getString(wifiSettingsState.isPermissible() 132 ? R.string.app_permission_summary_allowed 133 : R.string.app_permission_summary_not_allowed); 134 } 135 } 136