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.FragmentManager;
     20 import android.content.Context;
     21 import android.provider.Settings;
     22 import android.support.v7.preference.Preference;
     23 import android.view.View;
     24 import android.widget.Button;
     25 
     26 import com.android.internal.logging.nano.MetricsProto;
     27 import com.android.settings.R;
     28 import com.android.settings.applications.LayoutPreference;
     29 import com.android.settings.core.PreferenceControllerMixin;
     30 import com.android.settingslib.core.lifecycle.Lifecycle;
     31 
     32 public class ZenModeButtonPreferenceController extends AbstractZenModePreferenceController
     33         implements PreferenceControllerMixin {
     34 
     35     private static final String TAG = "EnableZenModeButton";
     36     protected static final String KEY = "zen_mode_settings_button_container";
     37     private Button mZenButtonOn;
     38     private Button mZenButtonOff;
     39     private FragmentManager mFragment;
     40 
     41     public ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager
     42             fragment) {
     43         super(context, KEY, lifecycle);
     44         mFragment = fragment;
     45     }
     46 
     47     @Override
     48     public boolean isAvailable() {
     49         return true;
     50     }
     51 
     52     @Override
     53     public String getPreferenceKey() {
     54         return KEY;
     55     }
     56 
     57     @Override
     58     public void updateState(Preference preference) {
     59         super.updateState(preference);
     60 
     61         if (null == mZenButtonOn) {
     62             mZenButtonOn = (Button) ((LayoutPreference) preference)
     63                     .findViewById(R.id.zen_mode_settings_turn_on_button);
     64             updateZenButtonOnClickListener();
     65         }
     66 
     67         if (null == mZenButtonOff) {
     68             mZenButtonOff = (Button) ((LayoutPreference) preference)
     69                     .findViewById(R.id.zen_mode_settings_turn_off_button);
     70             mZenButtonOff.setOnClickListener(v -> {
     71                 mMetricsFeatureProvider.action(mContext,
     72                         MetricsProto.MetricsEvent.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
     73                 mBackend.setZenMode(Settings.Global.ZEN_MODE_OFF);
     74             });
     75         }
     76 
     77         updateButtons();
     78     }
     79 
     80     private void updateButtons() {
     81         switch (getZenMode()) {
     82             case Settings.Global.ZEN_MODE_ALARMS:
     83             case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
     84             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
     85                 mZenButtonOff.setVisibility(View.VISIBLE);
     86                 mZenButtonOn.setVisibility(View.GONE);
     87                 break;
     88             case Settings.Global.ZEN_MODE_OFF:
     89             default:
     90                 mZenButtonOff.setVisibility(View.GONE);
     91                 updateZenButtonOnClickListener();
     92                 mZenButtonOn.setVisibility(View.VISIBLE);
     93         }
     94     }
     95 
     96     private void updateZenButtonOnClickListener() {
     97         int zenDuration = getZenDuration();
     98         switch (zenDuration) {
     99             case Settings.Global.ZEN_DURATION_PROMPT:
    100                 mZenButtonOn.setOnClickListener(v -> {
    101                     mMetricsFeatureProvider.action(mContext,
    102                             MetricsProto.MetricsEvent.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
    103                     new SettingsEnableZenModeDialog().show(mFragment, TAG);
    104                 });
    105                 break;
    106             case Settings.Global.ZEN_DURATION_FOREVER:
    107                 mZenButtonOn.setOnClickListener(v -> {
    108                     mMetricsFeatureProvider.action(mContext,
    109                             MetricsProto.MetricsEvent.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
    110                     mBackend.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
    111                 });
    112                 break;
    113             default:
    114                 mZenButtonOn.setOnClickListener(v -> {
    115                     mMetricsFeatureProvider.action(mContext,
    116                             MetricsProto.MetricsEvent.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
    117                     mBackend.setZenModeForDuration(zenDuration);
    118                 });
    119         }
    120     }
    121 }