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 
35 import java.io.FileDescriptor;
36 import java.io.PrintWriter;
37 
38 /**
39  * Implementation of VolumeComponent backed by the new volume dialog.
40  */
41 public class VolumeDialogComponent implements VolumeComponent {
42     private final SystemUI mSysui;
43     private final Context mContext;
44     private final VolumeDialogController mController;
45     private final ZenModeController mZenModeController;
46     private final VolumeDialog mDialog;
47     private final VolumePolicy mVolumePolicy = new VolumePolicy(
48             true,  // volumeDownToEnterSilent
49             true,  // volumeUpToExitSilent
50             true,  // doNotDisturbWhenSilent
51             400    // vibrateToSilentDebounce
52     );
53 
VolumeDialogComponent(SystemUI sysui, Context context, Handler handler, ZenModeController zen)54     public VolumeDialogComponent(SystemUI sysui, Context context, Handler handler,
55             ZenModeController zen) {
56         mSysui = sysui;
57         mContext = context;
58         mController = new VolumeDialogController(context, null) {
59             @Override
60             protected void onUserActivityW() {
61                 sendUserActivity();
62             }
63         };
64         mZenModeController = zen;
65         mDialog = new VolumeDialog(context, WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY,
66                 mController, zen, mVolumeDialogCallback);
67         applyConfiguration();
68     }
69 
sendUserActivity()70     private void sendUserActivity() {
71         final KeyguardViewMediator kvm = mSysui.getComponent(KeyguardViewMediator.class);
72         if (kvm != null) {
73             kvm.userActivity();
74         }
75     }
76 
applyConfiguration()77     private void applyConfiguration() {
78         mDialog.setStreamImportant(AudioManager.STREAM_ALARM, true);
79         mDialog.setStreamImportant(AudioManager.STREAM_SYSTEM, false);
80         mDialog.setShowHeaders(false);
81         mDialog.setAutomute(true);
82         mDialog.setSilentMode(false);
83         mController.setVolumePolicy(mVolumePolicy);
84         mController.showDndTile(true);
85     }
86 
87     @Override
getZenController()88     public ZenModeController getZenController() {
89         return mZenModeController;
90     }
91 
92     @Override
onConfigurationChanged(Configuration newConfig)93     public void onConfigurationChanged(Configuration newConfig) {
94         // noop
95     }
96 
97     @Override
dismissNow()98     public void dismissNow() {
99         mController.dismiss();
100     }
101 
102     @Override
dispatchDemoCommand(String command, Bundle args)103     public void dispatchDemoCommand(String command, Bundle args) {
104         // noop
105     }
106 
107     @Override
register()108     public void register() {
109         mController.register();
110         DndTile.setCombinedIcon(mContext, true);
111     }
112 
113     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)114     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
115         mController.dump(fd, pw, args);
116         mDialog.dump(pw);
117     }
118 
startSettings(Intent intent)119     private void startSettings(Intent intent) {
120         mSysui.getComponent(PhoneStatusBar.class).startActivityDismissingKeyguard(intent,
121                 true /* onlyProvisioned */, true /* dismissShade */);
122     }
123 
124     private final VolumeDialog.Callback mVolumeDialogCallback = new VolumeDialog.Callback() {
125         @Override
126         public void onSettingsClicked() {
127             startSettings(new Intent(Settings.ACTION_NOTIFICATION_SETTINGS));
128         }
129 
130         @Override
131         public void onZenSettingsClicked() {
132             startSettings(ZenModePanel.ZEN_SETTINGS);
133         }
134 
135         @Override
136         public void onZenPrioritySettingsClicked() {
137             startSettings(ZenModePanel.ZEN_PRIORITY_SETTINGS);
138         }
139     };
140 
141 }
142