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.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 
26 import androidx.annotation.VisibleForTesting;
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.Utils;
35 import com.android.settings.applications.AppInfoWithHeader;
36 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState;
37 import com.android.settings.applications.AppStateOverlayBridge;
38 import com.android.settings.applications.AppStateOverlayBridge.OverlayState;
39 import com.android.settings.overlay.FeatureFactory;
40 import com.android.settingslib.applications.ApplicationsState.AppEntry;
41 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
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 LOG_TAG = "DrawOverlayDetails";
48 
49     // Use a bridge to get the overlay details but don't initialize it to connect with all state.
50     // TODO: Break out this functionality into its own class.
51     private AppStateOverlayBridge mOverlayBridge;
52     private AppOpsManager mAppOpsManager;
53     private SwitchPreference mSwitchPref;
54     private OverlayState mOverlayState;
55 
56     @Override
onCreate(Bundle savedInstanceState)57     public void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59 
60         Context context = getActivity();
61         mOverlayBridge = new AppStateOverlayBridge(context, mState, null);
62         mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
63 
64         if (!Utils.isSystemAlertWindowEnabled(context)) {
65             mPackageInfo = null;
66             return;
67         }
68 
69         // find preferences
70         addPreferencesFromResource(R.xml.draw_overlay_permissions_details);
71         mSwitchPref = findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
72 
73         // install event listeners
74         mSwitchPref.setOnPreferenceChangeListener(this);
75     }
76 
77     // Override here so we don't have an empty screen
78     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)79     public View onCreateView(LayoutInflater inflater,
80             ViewGroup container,
81             Bundle savedInstanceState) {
82         // if we don't have a package info, show a page saying this is unsupported
83         if (mPackageInfo == null) {
84             return inflater.inflate(R.layout.manage_applications_apps_unsupported, null);
85         }
86         return super.onCreateView(inflater, container, savedInstanceState);
87     }
88 
89     @Override
onDestroy()90     public void onDestroy() {
91         super.onDestroy();
92         mOverlayBridge.release();
93     }
94 
95     @Override
onPreferenceClick(Preference preference)96     public boolean onPreferenceClick(Preference preference) {
97         return false;
98     }
99 
100     @Override
onPreferenceChange(Preference preference, Object newValue)101     public boolean onPreferenceChange(Preference preference, Object newValue) {
102         if (preference == mSwitchPref) {
103             if (mOverlayState != null && (Boolean) newValue != mOverlayState.isPermissible()) {
104                 setCanDrawOverlay(!mOverlayState.isPermissible());
105                 refreshUi();
106             }
107             return true;
108         }
109         return false;
110     }
111 
setCanDrawOverlay(boolean newState)112     private void setCanDrawOverlay(boolean newState) {
113         logSpecialPermissionChange(newState, mPackageName);
114         mAppOpsManager.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
115                 mPackageInfo.applicationInfo.uid, mPackageName, newState
116                         ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED);
117     }
118 
119     @VisibleForTesting
logSpecialPermissionChange(boolean newState, String packageName)120     void logSpecialPermissionChange(boolean newState, String packageName) {
121         int logCategory = newState ? SettingsEnums.APP_SPECIAL_PERMISSION_APPDRAW_ALLOW
122                 : SettingsEnums.APP_SPECIAL_PERMISSION_APPDRAW_DENY;
123         final MetricsFeatureProvider metricsFeatureProvider =
124                 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider();
125         metricsFeatureProvider.action(
126                 metricsFeatureProvider.getAttribution(getActivity()),
127                 logCategory,
128                 getMetricsCategory(),
129                 packageName,
130                 0);
131     }
132 
133     @Override
refreshUi()134     protected boolean refreshUi() {
135         if (mPackageInfo == null) {
136             return true;
137         }
138 
139         mOverlayState = mOverlayBridge.getOverlayInfo(mPackageName,
140                 mPackageInfo.applicationInfo.uid);
141 
142         boolean isAllowed = mOverlayState.isPermissible();
143         mSwitchPref.setChecked(isAllowed);
144         // you cannot ask a user to grant you a permission you did not have!
145         mSwitchPref.setEnabled(mOverlayState.permissionDeclared && mOverlayState.controlEnabled);
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     public int getMetricsCategory() {
157         return SettingsEnums.SYSTEM_ALERT_WINDOW_APPS;
158     }
159 
getSummary(Context context, AppEntry entry)160     public static CharSequence getSummary(Context context, AppEntry entry) {
161         OverlayState state;
162         if (entry.extraInfo instanceof OverlayState) {
163             state = (OverlayState) entry.extraInfo;
164         } else if (entry.extraInfo instanceof PermissionState) {
165             state = new OverlayState((PermissionState) entry.extraInfo);
166         } else {
167             state = new AppStateOverlayBridge(context, null, null).getOverlayInfo(
168                     entry.info.packageName, entry.info.uid);
169         }
170 
171         return getSummary(context, state);
172     }
173 
getSummary(Context context, OverlayState overlayState)174     public static CharSequence getSummary(Context context, OverlayState overlayState) {
175         return context.getString(overlayState.isPermissible() ?
176                 R.string.app_permission_summary_allowed
177                 : R.string.app_permission_summary_not_allowed);
178     }
179 }
180