1 /*
2  * Copyright (C) 2018 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 com.example.android.systemupdatersample.util;
18 
19 import android.content.Context;
20 import android.util.Log;
21 
22 import com.example.android.systemupdatersample.UpdateConfig;
23 
24 import java.io.File;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Optional;
32 
33 /**
34  * Utility class for working with json update configurations.
35  */
36 public final class UpdateConfigs {
37 
38     public static final String UPDATE_CONFIGS_ROOT = "configs/";
39 
40     /**
41      * @param configs update configs
42      * @return list of names
43      */
configsToNames(List<UpdateConfig> configs)44     public static String[] configsToNames(List<UpdateConfig> configs) {
45         return configs.stream().map(UpdateConfig::getName).toArray(String[]::new);
46     }
47 
48     /**
49      * @param context app context
50      * @return configs root directory
51      */
getConfigsRoot(Context context)52     public static String getConfigsRoot(Context context) {
53         return Paths
54                 .get(context.getFilesDir().toString(), UPDATE_CONFIGS_ROOT)
55                 .toString();
56     }
57 
58     /**
59      * @param context application context
60      * @return list of configs from directory {@link UpdateConfigs#getConfigsRoot}
61      */
getUpdateConfigs(Context context)62     public static List<UpdateConfig> getUpdateConfigs(Context context) {
63         File root = new File(getConfigsRoot(context));
64         ArrayList<UpdateConfig> configs = new ArrayList<>();
65         if (!root.exists()) {
66             return configs;
67         }
68         for (final File f : root.listFiles()) {
69             if (!f.isDirectory() && f.getName().endsWith(".json")) {
70                 try {
71                     String json = new String(Files.readAllBytes(f.toPath()),
72                             StandardCharsets.UTF_8);
73                     configs.add(UpdateConfig.fromJson(json));
74                 } catch (Exception e) {
75                     Log.e("UpdateConfigs", "Can't read/parse config file " + f.getName(), e);
76                     throw new RuntimeException(
77                             "Can't read/parse config file " + f.getName(), e);
78                 }
79             }
80         }
81         return configs;
82     }
83 
84     /**
85      * @param filename searches by given filename
86      * @param config searches in {@link UpdateConfig#getAbConfig()}
87      * @return offset and size of {@code filename} in the package zip file
88      *         stored as {@link UpdateConfig.PackageFile}.
89      */
getPropertyFile( final String filename, UpdateConfig config)90     public static Optional<UpdateConfig.PackageFile> getPropertyFile(
91             final String filename,
92             UpdateConfig config) {
93         return Arrays
94                 .stream(config.getAbConfig().getPropertyFiles())
95                 .filter(file -> filename.equals(file.getFilename()))
96                 .findFirst();
97     }
98 
UpdateConfigs()99     private UpdateConfigs() {}
100 }
101