Use compact String keys for Bundleable

To save bytes in serialized format, it uses shorter keys in Bundles.

PiperOrigin-RevId: 357492840
This commit is contained in:
gyumin 2021-02-15 02:22:22 +00:00 committed by kim-vde
parent 0b63c17a7e
commit c7751344d4
2 changed files with 51 additions and 27 deletions

View File

@ -16,11 +16,14 @@
package com.google.android.exoplayer2.audio; package com.google.android.exoplayer2.audio;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.Bundleable; import com.google.android.exoplayer2.Bundleable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/** /**
* Attributes for audio playback, which configure the underlying platform {@link * Attributes for audio playback, which configure the underlying platform {@link
@ -35,11 +38,6 @@ import com.google.android.exoplayer2.util.Util;
*/ */
public final class AudioAttributes implements Bundleable { public final class AudioAttributes implements Bundleable {
private static final String FIELD_CONTENT_TYPE = "contentType";
private static final String FIELD_FLAGS = "flags";
private static final String FIELD_USAGE = "usage";
private static final String FIELD_ALLOWED_CAPTURE_POLICY = "allowedCapturePolicy";
public static final AudioAttributes DEFAULT = new Builder().build(); public static final AudioAttributes DEFAULT = new Builder().build();
/** /**
@ -166,31 +164,46 @@ public final class AudioAttributes implements Bundleable {
return result; return result;
} }
// Bundleable implementation.
@Retention(RetentionPolicy.SOURCE)
@IntDef({FIELD_CONTENT_TYPE, FIELD_FLAGS, FIELD_USAGE, FIELD_ALLOWED_CAPTURE_POLICY})
private @interface FieldNumber {}
private static final int FIELD_CONTENT_TYPE = 0;
private static final int FIELD_FLAGS = 1;
private static final int FIELD_USAGE = 2;
private static final int FIELD_ALLOWED_CAPTURE_POLICY = 3;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(FIELD_CONTENT_TYPE, contentType); bundle.putInt(keyForField(FIELD_CONTENT_TYPE), contentType);
bundle.putInt(FIELD_FLAGS, flags); bundle.putInt(keyForField(FIELD_FLAGS), flags);
bundle.putInt(FIELD_USAGE, usage); bundle.putInt(keyForField(FIELD_USAGE), usage);
bundle.putInt(FIELD_ALLOWED_CAPTURE_POLICY, allowedCapturePolicy); bundle.putInt(keyForField(FIELD_ALLOWED_CAPTURE_POLICY), allowedCapturePolicy);
return bundle; return bundle;
} }
public static final Creator<AudioAttributes> CREATOR = public static final Creator<AudioAttributes> CREATOR =
bundle -> { bundle -> {
Builder builder = new Builder(); Builder builder = new Builder();
if (bundle.containsKey(FIELD_CONTENT_TYPE)) { if (bundle.containsKey(keyForField(FIELD_CONTENT_TYPE))) {
builder.setContentType(bundle.getInt(FIELD_CONTENT_TYPE)); builder.setContentType(bundle.getInt(keyForField(FIELD_CONTENT_TYPE)));
} }
if (bundle.containsKey(FIELD_FLAGS)) { if (bundle.containsKey(keyForField(FIELD_FLAGS))) {
builder.setFlags(bundle.getInt(FIELD_FLAGS)); builder.setFlags(bundle.getInt(keyForField(FIELD_FLAGS)));
} }
if (bundle.containsKey(FIELD_USAGE)) { if (bundle.containsKey(keyForField(FIELD_USAGE))) {
builder.setUsage(bundle.getInt(FIELD_USAGE)); builder.setUsage(bundle.getInt(keyForField(FIELD_USAGE)));
} }
if (bundle.containsKey(FIELD_ALLOWED_CAPTURE_POLICY)) { if (bundle.containsKey(keyForField(FIELD_ALLOWED_CAPTURE_POLICY))) {
builder.setAllowedCapturePolicy(bundle.getInt(FIELD_ALLOWED_CAPTURE_POLICY)); builder.setAllowedCapturePolicy(bundle.getInt(keyForField(FIELD_ALLOWED_CAPTURE_POLICY)));
} }
return builder.build(); return builder.build();
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }

View File

@ -28,10 +28,6 @@ import java.lang.annotation.Target;
/** Information about the playback device. */ /** Information about the playback device. */
public final class DeviceInfo implements Bundleable { public final class DeviceInfo implements Bundleable {
private static final String FIELD_PLAYBACK_TYPE = "playbackType";
private static final String FIELD_MIN_VOLUME = "minVolume";
private static final String FIELD_MAX_VOLUME = "maxVolume";
/** Types of playback. One of {@link #PLAYBACK_TYPE_LOCAL} or {@link #PLAYBACK_TYPE_REMOTE}. */ /** Types of playback. One of {@link #PLAYBACK_TYPE_LOCAL} or {@link #PLAYBACK_TYPE_REMOTE}. */
@Documented @Documented
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@ -87,21 +83,36 @@ public final class DeviceInfo implements Bundleable {
return result; return result;
} }
// Bundleable implementation.
@Retention(RetentionPolicy.SOURCE)
@IntDef({FIELD_PLAYBACK_TYPE, FIELD_MIN_VOLUME, FIELD_MAX_VOLUME})
private @interface FieldNumber {}
private static final int FIELD_PLAYBACK_TYPE = 0;
private static final int FIELD_MIN_VOLUME = 1;
private static final int FIELD_MAX_VOLUME = 2;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt(FIELD_PLAYBACK_TYPE, playbackType); bundle.putInt(keyForField(FIELD_PLAYBACK_TYPE), playbackType);
bundle.putInt(FIELD_MIN_VOLUME, minVolume); bundle.putInt(keyForField(FIELD_MIN_VOLUME), minVolume);
bundle.putInt(FIELD_MAX_VOLUME, maxVolume); bundle.putInt(keyForField(FIELD_MAX_VOLUME), maxVolume);
return bundle; return bundle;
} }
public static final Creator<DeviceInfo> CREATOR = public static final Creator<DeviceInfo> CREATOR =
bundle -> { bundle -> {
int playbackType = int playbackType =
bundle.getInt(FIELD_PLAYBACK_TYPE, /* defaultValue= */ PLAYBACK_TYPE_LOCAL); bundle.getInt(
int minVolume = bundle.getInt(FIELD_MIN_VOLUME, /* defaultValue= */ 0); keyForField(FIELD_PLAYBACK_TYPE), /* defaultValue= */ PLAYBACK_TYPE_LOCAL);
int maxVolume = bundle.getInt(FIELD_MAX_VOLUME, /* defaultValue= */ 0); int minVolume = bundle.getInt(keyForField(FIELD_MIN_VOLUME), /* defaultValue= */ 0);
int maxVolume = bundle.getInt(keyForField(FIELD_MAX_VOLUME), /* defaultValue= */ 0);
return new DeviceInfo(playbackType, minVolume, maxVolume); return new DeviceInfo(playbackType, minVolume, maxVolume);
}; };
private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
} }