Code example for Parcel

Methods: dataPosition, readBooleanArray, readByte, readInt, readLong, readString, setDataPosition, writeBooleanArray, writeByte, writeInt

0
     * @param source ContactDetail to copy from. 
     */ 
    public void copy(ContactDetail source) {
        android.os.Parcel _data = android.os.Parcel.obtain();
        source.writeToParcel(_data, 0);
        _data.setDataPosition(0);
        readFromParcel(_data);
    } 
 
    /** 
     * Default constructor for ContactDetail. 
     */ 
    public ContactDetail() { 
    } 
 
    /** 
     * Construct ContactDetail from supplied Parcel. 
     *  
     * @param in Parcel containing ContactDetails. 
     */ 
    private ContactDetail(Parcel in) {
        readFromParcel(in);
    } 
 
    /** 
     * @param rawVal 
     * @param inKey 
     * @param inType 
     */ 
    public void setValue(String rawVal, DetailKeys inKey, DetailKeyTypes inType) {
        key = inKey;
        keyType = inType;
        value = VCardHelper.makeSingleTextValue(rawVal);
    } 
 
    /** 
     * Fetches single value from a VCard entry using VCardHelper class. 
     *  
     * @return value as String. 
     */ 
    public String getValue() {
        return VCardHelper.getSingleTextValue(value);
    } 
 
    /** 
     * Set VCard name field using VCardHelper. 
     *  
     * @param name VCardHelper name item. 
     */ 
    public void setName(VCardHelper.Name name) {
        key = DetailKeys.VCARD_NAME;
        keyType = null;
        value = VCardHelper.makeName(name);
    } 
 
    /** 
     * Return VCard name field 
     *  
     * @return VCardHelper.Name containing name. 
     */ 
    public VCardHelper.Name getName() { 
        if (value != null && !value.equals(UNKNOWN_NAME)) {
            return VCardHelper.getName(value);
        } 
        return null; 
    } 
 
    /** 
     * Set VCard organisation field. 
     *  
     * @param org VCardHelper.Organisation containing representation of 
     *            organisation fields. 
     * @param type detailKeyTypes (i.e. HOME/WORK). 
     */ 
    public void setOrg(VCardHelper.Organisation org, DetailKeyTypes type) {
        key = DetailKeys.VCARD_ORG;
        keyType = type;
        value = VCardHelper.makeOrg(org);
    } 
 
    /** 
     * Return organisation field value. 
     *  
     * @return VCardHelper.Organisation containing organisation. 
     */ 
    public VCardHelper.Organisation getOrg() { 
        return VCardHelper.getOrg(value);
    } 
 
    /** 
     * Set VCard date field. 
     *  
     * @param time Time to set. 
     * @param inType detailKeyTypes for item (i.e BIRTHDAY). 
     */ 
    public void setDate(Time time, DetailKeyTypes inType) {
        key = DetailKeys.VCARD_DATE;
        keyType = inType;
        value = VCardHelper.makeDate(time);
    } 
 
    /** 
     * Get date value. 
     *  
     * @return Time containing date value. 
     */ 
    public Time getDate() {
        return VCardHelper.getDate(value);
    } 
 
    /** 
     * Set email address 
     *  
     * @param emailAddress String containing email address 
     * @param inType detailKeyTypes vale specifying address type. 
     */ 
    public void setEmail(String emailAddress, DetailKeyTypes inType) {
        key = DetailKeys.VCARD_EMAIL;
        keyType = inType;
        value = VCardHelper.makeEmail(emailAddress);
    } 
 
    /* 
     * Get email address value with assistance of VCardHelper. 
     * @return String containing email address. 
     */ 
    public String getEmail() {
        return VCardHelper.getEmail(value);
    } 
 
    /** 
     * Set postal address. 
     *  
     * @param address VCardHelper.PostalAddress containing postal address 
     *            fields. 
     * @param inType detailKeyTypes specifying address type. 
     */ 
    public void setPostalAddress(VCardHelper.PostalAddress address, DetailKeyTypes inType) {
        key = DetailKeys.VCARD_ADDRESS;
        keyType = inType;
        value = VCardHelper.makePostalAddress(address);
    } 
 
    /** 
     * Get postal address 
     *  
     * @return postal address placed in VCardHelper.PostalAddress. 
     */ 
    public VCardHelper.PostalAddress getPostalAddress() { 
        return VCardHelper.getPostalAddress(value);
    } 
 
    /** 
     * Set telephone number. 
     *  
     * @param tel String containing telephone number. 
     * @param inType detailKeyTypes identifying number type. 
     */ 
    public void setTel(String tel, DetailKeyTypes inType) {
        key = DetailKeys.VCARD_PHONE;
        keyType = inType;
        value = VCardHelper.makeTel(tel);
    } 
 
    /** 
     * Return telephone number as String 
     *  
     * @return String containing telephone number. 
     */ 
    public String getTel() {
        return VCardHelper.getTel(value);
    } 
 
    /** 
     * Attempt to set type based on supplied data. If type can not be determined 
     * the suppled data is used to populate 'alt;' field. 
     *  
     * @param typeData String containing type information. 
     */ 
    private void processTypeData(String typeData) {
        final int posIdx = typeData.indexOf(';');
        if (posIdx >= 0) {
            List<String> list = new ArrayList<String>();
            VCardHelper.getStringList(list, typeData);
            for (String type : list) {
                if (processType(type)) {
                    break; 
                } 
            } 
        } else { 
            processType(typeData);
        } 
        if (keyType == null && ((String)typeData).trim().length() > 0) {
            alt = ((String)typeData);
        } 
    } 
 
    /** 
     * Set key-type based on supplied key String. 
     *  
     * @param typeString String containing type. 
     * @return true if the type is supported, false otherwise. 
     */ 
    private boolean processType(String typeString) {
        if (typeString.equals(TYPE_PREFERRED)) {
            return false; 
        } 
        for (DetailKeyTypes type : DetailKeyTypes.values()) {
            if (typeString.equals(type.tag())) {
                keyType = type;
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Enumeration consisting of fields written to/from Parcel containing 
     * ContactDetail item. 
     */ 
    private enum MemberData { 
        LOCAL_DETAIL_ID,
        LOCAL_CONTACT_ID,
        KEY,
        KEY_TYPE,
        VALUE,
        DELETED,
        UPDATED,
        UNIQUE_ID,
        ORDER,
        LOCATION,
        ALT,
        PHOTO,
        PHOTO_MIME_TYPE,
        PHOTO_URL,
        SERVER_CONTACT_ID,
        NATIVE_CONTACT_ID,
        NATIVE_DETAIL_ID,
        NATIVE_VAL1,
        NATIVE_VAL2,
        NATIVE_VAL3;
    } 
 
    /** 
     * Read ContactDetail item from supplied Parcel. 
     *  
     * @param in PArcel containing ContactDetail. 
     */ 
    private void readFromParcel(Parcel in) {
        localDetailID = null; // Primary key in database
        localContactID = null;
        key = null;
        keyType = null;
        value = null;
        updated = null;
        unique_id = null;
        order = null;
        location = null;
        photo = null;
        photo_mime_type = null;
        photo_url = null;
        serverContactId = null;
        nativeContactId = null;
        nativeVal1 = null;
        nativeVal2 = null;
        nativeVal3 = null;
 
        boolean[] validDataList = new boolean[MemberData.values().length];
        in.readBooleanArray(validDataList);
 
        if (validDataList[MemberData.LOCAL_DETAIL_ID.ordinal()]) {
            localDetailID = in.readLong(); // Primary key in database
        } 
 
        if (validDataList[MemberData.LOCAL_CONTACT_ID.ordinal()]) {
            localContactID = in.readLong();
        } 
 
        if (validDataList[MemberData.KEY.ordinal()]) {
            key = DetailKeys.values()[in.readInt()];
        } 
 
        if (validDataList[MemberData.KEY_TYPE.ordinal()]) {
            keyType = DetailKeyTypes.values()[in.readInt()];
        } 
 
        if (validDataList[MemberData.VALUE.ordinal()]) {
            value = in.readString();
        } 
 
        if (validDataList[MemberData.DELETED.ordinal()]) {
            deleted = (in.readByte() == 0 ? false : true);
        } 
 
        if (validDataList[MemberData.UPDATED.ordinal()]) {
            updated = in.readLong();
        } 
 
        if (validDataList[MemberData.UNIQUE_ID.ordinal()]) {
            unique_id = in.readLong();
        } 
 
        if (validDataList[MemberData.ORDER.ordinal()]) {
            order = in.readInt();
        } 
 
        if (validDataList[MemberData.LOCATION.ordinal()]) {
            location = Location.CREATOR.createFromParcel(in);
        } 
 
        if (validDataList[MemberData.ALT.ordinal()]) {
            alt = in.readString();
        } 
 
        if (validDataList[MemberData.PHOTO.ordinal()]) {
            photo = Bitmap.CREATOR.createFromParcel(in);
        } 
 
        if (validDataList[MemberData.PHOTO_MIME_TYPE.ordinal()]) {
            photo_mime_type = in.readString();
        } 
 
        if (validDataList[MemberData.PHOTO_URL.ordinal()]) {
            photo_url = in.readString();
        } 
 
        if (validDataList[MemberData.SERVER_CONTACT_ID.ordinal()]) {
            serverContactId = in.readLong();
        } 
 
        if (validDataList[MemberData.NATIVE_CONTACT_ID.ordinal()]) {
            nativeContactId = in.readInt();
        } 
 
        if (validDataList[MemberData.NATIVE_DETAIL_ID.ordinal()]) {
            nativeDetailId = in.readInt();
        } 
 
        if (validDataList[MemberData.NATIVE_VAL1.ordinal()]) {
            nativeVal1 = in.readString();
        } 
 
        if (validDataList[MemberData.NATIVE_VAL2.ordinal()]) {
            nativeVal2 = in.readString();
        } 
 
        if (validDataList[MemberData.NATIVE_VAL3.ordinal()]) {
            nativeVal3 = in.readString();
        } 
    } 
 
    /** {@inheritDoc} */ 
    @Override 
    public int describeContents() { 
        return 1; 
    } 
 
    /** {@inheritDoc} */ 
    @Override 
    public void writeToParcel(Parcel dest, int flags) {
        boolean[] validDataList = new boolean[MemberData.values().length];
        int validDataPos = dest.dataPosition();
        dest.writeBooleanArray(validDataList); // placeholder for real array
 
        if (localDetailID != null) {
            validDataList[MemberData.LOCAL_DETAIL_ID.ordinal()] = true;
            dest.writeLong(localDetailID); // Primary key in database
        } 
 
        if (localContactID != null) {
            validDataList[MemberData.LOCAL_CONTACT_ID.ordinal()] = true;
            dest.writeLong(localContactID);
        } 
 
        if (key != null) {
            validDataList[MemberData.KEY.ordinal()] = true;
            dest.writeInt(key.ordinal());
        } 
 
        if (keyType != null) {
            validDataList[MemberData.KEY_TYPE.ordinal()] = true;
            dest.writeInt(keyType.ordinal());
        } 
 
        if (value != null) {
            validDataList[MemberData.VALUE.ordinal()] = true;
            dest.writeString(value);
        } 
 
        if (deleted != null) {
            validDataList[MemberData.DELETED.ordinal()] = true;
            dest.writeByte((byte)(deleted ? 1 : 0));
        } 
 
        if (updated != null) {
            validDataList[MemberData.UPDATED.ordinal()] = true;
            dest.writeLong(updated);
        } 
 
        if (unique_id != null) {
            validDataList[MemberData.UNIQUE_ID.ordinal()] = true;
            dest.writeLong(unique_id);
        } 
 
        if (order != null) {
            validDataList[MemberData.ORDER.ordinal()] = true;
            dest.writeInt(order);
        } 
 
        if (location != null) {
            validDataList[MemberData.LOCATION.ordinal()] = true;
            location.writeToParcel(dest, 0);
        } 
 
        if (alt != null) {
            validDataList[MemberData.ALT.ordinal()] = true;
            dest.writeString(alt);
        } 
 
        if (photo != null) {
            validDataList[MemberData.PHOTO.ordinal()] = true;
            photo.writeToParcel(dest, 0);
        } 
 
        if (photo_mime_type != null) {
            validDataList[MemberData.PHOTO_MIME_TYPE.ordinal()] = true;
            dest.writeString(photo_mime_type);
        } 
 
        if (photo_url != null) {
            validDataList[MemberData.PHOTO_URL.ordinal()] = true;
            dest.writeString(photo_url);
        } 
 
        if (serverContactId != null) {
            validDataList[MemberData.SERVER_CONTACT_ID.ordinal()] = true;
            dest.writeLong(serverContactId);
        } 
 
        if (nativeContactId != null) {
            validDataList[MemberData.NATIVE_CONTACT_ID.ordinal()] = true;
            dest.writeInt(nativeContactId);
        } 
 
        if (nativeDetailId != null) {
            validDataList[MemberData.NATIVE_DETAIL_ID.ordinal()] = true;
            dest.writeInt(nativeDetailId);
        } 
 
        if (nativeVal1 != null) {
            validDataList[MemberData.NATIVE_VAL1.ordinal()] = true;
            dest.writeString(nativeVal1);
        } 
 
        if (nativeVal2 != null) {
            validDataList[MemberData.NATIVE_VAL2.ordinal()] = true;
            dest.writeString(nativeVal2);
        } 
 
        if (nativeVal3 != null) {
            validDataList[MemberData.NATIVE_VAL3.ordinal()] = true;
            dest.writeString(nativeVal3);
        } 
 
        int currentPos = dest.dataPosition();
        dest.setDataPosition(validDataPos);
        dest.writeBooleanArray(validDataList); // real array
        dest.setDataPosition(currentPos);
    } 
 
    /** 
     * Interface to allow ContactDetail to be written and restored from a 
     * Parcel.