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 android.app.settings.SettingsEnums; 20 import android.os.Bundle; 21 22 import androidx.annotation.Nullable; 23 import androidx.annotation.VisibleForTesting; 24 import androidx.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settings.core.SubSettingLauncher; 28 import com.android.settings.dashboard.DashboardFragment; 29 import com.android.settingslib.flags.Flags; 30 31 class SourceAddedState extends AudioStreamStateHandler { 32 @VisibleForTesting 33 static final int AUDIO_STREAM_SOURCE_ADDED_STATE_SUMMARY = R.string.audio_streams_listening_now; 34 35 @Nullable private static SourceAddedState sInstance = null; 36 37 @VisibleForTesting SourceAddedState()38 SourceAddedState() {} 39 getInstance()40 static SourceAddedState getInstance() { 41 if (sInstance == null) { 42 sInstance = new SourceAddedState(); 43 } 44 return sInstance; 45 } 46 47 @Override performAction( AudioStreamPreference preference, AudioStreamsProgressCategoryController controller, AudioStreamsHelper helper)48 void performAction( 49 AudioStreamPreference preference, 50 AudioStreamsProgressCategoryController controller, 51 AudioStreamsHelper helper) { 52 var context = preference.getContext(); 53 // Saved connected metadata for user to re-join this broadcast later. 54 var cached = 55 mAudioStreamsRepository.getCachedMetadata(preference.getAudioStreamBroadcastId()); 56 if (cached != null) { 57 mAudioStreamsRepository.saveMetadata(context, cached); 58 } 59 if (!Flags.audioStreamMediaServiceByReceiveState()) { 60 helper.startMediaService( 61 context, 62 preference.getAudioStreamBroadcastId(), 63 String.valueOf(preference.getTitle())); 64 } 65 mMetricsFeatureProvider.action( 66 preference.getContext(), 67 SettingsEnums.ACTION_AUDIO_STREAM_JOIN_SUCCEED, 68 preference.getSourceOriginForLogging().ordinal()); 69 } 70 71 @Override getSummary()72 int getSummary() { 73 return AUDIO_STREAM_SOURCE_ADDED_STATE_SUMMARY; 74 } 75 76 @Override getOnClickListener( AudioStreamsProgressCategoryController controller)77 Preference.OnPreferenceClickListener getOnClickListener( 78 AudioStreamsProgressCategoryController controller) { 79 return preference -> { 80 var p = (AudioStreamPreference) preference; 81 Bundle broadcast = new Bundle(); 82 broadcast.putString( 83 AudioStreamDetailsFragment.BROADCAST_NAME_ARG, (String) p.getTitle()); 84 broadcast.putInt( 85 AudioStreamDetailsFragment.BROADCAST_ID_ARG, p.getAudioStreamBroadcastId()); 86 87 new SubSettingLauncher(p.getContext()) 88 .setTitleRes(R.string.audio_streams_detail_page_title) 89 .setDestination(AudioStreamDetailsFragment.class.getName()) 90 .setSourceMetricsCategory( 91 !(controller.getFragment() instanceof DashboardFragment) 92 ? SettingsEnums.PAGE_UNKNOWN 93 : ((DashboardFragment) controller.getFragment()) 94 .getMetricsCategory()) 95 .setArguments(broadcast) 96 .launch(); 97 return true; 98 }; 99 } 100 101 @Override getStateEnum()102 AudioStreamsProgressCategoryController.AudioStreamState getStateEnum() { 103 return AudioStreamsProgressCategoryController.AudioStreamState.SOURCE_ADDED; 104 } 105 } 106