• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.bluetooth;
18 
19 import android.app.Dialog;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 import android.util.Log;
26 import android.view.View;
27 import android.widget.Button;
28 import android.widget.TextView;
29 
30 import androidx.appcompat.app.AlertDialog;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.SubSettingLauncher;
34 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
35 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
36 import com.android.settingslib.bluetooth.LocalBluetoothManager;
37 import com.android.settingslib.media.MediaOutputConstants;
38 
39 /**
40  * This Dialog allowed users to do some actions for broadcast media or find the
41  * nearby broadcast sources.
42  */
43 public class BluetoothBroadcastDialog extends InstrumentedDialogFragment {
44 
45     public static final String KEY_APP_LABEL = "app_label";
46     public static final String KEY_DEVICE_ADDRESS =
47             BluetoothFindBroadcastsFragment.KEY_DEVICE_ADDRESS;
48     public static final String KEY_MEDIA_STREAMING = "media_streaming";
49 
50     private static final String TAG = "BTBroadcastsDialog";
51     private static final CharSequence UNKNOWN_APP_LABEL = "unknown";
52     private Context mContext;
53     private CharSequence mCurrentAppLabel = UNKNOWN_APP_LABEL;
54     private String mDeviceAddress;
55     private boolean mIsMediaStreaming;
56     private LocalBluetoothManager mLocalBluetoothManager;
57     private AlertDialog mAlertDialog;
58 
59     @Override
onCreate(Bundle savedInstanceState)60     public void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         mContext = getActivity();
63         mCurrentAppLabel = getActivity().getIntent().getCharSequenceExtra(KEY_APP_LABEL);
64         mDeviceAddress = getActivity().getIntent().getStringExtra(KEY_DEVICE_ADDRESS);
65         mIsMediaStreaming = getActivity().getIntent().getBooleanExtra(KEY_MEDIA_STREAMING, false);
66         mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
67         setShowsDialog(true);
68     }
69 
70     @Override
onCreateDialog(Bundle savedInstanceState)71     public Dialog onCreateDialog(Bundle savedInstanceState) {
72         View layout = View.inflate(mContext,
73                 com.android.settingslib.R.layout.broadcast_dialog, null);
74 
75         TextView title = layout.findViewById(com.android.settingslib.R.id.dialog_title);
76         TextView subTitle = layout.findViewById(com.android.settingslib.R.id.dialog_subtitle);
77 
78         Button broadcastBtn = layout.findViewById(com.android.settingslib.R.id.positive_btn);
79         if (isBroadcastSupported() && mIsMediaStreaming) {
80             title.setText(mContext.getString(R.string.bluetooth_broadcast_dialog_title));
81             subTitle.setText(
82                     mContext.getString(R.string.bluetooth_broadcast_dialog_broadcast_message));
83             broadcastBtn.setVisibility(View.VISIBLE);
84             if (TextUtils.isEmpty(mCurrentAppLabel)) {
85                 broadcastBtn.setText(mContext.getString(R.string.bluetooth_broadcast_dialog_title));
86             } else {
87                 broadcastBtn.setText(mContext.getString(
88                     R.string.bluetooth_broadcast_dialog_broadcast_app,
89                     String.valueOf(mCurrentAppLabel)));
90             }
91             broadcastBtn.setOnClickListener((view) -> {
92                 launchMediaOutputBroadcastDialog();
93             });
94         } else {
95             title.setText(mContext.getString(R.string.bluetooth_find_broadcast));
96             subTitle.setText(
97                     mContext.getString(R.string.bluetooth_broadcast_dialog_find_message));
98             broadcastBtn.setVisibility(View.GONE);
99         }
100 
101         Button findBroadcastBtn = layout.findViewById(com.android.settingslib.R.id.negative_btn);
102         findBroadcastBtn.setText(mContext.getString(R.string.bluetooth_find_broadcast));
103         findBroadcastBtn.setOnClickListener((view) -> {
104             launchFindBroadcastsActivity();
105         });
106 
107         Button cancelBtn = layout.findViewById(com.android.settingslib.R.id.neutral_btn);
108         cancelBtn.setOnClickListener((view) -> {
109             dismiss();
110             getActivity().finish();
111         });
112 
113         mAlertDialog = new AlertDialog.Builder(mContext,
114                 com.android.settingslib.R.style.Theme_AlertDialog_SettingsLib)
115             .setView(layout)
116             .create();
117 
118         return mAlertDialog;
119     }
120 
121     @Override
onStart()122     public void onStart() {
123         super.onStart();
124     }
125 
126     @Override
getMetricsCategory()127     public int getMetricsCategory() {
128         //TODO(b/228255796) : add new enum for find broadcast fragment
129         return SettingsEnums.PAGE_UNKNOWN;
130     }
131 
launchFindBroadcastsActivity()132     private void launchFindBroadcastsActivity() {
133         Bundle bundle = new Bundle();
134         bundle.putString(KEY_DEVICE_ADDRESS, mDeviceAddress);
135 
136         new SubSettingLauncher(mContext)
137                 .setTitleRes(R.string.bluetooth_find_broadcast_title)
138                 .setDestination(BluetoothFindBroadcastsFragment.class.getName())
139                 .setArguments(bundle)
140                 .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN)
141                 .launch();
142         dismissVolumePanel();
143     }
144 
launchMediaOutputBroadcastDialog()145     private void launchMediaOutputBroadcastDialog() {
146         if (startBroadcast()) {
147             mContext.sendBroadcast(new Intent()
148                     .setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME)
149                     .setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_BROADCAST_DIALOG)
150                     .putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME,
151                             getActivity().getPackageName()));
152             dismissVolumePanel();
153         }
154     }
155 
getLEAudioBroadcastProfile()156     private LocalBluetoothLeBroadcast getLEAudioBroadcastProfile() {
157         if (mLocalBluetoothManager != null && mLocalBluetoothManager.getProfileManager() != null) {
158             LocalBluetoothLeBroadcast bluetoothLeBroadcast =
159                     mLocalBluetoothManager.getProfileManager().getLeAudioBroadcastProfile();
160             if (bluetoothLeBroadcast != null) {
161                 return bluetoothLeBroadcast;
162             }
163         }
164         Log.d(TAG, "Can not get LE Audio Broadcast Profile");
165         return null;
166     }
167 
startBroadcast()168     private boolean startBroadcast() {
169         LocalBluetoothLeBroadcast btLeBroadcast = getLEAudioBroadcastProfile();
170         if (btLeBroadcast != null) {
171             btLeBroadcast.startBroadcast(String.valueOf(mCurrentAppLabel), null);
172             return true;
173         }
174         Log.d(TAG, "Can not broadcast successfully");
175         return false;
176     }
177 
dismissVolumePanel()178     private void dismissVolumePanel() {
179         // Dismiss volume panel
180         mContext.sendBroadcast(new Intent()
181                 .setPackage(MediaOutputConstants.SETTINGS_PACKAGE_NAME)
182                 .setAction(MediaOutputConstants.ACTION_CLOSE_PANEL));
183     }
184 
isBroadcastSupported()185     boolean isBroadcastSupported() {
186         LocalBluetoothLeBroadcast broadcast =
187                 mLocalBluetoothManager.getProfileManager().getLeAudioBroadcastProfile();
188         return broadcast != null;
189     }
190 }
191