Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/com/squareup/wire/EnumAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.squareup.wire;

import java.io.IOException;

/**
* Compatibility shim. The stock light_camera APK's generated {@code ltpb} enum adapters
* (e.g. {@code ViewPreferences$AspectRatio$ProtoAdapter_AspectRatio}) extend
* {@code com.squareup.wire.EnumAdapter} and call its {@code (Class)} constructor + override
* {@code fromValue(int)}. That abstract base class is NOT present in any released
* wire-runtime 2.0.0–2.2.0 (which ship the reflection-based {@code RuntimeEnumAdapter} instead),
* so {@code download-libs.sh}'s wire-runtime-2.2.0 lacks it and the adapters fail to link at
* runtime ({@code NoClassDefFoundError} during the first capture's image-save path).
*
* This restores the small abstract base, implemented on top of 2.2.0's {@link ProtoAdapter}
* exactly like {@code RuntimeEnumAdapter}: an enum is wire-encoded as a VARINT of its value.
*/
public abstract class EnumAdapter<E extends WireEnum> extends ProtoAdapter<E> {
protected EnumAdapter(Class<E> type) {
super(FieldEncoding.VARINT, type);
}

/** Subclasses (generated {@code ProtoAdapter_*}) map a proto int back to the enum constant. */
protected abstract E fromValue(int value);

@Override public int encodedSize(E value) {
return ProtoWriter.varint32Size(value.getValue());
}

@Override public void encode(ProtoWriter writer, E value) throws IOException {
writer.writeVarint32(value.getValue());
}

@Override public E decode(ProtoReader reader) throws IOException {
int value = reader.readVarint32();
return fromValue(value);
}
}