Example usage for android.os Bundle get

List of usage examples for android.os Bundle get

Introduction

In this page you can find the example usage for android.os Bundle get.

Prototype

@Nullable
public Object get(String key) 

Source Link

Document

Returns the entry with the given key as an object.

Usage

From source file:Main.java

/**
 * Converts a {@link android.os.Bundle} object to a {@code String}.
 *
 * @param bundle The converted bundle./*from ww w  .j av  a2s .c om*/
 * @return The string representation of the bundle.
 */
@NonNull
public static String toString(@Nullable final Bundle bundle) {
    if (bundle == null) {
        return "null";
    }

    if (bundle.isEmpty()) {
        return "";
    }

    final StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append('[');
    for (String key : bundle.keySet()) {
        stringBuilder.append('"').append(key).append("\":\"").append(bundle.get(key)).append('"')
                .append(ITEM_DIVIDER);
    }
    stringBuilder.setLength(stringBuilder.length() - ITEM_DIVIDER.length());
    stringBuilder.append(']');
    return stringBuilder.toString();
}

From source file:com.facebook.TestUtils.java

public static void assertEqualContents(final Bundle a, final Bundle b) {
    for (String key : a.keySet()) {
        if (!b.containsKey(key)) {
            Assert.fail("bundle does not include key " + key);
        }/*from  w w  w.ja v  a2 s  .  c  o m*/
        Assert.assertEquals(a.get(key), b.get(key));
    }
    for (String key : b.keySet()) {
        if (!a.containsKey(key)) {
            Assert.fail("bundle does not include key " + key);
        }
    }
}

From source file:ee.ioc.phon.android.speak.Utils.java

/**
 * <p>Traverses the given bundle looking for the given key. The search also
 * looks into embedded bundles and thus differs from {@code Bundle.get(String)}.
 * Returns the first found entry as an object. If the given bundle does not
 * contain the given key then returns {@code null}.</p>
 *
 * @param bundle bundle (e.g. intent extras)
 * @param key key of a bundle entry (possibly in an embedded bundle)
 * @return first matching key's value/*from  w  ww.j a  v  a  2s. c o m*/
 */
public static Object getBundleValue(Bundle bundle, String key) {
    for (String k : bundle.keySet()) {
        Object value = bundle.get(k);
        if (value instanceof Bundle) {
            Object deepValue = getBundleValue((Bundle) value, key);
            if (deepValue != null) {
                return deepValue;
            }
        } else if (key.equals(k)) {
            return value;
        }
    }
    return null;
}

From source file:com.cloudbees.gasp.loader.RESTLoader.java

private static List<BasicNameValuePair> paramsToList(Bundle params) {
    ArrayList<BasicNameValuePair> formList = new ArrayList<BasicNameValuePair>(params.size());

    for (String key : params.keySet()) {
        Object value = params.get(key);

        // We can only put Strings in a form entity, so we call the toString()
        // method to enforce. We also probably don't need to check for null here
        // but we do anyway because Bundle.get() can return null.
        if (value != null)
            formList.add(new BasicNameValuePair(key, value.toString()));
    }/* w  w  w.  j  a  v  a  2  s . c om*/

    return formList;
}

From source file:com.partypoker.poker.engagement.utils.EngagementBundleToJSON.java

/**
 * Recursive function to write a value to JSON.
 * @param json the JSON serializer./*from   w  w  w. j av a 2  s.  c  o m*/
 * @param value the value to write in JSON.
 */
private static void convert(JSONStringer json, Object value) throws JSONException {
    /* Handle null */
    if (value == null)
        json.value(null);

    /* The function is recursive if it encounters a bundle */
    else if (value instanceof Bundle) {
        /* Cast bundle */
        Bundle bundle = (Bundle) value;

        /* Open object */
        json.object();

        /* Traverse bundle */
        for (String key : bundle.keySet()) {
            /* Write key */
            json.key(key);

            /* Recursive call to write the value */
            convert(json, bundle.get(key));
        }

        /* End object */
        json.endObject();
    }

    /* Handle array, write it as a JSON array */
    else if (value.getClass().isArray()) {
        /* Open array */
        json.array();

        /* Recursive call on each value */
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++)
            convert(json, Array.get(value, i));

        /* Close array */
        json.endArray();
    }

    /* Handle ArrayList, write it as a JSON array */
    else if (value instanceof ArrayList<?>) {
        /* Open array */
        json.array();

        /* Recursive call on each value */
        ArrayList<?> arrayList = (ArrayList<?>) value;
        for (Object val : arrayList)
            convert(json, val);

        /* Close array */
        json.endArray();
    }

    /* Format throwable values with the stack trace */
    else if (value instanceof Throwable) {
        Throwable t = (Throwable) value;
        StringWriter text = new StringWriter();
        t.printStackTrace(new PrintWriter(text));
        json.value(text.toString());
    }

    /* Other values are handled directly by JSONStringer (numerical, boolean and String) */
    else
        json.value(value);
}

