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.audiostreams;
18 
19 import android.content.Context;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.lifecycle.DefaultLifecycleObserver;
24 import androidx.lifecycle.LifecycleOwner;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.core.BasePreferenceController;
29 
30 public class AudioStreamsActiveDeviceController extends BasePreferenceController
31         implements AudioStreamsActiveDeviceSummaryUpdater.OnSummaryChangeListener,
32                 DefaultLifecycleObserver {
33 
34     public static final String KEY = "audio_streams_active_device";
35     private final AudioStreamsActiveDeviceSummaryUpdater mSummaryHelper;
36     @Nullable private Preference mPreference;
37 
AudioStreamsActiveDeviceController(Context context, String preferenceKey)38     public AudioStreamsActiveDeviceController(Context context, String preferenceKey) {
39         super(context, preferenceKey);
40         mSummaryHelper = new AudioStreamsActiveDeviceSummaryUpdater(mContext, this);
41     }
42 
43     @Override
displayPreference(PreferenceScreen screen)44     public void displayPreference(PreferenceScreen screen) {
45         super.displayPreference(screen);
46         mPreference = screen.findPreference(KEY);
47     }
48 
49     @Override
getAvailabilityStatus()50     public int getAvailabilityStatus() {
51         return AVAILABLE;
52     }
53 
54     @Override
onSummaryChanged(String summary)55     public void onSummaryChanged(String summary) {
56         if (mPreference != null) {
57             mPreference.setSummary(summary);
58         }
59     }
60 
61     @Override
onResume(@onNull LifecycleOwner owner)62     public void onResume(@NonNull LifecycleOwner owner) {
63         mSummaryHelper.register(true);
64     }
65 
66     @Override
onStop(@onNull LifecycleOwner owner)67     public void onStop(@NonNull LifecycleOwner owner) {
68         mSummaryHelper.register(false);
69     }
70 }
71