Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2017 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.NotificationManager.Policy;
     20 import android.content.Context;
     21 import android.provider.Settings;
     22 import android.support.v14.preference.SwitchPreference;
     23 import android.support.v7.preference.Preference;
     24 import android.support.v7.preference.PreferenceScreen;
     25 import android.util.Log;
     26 
     27 import com.android.internal.logging.nano.MetricsProto;
     28 import com.android.settings.R;
     29 import com.android.settingslib.core.lifecycle.Lifecycle;
     30 
     31 public class ZenModeRepeatCallersPreferenceController extends AbstractZenModePreferenceController
     32         implements Preference.OnPreferenceChangeListener {
     33 
     34     protected static final String KEY = "zen_mode_repeat_callers";
     35 
     36     private final ZenModeBackend mBackend;
     37     private final int mRepeatCallersThreshold;
     38 
     39     public ZenModeRepeatCallersPreferenceController(Context context, Lifecycle lifecycle,
     40             int repeatCallersThreshold) {
     41         super(context, KEY, lifecycle);
     42 
     43         mRepeatCallersThreshold = repeatCallersThreshold;
     44         mBackend = ZenModeBackend.getInstance(context);
     45     }
     46 
     47     @Override
     48     public String getPreferenceKey() {
     49         return KEY;
     50     }
     51 
     52     @Override
     53     public boolean isAvailable() {
     54         return true;
     55     }
     56 
     57     @Override
     58     public void displayPreference(PreferenceScreen screen) {
     59         super.displayPreference(screen);
     60         setRepeatCallerSummary(screen.findPreference(KEY));
     61     }
     62 
     63     @Override
     64     public void updateState(Preference preference) {
     65         super.updateState(preference);
     66 
     67         SwitchPreference pref = (SwitchPreference) preference;
     68         switch (getZenMode()) {
     69             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
     70             case Settings.Global.ZEN_MODE_ALARMS:
     71                 pref.setEnabled(false);
     72                 pref.setChecked(false);
     73                 break;
     74             default:
     75                 boolean anyCallersCanBypassDnd = (mBackend.isPriorityCategoryEnabled(
     76                         Policy.PRIORITY_CATEGORY_CALLS)
     77                         && mBackend.getPriorityCallSenders() == Policy.PRIORITY_SENDERS_ANY);
     78                 // if any caller can bypass dnd then repeat callers preference is disabled
     79                 if (anyCallersCanBypassDnd) {
     80                     pref.setEnabled(false);
     81                     pref.setChecked(true);
     82                 } else {
     83                     pref.setEnabled(true);
     84                     pref.setChecked(mBackend.isPriorityCategoryEnabled(
     85                             Policy.PRIORITY_CATEGORY_REPEAT_CALLERS));
     86                 }
     87         }
     88     }
     89 
     90     @Override
     91     public boolean onPreferenceChange(Preference preference, Object newValue) {
     92         final boolean allowRepeatCallers = (Boolean) newValue;
     93         if (ZenModeSettingsBase.DEBUG) {
     94             Log.d(TAG, "onPrefChange allowRepeatCallers=" + allowRepeatCallers);
     95         }
     96         mMetricsFeatureProvider.action(mContext,
     97                 MetricsProto.MetricsEvent.ACTION_ZEN_ALLOW_REPEAT_CALLS, allowRepeatCallers);
     98         mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_REPEAT_CALLERS, allowRepeatCallers);
     99         return true;
    100     }
    101 
    102     private void setRepeatCallerSummary(Preference preference) {
    103         preference.setSummary(mContext.getString(R.string.zen_mode_repeat_callers_summary,
    104                 mRepeatCallersThreshold));
    105     }
    106 }
    107