DrawOverlayDetails.java revision 9aeaa197a93f60deb78eea06fbe95060e3190b15
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 */
16package com.android.settings.applications.appinfo;
17
18import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
19
20import android.app.AlertDialog;
21import android.app.AppOpsManager;
22import android.content.ActivityNotFoundException;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.os.Bundle;
28import android.os.UserHandle;
29import android.provider.Settings;
30import android.support.v14.preference.SwitchPreference;
31import android.support.v7.preference.Preference;
32import android.support.v7.preference.Preference.OnPreferenceChangeListener;
33import android.support.v7.preference.Preference.OnPreferenceClickListener;
34import android.util.Log;
35import android.view.Window;
36import android.view.WindowManager;
37
38import com.android.internal.annotations.VisibleForTesting;
39import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
40import com.android.settings.R;
41import com.android.settings.applications.AppInfoWithHeader;
42import com.android.settings.applications.AppStateAppOpsBridge.PermissionState;
43import com.android.settings.applications.AppStateOverlayBridge;
44import com.android.settings.applications.AppStateOverlayBridge.OverlayState;
45import com.android.settings.overlay.FeatureFactory;
46import com.android.settingslib.applications.ApplicationsState.AppEntry;
47
48public class DrawOverlayDetails extends AppInfoWithHeader implements OnPreferenceChangeListener,
49        OnPreferenceClickListener {
50
51    private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
52    private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference";
53    private static final String LOG_TAG = "DrawOverlayDetails";
54
55    private static final int [] APP_OPS_OP_CODE = {
56            AppOpsManager.OP_SYSTEM_ALERT_WINDOW
57    };
58
59    // Use a bridge to get the overlay details but don't initialize it to connect with all state.
60    // TODO: Break out this functionality into its own class.
61    private AppStateOverlayBridge mOverlayBridge;
62    private AppOpsManager mAppOpsManager;
63    private SwitchPreference mSwitchPref;
64    private Preference mOverlayPrefs;
65    private Intent mSettingsIntent;
66    private OverlayState mOverlayState;
67
68    @Override
69    public void onCreate(Bundle savedInstanceState) {
70        super.onCreate(savedInstanceState);
71
72        Context context = getActivity();
73        mOverlayBridge = new AppStateOverlayBridge(context, mState, null);
74        mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
75
76        // find preferences
77        addPreferencesFromResource(R.xml.draw_overlay_permissions_details);
78        mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
79        mOverlayPrefs = findPreference(KEY_APP_OPS_SETTINGS_PREFS);
80
81        // install event listeners
82        mSwitchPref.setOnPreferenceChangeListener(this);
83        mOverlayPrefs.setOnPreferenceClickListener(this);
84
85        mSettingsIntent = new Intent(Intent.ACTION_MAIN)
86                .setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
87    }
88
89    @Override
90    public void onResume() {
91        super.onResume();
92        getActivity().getWindow().addPrivateFlags(PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
93    }
94
95    @Override
96    public void onPause() {
97        super.onPause();
98        Window window = getActivity().getWindow();
99        WindowManager.LayoutParams attrs = window.getAttributes();
100        attrs.privateFlags &= ~PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
101        window.setAttributes(attrs);
102    }
103
104    @Override
105    public void onDestroy() {
106        super.onDestroy();
107        mOverlayBridge.release();
108    }
109
110    @Override
111    public boolean onPreferenceClick(Preference preference) {
112        if (preference == mOverlayPrefs) {
113            if (mSettingsIntent != null) {
114                try {
115                    getActivity().startActivityAsUser(mSettingsIntent, new UserHandle(mUserId));
116                } catch (ActivityNotFoundException e) {
117                    Log.w(LOG_TAG, "Unable to launch app draw overlay settings " + mSettingsIntent,
118                            e);
119                }
120            }
121            return true;
122        }
123        return false;
124    }
125
126    @Override
127    public boolean onPreferenceChange(Preference preference, Object newValue) {
128        if (preference == mSwitchPref) {
129            if (mOverlayState != null && (Boolean) newValue != mOverlayState.isPermissible()) {
130                setCanDrawOverlay(!mOverlayState.isPermissible());
131                refreshUi();
132            }
133            return true;
134        }
135        return false;
136    }
137
138    private void setCanDrawOverlay(boolean newState) {
139        logSpecialPermissionChange(newState, mPackageName);
140        mAppOpsManager.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
141                mPackageInfo.applicationInfo.uid, mPackageName, newState
142                ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED);
143    }
144
145    @VisibleForTesting
146    void logSpecialPermissionChange(boolean newState, String packageName) {
147        int logCategory = newState ? MetricsEvent.APP_SPECIAL_PERMISSION_APPDRAW_ALLOW
148                : MetricsEvent.APP_SPECIAL_PERMISSION_APPDRAW_DENY;
149        FeatureFactory.getFactory(getContext())
150                .getMetricsFeatureProvider().action(getContext(), logCategory, packageName);
151    }
152
153    @Override
154    protected boolean refreshUi() {
155        mOverlayState = mOverlayBridge.getOverlayInfo(mPackageName,
156                mPackageInfo.applicationInfo.uid);
157
158        boolean isAllowed = mOverlayState.isPermissible();
159        mSwitchPref.setChecked(isAllowed);
160        // you cannot ask a user to grant you a permission you did not have!
161        mSwitchPref.setEnabled(mOverlayState.permissionDeclared && mOverlayState.controlEnabled);
162        mOverlayPrefs.setEnabled(isAllowed);
163
164        ResolveInfo resolveInfo = mPm.resolveActivityAsUser(mSettingsIntent,
165                PackageManager.GET_META_DATA, mUserId);
166        if (resolveInfo == null) {
167            if (findPreference(KEY_APP_OPS_SETTINGS_PREFS) != null) {
168                getPreferenceScreen().removePreference(mOverlayPrefs);
169            }
170        }
171
172        return true;
173    }
174
175    @Override
176    protected AlertDialog createDialog(int id, int errorCode) {
177        return null;
178    }
179
180    @Override
181    public int getMetricsCategory() {
182        return MetricsEvent.SYSTEM_ALERT_WINDOW_APPS;
183    }
184
185    public static CharSequence getSummary(Context context, AppEntry entry) {
186        OverlayState state;
187        if (entry.extraInfo instanceof OverlayState) {
188            state = (OverlayState) entry.extraInfo;
189        } else if (entry.extraInfo instanceof PermissionState) {
190            state = new OverlayState((PermissionState) entry.extraInfo);
191        } else {
192            state = new AppStateOverlayBridge(context, null, null).getOverlayInfo(
193                    entry.info.packageName, entry.info.uid);
194        }
195
196        return getSummary(context, state);
197    }
198
199    public static CharSequence getSummary(Context context, OverlayState overlayState) {
200        return context.getString(overlayState.isPermissible() ?
201            R.string.app_permission_summary_allowed : R.string.app_permission_summary_not_allowed);
202    }
203}
204