aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/AndroidUtil.java
blob: 69d5cf6d7b8828c74caf9ea36cee466f2bf986ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.mobly.snippet.util;

import android.content.Intent;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class AndroidUtil {
    private AndroidUtil() {}

    public static void putExtrasFromJsonObject(JSONObject extras, Intent intent)
            throws JSONException {
        JSONArray names = extras.names();
        for (int i = 0; i < names.length(); i++) {
            String name = names.getString(i);
            Object data = extras.get(name);
            if (data == null) {
                continue;
            }
            if (data instanceof Integer) {
                intent.putExtra(name, (Integer) data);
            }
            if (data instanceof Float) {
                intent.putExtra(name, (Float) data);
            }
            if (data instanceof Double) {
                intent.putExtra(name, (Double) data);
            }
            if (data instanceof Long) {
                intent.putExtra(name, (Long) data);
            }
            if (data instanceof String) {
                intent.putExtra(name, (String) data);
            }
            if (data instanceof Boolean) {
                intent.putExtra(name, (Boolean) data);
            }
            // Nested JSONObject
            if (data instanceof JSONObject) {
                Bundle nestedBundle = new Bundle();
                intent.putExtra(name, nestedBundle);
                putNestedJSONObject((JSONObject) data, nestedBundle);
            }
            // Nested JSONArray. Doesn't support mixed types in single array
            if (data instanceof JSONArray) {
                // Empty array. No way to tell what type of data to pass on, so skipping
                if (((JSONArray) data).length() == 0) {
                    Log.e("Empty array not supported in JSONObject, skipping");
                    continue;
                }
                // Integer
                if (((JSONArray) data).get(0) instanceof Integer) {
                    Integer[] integerArrayData = new Integer[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        integerArrayData[j] = ((JSONArray) data).getInt(j);
                    }
                    intent.putExtra(name, integerArrayData);
                }
                // Double
                if (((JSONArray) data).get(0) instanceof Double) {
                    Double[] doubleArrayData = new Double[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        doubleArrayData[j] = ((JSONArray) data).getDouble(j);
                    }
                    intent.putExtra(name, doubleArrayData);
                }
                // Long
                if (((JSONArray) data).get(0) instanceof Long) {
                    Long[] longArrayData = new Long[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        longArrayData[j] = ((JSONArray) data).getLong(j);
                    }
                    intent.putExtra(name, longArrayData);
                }
                // String
                if (((JSONArray) data).get(0) instanceof String) {
                    String[] stringArrayData = new String[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        stringArrayData[j] = ((JSONArray) data).getString(j);
                    }
                    intent.putExtra(name, stringArrayData);
                }
                // Boolean
                if (((JSONArray) data).get(0) instanceof Boolean) {
                    Boolean[] booleanArrayData = new Boolean[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        booleanArrayData[j] = ((JSONArray) data).getBoolean(j);
                    }
                    intent.putExtra(name, booleanArrayData);
                }
            }
        }
    }

    // Contributed by Emmanuel T
    // Nested Array handling contributed by Sergey Zelenev
    private static void putNestedJSONObject(JSONObject jsonObject, Bundle bundle)
            throws JSONException {
        JSONArray names = jsonObject.names();
        for (int i = 0; i < names.length(); i++) {
            String name = names.getString(i);
            Object data = jsonObject.get(name);
            if (data == null) {
                continue;
            }
            if (data instanceof Integer) {
                bundle.putInt(name, ((Integer) data).intValue());
            }
            if (data instanceof Float) {
                bundle.putFloat(name, ((Float) data).floatValue());
            }
            if (data instanceof Double) {
                bundle.putDouble(name, ((Double) data).doubleValue());
            }
            if (data instanceof Long) {
                bundle.putLong(name, ((Long) data).longValue());
            }
            if (data instanceof String) {
                bundle.putString(name, (String) data);
            }
            if (data instanceof Boolean) {
                bundle.putBoolean(name, ((Boolean) data).booleanValue());
            }
            // Nested JSONObject
            if (data instanceof JSONObject) {
                Bundle nestedBundle = new Bundle();
                bundle.putBundle(name, nestedBundle);
                putNestedJSONObject((JSONObject) data, nestedBundle);
            }
            // Nested JSONArray. Doesn't support mixed types in single array
            if (data instanceof JSONArray) {
                // Empty array. No way to tell what type of data to pass on, so skipping
                if (((JSONArray) data).length() == 0) {
                    Log.e("Empty array not supported in nested JSONObject, skipping");
                    continue;
                }
                // Integer
                if (((JSONArray) data).get(0) instanceof Integer) {
                    int[] integerArrayData = new int[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        integerArrayData[j] = ((JSONArray) data).getInt(j);
                    }
                    bundle.putIntArray(name, integerArrayData);
                }
                // Double
                if (((JSONArray) data).get(0) instanceof Double) {
                    double[] doubleArrayData = new double[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        doubleArrayData[j] = ((JSONArray) data).getDouble(j);
                    }
                    bundle.putDoubleArray(name, doubleArrayData);
                }
                // Long
                if (((JSONArray) data).get(0) instanceof Long) {
                    long[] longArrayData = new long[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        longArrayData[j] = ((JSONArray) data).getLong(j);
                    }
                    bundle.putLongArray(name, longArrayData);
                }
                // String
                if (((JSONArray) data).get(0) instanceof String) {
                    String[] stringArrayData = new String[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        stringArrayData[j] = ((JSONArray) data).getString(j);
                    }
                    bundle.putStringArray(name, stringArrayData);
                }
                // Boolean
                if (((JSONArray) data).get(0) instanceof Boolean) {
                    boolean[] booleanArrayData = new boolean[((JSONArray) data).length()];
                    for (int j = 0; j < ((JSONArray) data).length(); ++j) {
                        booleanArrayData[j] = ((JSONArray) data).getBoolean(j);
                    }
                    bundle.putBooleanArray(name, booleanArrayData);
                }
            }
        }
    }
}