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; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.media.AudioAttributes; 23 import android.media.AudioManager; 24 import android.media.Ringtone; 25 import android.media.RingtoneManager; 26 import android.net.Uri; 27 import android.util.Log; 28 29 import androidx.annotation.NonNull; 30 import androidx.annotation.VisibleForTesting; 31 import androidx.lifecycle.LifecycleOwner; 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settings.R; 35 import com.android.settings.overlay.FeatureFactory; 36 import com.android.settingslib.bluetooth.BluetoothUtils; 37 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 38 39 public class AudioSharingPlaySoundPreferenceController 40 extends AudioSharingBasePreferenceController { 41 42 private static final String TAG = "AudioSharingPlaySoundPreferenceController"; 43 44 private static final String PREF_KEY = "audio_sharing_play_sound"; 45 46 private final MetricsFeatureProvider mMetricsFeatureProvider; 47 private Ringtone mRingtone; 48 AudioSharingPlaySoundPreferenceController(Context context)49 public AudioSharingPlaySoundPreferenceController(Context context) { 50 super(context, PREF_KEY); 51 mRingtone = RingtoneManager.getRingtone(context, getMediaVolumeUri()); 52 if (mRingtone != null) { 53 mRingtone.setStreamType(AudioManager.STREAM_MUSIC); 54 } 55 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 56 } 57 58 @Override getAvailabilityStatus()59 public int getAvailabilityStatus() { 60 return (mRingtone != null && BluetoothUtils.isAudioSharingUIAvailable(mContext)) 61 ? AVAILABLE 62 : UNSUPPORTED_ON_DEVICE; 63 } 64 65 @Override displayPreference(PreferenceScreen screen)66 public void displayPreference(PreferenceScreen screen) { 67 super.displayPreference(screen); 68 if (mPreference != null) { 69 mPreference.setOnPreferenceClickListener( 70 (v) -> { 71 if (mRingtone == null) { 72 Log.d(TAG, "Skip onClick due to ringtone is null"); 73 return true; 74 } 75 try { 76 mRingtone.setAudioAttributes( 77 new AudioAttributes.Builder(mRingtone.getAudioAttributes()) 78 .setFlags(AudioAttributes.FLAG_BYPASS_MUTE) 79 .addTag("VX_AOSP_SAMPLESOUND") 80 .build()); 81 if (!mRingtone.isPlaying()) { 82 mRingtone.play(); 83 mMetricsFeatureProvider.action( 84 mContext, 85 SettingsEnums.ACTION_AUDIO_SHARING_PLAY_TEST_SOUND); 86 } 87 } catch (Throwable e) { 88 Log.w(TAG, "Fail to play sample, error = " + e); 89 } 90 return true; 91 }); 92 } 93 } 94 95 @Override onStop(@onNull LifecycleOwner owner)96 public void onStop(@NonNull LifecycleOwner owner) { 97 super.onStop(owner); 98 if (mRingtone != null && mRingtone.isPlaying()) { 99 mRingtone.stop(); 100 } 101 } 102 103 @Override getPreferenceKey()104 public String getPreferenceKey() { 105 return PREF_KEY; 106 } 107 108 @VisibleForTesting setRingtone(Ringtone ringtone)109 void setRingtone(Ringtone ringtone) { 110 mRingtone = ringtone; 111 } 112 getMediaVolumeUri()113 private Uri getMediaVolumeUri() { 114 return Uri.parse( 115 ContentResolver.SCHEME_ANDROID_RESOURCE 116 + "://" 117 + mContext.getPackageName() 118 + "/" 119 + R.raw.media_volume); 120 } 121 } 122