1 /*
2  * Copyright (C) 2024 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.car.settings.sound;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.os.Bundle;
23 
24 import com.android.car.settings.R;
25 import com.android.car.ui.AlertDialogBuilder;
26 import com.android.car.ui.preference.CarUiDialogFragment;
27 import com.android.car.ui.recyclerview.CarUiRadioButtonListItem;
28 import com.android.car.ui.recyclerview.CarUiRadioButtonListItemAdapter;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Dialog fragment for selecting the media audio routes.
35  */
36 public class AudioRouteSelectionDialogFragment extends CarUiDialogFragment {
37     private Context mContext;
38     private AlertDialog mAlertDialog;
39     private AudioRoutesManager mAudioRoutesManager;
40     private int mUsage;
41 
AudioRouteSelectionDialogFragment(Context context)42     public AudioRouteSelectionDialogFragment(Context context) {
43         mContext = context;
44         mUsage = context.getResources().getInteger(R.integer.audio_route_selector_usage);
45         mAudioRoutesManager = new AudioRoutesManager(context, mUsage);
46     }
47 
48     @Override
onCreateDialog(Bundle savedInstanceState)49     public Dialog onCreateDialog(Bundle savedInstanceState) {
50         List<String> addressList = mAudioRoutesManager.getAudioRouteList();
51         List<CarUiRadioButtonListItem> itemList = new ArrayList<>();
52         for (String address : addressList) {
53             CarUiRadioButtonListItem item = new CarUiRadioButtonListItem();
54             item.setTitle(mAudioRoutesManager.getDeviceNameForAddress(address));
55             item.setOnItemClickedListener(l -> mAudioRoutesManager.updateAudioRoute(address));
56             itemList.add(item);
57             if (address.equals(mAudioRoutesManager.getActiveDeviceAddress())) {
58                 item.setChecked(true);
59             }
60         }
61         CarUiRadioButtonListItemAdapter adapter = new CarUiRadioButtonListItemAdapter(itemList);
62 
63         AlertDialogBuilder builder = new AlertDialogBuilder(requireActivity())
64                 .setTitle(mContext.getString(R.string.audio_route_selector_title))
65                 .setSingleChoiceItems(adapter)
66                 .setNeutralButton(R.string.audio_route_dialog_neutral_button_text,
67                         /* listener */ null);
68         mAlertDialog = builder.create();
69 
70         return mAlertDialog;
71     }
72 
73     @Override
onDialogClosed(boolean positiveResult)74     protected void onDialogClosed(boolean positiveResult) {
75         getActivity().finish();
76     }
77 }
78