[Audiosharing] Support play test sound

Test: manual
Bug: 305620450
Change-Id: Id3f3b99f94a8a91d76fb9442310789b87c39bca0
This commit is contained in:
Yiyi Shen
2024-01-12 16:51:25 +08:00
parent 10fa20446c
commit 4af5178f57
4 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?android:attr/colorControlNormal">
<path
android:pathData="M14,8C9.6,8 6,11.6 6,16H8C8,12.7 10.7,10 14,10V8Z"
android:fillColor="#4E4639"/>
<path
android:pathData="M14,6V4C7.4,4 2,9.4 2,16H4C4,10.5 8.5,6 14,6Z"
android:fillColor="#4E4639"/>
<path
android:pathData="M16,4V12.6C15.4,12.3 14.7,12 14,12C11.8,12 10,13.8 10,16C10,18.2 11.8,20 14,20C16.2,20 18,18.2 18,16V7H22V4H16ZM14,18C12.9,18 12,17.1 12,16C12,14.9 12.9,14 14,14C15.1,14 16,14.9 16,16C16,17.1 15.1,18 14,18Z"
android:fillColor="#4E4639"/>
</vector>

View File

@@ -31,6 +31,13 @@
android:title="@string/calls_and_alarms_device_title"
settings:controller="com.android.settings.connecteddevice.audiosharing.CallsAndAlarmsPreferenceController" />
<Preference
android:icon="@drawable/ic_audio_play_sample"
android:key="audio_sharing_play_sound"
android:summary="Everyone listening should hear it"
android:title="Play a test sound"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingPlaySoundPreferenceController" />
<com.android.settings.connecteddevice.audiosharing.AudioSharingNamePreference
android:key="audio_sharing_stream_name"
android:summary="********"

View File

@@ -34,6 +34,7 @@ public class AudioSharingDashboardFragment extends DashboardFragment
private AudioSharingSwitchBarController mSwitchBarController;
private AudioSharingDeviceVolumeGroupController mAudioSharingDeviceVolumeGroupController;
private CallsAndAlarmsPreferenceController mCallsAndAlarmsPreferenceController;
private AudioSharingPlaySoundPreferenceController mAudioSharingPlaySoundPreferenceController;
private AudioSharingNamePreferenceController mAudioSharingNamePreferenceController;
private AudioStreamsCategoryController mAudioStreamsCategoryController;
@@ -74,6 +75,8 @@ public class AudioSharingDashboardFragment extends DashboardFragment
mAudioSharingDeviceVolumeGroupController.init(this);
mCallsAndAlarmsPreferenceController = use(CallsAndAlarmsPreferenceController.class);
mCallsAndAlarmsPreferenceController.init(this);
mAudioSharingPlaySoundPreferenceController =
use(AudioSharingPlaySoundPreferenceController.class);
mAudioSharingNamePreferenceController = use(AudioSharingNamePreferenceController.class);
mAudioStreamsCategoryController = use(AudioStreamsCategoryController.class);
}
@@ -100,6 +103,7 @@ public class AudioSharingDashboardFragment extends DashboardFragment
private void updateVisibilityForAttachedPreferences() {
mAudioSharingDeviceVolumeGroupController.updateVisibility();
mCallsAndAlarmsPreferenceController.updateVisibility();
mAudioSharingPlaySoundPreferenceController.updateVisibility();
mAudioSharingNamePreferenceController.updateVisibility();
mAudioStreamsCategoryController.updateVisibility();
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
public class AudioSharingPlaySoundPreferenceController
extends AudioSharingBasePreferenceController {
private static final String TAG = "AudioSharingPlaySoundPreferenceController";
private static final String PREF_KEY = "audio_sharing_play_sound";
private final Ringtone mRingtone;
public AudioSharingPlaySoundPreferenceController(Context context) {
super(context, PREF_KEY);
mRingtone = RingtoneManager.getRingtone(context, getMediaVolumeUri());
if (mRingtone != null) {
mRingtone.setStreamType(AudioManager.STREAM_MUSIC);
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference.setVisible(mRingtone != null);
mPreference.setOnPreferenceClickListener(
(v) -> {
if (mRingtone == null) {
Log.d(TAG, "Skip onClick due to ringtone is null");
return true;
}
try {
mRingtone.setAudioAttributes(
new AudioAttributes.Builder(mRingtone.getAudioAttributes())
.setFlags(AudioAttributes.FLAG_BYPASS_MUTE)
.addTag("VX_AOSP_SAMPLESOUND")
.build());
if (!mRingtone.isPlaying()) {
mRingtone.play();
}
} catch (Throwable e) {
Log.w(TAG, "Fail to play sample, error = " + e);
}
return true;
});
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
super.onStop(owner);
if (mRingtone != null && mRingtone.isPlaying()) {
mRingtone.stop();
}
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
}
private Uri getMediaVolumeUri() {
return Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://"
+ mContext.getPackageName()
+ "/"
+ R.raw.media_volume);
}
}