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; |
| 28 | |
| 29 | /** |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 30 | * 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] | 31 | * be sent from server to the update app, but in this sample app it will be stored on the device. |
| 32 | */ |
| 33 | public class UpdateConfig implements Parcelable { |
| 34 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 35 | public static final int AB_INSTALL_TYPE_NON_STREAMING = 0; |
| 36 | public static final int AB_INSTALL_TYPE_STREAMING = 1; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 37 | |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 58 | 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 Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 68 | } |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 69 | if (c.mAbInstallType == AB_INSTALL_TYPE_STREAMING) { |
| 70 | JSONObject meta = o.getJSONObject("ab_streaming_metadata"); |
| 71 | JSONArray propertyFilesJson = meta.getJSONArray("property_files"); |
| 72 | InnerFile[] propertyFiles = |
| 73 | new InnerFile[propertyFilesJson.length()]; |
| 74 | for (int i = 0; i < propertyFilesJson.length(); i++) { |
| 75 | JSONObject p = propertyFilesJson.getJSONObject(i); |
| 76 | propertyFiles[i] = new InnerFile( |
| 77 | p.getString("filename"), |
| 78 | p.getLong("offset"), |
| 79 | p.getLong("size")); |
| 80 | } |
| 81 | c.mAbStreamingMetadata = new StreamingMetadata(propertyFiles); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 82 | } |
| 83 | c.mRawJson = json; |
| 84 | return c; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * these strings are represent types in JSON config files |
| 89 | */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 90 | private static final String AB_INSTALL_TYPE_NON_STREAMING_JSON = "NON_STREAMING"; |
| 91 | private static final String AB_INSTALL_TYPE_STREAMING_JSON = "STREAMING"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 92 | |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 100 | private int mAbInstallType; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 101 | |
| 102 | /** metadata is required only for streaming update */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 103 | private StreamingMetadata mAbStreamingMetadata; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 104 | |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 113 | this.mAbInstallType = in.readInt(); |
| 114 | this.mAbStreamingMetadata = (StreamingMetadata) in.readSerializable(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 115 | this.mRawJson = in.readString(); |
| 116 | } |
| 117 | |
| 118 | public UpdateConfig(String name, String url, int installType) { |
| 119 | this.mName = name; |
| 120 | this.mUrl = url; |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 121 | this.mAbInstallType = installType; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 122 | } |
| 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 137 | return mAbInstallType; |
| 138 | } |
| 139 | |
| 140 | public StreamingMetadata getStreamingMetadata() { |
| 141 | return mAbStreamingMetadata; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 145 | * @return File object for given url |
| 146 | */ |
| 147 | public File getUpdatePackageFile() { |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 148 | if (mAbInstallType != AB_INSTALL_TYPE_NON_STREAMING) { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 149 | 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 Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 166 | dest.writeInt(mAbInstallType); |
| 167 | dest.writeSerializable(mAbStreamingMetadata); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 168 | dest.writeString(mRawJson); |
| 169 | } |
| 170 | |
| 171 | /** |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 172 | * Metadata for streaming A/B update. |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 173 | */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 174 | public static class StreamingMetadata implements Serializable { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 175 | |
| 176 | private static final long serialVersionUID = 31042L; |
| 177 | |
| 178 | /** defines beginning of update data in archive */ |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 179 | private InnerFile[] mPropertyFiles; |
| 180 | |
| 181 | public StreamingMetadata() { |
| 182 | mPropertyFiles = new InnerFile[0]; |
| 183 | } |
| 184 | |
| 185 | public StreamingMetadata(InnerFile[] propertyFiles) { |
| 186 | this.mPropertyFiles = propertyFiles; |
| 187 | } |
| 188 | |
| 189 | public InnerFile[] getPropertyFiles() { |
| 190 | return mPropertyFiles; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Description of a file in an OTA package zip file. |
| 196 | */ |
| 197 | public static class InnerFile implements Serializable { |
| 198 | |
| 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 Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 205 | private long mOffset; |
| 206 | |
| 207 | /** size of the update data in archive */ |
| 208 | private long mSize; |
| 209 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 210 | public InnerFile(String filename, long offset, long size) { |
| 211 | this.mFilename = filename; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 212 | this.mOffset = offset; |
| 213 | this.mSize = size; |
| 214 | } |
| 215 | |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 216 | public String getFilename() { |
| 217 | return mFilename; |
| 218 | } |
| 219 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 220 | public long getOffset() { |
| 221 | return mOffset; |
| 222 | } |
| 223 | |
| 224 | public long getSize() { |
| 225 | return mSize; |
| 226 | } |
Zhomart Mukhamejanov | 963e3ee | 2018-04-26 21:07:05 -0700 | [diff] [blame] | 227 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | } |