blob: 03b61e076c734672f1e81cab55441f1f25298e21 [file] [log] [blame]
Heemin Seog11e0af32020-04-21 09:57:19 -07001/*
2 * Copyright (C) 2020 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.car.volume;
18
19import android.car.Car;
20import android.car.media.CarAudioManager;
21import android.content.Context;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.os.Handler;
25import android.util.Log;
26
27import com.android.systemui.R;
28import com.android.systemui.SystemUI;
29import com.android.systemui.car.CarServiceProvider;
30import com.android.systemui.dagger.qualifiers.Main;
31import com.android.systemui.volume.VolumeDialogComponent;
32
33import java.io.FileDescriptor;
34import java.io.PrintWriter;
35
36import javax.inject.Inject;
37import javax.inject.Singleton;
38
39import dagger.Lazy;
40
41/** The entry point for controlling the volume ui in cars. */
42@Singleton
43public class VolumeUI extends SystemUI {
44
45 private static final String TAG = "VolumeUI";
46 private final Resources mResources;
47 private final Handler mMainHandler;
48 private final CarServiceProvider mCarServiceProvider;
49 private final Lazy<VolumeDialogComponent> mVolumeDialogComponentLazy;
50
51 private final CarAudioManager.CarVolumeCallback mVolumeChangeCallback =
52 new CarAudioManager.CarVolumeCallback() {
53 @Override
54 public void onGroupVolumeChanged(int zoneId, int groupId, int flags) {
Agatha Maneb971132020-05-11 14:50:29 -070055 initVolumeDialogComponent();
56 }
57
58 @Override
59 public void onMasterMuteChanged(int zoneId, int flags) {
60 initVolumeDialogComponent();
61 }
62
63 private void initVolumeDialogComponent() {
Heemin Seog11e0af32020-04-21 09:57:19 -070064 if (mVolumeDialogComponent == null) {
65 mMainHandler.post(() -> {
66 mVolumeDialogComponent = mVolumeDialogComponentLazy.get();
67 mVolumeDialogComponent.register();
68 });
69 mCarAudioManager.unregisterCarVolumeCallback(mVolumeChangeCallback);
70 }
71 }
Heemin Seog11e0af32020-04-21 09:57:19 -070072 };
73
74 private boolean mEnabled;
75 private CarAudioManager mCarAudioManager;
76 private VolumeDialogComponent mVolumeDialogComponent;
77
78 @Inject
79 public VolumeUI(
80 Context context,
81 @Main Resources resources,
82 @Main Handler mainHandler,
83 CarServiceProvider carServiceProvider,
84 Lazy<VolumeDialogComponent> volumeDialogComponentLazy
85 ) {
86 super(context);
87 mResources = resources;
88 mMainHandler = mainHandler;
89 mCarServiceProvider = carServiceProvider;
90 mVolumeDialogComponentLazy = volumeDialogComponentLazy;
91 }
92
93 @Override
94 public void start() {
95 boolean enableVolumeUi = mResources.getBoolean(R.bool.enable_volume_ui);
96 mEnabled = enableVolumeUi;
97 if (!mEnabled) return;
98
99 mCarServiceProvider.addListener(car -> {
100 if (mCarAudioManager != null) {
101 return;
102 }
103
104 mCarAudioManager = (CarAudioManager) car.getCarManager(Car.AUDIO_SERVICE);
105 Log.d(TAG, "Registering mVolumeChangeCallback.");
106 // This volume call back is never unregistered because CarStatusBar is
107 // never destroyed.
108 mCarAudioManager.registerCarVolumeCallback(mVolumeChangeCallback);
109 });
110 }
111
112 @Override
113 protected void onConfigurationChanged(Configuration newConfig) {
114 super.onConfigurationChanged(newConfig);
115 if (!mEnabled) return;
116 if (mVolumeDialogComponent != null) {
117 mVolumeDialogComponent.onConfigurationChanged(newConfig);
118 }
119 }
120
121 @Override
122 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
123 pw.print("mEnabled="); pw.println(mEnabled);
124 if (!mEnabled) return;
125 if (mVolumeDialogComponent != null) {
126 mVolumeDialogComponent.dump(fd, pw, args);
127 }
128 }
129}