Home | History | Annotate | Download | only in volume
      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 package com.android.systemui.volume;
     17 
     18 import android.animation.LayoutTransition;
     19 import android.animation.ValueAnimator;
     20 import android.content.Context;
     21 import android.provider.Settings.Global;
     22 import android.service.notification.ZenModeConfig;
     23 import android.util.AttributeSet;
     24 import android.view.View;
     25 import android.widget.ImageView;
     26 import android.widget.LinearLayout;
     27 import android.widget.TextView;
     28 
     29 import com.android.systemui.R;
     30 import com.android.systemui.statusbar.policy.ZenModeController;
     31 
     32 import java.util.Objects;
     33 
     34 /**
     35  * Zen mode information (and end button) attached to the bottom of the volume dialog.
     36  */
     37 public class ZenFooter extends LinearLayout {
     38     private static final String TAG = Util.logTag(ZenFooter.class);
     39 
     40     private final Context mContext;
     41     private final SpTexts mSpTexts;
     42 
     43     private ImageView mIcon;
     44     private TextView mSummaryLine1;
     45     private TextView mSummaryLine2;
     46     private TextView mEndNowButton;
     47     private int mZen = -1;
     48     private ZenModeConfig mConfig;
     49     private ZenModeController mController;
     50 
     51     public ZenFooter(Context context, AttributeSet attrs) {
     52         super(context, attrs);
     53         mContext = context;
     54         mSpTexts = new SpTexts(mContext);
     55         final LayoutTransition layoutTransition = new LayoutTransition();
     56         layoutTransition.setDuration(new ValueAnimator().getDuration() / 2);
     57         setLayoutTransition(layoutTransition);
     58     }
     59 
     60     @Override
     61     protected void onFinishInflate() {
     62         super.onFinishInflate();
     63         mIcon = (ImageView) findViewById(R.id.volume_zen_icon);
     64         mSummaryLine1 = (TextView) findViewById(R.id.volume_zen_summary_line_1);
     65         mSummaryLine2 = (TextView) findViewById(R.id.volume_zen_summary_line_2);
     66         mEndNowButton = (TextView) findViewById(R.id.volume_zen_end_now);
     67         mSpTexts.add(mSummaryLine1);
     68         mSpTexts.add(mSummaryLine2);
     69         mSpTexts.add(mEndNowButton);
     70     }
     71 
     72     public void init(final ZenModeController controller) {
     73         controller.addCallback(new ZenModeController.Callback() {
     74             @Override
     75             public void onZenChanged(int zen) {
     76                 setZen(zen);
     77             }
     78             @Override
     79             public void onConfigChanged(ZenModeConfig config) {
     80                 setConfig(config);
     81             }
     82         });
     83         mEndNowButton.setOnClickListener(new OnClickListener() {
     84             @Override
     85             public void onClick(View v) {
     86                 controller.setZen(Global.ZEN_MODE_OFF, null, TAG);
     87             }
     88         });
     89         mZen = controller.getZen();
     90         mConfig = controller.getConfig();
     91         mController = controller;
     92         update();
     93     }
     94 
     95     private void setZen(int zen) {
     96         if (mZen == zen) return;
     97         mZen = zen;
     98         update();
     99     }
    100 
    101     private void setConfig(ZenModeConfig config) {
    102         if (Objects.equals(mConfig, config)) return;
    103         mConfig = config;
    104         update();
    105     }
    106 
    107     public boolean isZen() {
    108         return isZenPriority() || isZenAlarms() || isZenNone();
    109     }
    110 
    111     private boolean isZenPriority() {
    112         return mZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
    113     }
    114 
    115     private boolean isZenAlarms() {
    116         return mZen == Global.ZEN_MODE_ALARMS;
    117     }
    118 
    119     private boolean isZenNone() {
    120         return mZen == Global.ZEN_MODE_NO_INTERRUPTIONS;
    121     }
    122 
    123     public void update() {
    124         mIcon.setImageResource(isZenNone() ? R.drawable.ic_dnd_total_silence : R.drawable.ic_dnd);
    125         final String line1 =
    126                 isZenPriority() ? mContext.getString(R.string.interruption_level_priority)
    127                 : isZenAlarms() ? mContext.getString(R.string.interruption_level_alarms)
    128                 : isZenNone() ? mContext.getString(R.string.interruption_level_none)
    129                 : null;
    130         Util.setText(mSummaryLine1, line1);
    131 
    132         final boolean isForever = mConfig != null && mConfig.manualRule != null
    133                 && mConfig.manualRule.conditionId == null;
    134         final String line2 =
    135                 isForever ? mContext.getString(com.android.internal.R.string.zen_mode_forever_dnd)
    136                 : ZenModeConfig.getConditionSummary(mContext, mConfig, mController.getCurrentUser(),
    137                         true /*shortVersion*/);
    138         Util.setText(mSummaryLine2, line2);
    139     }
    140 
    141     public void onConfigurationChanged() {
    142         mSpTexts.update();
    143     }
    144 
    145 }
    146