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; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 22 | import org.json.JSONArray; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 23 | import org.json.JSONException; |
| 24 | import org.json.JSONObject; |
| 25 | |
| 26 | import java.io.File; |
| 27 | import java.io.Serializable; |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 28 | import java.util.Optional; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 31 | * An update description. It will be parsed from JSON, which is intended to |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 32 | * be sent from server to the update app, but in this sample app it will be stored on the device. |
| 33 | */ |
| 34 | public class UpdateConfig implements Parcelable { |
| 35 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 36 | public static final int AB_INSTALL_TYPE_NON_STREAMING = 0; |
| 37 | public static final int AB_INSTALL_TYPE_STREAMING = 1; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 38 | |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 59 | 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 Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 69 | } |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 70 | if (c.mAbInstallType == AB_INSTALL_TYPE_STREAMING) { |
| 71 | JSONObject meta = o.getJSONObject("ab_streaming_metadata"); |
| 72 | JSONArray propertyFilesJson = meta.getJSONArray("property_files"); |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 73 | PackageFile[] propertyFiles = |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 74 | new PackageFile[propertyFilesJson.length()]; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 75 | for (int i = 0; i < propertyFilesJson.length(); i++) { |
| 76 | JSONObject p = propertyFilesJson.getJSONObject(i); |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 77 | propertyFiles[i] = new PackageFile( |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 78 | p.getString("filename"), |
| 79 | p.getLong("offset"), |
| 80 | p.getLong("size")); |
| 81 | } |
Zhomart Mukhamejanov | bb8a215 | 2018-05-10 12:19:16 -0700 | [diff] [blame] | 82 | String authorization = null; |
| 83 | if (meta.has("authorization")) { |
| 84 | authorization = meta.getString("authorization"); |
| 85 | } |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 86 | c.mAbStreamingMetadata = new StreamingMetadata( |
| 87 | propertyFiles, |
Zhomart Mukhamejanov | bb8a215 | 2018-05-10 12:19:16 -0700 | [diff] [blame] | 88 | authorization); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 89 | } |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 90 | |
| 91 | // TODO: parse only for A/B updates when non-A/B is implemented |
| 92 | JSONObject ab = o.getJSONObject("ab_config"); |
| 93 | boolean forceSwitchSlot = ab.getBoolean("force_switch_slot"); |
| 94 | c.mAbConfig = new AbConfig(forceSwitchSlot); |
| 95 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 96 | c.mRawJson = json; |
| 97 | return c; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * these strings are represent types in JSON config files |
| 102 | */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 103 | private static final String AB_INSTALL_TYPE_NON_STREAMING_JSON = "NON_STREAMING"; |
| 104 | private static final String AB_INSTALL_TYPE_STREAMING_JSON = "STREAMING"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 105 | |
| 106 | /** name will be visible on UI */ |
| 107 | private String mName; |
| 108 | |
| 109 | /** update zip file URI, can be https:// or file:// */ |
| 110 | private String mUrl; |
| 111 | |
| 112 | /** non-streaming (first saves locally) OR streaming (on the fly) */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 113 | private int mAbInstallType; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 114 | |
| 115 | /** metadata is required only for streaming update */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 116 | private StreamingMetadata mAbStreamingMetadata; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 117 | |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 118 | /** A/B update configurations */ |
| 119 | private AbConfig mAbConfig; |
| 120 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 121 | private String mRawJson; |
| 122 | |
| 123 | protected UpdateConfig() { |
| 124 | } |
| 125 | |
| 126 | protected UpdateConfig(Parcel in) { |
| 127 | this.mName = in.readString(); |
| 128 | this.mUrl = in.readString(); |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 129 | this.mAbInstallType = in.readInt(); |
| 130 | this.mAbStreamingMetadata = (StreamingMetadata) in.readSerializable(); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 131 | this.mAbConfig = (AbConfig) in.readSerializable(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 132 | this.mRawJson = in.readString(); |
| 133 | } |
| 134 | |
| 135 | public UpdateConfig(String name, String url, int installType) { |
| 136 | this.mName = name; |
| 137 | this.mUrl = url; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 138 | this.mAbInstallType = installType; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | public String getName() { |
| 142 | return mName; |
| 143 | } |
| 144 | |
| 145 | public String getUrl() { |
| 146 | return mUrl; |
| 147 | } |
| 148 | |
| 149 | public String getRawJson() { |
| 150 | return mRawJson; |
| 151 | } |
| 152 | |
| 153 | public int getInstallType() { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 154 | return mAbInstallType; |
| 155 | } |
| 156 | |
| 157 | public StreamingMetadata getStreamingMetadata() { |
| 158 | return mAbStreamingMetadata; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 161 | public AbConfig getAbConfig() { |
| 162 | return mAbConfig; |
| 163 | } |
| 164 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 165 | /** |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 166 | * @return File object for given url |
| 167 | */ |
| 168 | public File getUpdatePackageFile() { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 169 | if (mAbInstallType != AB_INSTALL_TYPE_NON_STREAMING) { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 170 | throw new RuntimeException("Expected non-streaming install type"); |
| 171 | } |
| 172 | if (!mUrl.startsWith("file://")) { |
| 173 | throw new RuntimeException("url is expected to start with file://"); |
| 174 | } |
| 175 | return new File(mUrl.substring(7, mUrl.length())); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public int describeContents() { |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void writeToParcel(Parcel dest, int flags) { |
| 185 | dest.writeString(mName); |
| 186 | dest.writeString(mUrl); |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 187 | dest.writeInt(mAbInstallType); |
| 188 | dest.writeSerializable(mAbStreamingMetadata); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 189 | dest.writeSerializable(mAbConfig); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 190 | dest.writeString(mRawJson); |
| 191 | } |
| 192 | |
| 193 | /** |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 194 | * Metadata for streaming A/B update. |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 195 | */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 196 | public static class StreamingMetadata implements Serializable { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 197 | |
| 198 | private static final long serialVersionUID = 31042L; |
| 199 | |
| 200 | /** defines beginning of update data in archive */ |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 201 | private PackageFile[] mPropertyFiles; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 202 | |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 203 | /** |
| 204 | * SystemUpdaterSample receives the authorization token from the OTA server, in addition |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 205 | * to the package URL. It passes on the info to update_engine, so that the latter can |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 206 | * fetch the data from the package server directly with the token. |
| 207 | */ |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 208 | private String mAuthorization; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 209 | |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 210 | public StreamingMetadata(PackageFile[] propertyFiles, String authorization) { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 211 | this.mPropertyFiles = propertyFiles; |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 212 | this.mAuthorization = authorization; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 215 | public PackageFile[] getPropertyFiles() { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 216 | return mPropertyFiles; |
| 217 | } |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 218 | |
| 219 | public Optional<String> getAuthorization() { |
Zhomart Mukhamejanov | bb8a215 | 2018-05-10 12:19:16 -0700 | [diff] [blame] | 220 | return mAuthorization == null ? Optional.empty() : Optional.of(mAuthorization); |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 221 | } |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Description of a file in an OTA package zip file. |
| 226 | */ |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 227 | public static class PackageFile implements Serializable { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 228 | |
| 229 | private static final long serialVersionUID = 31043L; |
| 230 | |
| 231 | /** filename in an archive */ |
| 232 | private String mFilename; |
| 233 | |
| 234 | /** defines beginning of update data in archive */ |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 235 | private long mOffset; |
| 236 | |
| 237 | /** size of the update data in archive */ |
| 238 | private long mSize; |
| 239 | |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 240 | public PackageFile(String filename, long offset, long size) { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 241 | this.mFilename = filename; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 242 | this.mOffset = offset; |
| 243 | this.mSize = size; |
| 244 | } |
| 245 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 246 | public String getFilename() { |
| 247 | return mFilename; |
| 248 | } |
| 249 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 250 | public long getOffset() { |
| 251 | return mOffset; |
| 252 | } |
| 253 | |
| 254 | public long getSize() { |
| 255 | return mSize; |
| 256 | } |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 259 | /** |
| 260 | * A/B (seamless) update configurations. |
| 261 | */ |
| 262 | public static class AbConfig implements Serializable { |
| 263 | |
| 264 | private static final long serialVersionUID = 31044L; |
| 265 | |
| 266 | /** |
| 267 | * if set true device will boot to new slot, otherwise user manually |
| 268 | * switches slot on the screen. |
| 269 | */ |
| 270 | private boolean mForceSwitchSlot; |
| 271 | |
| 272 | public AbConfig(boolean forceSwitchSlot) { |
| 273 | this.mForceSwitchSlot = forceSwitchSlot; |
| 274 | } |
| 275 | |
| 276 | public boolean getForceSwitchSlot() { |
| 277 | return mForceSwitchSlot; |
| 278 | } |
| 279 | |
| 280 | } |
| 281 | |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 282 | } |