Example usage for android.os Parcel obtain

List of usage examples for android.os Parcel obtain

Introduction

In this page you can find the example usage for android.os Parcel obtain.

Prototype

public static Parcel obtain() 

Source Link

Document

Retrieve a new Parcel object from the pool.

Usage

From source file:Main.java

public static Bundle fromBase64(String serialized) {
    Bundle bundle = null;// w  ww. jav  a 2  s .c  o  m
    if (serialized != null) {
        Parcel parcel = Parcel.obtain();
        try {
            byte[] data = Base64.decode(serialized, 0);
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            bundle = parcel.readBundle();
        } finally {
            parcel.recycle();
        }
    }
    return bundle;
}

From source file:Main.java

public static byte[] toByteArray(Intent intent) {
    Parcel obtain = Parcel.obtain();
    intent.writeToParcel(obtain, 0);/*from  www. j a  va 2  s  . c o m*/
    byte[] byteArray = obtain.marshall();
    obtain.recycle();
    return byteArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static List<Parcelable> readParcelableList(Context context, String fileName, ClassLoader classLoader) {
    List<Parcelable> results = null;
    FileInputStream fis = null;/*from ww w. jav a 2 s .  c om*/
    ByteArrayOutputStream bos = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byte[] data = bos.toByteArray();

            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            results = parcel.readArrayList(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        results = null;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return results;
}

From source file:Main.java

/**
 * Returns a cached instance if such is available or a new one is created.
 * The returned instance is initialized from the given <code>event</code>.
 *
 * @param event The other event.//from  ww w.j  av a 2 s .  com
 * @return An instance.
 */
public static AccessibilityEvent obtain(AccessibilityEvent event) {
    final Parcel parcel = Parcel.obtain();

    // Write the event to the parcel and reset the data pointer.
    event.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);

    final AccessibilityEvent clone = AccessibilityEvent.CREATOR.createFromParcel(parcel);

    // Return the parcel to the global pool.
    parcel.recycle();

    return clone;
}

From source file:Main.java

public static byte[] toByteArray(Spanned spanned) {
    Parcel obtain = Parcel.obtain();
    TextUtils.writeToParcel(spanned, obtain, 0);
    byte[] byteArray = obtain.marshall();
    obtain.recycle();//from w  w  w . j a  v  a2 s.c  om
    return byteArray;
}

From source file:hochschuledarmstadt.photostream_tools.Fakes.java

public static Photo buildFakePhoto(int id, String filePath, String desc, boolean liked, boolean deleteable,
        int commentCount) {
    Parcel source = Parcel.obtain();
    source.writeInt(id);/*from ww w .  j  a v a 2 s. com*/
    source.writeString(filePath);
    source.writeString(desc);
    source.writeInt(liked ? 1 : 0);
    source.writeInt(deleteable ? 1 : 0);
    source.writeInt(commentCount);
    source.setDataPosition(0);
    Photo photo = Photo.CREATOR.createFromParcel(source);
    source.recycle();
    return photo;
}

From source file:Main.java

/**
 * Repairs the broken tag on HTC devices running Android 5.x
 * <p/>/*  w w w  .  j  a  va  2 s  .co  m*/
 * "It seems, the reason of this bug in TechExtras of NfcA is null. However, TechList contains MifareClassic." -bildin
 * For more information please refer to https://github.com/ikarus23/MifareClassicTool/issues/52#issuecomment-103797115
 * <p/>
 * Code source: https://github.com/ikarus23/MifareClassicTool/issues/52#issuecomment-104277445
 *
 * @param oTag The broken tag
 * @return The fixed tag
 */
public static Tag repairTag(Tag oTag) {
    if (oTag == null)
        return null;

    String[] sTechList = oTag.getTechList();

    Parcel oParcel, nParcel;

    oParcel = Parcel.obtain();
    oTag.writeToParcel(oParcel, 0);
    oParcel.setDataPosition(0);

    int len = oParcel.readInt();
    byte[] id = null;
    if (len >= 0) {
        id = new byte[len];
        oParcel.readByteArray(id);
    }
    int[] oTechList = new int[oParcel.readInt()];
    oParcel.readIntArray(oTechList);
    Bundle[] oTechExtras = oParcel.createTypedArray(Bundle.CREATOR);
    int serviceHandle = oParcel.readInt();
    int isMock = oParcel.readInt();
    IBinder tagService;
    if (isMock == 0) {
        tagService = oParcel.readStrongBinder();
    } else {
        tagService = null;
    }
    oParcel.recycle();

    int nfca_idx = -1;
    int mc_idx = -1;

    for (int idx = 0; idx < sTechList.length; idx++) {
        if (sTechList[idx].equals(NfcA.class.getName())) {
            nfca_idx = idx;
        } else if (sTechList[idx].equals(MifareClassic.class.getName())) {
            mc_idx = idx;
        }
    }

    if (nfca_idx >= 0 && mc_idx >= 0 && oTechExtras[mc_idx] == null) {
        oTechExtras[mc_idx] = oTechExtras[nfca_idx];
    } else {
        return oTag;
    }

    nParcel = Parcel.obtain();
    nParcel.writeInt(id.length);
    nParcel.writeByteArray(id);
    nParcel.writeInt(oTechList.length);
    nParcel.writeIntArray(oTechList);
    nParcel.writeTypedArray(oTechExtras, 0);
    nParcel.writeInt(serviceHandle);
    nParcel.writeInt(isMock);
    if (isMock == 0) {
        nParcel.writeStrongBinder(tagService);
    }
    nParcel.setDataPosition(0);

    Tag nTag = Tag.CREATOR.createFromParcel(nParcel);

    nParcel.recycle();

    return nTag;
}

From source file:com.scvngr.levelup.core.model.WebLinkTest.java

@SmallTest
public void testParcel_full() {
    final WebLink loyalty = WebLinkFixture.getFullModel(1);
    final Parcel parcel = Parcel.obtain();

    try {/*  ww  w .java  2  s  .  co  m*/
        loyalty.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);

        final WebLink parceled = WebLink.CREATOR.createFromParcel(parcel);
        assertEquals(loyalty, parceled);
    } finally {
        parcel.recycle();
    }
}

From source file:com.scvngr.levelup.core.model.LocationTest.java

@SmallTest
public void testParcel() throws JSONException {
    final JSONObject object = LocationFixture.getFullJsonObject();

    final Location location = new LocationJsonFactory().from(object);
    final Parcel parcel = Parcel.obtain();
    location.writeToParcel(parcel, 0);// ww  w.  j a v a 2s. co  m
    parcel.setDataPosition(0);

    final Location parceled = Location.CREATOR.createFromParcel(parcel);
    assertEquals(location, parceled);
    parcel.recycle();
}

From source file:com.scvngr.levelup.core.model.LoyaltyTest.java

@SmallTest
public void testParcel_full() {
    final Loyalty loyalty = LoyaltyFixture.getFullModel(1);
    final Parcel parcel = Parcel.obtain();

    try {//from  w  ww .ja va 2 s  . c o  m
        loyalty.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);

        final Loyalty parceled = Loyalty.CREATOR.createFromParcel(parcel);
        assertEquals(loyalty, parceled);
    } finally {
        parcel.recycle();
    }
}