Home | History | Annotate | Download | only in notification
      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 
     17 package com.android.settings.notification;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.app.DialogFragment;
     22 import android.app.NotificationManager;
     23 import android.content.Context;
     24 import android.content.DialogInterface;
     25 import android.content.pm.ApplicationInfo;
     26 import android.content.pm.PackageItemInfo;
     27 import android.content.pm.PackageManager;
     28 import android.database.ContentObserver;
     29 import android.net.Uri;
     30 import android.os.AsyncTask;
     31 import android.os.Bundle;
     32 import android.os.Handler;
     33 import android.os.Looper;
     34 import android.preference.Preference;
     35 import android.preference.Preference.OnPreferenceChangeListener;
     36 import android.preference.PreferenceScreen;
     37 import android.preference.SwitchPreference;
     38 import android.provider.Settings.Secure;
     39 import android.text.TextUtils;
     40 import android.util.ArraySet;
     41 import android.view.LayoutInflater;
     42 import android.view.View;
     43 import android.view.ViewGroup;
     44 import android.widget.TextView;
     45 
     46 import com.android.internal.logging.MetricsLogger;
     47 import com.android.settings.R;
     48 import com.android.settings.SettingsPreferenceFragment;
     49 
     50 import java.util.ArrayList;
     51 import java.util.Collections;
     52 import java.util.List;
     53 
     54 public class ZenAccessSettings extends SettingsPreferenceFragment {
     55 
     56     private final SettingObserver mObserver = new SettingObserver();
     57 
     58     private Context mContext;
     59     private PackageManager mPkgMan;
     60     private NotificationManager mNoMan;
     61     private TextView mEmpty;
     62 
     63     @Override
     64     protected int getMetricsCategory() {
     65         return MetricsLogger.NOTIFICATION_ZEN_MODE_ACCESS;
     66     }
     67 
     68     @Override
     69     public void onCreate(Bundle icicle) {
     70         super.onCreate(icicle);
     71 
     72         mContext = getActivity();
     73         mPkgMan = mContext.getPackageManager();
     74         mNoMan = mContext.getSystemService(NotificationManager.class);
     75         setPreferenceScreen(getPreferenceManager().createPreferenceScreen(mContext));
     76     }
     77 
     78     @Override
     79     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     80             Bundle savedInstanceState) {
     81         final View v =  inflater.inflate(R.layout.managed_service_settings, container, false);
     82         mEmpty = (TextView) v.findViewById(android.R.id.empty);
     83         mEmpty.setText(R.string.zen_access_empty_text);
     84         return v;
     85     }
     86 
     87     @Override
     88     public void onResume() {
     89         super.onResume();
     90         reloadList();
     91         getContentResolver().registerContentObserver(
     92                 Secure.getUriFor(Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES), false,
     93                 mObserver);
     94     }
     95 
     96     @Override
     97     public void onPause() {
     98         super.onPause();
     99         getContentResolver().unregisterContentObserver(mObserver);
    100     }
    101 
    102     private void reloadList() {
    103         final PreferenceScreen screen = getPreferenceScreen();
    104         screen.removeAll();
    105         final ArrayList<ApplicationInfo> apps = new ArrayList<>();
    106         final ArraySet<String> requesting = mNoMan.getPackagesRequestingNotificationPolicyAccess();
    107         if (requesting != null && !requesting.isEmpty()) {
    108             final List<ApplicationInfo> installed = mPkgMan.getInstalledApplications(0);
    109             if (installed != null) {
    110                 for (ApplicationInfo app : installed) {
    111                     if (requesting.contains(app.packageName)) {
    112                         apps.add(app);
    113                     }
    114                 }
    115             }
    116         }
    117         Collections.sort(apps, new PackageItemInfo.DisplayNameComparator(mPkgMan));
    118         for (ApplicationInfo app : apps) {
    119             final String pkg = app.packageName;
    120             final CharSequence label = app.loadLabel(mPkgMan);
    121             final SwitchPreference pref = new SwitchPreference(mContext);
    122             pref.setPersistent(false);
    123             pref.setIcon(app.loadIcon(mPkgMan));
    124             pref.setTitle(label);
    125             pref.setChecked(hasAccess(pkg));
    126             pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    127                 @Override
    128                 public boolean onPreferenceChange(Preference preference, Object newValue) {
    129                     final boolean access = (Boolean) newValue;
    130                     if (!access) {
    131                         // disabling access
    132                         setAccess(mContext, pkg, access);
    133                         return true;
    134                     }
    135                     // enabling access: show a scary dialog first
    136                     new ScaryWarningDialogFragment()
    137                             .setPkgInfo(pkg, label)
    138                             .show(getFragmentManager(), "dialog");
    139                     return false;
    140                 }
    141             });
    142             screen.addPreference(pref);
    143         }
    144         mEmpty.setVisibility(apps.isEmpty() ? View.VISIBLE : View.GONE);
    145     }
    146 
    147     private boolean hasAccess(String pkg) {
    148         return mNoMan.isNotificationPolicyAccessGrantedForPackage(pkg);
    149     }
    150 
    151     private static void setAccess(final Context context, final String pkg, final boolean access) {
    152         AsyncTask.execute(new Runnable() {
    153             @Override
    154             public void run() {
    155                 final NotificationManager mgr = context.getSystemService(NotificationManager.class);
    156                 mgr.setNotificationPolicyAccessGranted(pkg, access);
    157             }
    158         });
    159     }
    160 
    161     private final class SettingObserver extends ContentObserver {
    162         public SettingObserver() {
    163             super(new Handler(Looper.getMainLooper()));
    164         }
    165 
    166         @Override
    167         public void onChange(boolean selfChange, Uri uri) {
    168             reloadList();
    169         }
    170     }
    171 
    172     public static class ScaryWarningDialogFragment extends DialogFragment {
    173         static final String KEY_PKG = "p";
    174         static final String KEY_LABEL = "l";
    175 
    176         public ScaryWarningDialogFragment setPkgInfo(String pkg, CharSequence label) {
    177             Bundle args = new Bundle();
    178             args.putString(KEY_PKG, pkg);
    179             args.putString(KEY_LABEL, TextUtils.isEmpty(label) ? pkg : label.toString());
    180             setArguments(args);
    181             return this;
    182         }
    183 
    184         @Override
    185         public Dialog onCreateDialog(Bundle savedInstanceState) {
    186             super.onCreate(savedInstanceState);
    187             final Bundle args = getArguments();
    188             final String pkg = args.getString(KEY_PKG);
    189             final String label = args.getString(KEY_LABEL);
    190 
    191             final String title = getResources().getString(R.string.zen_access_warning_dialog_title,
    192                     label);
    193             final String summary = getResources()
    194                     .getString(R.string.zen_access_warning_dialog_summary);
    195             return new AlertDialog.Builder(getContext())
    196                     .setMessage(summary)
    197                     .setTitle(title)
    198                     .setCancelable(true)
    199                     .setPositiveButton(R.string.allow,
    200                             new DialogInterface.OnClickListener() {
    201                                 public void onClick(DialogInterface dialog, int id) {
    202                                     setAccess(getContext(), pkg, true);
    203                                 }
    204                             })
    205                     .setNegativeButton(R.string.deny,
    206                             new DialogInterface.OnClickListener() {
    207                                 public void onClick(DialogInterface dialog, int id) {
    208                                     // pass
    209                                 }
    210                             })
    211                     .create();
    212         }
    213     }
    214 }
    215