Example usage for android.os Parcel setDataPosition

List of usage examples for android.os Parcel setDataPosition

Introduction

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

Prototype

public final void setDataPosition(int pos) 

Source Link

Document

Move the current read/write position in the parcel.

Usage

From source file:Main.java

public static Parcel unmarshall(byte[] bytes) {
    Parcel parcel = Parcel.obtain();

    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);

    return parcel;
}

From source file:org.gearvrf.weartouchpad.MessageListenerService.java

private static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);
    T result = creator.createFromParcel(parcel);
    parcel.recycle();/*www  .jav a 2  s  . co  m*/
    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Parcelable readParcelable(Context context, String fileName, ClassLoader classLoader) {
    Parcelable parcelable = null;//w  w  w.  jav  a2 s . c  o  m
    FileInputStream fis = null;
    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);
            parcelable = parcel.readParcelable(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        parcelable = 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 parcelable;
}

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;/*w  w  w.  j a  v a 2s. c  o  m*/
    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.//  w  ww  . j av a  2s.  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 Bundle fromBase64(String serialized) {
    Bundle bundle = null;/*from   www.  j a  va 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

/**
 * Repairs the broken tag on HTC devices running Android 5.x
 * <p/>/*from   w w  w  . j a  v  a 2  s  . c  om*/
 * "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.oasisfeng.android.service.notification.StatusBarNotificationCompat.java

private static UserHandle toUserHandle(final int user) {
    final Parcel parcel = Parcel.obtain();
    try {/*from  w  w w .  j av  a  2  s  . c o  m*/
        parcel.writeInt(user);
        parcel.setDataPosition(0);
        return UserHandle.readFromParcel(parcel);
    } finally {
        parcel.recycle();
    }
}

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   w  ww  . jav a  2 s .c  o m
    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:at.bitfire.davdroid.resource.LocalGroup.java

/**
 * Processes all groups with non-null {@link #COLUMN_PENDING_MEMBERS}: the pending memberships
 * are (if possible) applied, keeping cached memberships in sync.
 * @param addressBook    address book to take groups from
 * @throws ContactsStorageException on contact provider errors
 *//*  w w w . ja v  a 2 s .  c o  m*/
public static void applyPendingMemberships(LocalAddressBook addressBook) throws ContactsStorageException {
    try {
        @Cleanup
        Cursor cursor = addressBook.provider.query(addressBook.syncAdapterURI(Groups.CONTENT_URI),
                new String[] { Groups._ID, COLUMN_PENDING_MEMBERS }, COLUMN_PENDING_MEMBERS + " IS NOT NULL",
                new String[] {}, null);

        BatchOperation batch = new BatchOperation(addressBook.provider);
        while (cursor != null && cursor.moveToNext()) {
            long id = cursor.getLong(0);
            Constants.log.fine("Assigning members to group " + id);

            // delete all memberships and cached memberships for this group
            batch.enqueue(new BatchOperation.Operation(ContentProviderOperation
                    .newDelete(addressBook.syncAdapterURI(ContactsContract.Data.CONTENT_URI))
                    .withSelection(
                            "(" + GroupMembership.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID
                                    + "=?) OR (" + CachedGroupMembership.MIMETYPE + "=? AND "
                                    + CachedGroupMembership.GROUP_ID + "=?)",
                            new String[] { GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(id),
                                    CachedGroupMembership.CONTENT_ITEM_TYPE, String.valueOf(id) })
                    .withYieldAllowed(true)));

            // extract list of member UIDs
            List<String> members = new LinkedList<>();
            byte[] raw = cursor.getBlob(1);
            @Cleanup("recycle")
            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(raw, 0, raw.length);
            parcel.setDataPosition(0);
            parcel.readStringList(members);

            // insert memberships
            for (String uid : members) {
                Constants.log.fine("Assigning member: " + uid);
                try {
                    LocalContact member = addressBook.findContactByUID(uid);
                    member.addToGroup(batch, id);
                } catch (FileNotFoundException e) {
                    Constants.log.log(Level.WARNING, "Group member not found: " + uid, e);
                }
            }

            // remove pending memberships
            batch.enqueue(new BatchOperation.Operation(ContentProviderOperation
                    .newUpdate(addressBook.syncAdapterURI(ContentUris.withAppendedId(Groups.CONTENT_URI, id)))
                    .withValue(COLUMN_PENDING_MEMBERS, null).withYieldAllowed(true)));

            batch.commit();
        }
    } catch (RemoteException e) {
        throw new ContactsStorageException("Couldn't get pending memberships", e);
    }
}