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.List;
22 
23 /**
24  * @hide
25  */
26 public class DevicePaths {
27     static final String[] PATHS = {
28         TEMPLATE
29     };
30 
31     private static final String APEX_DIR = "/apex";
32     private static final String APEX_ACONFIG_PATH_SUFFIX = "/etc/aconfig_flags.pb";
33 
34 
35     /**
36      * Returns the list of all on-device aconfig protos paths.
37      * @hide
38      */
parsedFlagsProtoPaths()39     public List<String> parsedFlagsProtoPaths() {
40         ArrayList<String> paths = new ArrayList(Arrays.asList(PATHS));
41 
42         File apexDirectory = new File(APEX_DIR);
43         if (!apexDirectory.isDirectory()) {
44             return paths;
45         }
46 
47         File[] subdirs = apexDirectory.listFiles();
48         if (subdirs == null) {
49             return paths;
50         }
51 
52         for (File prefix : subdirs) {
53             // For each mainline modules, there are two directories, one <modulepackage>/,
54             // and one <modulepackage>@<versioncode>/. Just read the former.
55             if (prefix.getAbsolutePath().contains("@")) {
56                 continue;
57             }
58 
59             File protoPath = new File(prefix + APEX_ACONFIG_PATH_SUFFIX);
60             if (!protoPath.exists()) {
61                 continue;
62             }
63 
64             paths.add(protoPath.getAbsolutePath());
65         }
66         return paths;
67     }
68 }
69