blob: 9bdd8b9e851633d11a38664770162aca9f6c1038 [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;
Zhomart Mukhamejanov6aa5fb02018-05-09 14:28:49 -070028import java.util.Optional;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070029
30/**
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070031 * An update description. It will be parsed from JSON, which is intended to
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070032 * be sent from server to the update app, but in this sample app it will be stored on the device.
33 */
34public class UpdateConfig implements Parcelable {
35
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070036 public static final int AB_INSTALL_TYPE_NON_STREAMING = 0;
37 public static final int AB_INSTALL_TYPE_STREAMING = 1;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070038
39 public static final Parcelable.Creator<UpdateConfig> CREATOR =
40 new Parcelable.Creator<UpdateConfig>() {
41 @Override
42 public UpdateConfig createFromParcel(Parcel source) {
43 return new UpdateConfig(source);
44 }
45
46 @Override
47 public UpdateConfig[] newArray(int size) {
48 return new UpdateConfig[size];
49 }
50 };
51
52 /** parse update config from json */
53 public static UpdateConfig fromJson(String json) throws JSONException {
54 UpdateConfig c = new UpdateConfig();
55
56 JSONObject o = new JSONObject(json);
57 c.mName = o.getString("name");
58 c.mUrl = o.getString("url");
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070059 switch (o.getString("ab_install_type")) {
60 case AB_INSTALL_TYPE_NON_STREAMING_JSON:
61 c.mAbInstallType = AB_INSTALL_TYPE_NON_STREAMING;
62 break;
63 case AB_INSTALL_TYPE_STREAMING_JSON:
64 c.mAbInstallType = AB_INSTALL_TYPE_STREAMING;
65 break;
66 default:
67 throw new JSONException("Invalid type, expected either "
68 + "NON_STREAMING or STREAMING, got " + o.getString("ab_install_type"));
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070069 }
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070070 if (c.mAbInstallType == AB_INSTALL_TYPE_STREAMING) {
71 JSONObject meta = o.getJSONObject("ab_streaming_metadata");
72 JSONArray propertyFilesJson = meta.getJSONArray("property_files");
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070073 PackageFile[] propertyFiles =
74 new PackageFile[propertyFilesJson.length()];
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070075 for (int i = 0; i < propertyFilesJson.length(); i++) {
76 JSONObject p = propertyFilesJson.getJSONObject(i);
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -070077 propertyFiles[i] = new PackageFile(
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070078 p.getString("filename"),
79 p.getLong("offset"),
80 p.getLong("size"));
81 }
Zhomart Mukhamejanovbb8a2152018-05-10 12:19:16 -070082 String authorization = null;
83 if (meta.has("authorization")) {
84 authorization = meta.getString("authorization");
85 }
Zhomart Mukhamejanov6aa5fb02018-05-09 14:28:49 -070086 c.mAbStreamingMetadata = new StreamingMetadata(
87 propertyFiles,
Zhomart Mukhamejanovbb8a2152018-05-10 12:19:16 -070088 authorization);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070089 }
90 c.mRawJson = json;
91 return c;
92 }
93
94 /**
95 * these strings are represent types in JSON config files
96 */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -070097 private static final String AB_INSTALL_TYPE_NON_STREAMING_JSON = "NON_STREAMING";
98 private static final String AB_INSTALL_TYPE_STREAMING_JSON = "STREAMING";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070099
100 /** name will be visible on UI */
101 private String mName;
102
103 /** update zip file URI, can be https:// or file:// */
104 private String mUrl;
105
106 /** non-streaming (first saves locally) OR streaming (on the fly) */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700107 private int mAbInstallType;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700108
109 /** metadata is required only for streaming update */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700110 private StreamingMetadata mAbStreamingMetadata;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700111
112 private String mRawJson;
113
114 protected UpdateConfig() {
115 }
116
117 protected UpdateConfig(Parcel in) {
118 this.mName = in.readString();
119 this.mUrl = in.readString();
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700120 this.mAbInstallType = in.readInt();
121 this.mAbStreamingMetadata = (StreamingMetadata) in.readSerializable();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700122 this.mRawJson = in.readString();
123 }
124
125 public UpdateConfig(String name, String url, int installType) {
126 this.mName = name;
127 this.mUrl = url;
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700128 this.mAbInstallType = installType;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700129 }
130
131 public String getName() {
132 return mName;
133 }
134
135 public String getUrl() {
136 return mUrl;
137 }
138
139 public String getRawJson() {
140 return mRawJson;
141 }
142
143 public int getInstallType() {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700144 return mAbInstallType;
145 }
146
147 public StreamingMetadata getStreamingMetadata() {
148 return mAbStreamingMetadata;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700149 }
150
151 /**
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700152 * @return File object for given url
153 */
154 public File getUpdatePackageFile() {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700155 if (mAbInstallType != AB_INSTALL_TYPE_NON_STREAMING) {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700156 throw new RuntimeException("Expected non-streaming install type");
157 }
158 if (!mUrl.startsWith("file://")) {
159 throw new RuntimeException("url is expected to start with file://");
160 }
161 return new File(mUrl.substring(7, mUrl.length()));
162 }
163
164 @Override
165 public int describeContents() {
166 return 0;
167 }
168
169 @Override
170 public void writeToParcel(Parcel dest, int flags) {
171 dest.writeString(mName);
172 dest.writeString(mUrl);
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700173 dest.writeInt(mAbInstallType);
174 dest.writeSerializable(mAbStreamingMetadata);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700175 dest.writeString(mRawJson);
176 }
177
178 /**
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700179 * Metadata for streaming A/B update.
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700180 */
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700181 public static class StreamingMetadata implements Serializable {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700182
183 private static final long serialVersionUID = 31042L;
184
185 /** defines beginning of update data in archive */
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700186 private PackageFile[] mPropertyFiles;
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700187
Zhomart Mukhamejanov6aa5fb02018-05-09 14:28:49 -0700188 /** SystemUpdaterSample receives the authorization token from the OTA server, in addition
189 * to the package URL. It passes on the info to update_engine, so that the latter can
190 * fetch the data from the package server directly with the token. */
191 private String mAuthorization;
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700192
Zhomart Mukhamejanov6aa5fb02018-05-09 14:28:49 -0700193 public StreamingMetadata(PackageFile[] propertyFiles, String authorization) {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700194 this.mPropertyFiles = propertyFiles;
Zhomart Mukhamejanov6aa5fb02018-05-09 14:28:49 -0700195 this.mAuthorization = authorization;
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700196 }
197
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700198 public PackageFile[] getPropertyFiles() {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700199 return mPropertyFiles;
200 }
Zhomart Mukhamejanov6aa5fb02018-05-09 14:28:49 -0700201
202 public Optional<String> getAuthorization() {
Zhomart Mukhamejanovbb8a2152018-05-10 12:19:16 -0700203 return mAuthorization == null ? Optional.empty() : Optional.of(mAuthorization);
Zhomart Mukhamejanov6aa5fb02018-05-09 14:28:49 -0700204 }
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700205 }
206
207 /**
208 * Description of a file in an OTA package zip file.
209 */
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700210 public static class PackageFile implements Serializable {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700211
212 private static final long serialVersionUID = 31043L;
213
214 /** filename in an archive */
215 private String mFilename;
216
217 /** defines beginning of update data in archive */
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700218 private long mOffset;
219
220 /** size of the update data in archive */
221 private long mSize;
222
Zhomart Mukhamejanov0dd5a832018-04-23 11:38:54 -0700223 public PackageFile(String filename, long offset, long size) {
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700224 this.mFilename = filename;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700225 this.mOffset = offset;
226 this.mSize = size;
227 }
228
Zhomart Mukhamejanov963e3ee2018-04-26 21:07:05 -0700229 public String getFilename() {
230 return mFilename;
231 }
232
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700233 public long getOffset() {
234 return mOffset;
235 }
236
237 public long getSize() {
238 return mSize;
239 }
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700240 }
241
242}