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 = |
| 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 | } |
| 90 | c.mRawJson = json; |
| 91 | return c; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * these strings are represent types in JSON config files |
| 96 | */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 97 | private static final String AB_INSTALL_TYPE_NON_STREAMING_JSON = "NON_STREAMING"; |
| 98 | private static final String AB_INSTALL_TYPE_STREAMING_JSON = "STREAMING"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 99 | |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 107 | private int mAbInstallType; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 108 | |
| 109 | /** metadata is required only for streaming update */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 110 | private StreamingMetadata mAbStreamingMetadata; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 111 | |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 120 | this.mAbInstallType = in.readInt(); |
| 121 | this.mAbStreamingMetadata = (StreamingMetadata) in.readSerializable(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 122 | this.mRawJson = in.readString(); |
| 123 | } |
| 124 | |
| 125 | public UpdateConfig(String name, String url, int installType) { |
| 126 | this.mName = name; |
| 127 | this.mUrl = url; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 128 | this.mAbInstallType = installType; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 129 | } |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 144 | return mAbInstallType; |
| 145 | } |
| 146 | |
| 147 | public StreamingMetadata getStreamingMetadata() { |
| 148 | return mAbStreamingMetadata; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 152 | * @return File object for given url |
| 153 | */ |
| 154 | public File getUpdatePackageFile() { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 155 | if (mAbInstallType != AB_INSTALL_TYPE_NON_STREAMING) { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 156 | 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 173 | dest.writeInt(mAbInstallType); |
| 174 | dest.writeSerializable(mAbStreamingMetadata); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 175 | dest.writeString(mRawJson); |
| 176 | } |
| 177 | |
| 178 | /** |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 179 | * Metadata for streaming A/B update. |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 180 | */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 181 | public static class StreamingMetadata implements Serializable { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 182 | |
| 183 | private static final long serialVersionUID = 31042L; |
| 184 | |
| 185 | /** defines beginning of update data in archive */ |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 186 | private PackageFile[] mPropertyFiles; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 187 | |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 188 | /** 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 192 | |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 193 | public StreamingMetadata(PackageFile[] propertyFiles, String authorization) { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 194 | this.mPropertyFiles = propertyFiles; |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 195 | this.mAuthorization = authorization; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 198 | public PackageFile[] getPropertyFiles() { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 199 | return mPropertyFiles; |
| 200 | } |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 201 | |
| 202 | public Optional<String> getAuthorization() { |
Zhomart Mukhamejanov | bb8a215 | 2018-05-10 12:19:16 -0700 | [diff] [blame] | 203 | return mAuthorization == null ? Optional.empty() : Optional.of(mAuthorization); |
Zhomart Mukhamejanov | 6aa5fb0 | 2018-05-09 14:28:49 -0700 | [diff] [blame] | 204 | } |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Description of a file in an OTA package zip file. |
| 209 | */ |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 210 | public static class PackageFile implements Serializable { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 211 | |
| 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 Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 218 | private long mOffset; |
| 219 | |
| 220 | /** size of the update data in archive */ |
| 221 | private long mSize; |
| 222 | |
Zhomart Mukhamejanov | 0dd5a83 | 2018-04-23 11:38:54 -0700 | [diff] [blame] | 223 | public PackageFile(String filename, long offset, long size) { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 224 | this.mFilename = filename; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 225 | this.mOffset = offset; |
| 226 | this.mSize = size; |
| 227 | } |
| 228 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 229 | public String getFilename() { |
| 230 | return mFilename; |
| 231 | } |
| 232 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 233 | public long getOffset() { |
| 234 | return mOffset; |
| 235 | } |
| 236 | |
| 237 | public long getSize() { |
| 238 | return mSize; |
| 239 | } |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | } |