blob: cbee18fcba67369deb3b0262a4c6d222c5cb9c5b [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
22import org.json.JSONException;
23import org.json.JSONObject;
24
25import java.io.File;
26import java.io.Serializable;
27
28/**
29 * UpdateConfig describes an update. It will be parsed from JSON, which is intended to
30 * be sent from server to the update app, but in this sample app it will be stored on the device.
31 */
32public class UpdateConfig implements Parcelable {
33
34 public static final int TYPE_NON_STREAMING = 0;
35 public static final int TYPE_STREAMING = 1;
36
37 public static final Parcelable.Creator<UpdateConfig> CREATOR =
38 new Parcelable.Creator<UpdateConfig>() {
39 @Override
40 public UpdateConfig createFromParcel(Parcel source) {
41 return new UpdateConfig(source);
42 }
43
44 @Override
45 public UpdateConfig[] newArray(int size) {
46 return new UpdateConfig[size];
47 }
48 };
49
50 /** parse update config from json */
51 public static UpdateConfig fromJson(String json) throws JSONException {
52 UpdateConfig c = new UpdateConfig();
53
54 JSONObject o = new JSONObject(json);
55 c.mName = o.getString("name");
56 c.mUrl = o.getString("url");
57 if (TYPE_NON_STREAMING_JSON.equals(o.getString("type"))) {
58 c.mInstallType = TYPE_NON_STREAMING;
59 } else if (TYPE_STREAMING_JSON.equals(o.getString("type"))) {
60 c.mInstallType = TYPE_STREAMING;
61 } else {
62 throw new JSONException("Invalid type, expected either "
63 + "NON_STREAMING or STREAMING, got " + o.getString("type"));
64 }
65 if (o.has("metadata")) {
66 c.mMetadata = new Metadata(
67 o.getJSONObject("metadata").getInt("offset"),
68 o.getJSONObject("metadata").getInt("size"));
69 }
70 c.mRawJson = json;
71 return c;
72 }
73
74 /**
75 * these strings are represent types in JSON config files
76 */
77 private static final String TYPE_NON_STREAMING_JSON = "NON_STREAMING";
78 private static final String TYPE_STREAMING_JSON = "STREAMING";
79
80 /** name will be visible on UI */
81 private String mName;
82
83 /** update zip file URI, can be https:// or file:// */
84 private String mUrl;
85
86 /** non-streaming (first saves locally) OR streaming (on the fly) */
87 private int mInstallType;
88
89 /** metadata is required only for streaming update */
90 private Metadata mMetadata;
91
92 private String mRawJson;
93
94 protected UpdateConfig() {
95 }
96
97 protected UpdateConfig(Parcel in) {
98 this.mName = in.readString();
99 this.mUrl = in.readString();
100 this.mInstallType = in.readInt();
101 this.mMetadata = (Metadata) in.readSerializable();
102 this.mRawJson = in.readString();
103 }
104
105 public UpdateConfig(String name, String url, int installType) {
106 this.mName = name;
107 this.mUrl = url;
108 this.mInstallType = installType;
109 }
110
111 public String getName() {
112 return mName;
113 }
114
115 public String getUrl() {
116 return mUrl;
117 }
118
119 public String getRawJson() {
120 return mRawJson;
121 }
122
123 public int getInstallType() {
124 return mInstallType;
125 }
126
127 /**
128 * "url" must be the file located on the device.
129 *
130 * @return File object for given url
131 */
132 public File getUpdatePackageFile() {
133 if (mInstallType != TYPE_NON_STREAMING) {
134 throw new RuntimeException("Expected non-streaming install type");
135 }
136 if (!mUrl.startsWith("file://")) {
137 throw new RuntimeException("url is expected to start with file://");
138 }
139 return new File(mUrl.substring(7, mUrl.length()));
140 }
141
142 @Override
143 public int describeContents() {
144 return 0;
145 }
146
147 @Override
148 public void writeToParcel(Parcel dest, int flags) {
149 dest.writeString(mName);
150 dest.writeString(mUrl);
151 dest.writeInt(mInstallType);
152 dest.writeSerializable(mMetadata);
153 dest.writeString(mRawJson);
154 }
155
156 /**
157 * Metadata for STREAMING update
158 */
159 public static class Metadata implements Serializable {
160
161 private static final long serialVersionUID = 31042L;
162
163 /** defines beginning of update data in archive */
164 private long mOffset;
165
166 /** size of the update data in archive */
167 private long mSize;
168
169 public Metadata(long offset, long size) {
170 this.mOffset = offset;
171 this.mSize = size;
172 }
173
174 public long getOffset() {
175 return mOffset;
176 }
177
178 public long getSize() {
179 return mSize;
180 }
181 }
182
183}