summaryrefslogtreecommitdiffstats
path: root/updater_sample/src
diff options
context:
space:
mode:
Diffstat (limited to 'updater_sample/src')
-rw-r--r--updater_sample/src/com/example/android/systemupdatersample/UpdateConfig.java107
-rw-r--r--updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java2
-rw-r--r--updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java93
-rw-r--r--updater_sample/src/com/example/android/systemupdatersample/util/UpdateConfigs.java2
4 files changed, 173 insertions, 31 deletions
diff --git a/updater_sample/src/com/example/android/systemupdatersample/UpdateConfig.java b/updater_sample/src/com/example/android/systemupdatersample/UpdateConfig.java
index cbee18fcb..23510e426 100644
--- a/updater_sample/src/com/example/android/systemupdatersample/UpdateConfig.java
+++ b/updater_sample/src/com/example/android/systemupdatersample/UpdateConfig.java
@@ -19,6 +19,7 @@ package com.example.android.systemupdatersample;
import android.os.Parcel;
import android.os.Parcelable;
+import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -26,13 +27,13 @@ import java.io.File;
import java.io.Serializable;
/**
- * UpdateConfig describes an update. It will be parsed from JSON, which is intended to
+ * An update description. It will be parsed from JSON, which is intended to
* be sent from server to the update app, but in this sample app it will be stored on the device.
*/
public class UpdateConfig implements Parcelable {
- public static final int TYPE_NON_STREAMING = 0;
- public static final int TYPE_STREAMING = 1;
+ public static final int AB_INSTALL_TYPE_NON_STREAMING = 0;
+ public static final int AB_INSTALL_TYPE_STREAMING = 1;
public static final Parcelable.Creator<UpdateConfig> CREATOR =
new Parcelable.Creator<UpdateConfig>() {
@@ -54,18 +55,30 @@ public class UpdateConfig implements Parcelable {
JSONObject o = new JSONObject(json);
c.mName = o.getString("name");
c.mUrl = o.getString("url");
- if (TYPE_NON_STREAMING_JSON.equals(o.getString("type"))) {
- c.mInstallType = TYPE_NON_STREAMING;
- } else if (TYPE_STREAMING_JSON.equals(o.getString("type"))) {
- c.mInstallType = TYPE_STREAMING;
- } else {
- throw new JSONException("Invalid type, expected either "
- + "NON_STREAMING or STREAMING, got " + o.getString("type"));
+ switch (o.getString("ab_install_type")) {
+ case AB_INSTALL_TYPE_NON_STREAMING_JSON:
+ c.mAbInstallType = AB_INSTALL_TYPE_NON_STREAMING;
+ break;
+ case AB_INSTALL_TYPE_STREAMING_JSON:
+ c.mAbInstallType = AB_INSTALL_TYPE_STREAMING;
+ break;
+ default:
+ throw new JSONException("Invalid type, expected either "
+ + "NON_STREAMING or STREAMING, got " + o.getString("ab_install_type"));
}
- if (o.has("metadata")) {
- c.mMetadata = new Metadata(
- o.getJSONObject("metadata").getInt("offset"),
- o.getJSONObject("metadata").getInt("size"));
+ if (c.mAbInstallType == AB_INSTALL_TYPE_STREAMING) {
+ JSONObject meta = o.getJSONObject("ab_streaming_metadata");
+ JSONArray propertyFilesJson = meta.getJSONArray("property_files");
+ InnerFile[] propertyFiles =
+ new InnerFile[propertyFilesJson.length()];
+ for (int i = 0; i < propertyFilesJson.length(); i++) {
+ JSONObject p = propertyFilesJson.getJSONObject(i);
+ propertyFiles[i] = new InnerFile(
+ p.getString("filename"),
+ p.getLong("offset"),
+ p.getLong("size"));
+ }
+ c.mAbStreamingMetadata = new StreamingMetadata(propertyFiles);
}
c.mRawJson = json;
return c;
@@ -74,8 +87,8 @@ public class UpdateConfig implements Parcelable {
/**
* these strings are represent types in JSON config files
*/
- private static final String TYPE_NON_STREAMING_JSON = "NON_STREAMING";
- private static final String TYPE_STREAMING_JSON = "STREAMING";
+ private static final String AB_INSTALL_TYPE_NON_STREAMING_JSON = "NON_STREAMING";
+ private static final String AB_INSTALL_TYPE_STREAMING_JSON = "STREAMING";
/** name will be visible on UI */
private String mName;
@@ -84,10 +97,10 @@ public class UpdateConfig implements Parcelable {
private String mUrl;
/** non-streaming (first saves locally) OR streaming (on the fly) */
- private int mInstallType;
+ private int mAbInstallType;
/** metadata is required only for streaming update */
- private Metadata mMetadata;
+ private StreamingMetadata mAbStreamingMetadata;
private String mRawJson;
@@ -97,15 +110,15 @@ public class UpdateConfig implements Parcelable {
protected UpdateConfig(Parcel in) {
this.mName = in.readString();
this.mUrl = in.readString();
- this.mInstallType = in.readInt();
- this.mMetadata = (Metadata) in.readSerializable();
+ this.mAbInstallType = in.readInt();
+ this.mAbStreamingMetadata = (StreamingMetadata) in.readSerializable();
this.mRawJson = in.readString();
}
public UpdateConfig(String name, String url, int installType) {
this.mName = name;
this.mUrl = url;
- this.mInstallType = installType;
+ this.mAbInstallType = installType;
}
public String getName() {
@@ -121,16 +134,18 @@ public class UpdateConfig implements Parcelable {
}
public int getInstallType() {
- return mInstallType;
+ return mAbInstallType;
+ }
+
+ public StreamingMetadata getStreamingMetadata() {
+ return mAbStreamingMetadata;
}
/**
- * "url" must be the file located on the device.
- *
* @return File object for given url
*/
public File getUpdatePackageFile() {
- if (mInstallType != TYPE_NON_STREAMING) {
+ if (mAbInstallType != AB_INSTALL_TYPE_NON_STREAMING) {
throw new RuntimeException("Expected non-streaming install type");
}
if (!mUrl.startsWith("file://")) {
@@ -148,29 +163,60 @@ public class UpdateConfig implements Parcelable {
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeString(mUrl);
- dest.writeInt(mInstallType);
- dest.writeSerializable(mMetadata);
+ dest.writeInt(mAbInstallType);
+ dest.writeSerializable(mAbStreamingMetadata);
dest.writeString(mRawJson);
}
/**
- * Metadata for STREAMING update
+ * Metadata for streaming A/B update.
*/
- public static class Metadata implements Serializable {
+ public static class StreamingMetadata implements Serializable {
private static final long serialVersionUID = 31042L;
/** defines beginning of update data in archive */
+ private InnerFile[] mPropertyFiles;
+
+ public StreamingMetadata() {
+ mPropertyFiles = new InnerFile[0];
+ }
+
+ public StreamingMetadata(InnerFile[] propertyFiles) {
+ this.mPropertyFiles = propertyFiles;
+ }
+
+ public InnerFile[] getPropertyFiles() {
+ return mPropertyFiles;
+ }
+ }
+
+ /**
+ * Description of a file in an OTA package zip file.
+ */
+ public static class InnerFile implements Serializable {
+
+ private static final long serialVersionUID = 31043L;
+
+ /** filename in an archive */
+ private String mFilename;
+
+ /** defines beginning of update data in archive */
private long mOffset;
/** size of the update data in archive */
private long mSize;
- public Metadata(long offset, long size) {
+ public InnerFile(String filename, long offset, long size) {
+ this.mFilename = filename;
this.mOffset = offset;
this.mSize = size;
}
+ public String getFilename() {
+ return mFilename;
+ }
+
public long getOffset() {
return mOffset;
}
@@ -178,6 +224,7 @@ public class UpdateConfig implements Parcelable {
public long getSize() {
return mSize;
}
+
}
}
diff --git a/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java b/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java
index 72e1b2469..8507a9e84 100644
--- a/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java
+++ b/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java
@@ -260,7 +260,7 @@ public class MainActivity extends Activity {
* Applies the given update
*/
private void applyUpdate(UpdateConfig config) {
- if (config.getInstallType() == UpdateConfig.TYPE_NON_STREAMING) {
+ if (config.getInstallType() == UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING) {
AbNonStreamingUpdate update = new AbNonStreamingUpdate(mUpdateEngine, config);
try {
update.execute();
diff --git a/updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java b/updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java
new file mode 100644
index 000000000..806f17351
--- /dev/null
+++ b/updater_sample/src/com/example/android/systemupdatersample/util/FileDownloader.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.systemupdatersample.util;
+
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * Downloads chunk of a file from given url using {@code offset} and {@code size},
+ * and saves to a given location.
+ *
+ * In real-life application this helper class should download from HTTP Server,
+ * but in this sample app it will only download from a local file.
+ */
+public final class FileDownloader {
+
+ private String mUrl;
+ private long mOffset;
+ private long mSize;
+ private File mOut;
+
+ public FileDownloader(String url, long offset, long size, File out) {
+ this.mUrl = url;
+ this.mOffset = offset;
+ this.mSize = size;
+ this.mOut = out;
+ }
+
+ /**
+ * Downloads the file with given offset and size.
+ */
+ public void download() throws IOException {
+ Log.d("FileDownloader", "downloading " + mOut.getName()
+ + " from " + mUrl
+ + " to " + mOut.getAbsolutePath());
+
+ URL url = new URL(mUrl);
+ URLConnection connection = url.openConnection();
+ connection.connect();
+
+ // download the file
+ try (InputStream input = connection.getInputStream()) {
+ try (OutputStream output = new FileOutputStream(mOut)) {
+ long skipped = input.skip(mOffset);
+ if (skipped != mOffset) {
+ throw new IOException("Can't download file "
+ + mUrl
+ + " with given offset "
+ + mOffset);
+ }
+ byte[] data = new byte[4096];
+ long total = 0;
+ while (total < mSize) {
+ int needToRead = (int) Math.min(4096, mSize - total);
+ int count = input.read(data, 0, needToRead);
+ if (count <= 0) {
+ break;
+ }
+ output.write(data, 0, count);
+ total += count;
+ }
+ if (total != mSize) {
+ throw new IOException("Can't download file "
+ + mUrl
+ + " with given size "
+ + mSize);
+ }
+ }
+ }
+ }
+
+}
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 089f8b2f2..71d4df8ab 100644
--- a/updater_sample/src/com/example/android/systemupdatersample/util/UpdateConfigs.java
+++ b/updater_sample/src/com/example/android/systemupdatersample/util/UpdateConfigs.java
@@ -17,6 +17,7 @@
package com.example.android.systemupdatersample.util;
import android.content.Context;
+import android.util.Log;
import com.example.android.systemupdatersample.UpdateConfig;
@@ -70,6 +71,7 @@ public final class UpdateConfigs {
StandardCharsets.UTF_8);
configs.add(UpdateConfig.fromJson(json));
} catch (Exception e) {
+ Log.e("UpdateConfigs", "Can't read/parse config file " + f.getName(), e);
throw new RuntimeException(
"Can't read/parse config file " + f.getName(), e);
}