1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.car.cts.property; 18 19 import android.util.ArrayMap; 20 21 import org.json.JSONException; 22 import org.json.JSONObject; 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.util.ArrayList; 27 import java.util.Iterator; 28 import java.util.List; 29 import java.util.Map; 30 31 /** 32 * A parser for CarSvcProps.json which is the config file for car property service. 33 */ 34 public final class CarSvcPropsParser { 35 private static final String CONFIG_RESOURCE_NAME = "CarSvcProps.json"; 36 private static final String JSON_FIELD_NAME_PROPERTIES = "properties"; 37 38 private final List<Integer> mAllSystemPropertyIds = new ArrayList<>(); 39 private final Map<String, List<Integer>> mSystemPropertyIdsByFlag = new ArrayMap<>(); 40 CarSvcPropsParser()41 public CarSvcPropsParser() { 42 String configString; 43 try (InputStream configFile = this.getClass().getClassLoader() 44 .getResourceAsStream(CONFIG_RESOURCE_NAME)) { 45 try { 46 configString = new String(configFile.readAllBytes()); 47 } catch (IOException e) { 48 throw new IllegalStateException( 49 "Cannot read from config file: " + CONFIG_RESOURCE_NAME, e); 50 } 51 } catch (IOException e) { 52 throw new IllegalStateException("Failed to close config resource stream", e); 53 } 54 55 JSONObject configJsonObject; 56 try { 57 configJsonObject = new JSONObject(configString); 58 } catch (JSONException e) { 59 throw new IllegalStateException("Config file: " + CONFIG_RESOURCE_NAME 60 + " does not contain a valid JSONObject.", e); 61 } 62 try { 63 JSONObject properties = configJsonObject.getJSONObject(JSON_FIELD_NAME_PROPERTIES); 64 Iterator<String> keysIt = properties.keys(); 65 while (keysIt.hasNext()) { 66 String propertyName = keysIt.next(); 67 JSONObject propertyObj = properties.getJSONObject(propertyName); 68 if (propertyObj.optBoolean("deprecated")) { 69 continue; 70 } 71 int propertyId = propertyObj.getInt("propertyId"); 72 mAllSystemPropertyIds.add(propertyId); 73 String featureFlag = propertyObj.optString("featureFlag"); 74 if (!featureFlag.isEmpty()) { 75 if (mSystemPropertyIdsByFlag.get(featureFlag) == null) { 76 mSystemPropertyIdsByFlag.put(featureFlag, new ArrayList<>()); 77 } 78 mSystemPropertyIdsByFlag.get(featureFlag).add(propertyId); 79 } 80 } 81 } catch (JSONException e) { 82 throw new IllegalStateException("Config file: " + CONFIG_RESOURCE_NAME 83 + " has invalid JSON format.", e); 84 } 85 } 86 87 /** 88 * Gets all the defined system property IDs. 89 */ getAllSystemPropertyIds()90 public List<Integer> getAllSystemPropertyIds() { 91 return new ArrayList<>(mAllSystemPropertyIds); 92 } 93 94 /** 95 * Gets the defined system property IDs under the given flag. 96 */ getSystemPropertyIdsForFlag(String flag)97 public List<Integer> getSystemPropertyIdsForFlag(String flag) { 98 List<Integer> ids = mSystemPropertyIdsByFlag.get(flag); 99 if (ids == null) { 100 return new ArrayList<Integer>(); 101 } 102 return new ArrayList<Integer>(ids); 103 } 104 } 105