blob: 1851724eded72f61b2da2ae8937dac9e7cfffcbc [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;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070022import org.json.JSONArray;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070023import org.json.JSONException;
24import org.json.JSONObject;
25
26import java.io.File;
27import java.io.Serializable;
28
29/**
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070030 * An update description. It will be parsed from JSON, which is intended to
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070031 * be sent from server to the update app, but in this sample app it will be stored on the device.
32 */
33public class UpdateConfig implements Parcelable {
34
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070035 public static final int AB_INSTALL_TYPE_NON_STREAMING = 0;
36 public static final int AB_INSTALL_TYPE_STREAMING = 1;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070037
38 public static final Parcelable.Creator<UpdateConfig> CREATOR =
39 new Parcelable.Creator<UpdateConfig>() {
40 @Override
41 public UpdateConfig createFromParcel(Parcel source) {
42 return new UpdateConfig(source);
43 }
44
45 @Override
46 public UpdateConfig[] newArray(int size) {
47 return new UpdateConfig[size];
48 }
49 };
50
51 /** parse update config from json */
52 public static UpdateConfig fromJson(String json) throws JSONException {
53 UpdateConfig c = new UpdateConfig();
54
55 JSONObject o = new JSONObject(json);
56 c.mName = o.getString("name");
57 c.mUrl = o.getString("url");
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070058 switch (o.getString("ab_install_type")) {
59 case AB_INSTALL_TYPE_NON_STREAMING_JSON:
60 c.mAbInstallType = AB_INSTALL_TYPE_NON_STREAMING;
61 break;
62 case AB_INSTALL_TYPE_STREAMING_JSON:
63 c.mAbInstallType = AB_INSTALL_TYPE_STREAMING;
64 break;
65 default:
66 throw new JSONException("Invalid type, expected either "
67 + "NON_STREAMING or STREAMING, got " + o.getString("ab_install_type"));
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070068 }
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070069 if (c.mAbInstallType == AB_INSTALL_TYPE_STREAMING) {
70 JSONObject meta = o.getJSONObject("ab_streaming_metadata");
71 JSONArray propertyFilesJson = meta.getJSONArray("property_files");
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070072 PackageFile[] propertyFiles =
73 new PackageFile[propertyFilesJson.length()];
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070074 for (int i = 0; i < propertyFilesJson.length(); i++) {
75 JSONObject p = propertyFilesJson.getJSONObject(i);
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070076 propertyFiles[i] = new PackageFile(
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070077 p.getString("filename"),
78 p.getLong("offset"),
79 p.getLong("size"));
80 }
81 c.mAbStreamingMetadata = new StreamingMetadata(propertyFiles);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070082 }
83 c.mRawJson = json;
84 return c;
85 }
86
87 /**
88 * these strings are represent types in JSON config files
89 */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070090 private static final String AB_INSTALL_TYPE_NON_STREAMING_JSON = "NON_STREAMING";
91 private static final String AB_INSTALL_TYPE_STREAMING_JSON = "STREAMING";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070092
93 /** name will be visible on UI */
94 private String mName;
95
96 /** update zip file URI, can be https:// or file:// */
97 private String mUrl;
98
99 /** non-streaming (first saves locally) OR streaming (on the fly) */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700100 private int mAbInstallType;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700101
102 /** metadata is required only for streaming update */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700103 private StreamingMetadata mAbStreamingMetadata;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700104
105 private String mRawJson;
106
107 protected UpdateConfig() {
108 }
109
110 protected UpdateConfig(Parcel in) {
111 this.mName = in.readString();
112 this.mUrl = in.readString();
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700113 this.mAbInstallType = in.readInt();
114 this.mAbStreamingMetadata = (StreamingMetadata) in.readSerializable();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700115 this.mRawJson = in.readString();
116 }
117
118 public UpdateConfig(String name, String url, int installType) {
119 this.mName = name;
120 this.mUrl = url;
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700121 this.mAbInstallType = installType;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700122 }
123
124 public String getName() {
125 return mName;
126 }
127
128 public String getUrl() {
129 return mUrl;
130 }
131
132 public String getRawJson() {
133 return mRawJson;
134 }
135
136 public int getInstallType() {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700137 return mAbInstallType;
138 }
139
140 public StreamingMetadata getStreamingMetadata() {
141 return mAbStreamingMetadata;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700142 }
143
144 /**
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700145 * @return File object for given url
146 */
147 public File getUpdatePackageFile() {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700148 if (mAbInstallType != AB_INSTALL_TYPE_NON_STREAMING) {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700149 throw new RuntimeException("Expected non-streaming install type");
150 }
151 if (!mUrl.startsWith("file://")) {
152 throw new RuntimeException("url is expected to start with file://");
153 }
154 return new File(mUrl.substring(7, mUrl.length()));
155 }
156
157 @Override
158 public int describeContents() {
159 return 0;
160 }
161
162 @Override
163 public void writeToParcel(Parcel dest, int flags) {
164 dest.writeString(mName);
165 dest.writeString(mUrl);
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700166 dest.writeInt(mAbInstallType);
167 dest.writeSerializable(mAbStreamingMetadata);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700168 dest.writeString(mRawJson);
169 }
170
171 /**
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700172 * Metadata for streaming A/B update.
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700173 */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700174 public static class StreamingMetadata implements Serializable {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700175
176 private static final long serialVersionUID = 31042L;
177
178 /** defines beginning of update data in archive */
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700179 private PackageFile[] mPropertyFiles;
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700180
181 public StreamingMetadata() {
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700182 mPropertyFiles = new PackageFile[0];
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700183 }
184
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700185 public StreamingMetadata(PackageFile[] propertyFiles) {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700186 this.mPropertyFiles = propertyFiles;
187 }
188
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700189 public PackageFile[] getPropertyFiles() {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700190 return mPropertyFiles;
191 }
192 }
193
194 /**
195 * Description of a file in an OTA package zip file.
196 */
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700197 public static class PackageFile implements Serializable {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700198
199 private static final long serialVersionUID = 31043L;
200
201 /** filename in an archive */
202 private String mFilename;
203
204 /** defines beginning of update data in archive */
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700205 private long mOffset;
206
207 /** size of the update data in archive */
208 private long mSize;
209
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700210 public PackageFile(String filename, long offset, long size) {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700211 this.mFilename = filename;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700212 this.mOffset = offset;
213 this.mSize = size;
214 }
215
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700216 public String getFilename() {
217 return mFilename;
218 }
219
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700220 public long getOffset() {
221 return mOffset;
222 }
223
224 public long getSize() {
225 return mSize;
226 }
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700227
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700228 }
229
230}