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.Activity;
     20 import android.app.AlertDialog;
     21 import android.app.AutomaticZenRule;
     22 import android.app.NotificationManager;
     23 import android.content.Context;
     24 import android.content.DialogInterface;
     25 import android.content.DialogInterface.OnClickListener;
     26 import android.content.Intent;
     27 import android.net.Uri;
     28 import android.os.Bundle;
     29 import android.service.notification.ConditionProviderService;
     30 import android.support.v7.preference.DropDownPreference;
     31 import android.support.v7.preference.Preference;
     32 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
     33 import android.support.v7.preference.Preference.OnPreferenceClickListener;
     34 import android.support.v7.preference.PreferenceScreen;
     35 import android.util.Log;
     36 import android.view.Menu;
     37 import android.view.MenuInflater;
     38 import android.view.MenuItem;
     39 import android.view.View;
     40 import android.widget.Switch;
     41 import android.widget.Toast;
     42 
     43 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     44 import com.android.settings.R;
     45 import com.android.settings.SettingsActivity;
     46 import com.android.settings.widget.SwitchBar;
     47 
     48 public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
     49         implements SwitchBar.OnSwitchChangeListener {
     50     protected static final String TAG = ZenModeSettingsBase.TAG;
     51     protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG;
     52 
     53     private static final String KEY_RULE_NAME = "rule_name";
     54     private static final String KEY_ZEN_MODE = "zen_mode";
     55 
     56     protected Context mContext;
     57     protected boolean mDisableListeners;
     58     protected AutomaticZenRule mRule;
     59     protected String mId;
     60 
     61     private boolean mDeleting;
     62     private Preference mRuleName;
     63     private SwitchBar mSwitchBar;
     64     private DropDownPreference mZenMode;
     65     private Toast mEnabledToast;
     66 
     67     abstract protected void onCreateInternal();
     68     abstract protected boolean setRule(AutomaticZenRule rule);
     69     abstract protected String getZenModeDependency();
     70     abstract protected void updateControlsInternal();
     71     abstract protected int getEnabledToastText();
     72 
     73     @Override
     74     public void onCreate(Bundle icicle) {
     75         super.onCreate(icicle);
     76 
     77         mContext = getActivity();
     78 
     79         final Intent intent = getActivity().getIntent();
     80         if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent);
     81         if (intent == null) {
     82             Log.w(TAG, "No intent");
     83             toastAndFinish();
     84             return;
     85         }
     86 
     87         mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID);
     88         if (DEBUG) Log.d(TAG, "mId=" + mId);
     89         if (refreshRuleOrFinish()) {
     90             return;
     91         }
     92 
     93         setHasOptionsMenu(true);
     94 
     95         onCreateInternal();
     96 
     97         final PreferenceScreen root = getPreferenceScreen();
     98         mRuleName = root.findPreference(KEY_RULE_NAME);
     99         mRuleName.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    100             @Override
    101             public boolean onPreferenceClick(Preference preference) {
    102                 showRuleNameDialog();
    103                 return true;
    104             }
    105         });
    106 
    107         mZenMode = (DropDownPreference) root.findPreference(KEY_ZEN_MODE);
    108         mZenMode.setEntries(new CharSequence[] {
    109                 getString(R.string.zen_mode_option_important_interruptions),
    110                 getString(R.string.zen_mode_option_alarms),
    111                 getString(R.string.zen_mode_option_no_interruptions),
    112         });
    113         mZenMode.setEntryValues(new CharSequence[] {
    114                 Integer.toString(NotificationManager.INTERRUPTION_FILTER_PRIORITY),
    115                 Integer.toString(NotificationManager.INTERRUPTION_FILTER_ALARMS),
    116                 Integer.toString(NotificationManager.INTERRUPTION_FILTER_NONE),
    117         });
    118         mZenMode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    119             @Override
    120             public boolean onPreferenceChange(Preference preference, Object newValue) {
    121                 if (mDisableListeners) return false;
    122                 final int zenMode = Integer.parseInt((String) newValue);
    123                 if (zenMode == mRule.getInterruptionFilter()) return false;
    124                 if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
    125                 mRule.setInterruptionFilter(zenMode);
    126                 setZenRule(mId, mRule);
    127                 return true;
    128             }
    129         });
    130         mZenMode.setOrder(10);  // sort at the bottom of the category
    131         mZenMode.setDependency(getZenModeDependency());
    132     }
    133 
    134     @Override
    135     public void onResume() {
    136         super.onResume();
    137         if (isUiRestricted()) {
    138             return;
    139         }
    140         updateControls();
    141     }
    142 
    143     @Override
    144     public void onActivityCreated(Bundle savedInstanceState) {
    145         super.onActivityCreated(savedInstanceState);
    146 
    147         final SettingsActivity activity = (SettingsActivity) getActivity();
    148         mSwitchBar = activity.getSwitchBar();
    149         mSwitchBar.addOnSwitchChangeListener(this);
    150         mSwitchBar.show();
    151     }
    152 
    153     @Override
    154     public void onDestroyView() {
    155         super.onDestroyView();
    156         mSwitchBar.removeOnSwitchChangeListener(this);
    157         mSwitchBar.hide();
    158     }
    159 
    160     @Override
    161     public void onSwitchChanged(Switch switchView, boolean isChecked) {
    162         if (DEBUG) Log.d(TAG, "onSwitchChanged " + isChecked);
    163         if (mDisableListeners) return;
    164         final boolean enabled = isChecked;
    165         if (enabled == mRule.isEnabled()) return;
    166         mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ENABLE_RULE, enabled);
    167         if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
    168         mRule.setEnabled(enabled);
    169         setZenRule(mId, mRule);
    170         if (enabled) {
    171             final int toastText = getEnabledToastText();
    172             if (toastText != 0) {
    173                 mEnabledToast = Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT);
    174                 mEnabledToast.show();
    175             }
    176         } else {
    177             if (mEnabledToast != null) {
    178                 mEnabledToast.cancel();
    179             }
    180         }
    181     }
    182 
    183     protected void updateRule(Uri newConditionId) {
    184         mRule.setConditionId(newConditionId);
    185         setZenRule(mId, mRule);
    186     }
    187 
    188     @Override
    189     protected void onZenModeChanged() {
    190         // noop
    191     }
    192 
    193     @Override
    194     protected void onZenModeConfigChanged() {
    195         if (!refreshRuleOrFinish()) {
    196             updateControls();
    197         }
    198     }
    199 
    200     @Override
    201     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    202         if (DEBUG) Log.d(TAG, "onCreateOptionsMenu");
    203         inflater.inflate(R.menu.zen_mode_rule, menu);
    204     }
    205 
    206     @Override
    207     public boolean onOptionsItemSelected(MenuItem item) {
    208         if (DEBUG) Log.d(TAG, "onOptionsItemSelected " + item.getItemId());
    209         if (item.getItemId() == R.id.delete) {
    210             mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_DELETE_RULE);
    211             showDeleteRuleDialog();
    212             return true;
    213         }
    214         return super.onOptionsItemSelected(item);
    215     }
    216 
    217     private void showRuleNameDialog() {
    218         new ZenRuleNameDialog(mContext, mRule.getName()) {
    219             @Override
    220             public void onOk(String ruleName) {
    221                 mRule.setName(ruleName);
    222                 setZenRule(mId, mRule);
    223             }
    224         }.show();
    225     }
    226 
    227     private boolean refreshRuleOrFinish() {
    228         mRule = getZenRule();
    229         if (DEBUG) Log.d(TAG, "mRule=" + mRule);
    230         if (!setRule(mRule)) {
    231             toastAndFinish();
    232             return true;
    233         }
    234         return false;
    235     }
    236 
    237     private void showDeleteRuleDialog() {
    238         final AlertDialog dialog = new AlertDialog.Builder(mContext)
    239                 .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, mRule.getName()))
    240                 .setNegativeButton(R.string.cancel, null)
    241                 .setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() {
    242                     @Override
    243                     public void onClick(DialogInterface dialog, int which) {
    244                         mMetricsFeatureProvider.action(mContext,
    245                                 MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
    246                         mDeleting = true;
    247                         removeZenRule(mId);
    248                     }
    249                 })
    250                 .show();
    251         final View messageView = dialog.findViewById(android.R.id.message);
    252         if (messageView != null) {
    253             messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
    254         }
    255     }
    256 
    257     private void toastAndFinish() {
    258         if (!mDeleting) {
    259             Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT)
    260                     .show();
    261         }
    262         getActivity().finish();
    263     }
    264 
    265     private void updateRuleName() {
    266         Activity activity = getActivity();
    267         if (activity != null) {
    268             activity.setTitle(mRule.getName());
    269             mRuleName.setSummary(mRule.getName());
    270         } else {
    271             if (DEBUG) Log.d(TAG, "updateRuleName - activity title and mRuleName "
    272                     + "not updated; getActivity() returned null");
    273         }
    274     }
    275 
    276     private AutomaticZenRule getZenRule() {
    277         return NotificationManager.from(mContext).getAutomaticZenRule(mId);
    278     }
    279 
    280     private void updateControls() {
    281         mDisableListeners = true;
    282         updateRuleName();
    283         updateControlsInternal();
    284         mZenMode.setValue(Integer.toString(mRule.getInterruptionFilter()));
    285         if (mSwitchBar != null) {
    286             mSwitchBar.setChecked(mRule.isEnabled());
    287         }
    288         mDisableListeners = false;
    289     }
    290 
    291 }
    292