• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.pm.ActivityInfo;
22 import android.content.res.Configuration;
23 import android.media.AudioManager;
24 import android.media.VolumePolicy;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.view.WindowManager.LayoutParams;
28 
29 import com.android.settingslib.applications.InterestingConfigChanges;
30 import com.android.systemui.plugins.ActivityStarter;
31 import com.android.systemui.Dependency;
32 import com.android.systemui.SystemUI;
33 import com.android.systemui.keyguard.KeyguardViewMediator;
34 import com.android.systemui.plugins.PluginDependencyProvider;
35 import com.android.systemui.plugins.VolumeDialog;
36 import com.android.systemui.plugins.VolumeDialogController;
37 import com.android.systemui.qs.tiles.DndTile;
38 import com.android.systemui.statusbar.policy.ExtensionController;
39 import com.android.systemui.statusbar.policy.ExtensionController.Extension;
40 import com.android.systemui.tuner.TunerService;
41 
42 import java.io.FileDescriptor;
43 import java.io.PrintWriter;
44 
45 /**
46  * Implementation of VolumeComponent backed by the new volume dialog.
47  */
48 public class VolumeDialogComponent implements VolumeComponent, TunerService.Tunable,
49         VolumeDialogControllerImpl.UserActivityListener{
50 
51     public static final String VOLUME_DOWN_SILENT = "sysui_volume_down_silent";
52     public static final String VOLUME_UP_SILENT = "sysui_volume_up_silent";
53     public static final String VOLUME_SILENT_DO_NOT_DISTURB = "sysui_do_not_disturb";
54 
55     public static final boolean DEFAULT_VOLUME_DOWN_TO_ENTER_SILENT = true;
56     public static final boolean DEFAULT_VOLUME_UP_TO_EXIT_SILENT = true;
57     public static final boolean DEFAULT_DO_NOT_DISTURB_WHEN_SILENT = true;
58 
59     private final SystemUI mSysui;
60     private final Context mContext;
61     private final VolumeDialogControllerImpl mController;
62     private final InterestingConfigChanges mConfigChanges = new InterestingConfigChanges(
63             ActivityInfo.CONFIG_FONT_SCALE | ActivityInfo.CONFIG_LOCALE
64             | ActivityInfo.CONFIG_ASSETS_PATHS);
65     private final Extension mExtension;
66     private VolumeDialog mDialog;
67     private VolumePolicy mVolumePolicy = new VolumePolicy(
68             DEFAULT_VOLUME_DOWN_TO_ENTER_SILENT,  // volumeDownToEnterSilent
69             DEFAULT_VOLUME_UP_TO_EXIT_SILENT,  // volumeUpToExitSilent
70             DEFAULT_DO_NOT_DISTURB_WHEN_SILENT,  // doNotDisturbWhenSilent
71             400    // vibrateToSilentDebounce
72     );
73 
VolumeDialogComponent(SystemUI sysui, Context context, Handler handler)74     public VolumeDialogComponent(SystemUI sysui, Context context, Handler handler) {
75         mSysui = sysui;
76         mContext = context;
77         mController = (VolumeDialogControllerImpl) Dependency.get(VolumeDialogController.class);
78         mController.setUserActivityListener(this);
79         // Allow plugins to reference the VolumeDialogController.
80         Dependency.get(PluginDependencyProvider.class)
81                 .allowPluginDependency(VolumeDialogController.class);
82         mExtension = Dependency.get(ExtensionController.class).newExtension(VolumeDialog.class)
83                 .withPlugin(VolumeDialog.class)
84                 .withDefault(this::createDefault)
85                 .withCallback(dialog -> {
86                     if (mDialog != null) {
87                         mDialog.destroy();
88                     }
89                     mDialog = dialog;
90                     mDialog.init(LayoutParams.TYPE_VOLUME_OVERLAY, mVolumeDialogCallback);
91                 }).build();
92         applyConfiguration();
93         Dependency.get(TunerService.class).addTunable(this, VOLUME_DOWN_SILENT, VOLUME_UP_SILENT,
94                 VOLUME_SILENT_DO_NOT_DISTURB);
95     }
96 
createDefault()97     private VolumeDialog createDefault() {
98         VolumeDialogImpl impl = new VolumeDialogImpl(mContext);
99         impl.setStreamImportant(AudioManager.STREAM_ALARM, true);
100         impl.setStreamImportant(AudioManager.STREAM_SYSTEM, false);
101         impl.setAutomute(true);
102         impl.setSilentMode(false);
103         return impl;
104     }
105 
106     @Override
onTuningChanged(String key, String newValue)107     public void onTuningChanged(String key, String newValue) {
108         if (VOLUME_DOWN_SILENT.equals(key)) {
109             final boolean volumeDownToEnterSilent = newValue != null
110                     ? Integer.parseInt(newValue) != 0
111                     : DEFAULT_VOLUME_DOWN_TO_ENTER_SILENT;
112             setVolumePolicy(volumeDownToEnterSilent,
113                     mVolumePolicy.volumeUpToExitSilent, mVolumePolicy.doNotDisturbWhenSilent,
114                     mVolumePolicy.vibrateToSilentDebounce);
115         } else if (VOLUME_UP_SILENT.equals(key)) {
116             final boolean volumeUpToExitSilent = newValue != null
117                     ? Integer.parseInt(newValue) != 0
118                     : DEFAULT_VOLUME_UP_TO_EXIT_SILENT;
119             setVolumePolicy(mVolumePolicy.volumeDownToEnterSilent,
120                     volumeUpToExitSilent, mVolumePolicy.doNotDisturbWhenSilent,
121                     mVolumePolicy.vibrateToSilentDebounce);
122         } else if (VOLUME_SILENT_DO_NOT_DISTURB.equals(key)) {
123             final boolean doNotDisturbWhenSilent = newValue != null
124                     ? Integer.parseInt(newValue) != 0
125                     : DEFAULT_DO_NOT_DISTURB_WHEN_SILENT;
126             setVolumePolicy(mVolumePolicy.volumeDownToEnterSilent,
127                     mVolumePolicy.volumeUpToExitSilent, doNotDisturbWhenSilent,
128                     mVolumePolicy.vibrateToSilentDebounce);
129         }
130     }
131 
setVolumePolicy(boolean volumeDownToEnterSilent, boolean volumeUpToExitSilent, boolean doNotDisturbWhenSilent, int vibrateToSilentDebounce)132     private void setVolumePolicy(boolean volumeDownToEnterSilent, boolean volumeUpToExitSilent,
133             boolean doNotDisturbWhenSilent, int vibrateToSilentDebounce) {
134         mVolumePolicy = new VolumePolicy(volumeDownToEnterSilent, volumeUpToExitSilent,
135                 doNotDisturbWhenSilent, vibrateToSilentDebounce);
136         mController.setVolumePolicy(mVolumePolicy);
137     }
138 
139     @Override
onUserActivity()140     public void onUserActivity() {
141         final KeyguardViewMediator kvm = mSysui.getComponent(KeyguardViewMediator.class);
142         if (kvm != null) {
143             kvm.userActivity();
144         }
145     }
146 
applyConfiguration()147     private void applyConfiguration() {
148         mController.setVolumePolicy(mVolumePolicy);
149         mController.showDndTile(true);
150     }
151 
152     @Override
onConfigurationChanged(Configuration newConfig)153     public void onConfigurationChanged(Configuration newConfig) {
154         if (mConfigChanges.applyNewConfig(mContext.getResources())) {
155             mExtension.reload();
156         }
157     }
158 
159     @Override
dismissNow()160     public void dismissNow() {
161         mController.dismiss();
162     }
163 
164     @Override
dispatchDemoCommand(String command, Bundle args)165     public void dispatchDemoCommand(String command, Bundle args) {
166         // noop
167     }
168 
169     @Override
register()170     public void register() {
171         mController.register();
172         DndTile.setCombinedIcon(mContext, true);
173     }
174 
175     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)176     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
177     }
178 
startSettings(Intent intent)179     private void startSettings(Intent intent) {
180         Dependency.get(ActivityStarter.class).startActivity(intent,
181                 true /* onlyProvisioned */, true /* dismissShade */);
182     }
183 
184     private final VolumeDialogImpl.Callback mVolumeDialogCallback = new VolumeDialogImpl.Callback() {
185         @Override
186         public void onZenSettingsClicked() {
187             startSettings(ZenModePanel.ZEN_SETTINGS);
188         }
189 
190         @Override
191         public void onZenPrioritySettingsClicked() {
192             startSettings(ZenModePanel.ZEN_PRIORITY_SETTINGS);
193         }
194     };
195 
196 }
197