Heemin Seog | 11e0af3 | 2020-04-21 09:57:19 -0700 | [diff] [blame] | 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 | if (mVolumeDialogComponent == null) { |
| 56 | mMainHandler.post(() -> { |
| 57 | mVolumeDialogComponent = mVolumeDialogComponentLazy.get(); |
| 58 | mVolumeDialogComponent.register(); |
| 59 | }); |
| 60 | mCarAudioManager.unregisterCarVolumeCallback(mVolumeChangeCallback); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onMasterMuteChanged(int zoneId, int flags) { |
| 66 | // ignored |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | private boolean mEnabled; |
| 71 | private CarAudioManager mCarAudioManager; |
| 72 | private VolumeDialogComponent mVolumeDialogComponent; |
| 73 | |
| 74 | @Inject |
| 75 | public VolumeUI( |
| 76 | Context context, |
| 77 | @Main Resources resources, |
| 78 | @Main Handler mainHandler, |
| 79 | CarServiceProvider carServiceProvider, |
| 80 | Lazy<VolumeDialogComponent> volumeDialogComponentLazy |
| 81 | ) { |
| 82 | super(context); |
| 83 | mResources = resources; |
| 84 | mMainHandler = mainHandler; |
| 85 | mCarServiceProvider = carServiceProvider; |
| 86 | mVolumeDialogComponentLazy = volumeDialogComponentLazy; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void start() { |
| 91 | boolean enableVolumeUi = mResources.getBoolean(R.bool.enable_volume_ui); |
| 92 | mEnabled = enableVolumeUi; |
| 93 | if (!mEnabled) return; |
| 94 | |
| 95 | mCarServiceProvider.addListener(car -> { |
| 96 | if (mCarAudioManager != null) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | mCarAudioManager = (CarAudioManager) car.getCarManager(Car.AUDIO_SERVICE); |
| 101 | Log.d(TAG, "Registering mVolumeChangeCallback."); |
| 102 | // This volume call back is never unregistered because CarStatusBar is |
| 103 | // never destroyed. |
| 104 | mCarAudioManager.registerCarVolumeCallback(mVolumeChangeCallback); |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void onConfigurationChanged(Configuration newConfig) { |
| 110 | super.onConfigurationChanged(newConfig); |
| 111 | if (!mEnabled) return; |
| 112 | if (mVolumeDialogComponent != null) { |
| 113 | mVolumeDialogComponent.onConfigurationChanged(newConfig); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 119 | pw.print("mEnabled="); pw.println(mEnabled); |
| 120 | if (!mEnabled) return; |
| 121 | if (mVolumeDialogComponent != null) { |
| 122 | mVolumeDialogComponent.dump(fd, pw, args); |
| 123 | } |
| 124 | } |
| 125 | } |