blob: bbefcaf160b5bf3bee78706dbcbb2b7acf441642 [file] [log] [blame]
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -07001/*
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
17package com.example.android.systemupdatersample.util;
18
19import android.content.Context;
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070020import android.util.Log;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070021
22import com.example.android.systemupdatersample.UpdateConfig;
23
24import java.io.File;
25import java.nio.charset.StandardCharsets;
26import java.nio.file.Files;
27import java.nio.file.Paths;
28import java.util.ArrayList;
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070029import java.util.Arrays;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070030import java.util.List;
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070031import java.util.Optional;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070032
33/**
34 * Utility class for working with json update configurations.
35 */
36public final class UpdateConfigs {
37
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070038 public static final String UPDATE_CONFIGS_ROOT = "configs/";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070039
40 /**
41 * @param configs update configs
42 * @return list of names
43 */
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 */
52 public static String getConfigsRoot(Context context) {
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070053 return Paths
54 .get(context.getFilesDir().toString(), UPDATE_CONFIGS_ROOT)
55 .toString();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070056 }
57
58 /**
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070059 * @param context application context
60 * @return list of configs from directory {@link UpdateConfigs#getConfigsRoot}
61 */
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) {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070075 Log.e("UpdateConfigs", "Can't read/parse config file " + f.getName(), e);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070076 throw new RuntimeException(
77 "Can't read/parse config file " + f.getName(), e);
78 }
79 }
80 }
81 return configs;
82 }
83
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070084 /**
85 * @param filename searches by given filename
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -070086 * @param config searches in {@link UpdateConfig#getAbConfig()}
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070087 * @return offset and size of {@code filename} in the package zip file
88 * stored as {@link UpdateConfig.PackageFile}.
89 */
90 public static Optional<UpdateConfig.PackageFile> getPropertyFile(
91 final String filename,
92 UpdateConfig config) {
93 return Arrays
Zhomart Mukhamejanov88712f72018-08-21 12:19:02 -070094 .stream(config.getAbConfig().getPropertyFiles())
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070095 .filter(file -> filename.equals(file.getFilename()))
96 .findFirst();
97 }
98
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070099 private UpdateConfigs() {}
100}