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