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.settings.connecteddevice.audiosharing.audiostreams;
18 
19 import static com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsScanQrCodeController.REQUEST_SCAN_BT_BROADCAST_QR_CODE;
20 
21 import android.app.Activity;
22 import android.app.settings.SettingsEnums;
23 import android.bluetooth.BluetoothLeBroadcastMetadata;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.util.Log;
28 
29 import androidx.annotation.Nullable;
30 
31 import com.android.settings.R;
32 import com.android.settings.dashboard.DashboardFragment;
33 import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt;
34 import com.android.settingslib.bluetooth.BluetoothUtils;
35 
36 public class AudioStreamsDashboardFragment extends DashboardFragment {
37     public static final String KEY_BROADCAST_METADATA = "key_broadcast_metadata";
38     private static final String TAG = "AudioStreamsDashboardFrag";
39     private static final boolean DEBUG = BluetoothUtils.D;
40     private AudioStreamsProgressCategoryController mAudioStreamsProgressCategoryController;
41 
AudioStreamsDashboardFragment()42     public AudioStreamsDashboardFragment() {
43         super();
44     }
45 
46     @Override
getMetricsCategory()47     public int getMetricsCategory() {
48         return SettingsEnums.AUDIO_STREAM_MAIN;
49     }
50 
51     @Override
getLogTag()52     protected String getLogTag() {
53         return TAG;
54     }
55 
56     @Override
getHelpResource()57     public int getHelpResource() {
58         return R.string.help_url_audio_sharing;
59     }
60 
61     @Override
getPreferenceScreenResId()62     protected int getPreferenceScreenResId() {
63         return R.xml.bluetooth_le_audio_streams;
64     }
65 
66     @Override
onCreate(Bundle savedInstanceState)67     public void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69     }
70 
71     @Override
onAttach(Context context)72     public void onAttach(Context context) {
73         super.onAttach(context);
74         use(AudioStreamsScanQrCodeController.class).setFragment(this);
75         mAudioStreamsProgressCategoryController = use(AudioStreamsProgressCategoryController.class);
76         mAudioStreamsProgressCategoryController.setFragment(this);
77 
78         if (getArguments() != null) {
79             var broadcastMetadata =
80                     getArguments()
81                             .getParcelable(
82                                     KEY_BROADCAST_METADATA, BluetoothLeBroadcastMetadata.class);
83             if (broadcastMetadata != null) {
84                 mAudioStreamsProgressCategoryController.setSourceFromQrCode(
85                         broadcastMetadata, SourceOriginForLogging.QR_CODE_SCAN_OTHER);
86                 mMetricsFeatureProvider.action(
87                         getContext(),
88                         SettingsEnums.ACTION_AUDIO_STREAM_QR_CODE_SCAN_SUCCEED,
89                         SourceOriginForLogging.QR_CODE_SCAN_OTHER.ordinal());
90             }
91         }
92     }
93 
94     @Override
onActivityCreated(Bundle savedInstanceState)95     public void onActivityCreated(Bundle savedInstanceState) {
96         super.onActivityCreated(savedInstanceState);
97     }
98 
99     @Override
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)100     public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
101         super.onActivityResult(requestCode, resultCode, data);
102         if (DEBUG) {
103             Log.d(
104                     TAG,
105                     "onActivityResult() requestCode : "
106                             + requestCode
107                             + " resultCode : "
108                             + resultCode);
109         }
110         if (requestCode == REQUEST_SCAN_BT_BROADCAST_QR_CODE) {
111             if (resultCode == Activity.RESULT_OK) {
112                 String broadcastMetadata =
113                         data != null ? data.getStringExtra(KEY_BROADCAST_METADATA) : "";
114                 BluetoothLeBroadcastMetadata source =
115                         BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(
116                                 broadcastMetadata);
117                 if (source == null) {
118                     Log.w(TAG, "onActivityResult() source is null!");
119                     return;
120                 }
121                 if (DEBUG) {
122                     Log.d(TAG, "onActivityResult() broadcastId : " + source.getBroadcastId());
123                 }
124                 if (mAudioStreamsProgressCategoryController == null) {
125                     Log.w(
126                             TAG,
127                             "onActivityResult() AudioStreamsProgressCategoryController is null!");
128                     return;
129                 }
130                 mAudioStreamsProgressCategoryController.setSourceFromQrCode(
131                         source, SourceOriginForLogging.QR_CODE_SCAN_SETTINGS);
132                 mMetricsFeatureProvider.action(
133                         getContext(),
134                         SettingsEnums.ACTION_AUDIO_STREAM_QR_CODE_SCAN_SUCCEED,
135                         SourceOriginForLogging.QR_CODE_SCAN_SETTINGS.ordinal());
136             }
137         }
138     }
139 }
140