Concatenate two Parcelable Bundles containing char[] to form one Parcelable. - Android Android OS

Android examples for Android OS:Parcel

Description

Concatenate two Parcelable Bundles containing char[] to form one Parcelable.

Demo Code

/** //from w  w w  .j  a v  a2 s  . com
 ** Copyright (C) SAS Institute, All rights reserved.
 ** General Public License: http://www.opensource.org/licenses/gpl-license.php
 **/
//package com.java2s;
import android.os.Bundle;

import android.os.Parcelable;

public class Main {
    /** key used to extract serialized Properties object from Bundle. */
    public static final String BUNDLE_PROPERTIES = "properties";

    /**
     * Concatenate two Parcelable Bundles containing char[] to form one Parcelable.<br>
     * The char[] message is stored via Bundle.putCharArray using {@link #BUNDLE_PROPERTIES} as the key for the item.<br>
     *  
     * @param one       Parcelable Bundle, containing char[] message
     * @param another    Parcelable Bundle, containing char[] message
     * @return Parcelable Bundle, including Parcelable one and another.
     * @see Bundle#putCharArray(String, char[])
     * @see Bundle#getCharArray(String)
     */
    public static Parcelable assembleParcelProps(Parcelable one,
            Parcelable another) {
        Bundle bundle = new Bundle();
        char[] characters = null;
        char[] characters1 = null;
        char[] characters2 = null;
        int length = 0;

        if (one != null) {
            characters1 = ((Bundle) one).getCharArray(BUNDLE_PROPERTIES);
            if (characters1 != null)
                length += characters1.length;
        }
        if (another != null) {
            characters2 = ((Bundle) another)
                    .getCharArray(BUNDLE_PROPERTIES);
            if (characters2 != null)
                length += characters2.length;
        }

        characters = new char[length];

        if (characters1 != null && characters2 != null) {
            System.arraycopy(characters1, 0, characters, 0,
                    characters1.length);
            System.arraycopy(characters2, 0, characters,
                    characters1.length, characters2.length);
        } else if (characters1 != null && characters2 == null) {
            System.arraycopy(characters1, 0, characters, 0,
                    characters1.length);
        } else if (characters1 == null && characters2 != null) {
            System.arraycopy(characters2, 0, characters, 0,
                    characters2.length);
        }

        bundle.putCharArray(BUNDLE_PROPERTIES, characters);
        bundle.setClassLoader(Bundle.class.getClassLoader());
        return bundle;
    }
}

Related Tutorials