Example usage for android.os Bundle keySet

List of usage examples for android.os Bundle keySet

Introduction

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

Prototype

public Set<String> keySet() 

Source Link

Document

Returns a Set containing the Strings used as keys in this Bundle.

Usage

From source file:Main.java

/**
 * Converts a {@link android.os.Bundle} object to a {@code String}.
 *
 * @param bundle The converted bundle./*w  w w  .  j ava  2s. c  o m*/
 * @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.jameswolfeoliver.pigeon.Utilities.Utils.java

public static String bundleToString(Bundle bundle) {
    StringBuilder out = new StringBuilder("Bundle[");

    if (bundle == null) {
        out.append("null");
    } else {/*ww  w  .j  a  v a  2 s  .c  o  m*/
        boolean first = true;
        for (String key : bundle.keySet()) {
            if (!first) {
                out.append(", ");
            }

            out.append(key).append('=');

            Object value = bundle.get(key);

            if (value instanceof int[]) {
                out.append(Arrays.toString((int[]) value));
            } else if (value instanceof byte[]) {
                out.append(Arrays.toString((byte[]) value));
            } else if (value instanceof boolean[]) {
                out.append(Arrays.toString((boolean[]) value));
            } else if (value instanceof short[]) {
                out.append(Arrays.toString((short[]) value));
            } else if (value instanceof long[]) {
                out.append(Arrays.toString((long[]) value));
            } else if (value instanceof float[]) {
                out.append(Arrays.toString((float[]) value));
            } else if (value instanceof double[]) {
                out.append(Arrays.toString((double[]) value));
            } else if (value instanceof String[]) {
                out.append(Arrays.toString((String[]) value));
            } else if (value instanceof CharSequence[]) {
                out.append(Arrays.toString((CharSequence[]) value));
            } else if (value instanceof Parcelable[]) {
                out.append(Arrays.toString((Parcelable[]) value));
            } else if (value instanceof Bundle) {
                out.append(bundleToString((Bundle) value));
            } else {
                out.append(value);
            }

            first = false;
        }
    }

    out.append("]");
    return out.toString();
}

From source file:org.fs.ghanaian.util.JsonUtility.java

public static JSONObject parseObject(Bundle bundle) {
    JSONObject jsonObject = new JSONObject();

    if (bundle != null) {
        Set<String> keys = bundle.keySet();
        for (String key : keys) {
            Object object = bundle.get(key);

            if (object instanceof Bundle) {
                object = parseObject((Bundle) object);
            }/*from w  w  w  .java  2s. c  o m*/

            try {
                jsonObject.put(key, object);
            } catch (JSONException e) {
                if (isLogEnabled()) {
                    e.printStackTrace();
                }
            }
        }
    }

    return jsonObject;
}

From source file:com.groupme.sdk.util.HttpUtils.java

public static List<NameValuePair> bundleToList(Bundle params) {
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();

    /* TODO: URL encode the params. */
    for (String key : params.keySet()) {
        pairs.add(new BasicNameValuePair(key, params.getString(key)));
    }//w w  w.j a v  a  2 s. c o  m

    return pairs;
}

From source file:se.hyperlab.tigcm.TiGCMModule.java

public static HashMap<String, Object> bundleToHashMap(Bundle source) {
    HashMap<String, Object> result = new HashMap<String, Object>();
    if (source != null) {
        for (String key : source.keySet()) {
            result.put(key, source.get(key));
        }/*from  w  w  w .j av a  2 s . c  om*/
    }

    return result;
}

From source file:com.googlecode.android_scripting.jsonrpc.JsonBuilder.java

private static JSONObject buildJsonBundle(Bundle bundle) throws JSONException {
    JSONObject result = new JSONObject();
    for (String key : bundle.keySet()) {
        result.put(key, build(bundle.get(key)));
    }//from  w w w. j ava2 s .c om
    return result;
}

From source file:org.opendatakit.common.android.utilities.AndroidUtils.java

public static JSONObject convertFromBundle(String appName, Bundle b) throws JSONException {
    JSONObject jo = new JSONObject();
    Set<String> keys = b.keySet();
    for (String key : keys) {
        Object o = b.get(key);/*from ww w .  j a v  a2  s  .  com*/
        if (o == null) {
            jo.put(key, JSONObject.NULL);
        } else if (o.getClass().isArray()) {
            JSONArray ja = new JSONArray();
            Class<?> t = o.getClass().getComponentType();
            if (t.equals(long.class)) {
                long[] a = (long[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put(a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(int.class)) {
                int[] a = (int[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put(a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(double.class)) {
                double[] a = (double[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put(a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(boolean.class)) {
                boolean[] a = (boolean[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put(a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(Long.class)) {
                Long[] a = (Long[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put((a[i] == null) ? JSONObject.NULL : a[i]);
                }
            } else if (t.equals(Integer.class)) {
                Integer[] a = (Integer[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put((a[i] == null) ? JSONObject.NULL : a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(Double.class)) {
                Double[] a = (Double[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put((a[i] == null) ? JSONObject.NULL : a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(Boolean.class)) {
                Boolean[] a = (Boolean[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put((a[i] == null) ? JSONObject.NULL : a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(String.class)) {
                String[] a = (String[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put((a[i] == null) ? JSONObject.NULL : a[i]);
                }
                jo.put(key, ja);
            } else if (t.equals(Bundle.class) || Bundle.class.isAssignableFrom(t)) {
                Bundle[] a = (Bundle[]) o;
                for (int i = 0; i < a.length; ++i) {
                    ja.put((a[i] == null) ? JSONObject.NULL : convertFromBundle(appName, a[i]));
                }
                jo.put(key, ja);
            } else if (t.equals(byte.class)) {
                WebLogger.getLogger(appName).w(tag, "byte array returned -- ignoring");
            } else {
                throw new JSONException("unrecognized class");
            }
        } else if (o instanceof Bundle) {
            jo.put(key, convertFromBundle(appName, (Bundle) o));
        } else if (o instanceof String) {
            jo.put(key, b.getString(key));
        } else if (o instanceof Boolean) {
            jo.put(key, b.getBoolean(key));
        } else if (o instanceof Integer) {
            jo.put(key, b.getInt(key));
        } else if (o instanceof Long) {
            jo.put(key, b.getLong(key));
        } else if (o instanceof Double) {
            jo.put(key, b.getDouble(key));
        }
    }
    return jo;
}

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 www .j  a  v a 2  s . c  om*/
 */
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.airbop.library.simple.CommonUtilities.java

static void displayMessageFromIntent(Context context, Intent intent) {
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Set<String> keys = bundle.keySet();
            if (keys != null) {
                for (String key : keys) {
                    Object o = bundle.get(key);
                    if (o != null) {
                        displayMessage(context, "Key: " + key + " value: " + o);
                    }/*from   ww  w .j  ava  2s  .co  m*/
                }
            }
        } else {
            displayMessage(context, "Extras are null");
        }
    } else {
        displayMessage(context, "Intent is 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()));
    }/*from   w  ww  . j a  va  2 s  .co m*/

    return formList;
}