Use ImmutableSet to store discovered encodings in AudioCapabilities.java
This commit is contained in:
parent
924723d6b3
commit
53f35f46f5
@ -516,30 +516,6 @@ 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}.
|
||||||
*
|
*
|
||||||
|
@ -101,36 +101,33 @@ 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 = {};
|
ImmutableSet.Builder supportedEncodings = new ImmutableSet.Builder<>();
|
||||||
|
|
||||||
if (deviceMaySetExternalSurroundSoundGlobalSetting()
|
if (deviceMaySetExternalSurroundSoundGlobalSetting()
|
||||||
&& Global.getInt(context.getContentResolver(), EXTERNAL_SURROUND_SOUND_KEY, 0) == 1) {
|
&& Global.getInt(context.getContentResolver(), EXTERNAL_SURROUND_SOUND_KEY, 0) == 1) {
|
||||||
supportedEncodings = EXTERNAL_SURROUND_SOUND_ENCODINGS;
|
supportedEncodings.addAll(Ints.asList(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))) {
|
||||||
supportedEncodings = Ints.concat(supportedEncodings,
|
supportedEncodings.addAll(Ints.asList(Api29.getDirectPlaybackSupportedEncodings()));
|
||||||
Api29.getDirectPlaybackSupportedEncodings(Api29.getAllSurroundEncodingsMaybeSupported()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intent == null || intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 0) {
|
if (intent == null || intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 0) {
|
||||||
if (supportedEncodings.length == 0) {
|
if (supportedEncodings.build().isEmpty()) {
|
||||||
return DEFAULT_AUDIO_CAPABILITIES;
|
return DEFAULT_AUDIO_CAPABILITIES;
|
||||||
} else {
|
} else {
|
||||||
supportedEncodings = Util.nullSafeIntegerArrayDistinct(supportedEncodings);
|
return new AudioCapabilities(Ints.toArray(supportedEncodings.build()), /* defaultValue= */
|
||||||
return new AudioCapabilities(supportedEncodings, /* defaultValue= */
|
|
||||||
DEFAULT_MAX_CHANNEL_COUNT);
|
DEFAULT_MAX_CHANNEL_COUNT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
supportedEncodings = Ints.concat(supportedEncodings,
|
supportedEncodings.addAll(Ints.asList(
|
||||||
intent.getIntArrayExtra(AudioManager.EXTRA_ENCODINGS));
|
intent.getIntArrayExtra(AudioManager.EXTRA_ENCODINGS)));
|
||||||
supportedEncodings = Util.nullSafeIntegerArrayDistinct(supportedEncodings);
|
|
||||||
return new AudioCapabilities(
|
return new AudioCapabilities(
|
||||||
supportedEncodings, /* defaultValue= */ DEFAULT_MAX_CHANNEL_COUNT);
|
Ints.toArray(supportedEncodings.build()), /* defaultValue= */ DEFAULT_MAX_CHANNEL_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -365,7 +362,8 @@ public final class AudioCapabilities {
|
|||||||
private Api29() {}
|
private Api29() {}
|
||||||
|
|
||||||
@DoNotInline
|
@DoNotInline
|
||||||
public static int[] getDirectPlaybackSupportedEncodings(int[] encodings) {
|
public static int[] getDirectPlaybackSupportedEncodings() {
|
||||||
|
int[] encodings = Api29.getAllSurroundEncodingsMaybeSupported();
|
||||||
ImmutableList.Builder<Integer> supportedEncodingsListBuilder = ImmutableList.builder();
|
ImmutableList.Builder<Integer> supportedEncodingsListBuilder = ImmutableList.builder();
|
||||||
for (int encoding : encodings) {
|
for (int encoding : encodings) {
|
||||||
if (AudioTrack.isDirectPlaybackSupported(
|
if (AudioTrack.isDirectPlaybackSupported(
|
||||||
@ -408,6 +406,7 @@ public final class AudioCapabilities {
|
|||||||
/**
|
/**
|
||||||
* Returns an array list of surround encodings that maybe supported.
|
* Returns an array list of surround encodings that maybe supported.
|
||||||
*/
|
*/
|
||||||
|
@DoNotInline
|
||||||
private static int[] getAllSurroundEncodingsMaybeSupported() {
|
private static int[] getAllSurroundEncodingsMaybeSupported() {
|
||||||
ImmutableList.Builder<Integer> encodings = new ImmutableList.Builder<>();
|
ImmutableList.Builder<Integer> encodings = new ImmutableList.Builder<>();
|
||||||
for (int encoding : ALL_SURROUND_ENCODINGS_AND_MAX_CHANNELS.keySet()) {
|
for (int encoding : ALL_SURROUND_ENCODINGS_AND_MAX_CHANNELS.keySet()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user