Refactor Audio Capabilities

This commit is contained in:
Cedric T 2023-05-10 16:18:07 +08:00 committed by Tianyi Feng
parent abc46d4319
commit 753257e454
2 changed files with 58 additions and 21 deletions

View File

@ -536,6 +536,30 @@ public final class Util {
return concatenation; return concatenation;
} }
/**
* Remove duplicates from an Integer array.
*
* @param inputs The input integer array.
* @return The integer array without duplicates.
*/
@UnstableApi
@SuppressWarnings("nullness:assignment")
public static int[] nullSafeIntegerArrayDistinct(int[] inputs) {
int end = inputs.length;
for (int i = 0; i < end; i++) {
for (int j = i + 1; j < end; j++) {
if (inputs[i] == inputs[j]) {
inputs[j] = inputs[end - 1];
end--;
j--;
}
}
}
int[] newlist = new int[end];
System.arraycopy(inputs, 0, newlist, 0, end);
return newlist;
}
/** /**
* Copies the contents of {@code list} into {@code array}. * Copies the contents of {@code list} into {@code array}.
* *
@ -552,6 +576,23 @@ public final class Util {
list.toArray(array); list.toArray(array);
} }
/**
* Creates a new Integer array from a List of Integers.
*
* @param list The list of integers to convert.
* @return Created array of integers.
*/
@UnstableApi
@SuppressWarnings("nullness:assignment")
public static int[] nullSafeIntegerListToArray(List<Integer> list) {
int[] intList = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
intList[i] = list.get(i);
}
return intList;
}
/** /**
* Creates a {@link Handler} on the current {@link Looper} thread. * Creates a {@link Handler} on the current {@link Looper} thread.
* *

View File

@ -94,19 +94,6 @@ public final class AudioCapabilities {
return getCapabilities(context, intent); return getCapabilities(context, intent);
} }
private static AudioCapabilities getExternalSurroundCapabilities(@Nullable Intent intent) {
int[] supportedEncodings = EXTERNAL_SURROUND_SOUND_ENCODINGS;
if (Util.SDK_INT >= 29) {
// Check if DTS Encodings are supported via Direct Playback.
int[] dtsEncodings = {AudioFormat.ENCODING_DTS, AudioFormat.ENCODING_DTS_HD};
supportedEncodings = Util.nullSafeIntegerArrayConcatenation(supportedEncodings,
Api29.getDirectPlaybackSupportedEncodings(dtsEncodings));
}
supportedEncodings = Arrays.stream(supportedEncodings).distinct().toArray();
return new AudioCapabilities(supportedEncodings,/* defaultValue= */ DEFAULT_MAX_CHANNEL_COUNT);
}
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
/* package */ static AudioCapabilities getCapabilities(Context context, @Nullable Intent intent) { /* package */ static AudioCapabilities getCapabilities(Context context, @Nullable Intent intent) {
// If a connection to Bluetooth device is detected, we only return the minimum capabilities that // If a connection to Bluetooth device is detected, we only return the minimum capabilities that
@ -114,27 +101,36 @@ public final class AudioCapabilities {
if (Util.SDK_INT >= 23 && Api23.isBluetoothConnected(context)) { if (Util.SDK_INT >= 23 && Api23.isBluetoothConnected(context)) {
return DEFAULT_AUDIO_CAPABILITIES; return DEFAULT_AUDIO_CAPABILITIES;
} }
int[] supportedEncodings = {};
if (deviceMaySetExternalSurroundSoundGlobalSetting() if (deviceMaySetExternalSurroundSoundGlobalSetting()
&& Global.getInt(context.getContentResolver(), EXTERNAL_SURROUND_SOUND_KEY, 0) == 1) { && Global.getInt(context.getContentResolver(), EXTERNAL_SURROUND_SOUND_KEY, 0) == 1) {
return getExternalSurroundCapabilities(intent); supportedEncodings = EXTERNAL_SURROUND_SOUND_ENCODINGS;
} }
// AudioTrack.isDirectPlaybackSupported returns true for encodings that are supported for audio // AudioTrack.isDirectPlaybackSupported returns true for encodings that are supported for audio
// offload, as well as for encodings we want to list for passthrough mode. Therefore we only use // offload, as well as for encodings we want to list for passthrough mode. Therefore we only use
// it on TV and automotive devices, which generally shouldn't support audio offload for surround // it on TV and automotive devices, which generally shouldn't support audio offload for surround
// encodings. // encodings.
if (Util.SDK_INT >= 29 && (Util.isTv(context) || Util.isAutomotive(context))) { if (Util.SDK_INT >= 29 && (Util.isTv(context) || Util.isAutomotive(context))) {
return new AudioCapabilities( supportedEncodings = Util.nullSafeIntegerArrayConcatenation(supportedEncodings,
Api29.getDirectPlaybackSupportedEncodings( Api29.getDirectPlaybackSupportedEncodings(
Api29.getAllSurroundEncodingsMaybeSupported().stream().mapToInt(Integer::intValue) Util.nullSafeIntegerListToArray(Api29.getAllSurroundEncodingsMaybeSupported())));
.toArray()), DEFAULT_MAX_CHANNEL_COUNT);
} }
if (intent == null || intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 0) { if (intent == null || intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 0) {
return DEFAULT_AUDIO_CAPABILITIES; if (supportedEncodings.length == 0) {
return DEFAULT_AUDIO_CAPABILITIES;
} else {
return new AudioCapabilities(supportedEncodings, /* defaultValue= */
DEFAULT_MAX_CHANNEL_COUNT);
}
} }
supportedEncodings = Util.nullSafeIntegerArrayConcatenation(supportedEncodings,
intent.getIntArrayExtra(AudioManager.EXTRA_ENCODINGS));
supportedEncodings = Util.nullSafeIntegerArrayDistinct(supportedEncodings);
return new AudioCapabilities( return new AudioCapabilities(
intent.getIntArrayExtra(AudioManager.EXTRA_ENCODINGS), supportedEncodings, /* defaultValue= */ DEFAULT_MAX_CHANNEL_COUNT);
intent.getIntExtra(
AudioManager.EXTRA_MAX_CHANNEL_COUNT, /* defaultValue= */ DEFAULT_MAX_CHANNEL_COUNT));
} }
/** /**