From 0dd5a83d0ec4c9d7b51843dd6eebd5ce92a1ad92 Mon Sep 17 00:00:00 2001 From: Zhomart Mukhamejanov Date: Mon, 23 Apr 2018 11:38:54 -0700 Subject: updater_sample: add streaming support - UpdateConfigs: add helper methods for streaming - add PrepareStreamingService intent service Test: manually and junit4 Bug: 77148143 Change-Id: I61711eb9abe051987e725fbd94f8cd029ff21dd3 --- .../systemupdatersample/util/FileDownloader.java | 13 ++++++----- .../systemupdatersample/util/UpdateConfigs.java | 26 +++++++++++++++++----- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'updater_sample/src/com/example/android/systemupdatersample/util') diff --git a/updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java b/updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java index 5c1d71117..ddd0919b8 100644 --- a/updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java +++ b/updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java @@ -38,22 +38,23 @@ public final class FileDownloader { private String mUrl; private long mOffset; private long mSize; - private File mOut; + private File mDestination; - public FileDownloader(String url, long offset, long size, File out) { + public FileDownloader(String url, long offset, long size, File destination) { this.mUrl = url; this.mOffset = offset; this.mSize = size; - this.mOut = out; + this.mDestination = destination; } /** * Downloads the file with given offset and size. + * @throws IOException when can't download the file */ public void download() throws IOException { - Log.d("FileDownloader", "downloading " + mOut.getName() + Log.d("FileDownloader", "downloading " + mDestination.getName() + " from " + mUrl - + " to " + mOut.getAbsolutePath()); + + " to " + mDestination.getAbsolutePath()); URL url = new URL(mUrl); URLConnection connection = url.openConnection(); @@ -61,7 +62,7 @@ public final class FileDownloader { // download the file try (InputStream input = connection.getInputStream()) { - try (OutputStream output = new FileOutputStream(mOut)) { + try (OutputStream output = new FileOutputStream(mDestination)) { long skipped = input.skip(mOffset); if (skipped != mOffset) { throw new IOException("Can't download file " diff --git a/updater_sample/src/com/example/android/systemupdatersample/util/UpdateConfigs.java b/updater_sample/src/com/example/android/systemupdatersample/util/UpdateConfigs.java index 71d4df8ab..5080cb6d8 100644 --- a/updater_sample/src/com/example/android/systemupdatersample/util/UpdateConfigs.java +++ b/updater_sample/src/com/example/android/systemupdatersample/util/UpdateConfigs.java @@ -26,14 +26,16 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Optional; /** * Utility class for working with json update configurations. */ public final class UpdateConfigs { - private static final String UPDATE_CONFIGS_ROOT = "configs/"; + public static final String UPDATE_CONFIGS_ROOT = "configs/"; /** * @param configs update configs @@ -48,13 +50,12 @@ public final class UpdateConfigs { * @return configs root directory */ public static String getConfigsRoot(Context context) { - return Paths.get(context.getFilesDir().toString(), - UPDATE_CONFIGS_ROOT).toString(); + return Paths + .get(context.getFilesDir().toString(), UPDATE_CONFIGS_ROOT) + .toString(); } /** - * It parses only {@code .json} files. - * * @param context application context * @return list of configs from directory {@link UpdateConfigs#getConfigsRoot} */ @@ -80,5 +81,20 @@ public final class UpdateConfigs { return configs; } + /** + * @param filename searches by given filename + * @param config searches in {@link UpdateConfig#getStreamingMetadata()} + * @return offset and size of {@code filename} in the package zip file + * stored as {@link UpdateConfig.PackageFile}. + */ + public static Optional getPropertyFile( + final String filename, + UpdateConfig config) { + return Arrays + .stream(config.getStreamingMetadata().getPropertyFiles()) + .filter(file -> filename.equals(file.getFilename())) + .findFirst(); + } + private UpdateConfigs() {} } -- cgit v1.2.3