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 import android.util.AttributeSet;
21 
22 import androidx.annotation.NonNull;
23 
24 import com.android.settings.ProgressCategory;
25 import com.android.settings.R;
26 
27 import java.util.ArrayList;
28 import java.util.Comparator;
29 import java.util.List;
30 
31 public class AudioStreamsProgressCategoryPreference extends ProgressCategory {
32 
AudioStreamsProgressCategoryPreference(Context context)33     public AudioStreamsProgressCategoryPreference(Context context) {
34         super(context);
35         init();
36     }
37 
AudioStreamsProgressCategoryPreference(Context context, AttributeSet attrs)38     public AudioStreamsProgressCategoryPreference(Context context, AttributeSet attrs) {
39         super(context, attrs);
40         init();
41     }
42 
AudioStreamsProgressCategoryPreference( Context context, AttributeSet attrs, int defStyleAttr)43     public AudioStreamsProgressCategoryPreference(
44             Context context, AttributeSet attrs, int defStyleAttr) {
45         super(context, attrs, defStyleAttr);
46         init();
47     }
48 
AudioStreamsProgressCategoryPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)49     public AudioStreamsProgressCategoryPreference(
50             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
51         super(context, attrs, defStyleAttr, defStyleRes);
52         init();
53     }
54 
addAudioStreamPreference( @onNull AudioStreamPreference preference, Comparator<AudioStreamPreference> comparator)55     void addAudioStreamPreference(
56             @NonNull AudioStreamPreference preference,
57             Comparator<AudioStreamPreference> comparator) {
58         super.addPreference(preference);
59 
60         List<AudioStreamPreference> preferences = getAllAudioStreamPreferences();
61         preferences.sort(comparator);
62         for (int i = 0; i < preferences.size(); i++) {
63             // setOrder to i + 1, since the order 0 preference should always be the
64             // "audio_streams_scan_qr_code"
65             preferences.get(i).setOrder(i + 1);
66         }
67     }
68 
removeAudioStreamPreferences()69     void removeAudioStreamPreferences() {
70         List<AudioStreamPreference> streams = getAllAudioStreamPreferences();
71         for (var toRemove : streams) {
72             removePreference(toRemove);
73         }
74     }
75 
getAllAudioStreamPreferences()76     private List<AudioStreamPreference> getAllAudioStreamPreferences() {
77         List<AudioStreamPreference> streams = new ArrayList<>();
78         for (int i = 0; i < getPreferenceCount(); i++) {
79             if (getPreference(i) instanceof AudioStreamPreference) {
80                 streams.add((AudioStreamPreference) getPreference(i));
81             }
82         }
83         return streams;
84     }
85 
init()86     private void init() {
87         setEmptyTextRes(R.string.audio_streams_empty);
88     }
89 }
90