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 
     17 package com.android.systemui.volume;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.res.Configuration;
     22 import android.media.AudioManager;
     23 import android.media.VolumePolicy;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.provider.Settings;
     27 import android.view.WindowManager;
     28 
     29 import com.android.systemui.SystemUI;
     30 import com.android.systemui.keyguard.KeyguardViewMediator;
     31 import com.android.systemui.qs.tiles.DndTile;
     32 import com.android.systemui.statusbar.phone.PhoneStatusBar;
     33 import com.android.systemui.statusbar.policy.ZenModeController;
     34 import com.android.systemui.tuner.TunerService;
     35 
     36 import java.io.FileDescriptor;
     37 import java.io.PrintWriter;
     38 
     39 /**
     40  * Implementation of VolumeComponent backed by the new volume dialog.
     41  */
     42 public class VolumeDialogComponent implements VolumeComponent, TunerService.Tunable {
     43 
     44     public static final String VOLUME_DOWN_SILENT = "sysui_volume_down_silent";
     45     public static final String VOLUME_UP_SILENT = "sysui_volume_up_silent";
     46     public static final String VOLUME_SILENT_DO_NOT_DISTURB = "sysui_do_not_disturb";
     47 
     48     public static final boolean DEFAULT_VOLUME_DOWN_TO_ENTER_SILENT = true;
     49     public static final boolean DEFAULT_VOLUME_UP_TO_EXIT_SILENT = true;
     50     public static final boolean DEFAULT_DO_NOT_DISTURB_WHEN_SILENT = true;
     51 
     52     private final SystemUI mSysui;
     53     private final Context mContext;
     54     private final VolumeDialogController mController;
     55     private final ZenModeController mZenModeController;
     56     private final VolumeDialog mDialog;
     57     private VolumePolicy mVolumePolicy = new VolumePolicy(
     58             DEFAULT_VOLUME_DOWN_TO_ENTER_SILENT,  // volumeDownToEnterSilent
     59             DEFAULT_VOLUME_UP_TO_EXIT_SILENT,  // volumeUpToExitSilent
     60             DEFAULT_DO_NOT_DISTURB_WHEN_SILENT,  // doNotDisturbWhenSilent
     61             400    // vibrateToSilentDebounce
     62     );
     63 
     64     public VolumeDialogComponent(SystemUI sysui, Context context, Handler handler,
     65             ZenModeController zen) {
     66         mSysui = sysui;
     67         mContext = context;
     68         mController = new VolumeDialogController(context, null) {
     69             @Override
     70             protected void onUserActivityW() {
     71                 sendUserActivity();
     72             }
     73         };
     74         mZenModeController = zen;
     75         mDialog = new VolumeDialog(context, WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY,
     76                 mController, zen, mVolumeDialogCallback);
     77         applyConfiguration();
     78         TunerService.get(mContext).addTunable(this, VOLUME_DOWN_SILENT, VOLUME_UP_SILENT,
     79                 VOLUME_SILENT_DO_NOT_DISTURB);
     80     }
     81 
     82     @Override
     83     public void onTuningChanged(String key, String newValue) {
     84         if (VOLUME_DOWN_SILENT.equals(key)) {
     85             final boolean volumeDownToEnterSilent = newValue != null
     86                     ? Integer.parseInt(newValue) != 0
     87                     : DEFAULT_VOLUME_DOWN_TO_ENTER_SILENT;
     88             setVolumePolicy(volumeDownToEnterSilent,
     89                     mVolumePolicy.volumeUpToExitSilent, mVolumePolicy.doNotDisturbWhenSilent,
     90                     mVolumePolicy.vibrateToSilentDebounce);
     91         } else if (VOLUME_UP_SILENT.equals(key)) {
     92             final boolean volumeUpToExitSilent = newValue != null
     93                     ? Integer.parseInt(newValue) != 0
     94                     : DEFAULT_VOLUME_UP_TO_EXIT_SILENT;
     95             setVolumePolicy(mVolumePolicy.volumeDownToEnterSilent,
     96                     volumeUpToExitSilent, mVolumePolicy.doNotDisturbWhenSilent,
     97                     mVolumePolicy.vibrateToSilentDebounce);
     98         } else if (VOLUME_SILENT_DO_NOT_DISTURB.equals(key)) {
     99             final boolean doNotDisturbWhenSilent = newValue != null
    100                     ? Integer.parseInt(newValue) != 0
    101                     : DEFAULT_DO_NOT_DISTURB_WHEN_SILENT;
    102             setVolumePolicy(mVolumePolicy.volumeDownToEnterSilent,
    103                     mVolumePolicy.volumeUpToExitSilent, doNotDisturbWhenSilent,
    104                     mVolumePolicy.vibrateToSilentDebounce);
    105         }
    106     }
    107 
    108     private void setVolumePolicy(boolean volumeDownToEnterSilent, boolean volumeUpToExitSilent,
    109             boolean doNotDisturbWhenSilent, int vibrateToSilentDebounce) {
    110         mVolumePolicy = new VolumePolicy(volumeDownToEnterSilent, volumeUpToExitSilent,
    111                 doNotDisturbWhenSilent, vibrateToSilentDebounce);
    112         mController.setVolumePolicy(mVolumePolicy);
    113     }
    114 
    115     private void sendUserActivity() {
    116         final KeyguardViewMediator kvm = mSysui.getComponent(KeyguardViewMediator.class);
    117         if (kvm != null) {
    118             kvm.userActivity();
    119         }
    120     }
    121 
    122     private void applyConfiguration() {
    123         mDialog.setStreamImportant(AudioManager.STREAM_ALARM, true);
    124         mDialog.setStreamImportant(AudioManager.STREAM_SYSTEM, false);
    125         mDialog.setAutomute(true);
    126         mDialog.setSilentMode(false);
    127         mController.setVolumePolicy(mVolumePolicy);
    128         mController.showDndTile(true);
    129     }
    130 
    131     @Override
    132     public ZenModeController getZenController() {
    133         return mZenModeController;
    134     }
    135 
    136     @Override
    137     public void onConfigurationChanged(Configuration newConfig) {
    138         // noop
    139     }
    140 
    141     @Override
    142     public void dismissNow() {
    143         mController.dismiss();
    144     }
    145 
    146     @Override
    147     public void dispatchDemoCommand(String command, Bundle args) {
    148         // noop
    149     }
    150 
    151     @Override
    152     public void register() {
    153         mController.register();
    154         DndTile.setCombinedIcon(mContext, true);
    155     }
    156 
    157     @Override
    158     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    159         mController.dump(fd, pw, args);
    160         mDialog.dump(pw);
    161     }
    162 
    163     private void startSettings(Intent intent) {
    164         mSysui.getComponent(PhoneStatusBar.class).startActivityDismissingKeyguard(intent,
    165                 true /* onlyProvisioned */, true /* dismissShade */);
    166     }
    167 
    168     private final VolumeDialog.Callback mVolumeDialogCallback = new VolumeDialog.Callback() {
    169         @Override
    170         public void onZenSettingsClicked() {
    171             startSettings(ZenModePanel.ZEN_SETTINGS);
    172         }
    173 
    174         @Override
    175         public void onZenPrioritySettingsClicked() {
    176             startSettings(ZenModePanel.ZEN_PRIORITY_SETTINGS);
    177         }
    178     };
    179 
    180 }
    181