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
17package com.android.systemui.volume;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.res.Configuration;
22import android.media.AudioManager;
23import android.media.VolumePolicy;
24import android.os.Bundle;
25import android.os.Handler;
26import android.provider.Settings;
27import android.view.WindowManager;
28
29import com.android.systemui.SystemUI;
30import com.android.systemui.keyguard.KeyguardViewMediator;
31import com.android.systemui.qs.tiles.DndTile;
32import com.android.systemui.statusbar.phone.PhoneStatusBar;
33import com.android.systemui.statusbar.policy.ZenModeController;
34
35import java.io.FileDescriptor;
36import java.io.PrintWriter;
37
38/**
39 * Implementation of VolumeComponent backed by the new volume dialog.
40 */
41public 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
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
70    private void sendUserActivity() {
71        final KeyguardViewMediator kvm = mSysui.getComponent(KeyguardViewMediator.class);
72        if (kvm != null) {
73            kvm.userActivity();
74        }
75    }
76
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
88    public ZenModeController getZenController() {
89        return mZenModeController;
90    }
91
92    @Override
93    public void onConfigurationChanged(Configuration newConfig) {
94        // noop
95    }
96
97    @Override
98    public void dismissNow() {
99        mController.dismiss();
100    }
101
102    @Override
103    public void dispatchDemoCommand(String command, Bundle args) {
104        // noop
105    }
106
107    @Override
108    public void register() {
109        mController.register();
110        DndTile.setCombinedIcon(mContext, true);
111    }
112
113    @Override
114    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
115        mController.dump(fd, pw, args);
116        mDialog.dump(pw);
117    }
118
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