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