summaryrefslogtreecommitdiff
path: root/libraries/automotive-helpers/utility-helper/src/android/platform/helpers/AutoJsonUtility.java
blob: 306a801e3cce01b6840a4a946f7e0f15ba021f70 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * 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 android.platform.helpers;

import androidx.test.InstrumentationRegistry;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class AutoJsonUtility {
    private static final String LOG_TAG = AutoJsonUtility.class.getSimpleName();

    // Config File Path Param
    private static final String CONFIG_FILE_PATH_PARAM = "config-file-path";

    // Application Config
    public static final String APPLICATION_CONFIG = "APPLICATION_CONFIG";

    private Map<String, String> mApplicationConfigMap;

    // Config Utility Map
    private Map<String, IAutoConfigUtility> configUtilityMap;

    // Resources
    private static final String PATH_KEY = "PATH";
    private static final String OPTIONS_KEY = "OPTIONS";
    private static final String PATH_DELIMETER = ">";
    private static final String TYPE_KEY = "TYPE";
    private static final String VALUE_KEY = "VALUE";
    private static final String PACKAGE_KEY = "PACKAGE";

    private static AutoJsonUtility sAutoJsonUtilityInstance = null;

    private AutoJsonUtility() {
        initializeConfigMaps();
        buildConfigMaps();
    }

    /** Get Auto Json Utility Instance */
    public static AutoJsonUtility getInstance() {
        if (sAutoJsonUtilityInstance == null) {
            sAutoJsonUtilityInstance = new AutoJsonUtility();
        }
        return sAutoJsonUtilityInstance;
    }

    /** Get Path for given Setting */
    public String[] getSettingPath(String setting) {
        return configUtilityMap.get(AutoConfigConstants.SETTINGS).getPath(setting);
    }

    /** Get menu options in given setting */
    public String[] getSettingOptions(String setting) {
        return configUtilityMap.get(AutoConfigConstants.SETTINGS).getAvailableOptions(setting);
    }

    /** Get config value for given config */
    public String getApplicationConfig(String config) {
        return mApplicationConfigMap.get(config);
    }

    /** Get the UI resource configuration */
    public AutoConfigResource getResourceFromConfig(
            String application, String config, String resource) {
        if (configUtilityMap.get(application) == null) {
            // Unknown Application
            return null;
        }
        return configUtilityMap.get(application).getResourceConfiguration(config, resource);
    }

    /** Initialize Config Maps */
    private void initializeConfigMaps() {
        // Initialize Application Config Map
        mApplicationConfigMap = new HashMap<String, String>();

        // Initialize Config Utility Map
        configUtilityMap = new HashMap<String, IAutoConfigUtility>();

        // load config uitilities for supported applications
        loadConfigUtilitiesForSupportedApplications();
    }

    /** Load config uitilities for supported applications */
    private void loadConfigUtilitiesForSupportedApplications() {
        // Phone/Dialer
        configUtilityMap.put(AutoConfigConstants.PHONE, AutoDialConfigUtility.getInstance());

        // Settings
        configUtilityMap.put(AutoConfigConstants.SETTINGS, AutoSettingsConfigUtility.getInstance());

        // Notifications
        configUtilityMap.put(
                AutoConfigConstants.NOTIFICATIONS, AutoNotificationsConfigUtility.getInstance());

        // Media Center
        configUtilityMap.put(
                AutoConfigConstants.MEDIA_CENTER, AutoMediaCenterConfigUtility.getInstance());

        // App Grid
        configUtilityMap.put(AutoConfigConstants.APP_GRID, AutoAppGridConfigUtility.getInstance());

        // Home
        configUtilityMap.put(AutoConfigConstants.HOME, AutoHomeConfigUtility.getInstance());

        // Lock Screen
        configUtilityMap.put(
                AutoConfigConstants.LOCK_SCREEN, AutoLockScreenConfigUtility.getInstance());
    }

    /** Build Config Maps */
    private void buildConfigMaps() {
        // Build Config Maps with default values ( based on Reference Devices )
        buildDefaultConfigMaps();

        // Read Config File Path Param
        String configFilePath =
                InstrumentationRegistry.getArguments().getString(CONFIG_FILE_PATH_PARAM, null);

        // If Config File is provided update the config maps with data from the file
        if (configFilePath != null) {
            updateConfigMapsFromFile(configFilePath);
        }
    }

    /** Build Default Config Maps ( Based on Reference Device ) */
    private void buildDefaultConfigMaps() {
        for (String application : configUtilityMap.keySet()) {
            configUtilityMap.get(application).loadDefaultConfig(mApplicationConfigMap);
        }
    }

    /**
     * Reads and parses the Config JSON file and update the Config Maps Sample JSON {
     * APPLICATION_NAME_1: { APPLICATION_CONFIG: { APPLICATION_PACKAGE:
     * "android.application.package" }, APPLICATION_RESOURCES: { PATH: "APPLICATION_PATH [ e.g.
     * Settings > System ]", OPTIONS: [ "APPLICATION_SETTINGS_OPTION_1"
     * "APPLICATION_SETTINGS_OPTION_2" ], APPLICATION_RESOURCE_1: { TYPE: "RESOURCE_ID", VALUE:
     * "RESOURCE_ID_VALUE", PACKAGE: "RESOURCE_PACKAGE" }, APPLICATION_RESOURCE_2: { TYPE: "TEXT",
     * VALUE: "TEXT_VALUE" } } }, APPLICATION_NAME_2: { ... } }
     *
     * @param configFilePath Path for the config file
     */
    private void updateConfigMapsFromFile(String configFilePath) {
        // Read data from config file
        String configFileData = readConfigFile(configFilePath);
        if (configFileData == null || configFileData.isEmpty()) {
            throw new RuntimeException("Config file is empty.");
        }
        try {
            // Parse json data from config file
            // and update config maps from the parsed JSON config data
            JSONObject configFileDataJson = new JSONObject(configFileData);

            Iterator<String> applicationNames = configFileDataJson.keys();
            while (applicationNames.hasNext()) {
                String applicationName = applicationNames.next();
                if (!isApplicationSupported(applicationName)) {
                    throwUnknownObjectException(applicationName);
                }
                Object applicationConfig = configFileDataJson.get(applicationName);
                if (applicationConfig == null || !isJsonObject(applicationConfig)) {
                    throwJsonObjectException(applicationName);
                }
                parseJsonAndUpdateConfigMaps(applicationName, (JSONObject) applicationConfig);
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /** Parse JSON data and Update Config Maps with data from JSON */
    private void parseJsonAndUpdateConfigMaps(String appName, JSONObject appConfiguration)
            throws JSONException {
        Iterator<String> appConfigAndResourceKeys = appConfiguration.keys();
        while (appConfigAndResourceKeys.hasNext()) {
            String appConfigAndResourceKey = appConfigAndResourceKeys.next();
            Object appConfigAndResource = appConfiguration.get(appConfigAndResourceKey);
            if (appConfigAndResource == null || !isJsonObject(appConfigAndResource)) {
                throwJsonObjectException(appName, appConfigAndResourceKey);
            }
            if (APPLICATION_CONFIG.equals(appConfigAndResourceKey)) {
                parseJsonAndUpdateApplicationConfigMap(
                        appName, appConfigAndResourceKey, (JSONObject) appConfigAndResource);
                continue;
            }
            if (!isValidConfiguration(appName, appConfigAndResourceKey)) {
                throwUnknownObjectException(appName, appConfigAndResourceKey);
            }
            parseJsonAndUpdateApplicationResources(
                    appName, appConfigAndResourceKey, (JSONObject) appConfigAndResource);
        }
    }

    /** Parse JSON and Update Application Config Map with data from JSON */
    private void parseJsonAndUpdateApplicationConfigMap(
            String appName, String configName, JSONObject config) throws JSONException {
        Iterator<String> configKeys = config.keys();
        while (configKeys.hasNext()) {
            String configKey = configKeys.next();
            if (!isValidApplicationConfig(configKey)) {
                throwUnknownObjectException(appName, configName, configKey);
            }
            Object applicationConfig = config.get(configKey);
            if (applicationConfig == null || !isJsonString(applicationConfig)) {
                throwJsonStringException(appName, configName, configKey);
            }
            String appConfig = (String) applicationConfig;
            mApplicationConfigMap.put(configKey, appConfig);
        }
    }

    /** Parse JSON and Update Resources for given application with the data from JSON */
    private void parseJsonAndUpdateApplicationResources(
            String appName, String configName, JSONObject config) throws JSONException {
        Iterator<String> resourceNames = config.keys();
        while (resourceNames.hasNext()) {
            String resourceName = resourceNames.next();
            if (PATH_KEY.equals(resourceName)) {
                parseJsonAndUpdateConfigPath(appName, configName, config);
                continue;
            }
            if (OPTIONS_KEY.equals(resourceName)) {
                parseJsonAndUpdateConfigOptions(appName, configName, config);
                continue;
            }
            if (!isValidResourceConfig(appName, configName, resourceName)) {
                throwUnknownObjectException(appName, configName, resourceName);
            }
            Object applicationResource = config.get(resourceName);
            if (applicationResource == null || !isJsonObject(applicationResource)) {
                throwJsonObjectException(appName, configName, resourceName);
            }
            parseJsonAndUpdateResourcesInConfigMaps(
                    appName, configName, resourceName, (JSONObject) applicationResource);
        }
    }

    /** Validate if given resource exists in the configuration for the given application */
    private boolean isValidResourceConfig(String appName, String configName, String resourceName) {
        return configUtilityMap.get(appName).isValidResource(configName, resourceName);
    }

    /** Validate if given config exists for the application */
    private boolean isValidConfiguration(String appName, String configName) {
        return configUtilityMap.get(appName).isValidConfiguration(configName);
    }

    /** Parse Json and Update resources in Config Maps for given Application */
    private void parseJsonAndUpdateResourcesInConfigMaps(
            String appName, String configName, String resourceName, JSONObject config)
            throws JSONException {
        Object resourceType = config.get(TYPE_KEY);
        if (resourceType == null || !isJsonString(resourceType)) {
            throwJsonStringException(appName, configName, resourceName, TYPE_KEY);
        }
        Object resourceValue = config.get(VALUE_KEY);
        if (resourceValue == null || !isJsonString(resourceValue)) {
            throwJsonStringException(appName, configName, resourceName, VALUE_KEY);
        }
        configUtilityMap
                .get(appName)
                .getResourceConfiguration(configName, resourceName)
                .setResourceType((String) resourceType);
        configUtilityMap
                .get(appName)
                .getResourceConfiguration(configName, resourceName)
                .setResourceValue((String) resourceValue);
        if (AutoConfigConstants.RESOURCE_ID.equals((String) resourceType)) {
            if (!config.has(PACKAGE_KEY) || config.isNull(PACKAGE_KEY)) {
                throw new RuntimeException(
                        String.format(
                                "Config file is invalid. %s is required for "
                                        + "Resource %s for %s in %s application.",
                                PACKAGE_KEY, resourceName, configName, appName));
            }
            Object resourcePackage = config.get(PACKAGE_KEY);
            if (resourcePackage == null || !isJsonString(resourcePackage)) {
                throwJsonStringException(appName, configName, resourceName, PACKAGE_KEY);
            }
            configUtilityMap
                    .get(appName)
                    .getResourceConfiguration(configName, resourceName)
                    .setResourcePackage((String) resourcePackage);
        }
    }

    /** Parse JSON and update config options for the given application */
    private void parseJsonAndUpdateConfigOptions(
            String appName, String configName, JSONObject config) throws JSONException {
        if (!isValidOptionsConfig(appName, configName)) {
            throwUnknownObjectException(appName, configName, OPTIONS_KEY);
        }
        Object configOptions = config.get(OPTIONS_KEY);
        if (configOptions == null || !isJsonArray(configOptions)) {
            throwJsonArrayException(appName, configName, OPTIONS_KEY);
        }
        JSONArray options = (JSONArray) configOptions;
        String[] optionsArray = new String[options.length()];
        for (int i = 0; i < options.length(); i++) {
            Object option = options.get(i);
            if (option == null || !isJsonString(option)) {
                throwJsonStringException(appName, configName, OPTIONS_KEY, "index-" + i);
            }
            String configOption = (String) option;
            if (configOption == null || configOption.trim().isEmpty()) {
                throw new RuntimeException(
                        String.format(
                                "Config file is invalid. %s in %s for %s contains null or empty"
                                        + " string.",
                                OPTIONS_KEY, configName, appName));
            }
            optionsArray[i] = configOption.trim();
        }
        addOptionsToMap(appName, configName, optionsArray);
    }

    /** Add given options to application options map */
    private void addOptionsToMap(String appName, String configName, String[] options) {
        configUtilityMap.get(appName).addAvailableOptions(configName, options);
    }

    /** Validate if given config exist in application options map */
    private boolean isValidOptionsConfig(String appName, String configName) {
        return configUtilityMap.get(appName).isValidOption(configName);
    }

    /** Parse JSON and update path for config in the given application */
    private void parseJsonAndUpdateConfigPath(String appName, String configName, JSONObject config)
            throws JSONException {
        if (!isValidPathConfig(appName, configName)) {
            throwUnknownObjectException(appName, configName, PATH_KEY);
        }
        Object configPath = config.get(PATH_KEY);
        if (configPath == null || !isJsonString(configPath)) {
            throwJsonStringException(appName, configName, PATH_KEY);
        }
        String path = (String) configPath;
        String[] paths = path.split(PATH_DELIMETER);
        if (paths.length <= 1) {
            throw new RuntimeException(
                    String.format(
                            "Config file is invalid. %s in %s for %s should be a valid path.",
                            PATH_KEY, configName, appName));
        }
        String[] pathsArray = new String[paths.length - 1];
        for (int i = 1; i < paths.length; i++) {
            pathsArray[i - 1] = paths[i].trim();
        }
        addPathToMap(appName, configName, pathsArray);
    }

    /** Add the given path for Application Paths Map */
    private void addPathToMap(String appName, String configName, String[] paths) {
        configUtilityMap.get(appName).addPath(configName, paths);
    }

    /** Read data from config file */
    private String readConfigFile(String configFilePath) {
        String configFileData = null;
        try {
            File configFile = new File(configFilePath);
            if (configFile == null || !configFile.canRead()) {
                throw new IOException("Cannot read the Resource file - " + configFilePath);
            }
            FileReader configFileReader = new FileReader(configFile);
            BufferedReader configFileBufferedReader = new BufferedReader(configFileReader);
            StringBuilder stringBuilderObj = new StringBuilder();
            String configFileDataLine = configFileBufferedReader.readLine();
            while (configFileDataLine != null) {
                stringBuilderObj.append(configFileDataLine).append("\n");
                configFileDataLine = configFileBufferedReader.readLine();
            }
            configFileBufferedReader.close();
            configFileData = stringBuilderObj.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return configFileData;
    }

    /** Validate if given config exist in application paths map */
    private boolean isValidPathConfig(String appName, String configName) {
        return configUtilityMap.get(appName).isValidPath(configName);
    }

    /** Validate for Supported Applications */
    private boolean isApplicationSupported(String applicationName) {
        if (configUtilityMap.get(applicationName) != null) {
            return true;
        }
        return false;
    }

    /** Validate given application config */
    private boolean isValidApplicationConfig(String appConfig) {
        return (mApplicationConfigMap.get(appConfig) != null);
    }

    /** Validate for Json String */
    private boolean isJsonString(Object object) {
        return !(object instanceof JSONObject || object instanceof JSONArray);
    }

    /** Validate for Json Object */
    private boolean isJsonObject(Object object) {
        return (object instanceof JSONObject);
    }

    /** Validate for Json Array */
    private boolean isJsonArray(Object object) {
        return (object instanceof JSONArray);
    }

    /** Throw Exception for Invalid Json Array */
    private void throwJsonArrayException(String... configs) {
        String message =
                String.format(
                        "Config file is invalid. Please validate %s config. It should be a JSON"
                                + " Array.",
                        Arrays.toString(configs));
        throw new RuntimeException(message);
    }

    /** Throw Exception for Invalid Json String Object */
    private void throwJsonStringException(String... configs) {
        String message =
                String.format(
                        "Config file is invalid. Please validate %s config. It should be a String.",
                        Arrays.toString(configs));
        throw new RuntimeException(message);
    }

    /** Throw Exception for Invalid Json Object */
    private void throwJsonObjectException(String... configs) {
        String message =
                String.format(
                        "Config file is invalid. Please validate %s config. It should be a JSON"
                                + " Object.",
                        Arrays.toString(configs));
        throw new RuntimeException(message);
    }

    /** Throw Exception for Unknown Object */
    private void throwUnknownObjectException(String... configs) {
        String message =
                String.format(
                        "Config file is invalid. Unknown config %s.", Arrays.toString(configs));
        throw new RuntimeException(message);
    }
}