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