1 /*
2  * Copyright (C) 2023 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.settings.connecteddevice.audiosharing;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.os.Bundle;
22 
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.settings.R;
26 import com.android.settings.SettingsActivity;
27 import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsCategoryController;
28 import com.android.settings.dashboard.DashboardFragment;
29 import com.android.settings.widget.SettingsMainSwitchBar;
30 
31 public class AudioSharingDashboardFragment extends DashboardFragment
32         implements AudioSharingSwitchBarController.OnAudioSharingStateChangedListener {
33     private static final String TAG = "AudioSharingDashboardFrag";
34 
35     SettingsMainSwitchBar mMainSwitchBar;
36     private AudioSharingDeviceVolumeGroupController mAudioSharingDeviceVolumeGroupController;
37     private AudioSharingCallAudioPreferenceController mAudioSharingCallAudioPreferenceController;
38     private AudioSharingPlaySoundPreferenceController mAudioSharingPlaySoundPreferenceController;
39     private AudioStreamsCategoryController mAudioStreamsCategoryController;
40 
AudioSharingDashboardFragment()41     public AudioSharingDashboardFragment() {
42         super();
43     }
44 
45     @Override
getMetricsCategory()46     public int getMetricsCategory() {
47         return SettingsEnums.AUDIO_SHARING_SETTINGS;
48     }
49 
50     @Override
getLogTag()51     protected String getLogTag() {
52         return TAG;
53     }
54 
55     @Override
getHelpResource()56     public int getHelpResource() {
57         return R.string.help_url_audio_sharing;
58     }
59 
60     @Override
getPreferenceScreenResId()61     protected int getPreferenceScreenResId() {
62         return R.xml.bluetooth_le_audio_sharing;
63     }
64 
65     @Override
onAttach(Context context)66     public void onAttach(Context context) {
67         super.onAttach(context);
68         mAudioSharingDeviceVolumeGroupController =
69                 use(AudioSharingDeviceVolumeGroupController.class);
70         mAudioSharingDeviceVolumeGroupController.init(this);
71         mAudioSharingCallAudioPreferenceController =
72                 use(AudioSharingCallAudioPreferenceController.class);
73         mAudioSharingCallAudioPreferenceController.init(this);
74         mAudioSharingPlaySoundPreferenceController =
75                 use(AudioSharingPlaySoundPreferenceController.class);
76         mAudioStreamsCategoryController = use(AudioStreamsCategoryController.class);
77     }
78 
79     @Override
onActivityCreated(Bundle savedInstanceState)80     public void onActivityCreated(Bundle savedInstanceState) {
81         super.onActivityCreated(savedInstanceState);
82         // Assume we are in a SettingsActivity. This is only safe because we currently use
83         // SettingsActivity as base for all preference fragments.
84         final SettingsActivity activity = (SettingsActivity) getActivity();
85         mMainSwitchBar = activity.getSwitchBar();
86         mMainSwitchBar.setTitle(getText(R.string.audio_sharing_switch_title));
87         AudioSharingSwitchBarController switchBarController =
88                 new AudioSharingSwitchBarController(activity, mMainSwitchBar, this);
89         switchBarController.init(this);
90         getSettingsLifecycle().addObserver(switchBarController);
91         mMainSwitchBar.show();
92     }
93 
94     @Override
onAudioSharingStateChanged()95     public void onAudioSharingStateChanged() {
96         updateVisibilityForAttachedPreferences();
97     }
98 
99     @Override
onAudioSharingProfilesConnected()100     public void onAudioSharingProfilesConnected() {
101         onProfilesConnectedForAttachedPreferences();
102     }
103 
104     /** Test only: set mock controllers for the {@link AudioSharingDashboardFragment} */
105     @VisibleForTesting
setControllers( AudioSharingDeviceVolumeGroupController volumeGroupController, AudioSharingCallAudioPreferenceController callAudioController, AudioSharingPlaySoundPreferenceController playSoundController, AudioStreamsCategoryController streamsCategoryController)106     void setControllers(
107             AudioSharingDeviceVolumeGroupController volumeGroupController,
108             AudioSharingCallAudioPreferenceController callAudioController,
109             AudioSharingPlaySoundPreferenceController playSoundController,
110             AudioStreamsCategoryController streamsCategoryController) {
111         mAudioSharingDeviceVolumeGroupController = volumeGroupController;
112         mAudioSharingCallAudioPreferenceController = callAudioController;
113         mAudioSharingPlaySoundPreferenceController = playSoundController;
114         mAudioStreamsCategoryController = streamsCategoryController;
115     }
116 
updateVisibilityForAttachedPreferences()117     private void updateVisibilityForAttachedPreferences() {
118         mAudioSharingDeviceVolumeGroupController.updateVisibility();
119         mAudioSharingCallAudioPreferenceController.updateVisibility();
120         mAudioSharingPlaySoundPreferenceController.updateVisibility();
121         mAudioStreamsCategoryController.updateVisibility();
122     }
123 
onProfilesConnectedForAttachedPreferences()124     private void onProfilesConnectedForAttachedPreferences() {
125         mAudioSharingDeviceVolumeGroupController.onAudioSharingProfilesConnected();
126     }
127 }
128