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