package com.coolcloud.uac.android.common.util;

import android.content.Intent;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class KVUtils {
    public static String bundle2JSON(Bundle bundle) throws JSONException {
        JSONObject jo = new JSONObject();
        for (String key : bundle.keySet()) {
            Object value = bundle.get(key);
            if (value instanceof Number) {
                put(jo, key, String.valueOf(value));
            } else {
                put(jo, key, bundle.getString(key));
            }
        }
        return jo.toString();
    }

    public static Bundle JSON2Bundle(String json) throws JSONException {
        Bundle bundle = new Bundle();
        JSONObject jo = new JSONObject(json);
        JSONArray ja = jo.names();
        for (int i = 0; i < ja.length(); i++) {
            String key = ja.getString(i);
            put(bundle, key, (String) jo.get(key));
        }
        return bundle;
    }

    public static JSONObject put(JSONObject jo, Bundle bundle, String key) throws JSONException {
        if (!(jo == null || bundle == null)) {
            put(jo, key, get(bundle, key));
        }
        return jo;
    }

    public static JSONObject put(JSONObject jo, String key, String value) throws JSONException {
        if (!(jo == null || TextUtils.empty(key) || TextUtils.empty(value))) {
            jo.put(key, value);
        }
        return jo;
    }

    public static String get(JSONObject jo, String key) {
        if (jo != null) {
            try {
                if (!TextUtils.empty(key) && jo.has(key)) {
                    return jo.getString(key);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String get(Bundle bundle, String key) {
        if (bundle == null || TextUtils.empty(key)) {
            return null;
        }
        return bundle.getString(key);
    }

    public static String get(Bundle bundle, String key, String defaultValue) {
        if (bundle == null || TextUtils.empty(key) || !bundle.containsKey(key)) {
            return defaultValue;
        }
        return bundle.getString(key);
    }

    public static boolean get(Bundle bundle, String key, boolean defaultValue) {
        if (bundle == null || TextUtils.empty(key) || !bundle.containsKey(key)) {
            return defaultValue;
        }
        return bundle.getBoolean(key);
    }

    public static int getInt(Bundle bundle, String key, int defaultValue) {
        if (bundle == null || TextU