1 /* 2 * Copyright (C) 2015 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 package com.android.settings.applications; 17 18 import android.app.AlertDialog; 19 import android.app.AppOpsManager; 20 import android.content.ActivityNotFoundException; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.PackageManager; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.provider.Settings; 28 import android.support.v14.preference.SwitchPreference; 29 import android.support.v7.preference.Preference; 30 import android.support.v7.preference.Preference.OnPreferenceChangeListener; 31 import android.support.v7.preference.Preference.OnPreferenceClickListener; 32 import android.util.Log; 33 34 import com.android.internal.logging.MetricsProto.MetricsEvent; 35 import com.android.settings.R; 36 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState; 37 import com.android.settings.applications.AppStateWriteSettingsBridge.WriteSettingsState; 38 import com.android.settingslib.applications.ApplicationsState.AppEntry; 39 40 import java.util.List; 41 42 public class WriteSettingsDetails extends AppInfoWithHeader implements OnPreferenceChangeListener, 43 OnPreferenceClickListener { 44 45 private static final String KEY_APP_OPS_PREFERENCE_SCREEN = "app_ops_preference_screen"; 46 private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch"; 47 private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference"; 48 private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description"; 49 private static final String LOG_TAG = "WriteSettingsDetails"; 50 51 private static final int [] APP_OPS_OP_CODE = { 52 AppOpsManager.OP_WRITE_SETTINGS 53 }; 54 55 // Use a bridge to get the overlay details but don't initialize it to connect with all state. 56 // TODO: Break out this functionality into its own class. 57 private AppStateWriteSettingsBridge mAppBridge; 58 private AppOpsManager mAppOpsManager; 59 private SwitchPreference mSwitchPref; 60 private Preference mWriteSettingsPrefs; 61 private Preference mWriteSettingsDesc; 62 private Intent mSettingsIntent; 63 private WriteSettingsState mWriteSettingsState; 64 65 @Override onCreate(Bundle savedInstanceState)66 public void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 69 Context context = getActivity(); 70 mAppBridge = new AppStateWriteSettingsBridge(context, mState, null); 71 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 72 73 addPreferencesFromResource(R.xml.app_ops_permissions_details); 74 mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH); 75 mWriteSettingsPrefs = findPreference(KEY_APP_OPS_SETTINGS_PREFS); 76 mWriteSettingsDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC); 77 78 getPreferenceScreen().setTitle(R.string.write_settings); 79 mSwitchPref.setTitle(R.string.permit_write_settings); 80 mWriteSettingsPrefs.setTitle(R.string.write_settings_preference); 81 mWriteSettingsDesc.setSummary(R.string.write_settings_description); 82 83 mSwitchPref.setOnPreferenceChangeListener(this); 84 mWriteSettingsPrefs.setOnPreferenceClickListener(this); 85 86 mSettingsIntent = new Intent(Intent.ACTION_MAIN) 87 .addCategory(Settings.INTENT_CATEGORY_USAGE_ACCESS_CONFIG) 88 .setPackage(mPackageName); 89 } 90 91 @Override onPreferenceClick(Preference preference)92 public boolean onPreferenceClick(Preference preference) { 93 if (preference == mWriteSettingsPrefs) { 94 if (mSettingsIntent != null) { 95 try { 96 getActivity().startActivityAsUser(mSettingsIntent, new UserHandle(mUserId)); 97 } catch (ActivityNotFoundException e) { 98 Log.w(LOG_TAG, "Unable to launch write system settings " + mSettingsIntent, e); 99 } 100 } 101 return true; 102 } 103 return false; 104 } 105 106 @Override onPreferenceChange(Preference preference, Object newValue)107 public boolean onPreferenceChange(Preference preference, Object newValue) { 108 if (preference == mSwitchPref) { 109 if (mWriteSettingsState != null && (Boolean) newValue != mWriteSettingsState 110 .isPermissible()) { 111 setCanWriteSettings(!mWriteSettingsState.isPermissible()); 112 refreshUi(); 113 } 114 return true; 115 } 116 return false; 117 } 118 setCanWriteSettings(boolean newState)119 private void setCanWriteSettings(boolean newState) { 120 mAppOpsManager.setMode(AppOpsManager.OP_WRITE_SETTINGS, 121 mPackageInfo.applicationInfo.uid, mPackageName, newState 122 ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED); 123 } 124 canWriteSettings(String pkgName)125 private boolean canWriteSettings(String pkgName) { 126 int result = mAppOpsManager.noteOpNoThrow(AppOpsManager.OP_WRITE_SETTINGS, 127 mPackageInfo.applicationInfo.uid, pkgName); 128 if (result == AppOpsManager.MODE_ALLOWED) { 129 return true; 130 } 131 132 return false; 133 } 134 135 @Override refreshUi()136 protected boolean refreshUi() { 137 mWriteSettingsState = mAppBridge.getWriteSettingsInfo(mPackageName, 138 mPackageInfo.applicationInfo.uid); 139 140 boolean canWrite = mWriteSettingsState.isPermissible(); 141 mSwitchPref.setChecked(canWrite); 142 // you can't ask a user for a permission you didn't even declare! 143 mSwitchPref.setEnabled(mWriteSettingsState.permissionDeclared); 144 mWriteSettingsPrefs.setEnabled(canWrite); 145 getPreferenceScreen().removePreference(mWriteSettingsPrefs); 146 147 return true; 148 } 149 150 @Override createDialog(int id, int errorCode)151 protected AlertDialog createDialog(int id, int errorCode) { 152 return null; 153 } 154 155 @Override getMetricsCategory()156 protected int getMetricsCategory() { 157 return MetricsEvent.SYSTEM_ALERT_WINDOW_APPS; 158 } 159 getSummary(Context context, AppEntry entry)160 public static CharSequence getSummary(Context context, AppEntry entry) { 161 WriteSettingsState state; 162 if (entry.extraInfo instanceof WriteSettingsState) { 163 state = (WriteSettingsState) entry.extraInfo; 164 } else if (entry.extraInfo instanceof PermissionState) { 165 state = new WriteSettingsState((PermissionState) entry.extraInfo); 166 } else { 167 state = new AppStateWriteSettingsBridge(context, null, null).getWriteSettingsInfo( 168 entry.info.packageName, entry.info.uid); 169 } 170 171 return getSummary(context, state); 172 } 173 getSummary(Context context, WriteSettingsState writeSettingsState)174 public static CharSequence getSummary(Context context, WriteSettingsState writeSettingsState) { 175 return context.getString(writeSettingsState.isPermissible() ? R.string.write_settings_on : 176 R.string.write_settings_off); 177 } 178 getSummary(Context context, String pkg)179 public static CharSequence getSummary(Context context, String pkg) { 180 // first check if pkg is a system pkg 181 boolean isSystem = false; 182 PackageManager packageManager = context.getPackageManager(); 183 try { 184 ApplicationInfo appInfo = packageManager.getApplicationInfo(pkg, 0); 185 if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 186 isSystem = true; 187 } 188 } catch (PackageManager.NameNotFoundException e) { 189 // pkg doesn't even exist? 190 Log.w(LOG_TAG, "Package " + pkg + " not found", e); 191 return context.getString(R.string.write_settings_off); 192 } 193 194 AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context 195 .APP_OPS_SERVICE); 196 List<AppOpsManager.PackageOps> packageOps = appOpsManager.getPackagesForOps( 197 APP_OPS_OP_CODE); 198 if (packageOps == null) { 199 return context.getString(R.string.write_settings_off); 200 } 201 202 int uid = isSystem ? 0 : -1; 203 for (AppOpsManager.PackageOps packageOp : packageOps) { 204 if (pkg.equals(packageOp.getPackageName())) { 205 uid = packageOp.getUid(); 206 break; 207 } 208 } 209 210 if (uid == -1) { 211 return context.getString(R.string.write_settings_off); 212 } 213 214 int mode = appOpsManager.noteOpNoThrow(AppOpsManager.OP_WRITE_SETTINGS, uid, pkg); 215 return context.getString((mode == AppOpsManager.MODE_ALLOWED) ? 216 R.string.write_settings_on : R.string.write_settings_off); 217 } 218 } 219