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.AppStateOverlayBridge.OverlayState; 38 import com.android.settingslib.applications.ApplicationsState.AppEntry; 39 40 public class DrawOverlayDetails extends AppInfoWithHeader implements OnPreferenceChangeListener, 41 OnPreferenceClickListener { 42 43 private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch"; 44 private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference"; 45 private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description"; 46 private static final String LOG_TAG = "DrawOverlayDetails"; 47 48 private static final int [] APP_OPS_OP_CODE = { 49 AppOpsManager.OP_SYSTEM_ALERT_WINDOW 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 AppStateOverlayBridge mOverlayBridge; 55 private AppOpsManager mAppOpsManager; 56 private SwitchPreference mSwitchPref; 57 private Preference mOverlayPrefs; 58 private Preference mOverlayDesc; 59 private Intent mSettingsIntent; 60 private OverlayState mOverlayState; 61 62 @Override onCreate(Bundle savedInstanceState)63 public void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 66 Context context = getActivity(); 67 mOverlayBridge = new AppStateOverlayBridge(context, mState, null); 68 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 69 70 // find preferences 71 addPreferencesFromResource(R.xml.app_ops_permissions_details); 72 mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH); 73 mOverlayPrefs = findPreference(KEY_APP_OPS_SETTINGS_PREFS); 74 mOverlayDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC); 75 76 // set title/summary for all of them 77 getPreferenceScreen().setTitle(R.string.draw_overlay); 78 mSwitchPref.setTitle(R.string.permit_draw_overlay); 79 mOverlayPrefs.setTitle(R.string.app_overlay_permission_preference); 80 mOverlayDesc.setSummary(R.string.allow_overlay_description); 81 82 // install event listeners 83 mSwitchPref.setOnPreferenceChangeListener(this); 84 mOverlayPrefs.setOnPreferenceClickListener(this); 85 86 mSettingsIntent = new Intent(Intent.ACTION_MAIN) 87 .setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); 88 } 89 90 @Override onPreferenceClick(Preference preference)91 public boolean onPreferenceClick(Preference preference) { 92 if (preference == mOverlayPrefs) { 93 if (mSettingsIntent != null) { 94 try { 95 getActivity().startActivityAsUser(mSettingsIntent, new UserHandle(mUserId)); 96 } catch (ActivityNotFoundException e) { 97 Log.w(LOG_TAG, "Unable to launch app draw overlay settings " + mSettingsIntent, e); 98 } 99 } 100 return true; 101 } 102 return false; 103 } 104 105 @Override onPreferenceChange(Preference preference, Object newValue)106 public boolean onPreferenceChange(Preference preference, Object newValue) { 107 if (preference == mSwitchPref) { 108 if (mOverlayState != null && (Boolean) newValue != mOverlayState.isPermissible()) { 109 setCanDrawOverlay(!mOverlayState.isPermissible()); 110 refreshUi(); 111 } 112 return true; 113 } 114 return false; 115 } 116 setCanDrawOverlay(boolean newState)117 private void setCanDrawOverlay(boolean newState) { 118 mAppOpsManager.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, 119 mPackageInfo.applicationInfo.uid, mPackageName, newState 120 ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED); 121 } 122 canDrawOverlay(String pkgName)123 private boolean canDrawOverlay(String pkgName) { 124 int result = mAppOpsManager.noteOpNoThrow(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, 125 mPackageInfo.applicationInfo.uid, pkgName); 126 if (result == AppOpsManager.MODE_ALLOWED) { 127 return true; 128 } 129 130 return false; 131 } 132 133 @Override refreshUi()134 protected boolean refreshUi() { 135 mOverlayState = mOverlayBridge.getOverlayInfo(mPackageName, 136 mPackageInfo.applicationInfo.uid); 137 138 boolean isAllowed = mOverlayState.isPermissible(); 139 mSwitchPref.setChecked(isAllowed); 140 // you cannot ask a user to grant you a permission you did not have! 141 mSwitchPref.setEnabled(mOverlayState.permissionDeclared); 142 mOverlayPrefs.setEnabled(isAllowed); 143 getPreferenceScreen().removePreference(mOverlayPrefs); 144 145 return true; 146 } 147 148 @Override createDialog(int id, int errorCode)149 protected AlertDialog createDialog(int id, int errorCode) { 150 return null; 151 } 152 153 @Override getMetricsCategory()154 protected int getMetricsCategory() { 155 return MetricsEvent.SYSTEM_ALERT_WINDOW_APPS; 156 } 157 getSummary(Context context, AppEntry entry)158 public static CharSequence getSummary(Context context, AppEntry entry) { 159 OverlayState state; 160 if (entry.extraInfo instanceof OverlayState) { 161 state = (OverlayState) entry.extraInfo; 162 } else if (entry.extraInfo instanceof PermissionState) { 163 state = new OverlayState((PermissionState) entry.extraInfo); 164 } else { 165 state = new AppStateOverlayBridge(context, null, null).getOverlayInfo( 166 entry.info.packageName, entry.info.uid); 167 } 168 169 return getSummary(context, state); 170 } 171 getSummary(Context context, OverlayState overlayState)172 public static CharSequence getSummary(Context context, OverlayState overlayState) { 173 return context.getString(overlayState.isPermissible() ? 174 R.string.system_alert_window_on : R.string.system_alert_window_off); 175 } 176 getSummary(Context context, String pkg)177 public static CharSequence getSummary(Context context, String pkg) { 178 // first check if pkg is a system pkg 179 PackageManager packageManager = context.getPackageManager(); 180 int uid = -1; 181 try { 182 ApplicationInfo appInfo = packageManager.getApplicationInfo(pkg, 0); 183 uid = appInfo.uid; 184 if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 185 return context.getString(R.string.system_alert_window_on); 186 } 187 } catch (PackageManager.NameNotFoundException e) { 188 // pkg doesn't even exist? 189 Log.w(LOG_TAG, "Package " + pkg + " not found", e); 190 return context.getString(R.string.system_alert_window_off); 191 } 192 193 AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context 194 .APP_OPS_SERVICE); 195 if (uid == -1) { 196 return context.getString(R.string.system_alert_window_off); 197 } 198 199 int mode = appOpsManager.noteOpNoThrow(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, uid, pkg); 200 return context.getString((mode == AppOpsManager.MODE_ALLOWED) ? 201 R.string.system_alert_window_on : R.string.system_alert_window_off); 202 } 203 } 204