1 /*
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 
17 package com.android.systemui.car.volume;
18 
19 import android.car.Car;
20 import android.car.media.CarAudioManager;
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.content.res.Resources;
24 import android.os.Handler;
25 import android.util.Log;
26 
27 import com.android.systemui.R;
28 import com.android.systemui.SystemUI;
29 import com.android.systemui.car.CarServiceProvider;
30 import com.android.systemui.dagger.qualifiers.Main;
31 import com.android.systemui.volume.VolumeDialogComponent;
32 
33 import java.io.FileDescriptor;
34 import java.io.PrintWriter;
35 
36 import javax.inject.Inject;
37 import javax.inject.Singleton;
38 
39 import dagger.Lazy;
40 
41 /** The entry point for controlling the volume ui in cars. */
42 @Singleton
43 public 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) {
55                     initVolumeDialogComponent();
56                 }
57 
58                 @Override
59                 public void onMasterMuteChanged(int zoneId, int flags) {
60                     initVolumeDialogComponent();
61                 }
62 
63                 private void initVolumeDialogComponent() {
64                     if (mVolumeDialogComponent == null) {
65                         mMainHandler.post(() -> {
66                             mVolumeDialogComponent = mVolumeDialogComponentLazy.get();
67                             mVolumeDialogComponent.register();
68                         });
69                         mCarAudioManager.unregisterCarVolumeCallback(mVolumeChangeCallback);
70                     }
71                 }
72             };
73 
74     private boolean mEnabled;
75     private CarAudioManager mCarAudioManager;
76     private VolumeDialogComponent mVolumeDialogComponent;
77 
78     @Inject
VolumeUI( Context context, @Main Resources resources, @Main Handler mainHandler, CarServiceProvider carServiceProvider, Lazy<VolumeDialogComponent> volumeDialogComponentLazy )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
start()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
onConfigurationChanged(Configuration newConfig)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
dump(FileDescriptor fd, PrintWriter pw, String[] args)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 }
130