• 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.VolumePolicy;
24 import android.os.Bundle;
25 import android.provider.Settings;
26 import android.view.WindowManager.LayoutParams;
27 
28 import com.android.internal.R;
29 import com.android.settingslib.applications.InterestingConfigChanges;
30 import com.android.systemui.dagger.SysUISingleton;
31 import com.android.systemui.demomode.DemoMode;
32 import com.android.systemui.demomode.DemoModeController;
33 import com.android.systemui.keyguard.KeyguardViewMediator;
34 import com.android.systemui.plugins.ActivityStarter;
35 import com.android.systemui.plugins.PluginDependencyProvider;
36 import com.android.systemui.plugins.VolumeDialog;
37 import com.android.systemui.plugins.VolumeDialogController;
38 import com.android.systemui.qs.tiles.DndTile;
39 import com.android.systemui.statusbar.policy.ExtensionController;
40 import com.android.systemui.tuner.TunerService;
41 
42 import java.io.PrintWriter;
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 import javax.inject.Inject;
47 
48 /**
49  * Implementation of VolumeComponent backed by the new volume dialog.
50  */
51 @SysUISingleton
52 public class VolumeDialogComponent implements VolumeComponent, TunerService.Tunable,
53         VolumeDialogControllerImpl.UserActivityListener{
54 
55     public static final String VOLUME_DOWN_SILENT = "sysui_volume_down_silent";
56     public static final String VOLUME_UP_SILENT = "sysui_volume_up_silent";
57     public static final String VOLUME_SILENT_DO_NOT_DISTURB = "sysui_do_not_disturb";
58 
59     private final boolean mDefaultVolumeDownToEnterSilent;
60     public static final boolean DEFAULT_VOLUME_UP_TO_EXIT_SILENT = false;
61     public static final boolean DEFAULT_DO_NOT_DISTURB_WHEN_SILENT = false;
62 
63     private static final Intent ZEN_SETTINGS =
64             new Intent(Settings.ACTION_ZEN_MODE_SETTINGS);
65     private static final Intent ZEN_PRIORITY_SETTINGS =
66             new Intent(Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS);
67 
68     protected final Context mContext;
69     private final VolumeDialogControllerImpl mController;
70     private final InterestingConfigChanges mConfigChanges = new InterestingConfigChanges(
71             ActivityInfo.CONFIG_FONT_SCALE | ActivityInfo.CONFIG_LOCALE
72             | ActivityInfo.CONFIG_ASSETS_PATHS | ActivityInfo.CONFIG_UI_MODE);
73     private final KeyguardViewMediator mKeyguardViewMediator;
74     private final ActivityStarter mActivityStarter;
75     private VolumeDialog mDialog;
76     private VolumePolicy mVolumePolicy;
77 
78     @Inject
VolumeDialogComponent( Context context, KeyguardViewMediator keyguardViewMediator, ActivityStarter activityStarter, VolumeDialogControllerImpl volumeDialogController, DemoModeController demoModeController, PluginDependencyProvider pluginDependencyProvider, ExtensionController extensionController, TunerService tunerService, VolumeDialog volumeDialog)79     public VolumeDialogComponent(
80             Context context,
81             KeyguardViewMediator keyguardViewMediator,
82             ActivityStarter activityStarter,
83             VolumeDialogControllerImpl volumeDialogController,
84             DemoModeController demoModeController,
85             PluginDependencyProvider pluginDependencyProvider,
86             ExtensionController extensionController,
87             TunerService tunerService,
88             VolumeDialog volumeDialog) {
89         mContext = context;
90         mKeyguardViewMediator = keyguardViewMediator;
91         mActivityStarter = activityStarter;
92         mController = volumeDialogController;
93         mController.setUserActivityListener(this);
94         // Allow plugins to reference the VolumeDialogController.
95         pluginDependencyProvider.allowPluginDependency(VolumeDialogController.class);
96         extensionController.newExtension(VolumeDialog.class)
97                 .withPlugin(VolumeDialog.class)
98                 .withDefault(() -> volumeDialog)
99                 .withCallback(dialog -> {
100                     if (mDialog != null) {
101                         mDialog.destroy();
102                     }
103                     mDialog = dialog;
104                     mDialog.init(LayoutParams.TYPE_VOLUME_OVERLAY, mVolumeDialogCallback);
105                 }).build();
106 
107 
108         mDefaultVolumeDownToEnterSilent = mContext.getResources()
109                 .getBoolean(R.bool.config_volume_down_to_enter_silent);
110 
111         mVolumePolicy = new VolumePolicy(
112                 mDefaultVolumeDownToEnterSilent,  // volumeDownToEnterSilent
113                 DEFAULT_VOLUME_UP_TO_EXIT_SILENT,  // volumeUpToExitSilent
114                 DEFAULT_DO_NOT_DISTURB_WHEN_SILENT,  // doNotDisturbWhenSilent
115                 400    // vibrateToSilentDebounce
116         );
117 
118         applyConfiguration();
119 
120         tunerService.addTunable(this, VOLUME_DOWN_SILENT, VOLUME_UP_SILENT,
121                 VOLUME_SILENT_DO_NOT_DISTURB);
122         demoModeController.addCallback(this);
123     }
124 
125     @Override
onTuningChanged(String key, String newValue)126     public void onTuningChanged(String key, String newValue) {
127         boolean volumeDownToEnterSilent = mVolumePolicy.volumeDownToEnterSilent;
128         boolean volumeUpToExitSilent = mVolumePolicy.volumeUpToExitSilent;
129         boolean doNotDisturbWhenSilent = mVolumePolicy.doNotDisturbWhenSilent;
130 
131         if (VOLUME_DOWN_SILENT.equals(key)) {
132             volumeDownToEnterSilent =
133                 TunerService.parseIntegerSwitch(newValue, mDefaultVolumeDownToEnterSilent);
134         } else if (VOLUME_UP_SILENT.equals(key)) {
135             volumeUpToExitSilent =
136                 TunerService.parseIntegerSwitch(newValue, DEFAULT_VOLUME_UP_TO_EXIT_SILENT);
137         } else if (VOLUME_SILENT_DO_NOT_DISTURB.equals(key)) {
138             doNotDisturbWhenSilent =
139                 TunerService.parseIntegerSwitch(newValue, DEFAULT_DO_NOT_DISTURB_WHEN_SILENT);
140         }
141 
142         setVolumePolicy(volumeDownToEnterSilent, volumeUpToExitSilent, doNotDisturbWhenSilent,
143                 mVolumePolicy.vibrateToSilentDebounce);
144     }
145 
setVolumePolicy(boolean volumeDownToEnterSilent, boolean volumeUpToExitSilent, boolean doNotDisturbWhenSilent, int vibrateToSilentDebounce)146     private void setVolumePolicy(boolean volumeDownToEnterSilent, boolean volumeUpToExitSilent,
147             boolean doNotDisturbWhenSilent, int vibrateToSilentDebounce) {
148         mVolumePolicy = new VolumePolicy(volumeDownToEnterSilent, volumeUpToExitSilent,
149                 doNotDisturbWhenSilent, vibrateToSilentDebounce);
150         mController.setVolumePolicy(mVolumePolicy);
151     }
152 
setEnableDialogs(boolean volumeUi, boolean safetyWarning)153     void setEnableDialogs(boolean volumeUi, boolean safetyWarning) {
154         mController.setEnableDialogs(volumeUi, safetyWarning);
155     }
156 
157     @Override
onUserActivity()158     public void onUserActivity() {
159         mKeyguardViewMediator.userActivity();
160     }
161 
applyConfiguration()162     private void applyConfiguration() {
163         mController.setVolumePolicy(mVolumePolicy);
164         mController.showDndTile();
165     }
166 
167     @Override
onConfigurationChanged(Configuration newConfig)168     public void onConfigurationChanged(Configuration newConfig) {
169         if (mConfigChanges.applyNewConfig(mContext.getResources())) {
170             mController.mCallbacks.onConfigurationChanged();
171         }
172     }
173 
174     @Override
dismissNow()175     public void dismissNow() {
176         mController.dismiss();
177     }
178 
179     @Override
dispatchDemoCommand(String command, Bundle args)180     public void dispatchDemoCommand(String command, Bundle args) {
181         // noop
182     }
183 
184     @Override
demoCommands()185     public List<String> demoCommands() {
186         List<String> s = new ArrayList<>();
187         s.add(DemoMode.COMMAND_VOLUME);
188         return s;
189     }
190 
191     @Override
register()192     public void register() {
193         mController.register();
194         DndTile.setCombinedIcon(mContext, true);
195     }
196 
197     @Override
dump(PrintWriter pw, String[] args)198     public void dump(PrintWriter pw, String[] args) {
199     }
200 
startSettings(Intent intent)201     private void startSettings(Intent intent) {
202         mActivityStarter.startActivity(intent, true /* onlyProvisioned */, true /* dismissShade */);
203     }
204 
205     private final VolumeDialogImpl.Callback mVolumeDialogCallback = new VolumeDialogImpl.Callback() {
206         @Override
207         public void onZenSettingsClicked() {
208             startSettings(ZEN_SETTINGS);
209         }
210 
211         @Override
212         public void onZenPrioritySettingsClicked() {
213             startSettings(ZEN_PRIORITY_SETTINGS);
214         }
215     };
216 
217 }
218