From source file:Main.java

public static String getMetaValue(Context context, String metaKey) {
    Bundle metaData = null;
    String metaValue = null;/*from w w w.j  a v a 2 s.  com*/
    if (context == null || metaKey == null) {
        return null;
    }
    try {
        ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA);
        if (null != ai) {
            metaData = ai.metaData;
        }
        if (null != metaData) {
            metaValue = String.valueOf(metaData.get(metaKey));
        }
    } catch (PackageManager.NameNotFoundException e) {

    }
    return metaValue;
}

From source file:at.wada811.utils.IntentUtils.java

public static String dump(Intent intent) throws JSONException {
    JSONObject json = new JSONObject();
    json.put("action", intent.getAction());
    if (intent.getCategories() != null) {
        JSONArray categories = new JSONArray();
        for (String category : intent.getCategories()) {
            categories.put(category);//w  ww  .j  ava 2s . co  m
        }
        json.put("category", categories);
    }
    json.put("type", intent.getType());
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        JSONObject extras = new JSONObject();
        for (String key : bundle.keySet()) {
            extras.put(key, bundle.get(key));
        }
        json.put("extras", extras);
    }
    return json.toString(4);
}

From source file:com.arellomobile.android.push.PushGCMIntentService.java

private static void generateBroadcast(Context context, Bundle extras) {
    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(context.getPackageName() + ".action.PUSH_MESSAGE_RECEIVE");
    broadcastIntent.putExtras(extras);/*w w  w.  j  a  v a  2s  .com*/

    JSONObject dataObject = new JSONObject();
    try {
        if (extras.containsKey("title")) {
            dataObject.put("title", extras.get("title"));
        }
        if (extras.containsKey("u")) {
            dataObject.put("userdata", new JSONObject(extras.getString("u")));
        }
    } catch (JSONException e) {
        // pass
    }

    broadcastIntent.putExtra(BasePushMessageReceiver.DATA_KEY, dataObject.toString());

    context.sendBroadcast(broadcastIntent, context.getPackageName() + ".permission.C2D_MESSAGE");
}

From source file:com.facebook.android.library.Util.java

/**
 * Generate the multi-part post body providing the parameters and boundary
 * string/* www .  ja va  2 s .c om*/
 * 
 * @param parameters
 *            the parameters need to be posted
 * @param boundary
 *            the random string as boundary
 * @return a string of the post body
 */
public static String encodePostBody(Bundle parameters, String boundary) {
    if (parameters == null)
        return "";
    StringBuilder sb = new StringBuilder();

    for (String key : parameters.keySet()) {
        if (parameters.get(key) instanceof byte[]) {
            continue;
        }

        sb.append("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n" + parameters.getString(key));
        sb.append("\r\n" + "--" + boundary + "\r\n");
    }

    return sb.toString();
}

From source file:com.facebook.android.Util.java

/**
 * Generate the multi-part post body providing the parameters and boundary
 * string/*from www.ja v a2 s . c o  m*/
 * 
 * @param parameters the parameters need to be posted
 * @param boundary the random string as boundary
 * @return a string of the post body
 */
@Deprecated
public static String encodePostBody(Bundle parameters, String boundary) {
    if (parameters == null)
        return "";
    StringBuilder sb = new StringBuilder();

    for (String key : parameters.keySet()) {
        Object parameter = parameters.get(key);
        if (!(parameter instanceof String)) {
            continue;
        }

        sb.append("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n" + parameter);
        sb.append("\r\n" + "--" + boundary + "\r\n");
    }

    return sb.toString();
}