• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.io.File;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.stream.Collectors;
25 
26 /**
27  * A host lib that can read all aconfig proto file paths on a given device.
28  * This lib is only available on device with root access (userdebug/eng).
29  */
30 public class HostDeviceProtos {
31     /**
32      * An interface that executes ADB command and return the result.
33      */
34     public static interface AdbCommandExecutor {
35         /** Executes the ADB command. */
executeAdbCommand(String command)36         String executeAdbCommand(String command);
37     }
38 
39     static final String[] PATHS = {
40         TEMPLATE
41     };
42 
43     static final String[] MAINLINE_PATHS = {
44         MAINLINE_T
45     };
46 
47     private static final String APEX_DIR = "/apex";
48     private static final String RECURSIVELY_LIST_APEX_DIR_COMMAND =
49         "shell su 0 find /apex | grep aconfig_flags";
50     private static final String APEX_ACONFIG_PATH_SUFFIX = "/etc/aconfig_flags.pb";
51 
52 
53     /**
54      * Returns the list of all on-device aconfig proto paths from host side.
55      */
parsedFlagsProtoPaths(AdbCommandExecutor adbCommandExecutor)56     public static List<String> parsedFlagsProtoPaths(AdbCommandExecutor adbCommandExecutor) {
57         ArrayList<String> paths = new ArrayList(Arrays.asList(PATHS));
58 
59         String adbCommandOutput = adbCommandExecutor.executeAdbCommand(
60             RECURSIVELY_LIST_APEX_DIR_COMMAND);
61 
62         if (adbCommandOutput == null || adbCommandOutput.isEmpty()) {
63             paths.addAll(Arrays.asList(MAINLINE_PATHS));
64             return paths;
65         }
66 
67         Set<String> allFiles = new HashSet<>(Arrays.asList(adbCommandOutput.split("\n")));
68 
69         Set<String> subdirs = allFiles.stream().map(file -> {
70             String[] filePaths = file.split("/");
71             // The first element is "", the second element is "apex".
72             return filePaths.length > 2 ? filePaths[2] : "";
73         }).collect(Collectors.toSet());
74 
75         for (String prefix : subdirs) {
76             // For each mainline modules, there are two directories, one <modulepackage>/,
77             // and one <modulepackage>@<versioncode>/. Just read the former.
78             if (prefix.contains("@")) {
79                 continue;
80             }
81 
82             String protoPath = APEX_DIR + "/" + prefix + APEX_ACONFIG_PATH_SUFFIX;
83             if (allFiles.contains(protoPath)) {
84                 paths.add(protoPath);
85             }
86         }
87         return paths;
88     }
89 }
90