Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 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; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 20 | import android.util.Log; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 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; |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 29 | import java.util.Arrays; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 30 | import java.util.List; |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 31 | import java.util.Optional; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Utility class for working with json update configurations. |
| 35 | */ |
| 36 | public final class UpdateConfigs { |
| 37 | |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 38 | public static final String UPDATE_CONFIGS_ROOT = "configs/"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 39 | |
| 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 Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 53 | return Paths |
| 54 | .get(context.getFilesDir().toString(), UPDATE_CONFIGS_ROOT) |
| 55 | .toString(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 59 | * @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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 75 | Log.e("UpdateConfigs", "Can't read/parse config file " + f.getName(), e); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 76 | throw new RuntimeException( |
| 77 | "Can't read/parse config file " + f.getName(), e); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return configs; |
| 82 | } |
| 83 | |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 84 | /** |
| 85 | * @param filename searches by given filename |
Zhomart Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 86 | * @param config searches in {@link UpdateConfig#getAbConfig()} |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 87 | * @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 Mukhamejanov | 88712f7 | 2018-08-21 12:19:02 -0700 | [diff] [blame] | 94 | .stream(config.getAbConfig().getPropertyFiles()) |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 95 | .filter(file -> filename.equals(file.getFilename())) |
| 96 | .findFirst(); |
| 97 | } |
| 98 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 99 | private UpdateConfigs() {} |
| 100 | } |