1 /* 2 * Copyright (C) 2024 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 package android.aconfig; 17 18 import android.aconfig.nano.Aconfig.parsed_flag; 19 import android.aconfig.nano.Aconfig.parsed_flags; 20 21 import java.io.File; 22 import java.io.FileInputStream; 23 import java.io.IOException; 24 import java.util.ArrayList; 25 import java.util.Arrays; 26 import java.util.List; 27 28 /** 29 * @hide 30 */ 31 public class DeviceProtos { 32 public static final String[] PATHS = { 33 TEMPLATE 34 }; 35 36 private static final String APEX_DIR = "/apex"; 37 private static final String APEX_ACONFIG_PATH_SUFFIX = "/etc/aconfig_flags.pb"; 38 39 /** 40 * Returns a list of all on-device aconfig protos. 41 * 42 * May throw an exception if the protos can't be read at the call site. For 43 * example, some of the protos are in the apex/ partition, which is mounted 44 * somewhat late in the boot process. 45 * 46 * @throws IOException if we can't read one of the protos yet 47 * @return a list of all on-device aconfig protos 48 */ loadAndParseFlagProtos()49 public static List<parsed_flag> loadAndParseFlagProtos() throws IOException { 50 ArrayList<parsed_flag> result = new ArrayList(); 51 52 for (String path : parsedFlagsProtoPaths()) { 53 try (FileInputStream inputStream = new FileInputStream(path)) { 54 parsed_flags parsedFlags = parsed_flags.parseFrom(inputStream.readAllBytes()); 55 for (parsed_flag flag : parsedFlags.parsedFlag) { 56 result.add(flag); 57 } 58 } 59 } 60 61 return result; 62 } 63 64 /** 65 * Returns the list of all on-device aconfig protos paths. 66 * @hide 67 */ parsedFlagsProtoPaths()68 public static List<String> parsedFlagsProtoPaths() { 69 ArrayList<String> paths = new ArrayList(Arrays.asList(PATHS)); 70 71 File apexDirectory = new File(APEX_DIR); 72 if (!apexDirectory.isDirectory()) { 73 return paths; 74 } 75 76 File[] subdirs = apexDirectory.listFiles(); 77 if (subdirs == null) { 78 return paths; 79 } 80 81 for (File prefix : subdirs) { 82 // For each mainline modules, there are two directories, one <modulepackage>/, 83 // and one <modulepackage>@<versioncode>/. Just read the former. 84 if (prefix.getAbsolutePath().contains("@")) { 85 continue; 86 } 87 88 File protoPath = new File(prefix + APEX_ACONFIG_PATH_SUFFIX); 89 if (!protoPath.exists()) { 90 continue; 91 } 92 93 paths.add(protoPath.getAbsolutePath()); 94 } 95 return paths; 96 } 97 } 98