android.os.Parcelable.Creator Java Examples

The following examples show how to use android.os.Parcelable.Creator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ByteStringToParcelableConverter.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link ByteString} into a {@link Parcelable}.
 *
 * @param byteString the {@link ByteString} encoding of the {@link Parcelable}
 * @return an instance of {@link Parcelable}
 */
@Override
public Parcelable convert(@NonNull ByteString byteString) {
  Parcel parcel = Parcel.obtain();
  byte[] bytes = byteString.toByteArray();
  Parcelable fromParcel = null;
  try {
    parcel.unmarshall(bytes, 0, bytes.length);
    // Move the current read/write position to the beginning
    parcel.setDataPosition(0);
    Field creatorField = parcelableClass.getField("CREATOR");
    Parcelable.Creator<?> creator = (Creator) creatorField.get(null);
    fromParcel = parcelableClass.cast(creator.createFromParcel(parcel));
  } catch (NoSuchFieldException nsfe) {
    throw new RemoteProtocolException(
        String.format(
            Locale.ROOT,
            "Cannot find CREATOR field for Parcelable %s",
            parcelableClass.getName()),
        nsfe);
  } catch (IllegalAccessException iae) {
    throw new RemoteProtocolException(
        String.format(
            Locale.ROOT,
            "Cannot create instance of %s. CREATOR field is inaccessible",
            parcelableClass.getName()),
        iae);
  } finally {
    if (parcel != null) {
      parcel.recycle();
    }
  }
  return fromParcel;
}
 
Example #2
Source File: Postman.java    From postman with MIT License 5 votes vote down vote up
private static <T> Parcelable.Creator<T> newCreator(final Class<T> clazz) {
    return new Parcelable.Creator<T>() {
        @Override
        public T createFromParcel(Parcel source) {
            return readFromParcel(clazz, source);
        }

        @Override
        public T[] newArray(int size) {
            return Postman.newArray(clazz, size);
        }
    };
}
 
Example #3
Source File: ParcelableCompat.java    From letv with Apache License 2.0 4 votes vote down vote up
public static <T> Creator<T> newCreator(ParcelableCompatCreatorCallbacks<T> callbacks) {
    if (VERSION.SDK_INT >= 13) {
        return ParcelableCompatCreatorHoneycombMR2Stub.instantiate(callbacks);
    }
    return new CompatCreator(callbacks);
}
 
Example #4
Source File: ParcelableSubject.java    From android-test with Apache License 2.0 4 votes vote down vote up
public void recreatesEqual(Creator<T> creator) {
  T recreated = forceParcel(actual, creator);
  check("recreatesEqual()").that(actual).isEqualTo(recreated);
}
 
Example #5
Source File: Parcelables.java    From android-test with Apache License 2.0 3 votes vote down vote up
/**
 * Parcelables are lazily marshalled, meaning that in typical testing, no marshalling would occur
 * and would therefore go untested. This forces marshalling to happen for a Parcelable.
 *
 * <p>This utility will marshall the provided Parcelable, and attempt to recreate it with the
 * given CREATOR. It is up to the caller to validate the two instances are equivalent.
 *
 * @param parcelable the parcelable to marshall.
 * @param creator the CREATOR field for that parcelable.
 * @return a new instance of the parcelable that has been unmarshalled.
 */
public static <T extends Parcelable> T forceParcel(T parcelable, Creator<T> creator) {
  Parcel parcel = Parcel.obtain();
  try {
    parcelable.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    return creator.createFromParcel(parcel);
  } finally {
    parcel.recycle();
  }
}
 
Example #6
Source File: Postman.java    From postman with MIT License 3 votes vote down vote up
/**
 * Get a {@link Creator} that can be used for the {@code CREATOR} field of a {@link
 * Parcelable}.
 *
 * @param clazz The class associated with the type the the Creator will create.
 * @param <T> The type the Creator will create.
 *
 * @return A fully implemented Creator for the specified type.
 *
 * @throws PostmanException if there is no {@link Parceler} associated with the given type.
 */
public static <T> Parcelable.Creator<T> getCreator(Class<T> clazz) throws PostmanException {
    @SuppressWarnings("unchecked")
    Parcelable.Creator<T> creator = (Parcelable.Creator<T>) creatorMap.get(clazz);
    if (creator == null) {
        creator = newCreator(clazz);
        creatorMap.put(clazz, creator);
    }
    return creator;
}