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.AutomaticZenRule;
     20 import android.app.Fragment;
     21 import android.content.Context;
     22 import android.support.v7.preference.Preference;
     23 import android.support.v7.preference.PreferenceScreen;
     24 import android.widget.Switch;
     25 
     26 import com.android.settings.R;
     27 import com.android.settings.applications.LayoutPreference;
     28 import com.android.settings.widget.SwitchBar;
     29 import com.android.settingslib.core.lifecycle.Lifecycle;
     30 
     31 public class ZenAutomaticRuleSwitchPreferenceController extends
     32         AbstractZenModeAutomaticRulePreferenceController implements
     33         SwitchBar.OnSwitchChangeListener {
     34 
     35     private static final String KEY = "zen_automatic_rule_switch";
     36     private AutomaticZenRule mRule;
     37     private String mId;
     38     private SwitchBar mSwitchBar;
     39 
     40     public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent,
     41             Lifecycle lifecycle) {
     42         super(context, KEY, parent, lifecycle);
     43     }
     44 
     45     @Override
     46     public String getPreferenceKey() {
     47         return KEY;
     48     }
     49 
     50     @Override
     51     public boolean isAvailable() {
     52         return mRule != null && mId != null;
     53     }
     54 
     55     @Override
     56     public void displayPreference(PreferenceScreen screen) {
     57         super.displayPreference(screen);
     58         LayoutPreference pref = (LayoutPreference) screen.findPreference(KEY);
     59         mSwitchBar = pref.findViewById(R.id.switch_bar);
     60 
     61         if (mSwitchBar != null) {
     62             mSwitchBar.setSwitchBarText(R.string.zen_mode_use_automatic_rule,
     63                     R.string.zen_mode_use_automatic_rule);
     64             try {
     65                 mSwitchBar.addOnSwitchChangeListener(this);
     66             } catch (IllegalStateException e) {
     67                 // an exception is thrown if you try to add the listener twice
     68             }
     69             mSwitchBar.show();
     70         }
     71     }
     72 
     73 
     74     public void onResume(AutomaticZenRule rule, String id) {
     75         mRule = rule;
     76         mId = id;
     77     }
     78 
     79     public void updateState(Preference preference) {
     80         if (mRule != null) {
     81             mSwitchBar.setChecked(mRule.isEnabled());
     82         }
     83     }
     84 
     85     @Override
     86     public void onSwitchChanged(Switch switchView, boolean isChecked) {
     87         final boolean enabled = isChecked;
     88         if (enabled == mRule.isEnabled()) return;
     89         mRule.setEnabled(enabled);
     90         mBackend.setZenRule(mId, mRule);
     91     }
     92 }
     93