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.appinfo; 17 18 import android.app.AppOpsManager; 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.os.Bundle; 25 import android.provider.Settings; 26 27 import androidx.appcompat.app.AlertDialog; 28 import androidx.preference.Preference; 29 import androidx.preference.Preference.OnPreferenceChangeListener; 30 import androidx.preference.Preference.OnPreferenceClickListener; 31 import androidx.preference.SwitchPreference; 32 33 import com.android.settings.R; 34 import com.android.settings.applications.AppInfoWithHeader; 35 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState; 36 import com.android.settings.applications.AppStateWriteSettingsBridge; 37 import com.android.settings.applications.AppStateWriteSettingsBridge.WriteSettingsState; 38 import com.android.settings.overlay.FeatureFactory; 39 import com.android.settingslib.applications.ApplicationsState.AppEntry; 40 41 public class WriteSettingsDetails extends AppInfoWithHeader implements OnPreferenceChangeListener, 42 OnPreferenceClickListener { 43 44 private static final String KEY_APP_OPS_PREFERENCE_SCREEN = "app_ops_preference_screen"; 45 private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch"; 46 private static final String LOG_TAG = "WriteSettingsDetails"; 47 48 private static final int [] APP_OPS_OP_CODE = { 49 AppOpsManager.OP_WRITE_SETTINGS 50 }; 51 52 // Use a bridge to get the overlay details but don't initialize it to connect with all state. 53 // TODO: Break out this functionality into its own class. 54 private AppStateWriteSettingsBridge mAppBridge; 55 private AppOpsManager mAppOpsManager; 56 private SwitchPreference mSwitchPref; 57 private Intent mSettingsIntent; 58 private WriteSettingsState mWriteSettingsState; 59 60 @Override onCreate(Bundle savedInstanceState)61 public void onCreate(Bundle savedInstanceState) { 62 super.onCreate(savedInstanceState); 63 64 Context context = getActivity(); 65 mAppBridge = new AppStateWriteSettingsBridge(context, mState, null); 66 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 67 68 addPreferencesFromResource(R.xml.write_system_settings_permissions_details); 69 mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH); 70 71 mSwitchPref.setOnPreferenceChangeListener(this); 72 73 mSettingsIntent = new Intent(Intent.ACTION_MAIN) 74 .addCategory(Settings.INTENT_CATEGORY_USAGE_ACCESS_CONFIG) 75 .setPackage(mPackageName); 76 } 77 78 @Override onPreferenceClick(Preference preference)79 public boolean onPreferenceClick(Preference preference) { 80 return false; 81 } 82 83 @Override onPreferenceChange(Preference preference, Object newValue)84 public boolean onPreferenceChange(Preference preference, Object newValue) { 85 if (preference == mSwitchPref) { 86 if (mWriteSettingsState != null && (Boolean) newValue != mWriteSettingsState 87 .isPermissible()) { 88 setCanWriteSettings(!mWriteSettingsState.isPermissible()); 89 refreshUi(); 90 } 91 return true; 92 } 93 return false; 94 } 95 setCanWriteSettings(boolean newState)96 private void setCanWriteSettings(boolean newState) { 97 logSpecialPermissionChange(newState, mPackageName); 98 mAppOpsManager.setMode(AppOpsManager.OP_WRITE_SETTINGS, 99 mPackageInfo.applicationInfo.uid, mPackageName, newState 100 ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED); 101 } 102 logSpecialPermissionChange(boolean newState, String packageName)103 void logSpecialPermissionChange(boolean newState, String packageName) { 104 int logCategory = newState ? SettingsEnums.APP_SPECIAL_PERMISSION_SETTINGS_CHANGE_ALLOW 105 : SettingsEnums.APP_SPECIAL_PERMISSION_SETTINGS_CHANGE_DENY; 106 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider().action(getContext(), 107 logCategory, packageName); 108 } 109 canWriteSettings(String pkgName)110 private boolean canWriteSettings(String pkgName) { 111 int result = mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_SETTINGS, 112 mPackageInfo.applicationInfo.uid, pkgName); 113 if (result == AppOpsManager.MODE_ALLOWED) { 114 return true; 115 } 116 117 return false; 118 } 119 120 @Override refreshUi()121 protected boolean refreshUi() { 122 mWriteSettingsState = mAppBridge.getWriteSettingsInfo(mPackageName, 123 mPackageInfo.applicationInfo.uid); 124 125 boolean canWrite = mWriteSettingsState.isPermissible(); 126 mSwitchPref.setChecked(canWrite); 127 // you can't ask a user for a permission you didn't even declare! 128 mSwitchPref.setEnabled(mWriteSettingsState.permissionDeclared); 129 130 ResolveInfo resolveInfo = mPm.resolveActivityAsUser(mSettingsIntent, 131 PackageManager.GET_META_DATA, mUserId); 132 return true; 133 } 134 135 @Override createDialog(int id, int errorCode)136 protected AlertDialog createDialog(int id, int errorCode) { 137 return null; 138 } 139 140 @Override getMetricsCategory()141 public int getMetricsCategory() { 142 return SettingsEnums.SYSTEM_ALERT_WINDOW_APPS; 143 } 144 getSummary(Context context, AppEntry entry)145 public static CharSequence getSummary(Context context, AppEntry entry) { 146 WriteSettingsState state; 147 if (entry.extraInfo instanceof WriteSettingsState) { 148 state = (WriteSettingsState) entry.extraInfo; 149 } else if (entry.extraInfo instanceof PermissionState) { 150 state = new WriteSettingsState((PermissionState) entry.extraInfo); 151 } else { 152 state = new AppStateWriteSettingsBridge(context, null, null).getWriteSettingsInfo( 153 entry.info.packageName, entry.info.uid); 154 } 155 156 return getSummary(context, state); 157 } 158 getSummary(Context context, WriteSettingsState writeSettingsState)159 public static CharSequence getSummary(Context context, WriteSettingsState writeSettingsState) { 160 return context.getString(writeSettingsState.isPermissible() 161 ? R.string.app_permission_summary_allowed 162 : R.string.app_permission_summary_not_allowed); 163 } 164 } 165