1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dynsystem;
import android.content.Context;
import android.gsi.GsiProgress;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.image.DynamicSystemManager;
import android.util.Log;
import android.webkit.URLUtil;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Locale;
import java.util.zip.GZIPInputStream;
class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> {
private static final String TAG = "InstallationAsyncTask";
private static final int READ_BUFFER_SIZE = 1 << 13;
private class InvalidImageUrlException extends RuntimeException {
private InvalidImageUrlException(String message) {
super(message);
}
}
/** Not completed, including being cancelled */
static final int NO_RESULT = 0;
static final int RESULT_OK = 1;
static final int RESULT_ERROR_IO = 2;
static final int RESULT_ERROR_INVALID_URL = 3;
static final int RESULT_ERROR_EXCEPTION = 6;
interface InstallStatusListener {
void onProgressUpdate(long installedSize);
void onResult(int resultCode, Throwable detail);
void onCancelled();
}
private final String mUrl;
private final long mSystemSize;
private final long mUserdataSize;
private final Context mContext;
private final DynamicSystemManager mDynSystem;
private final InstallStatusListener mListener;
private DynamicSystemManager.Session mInstallationSession;
private int mResult = NO_RESULT;
private InputStream mStream;
InstallationAsyncTask(String url, long systemSize, long userdataSize, Context context,
DynamicSystemManager dynSystem, InstallStatusListener listener) {
mUrl = url;
mSystemSize = systemSize;
mUserdataSize = userdataSize;
mContext = context;
mDynSystem = dynSystem;
mListener = listener;
}
@Override
protected void onPreExecute() {
mListener.onProgressUpdate(0);
}
@Override
protected Throwable doInBackground(String... voids) {
Log.d(TAG, "Start doInBackground(), URL: " + mUrl);
try {
long installedSize = 0;
long reportedInstalledSize = 0;
long minStepToReport = (mSystemSize + mUserdataSize) / 100;
// init input stream before calling startInstallation(), which takes 90 seconds.
initInputStream();
Thread thread = new Thread(() -> {
mInstallationSession =
mDynSystem.startInstallation(mSystemSize, mUserdataSize);
});
thread.start();
while (thread.isAlive()) {
if (isCancelled()) {
boolean aborted = mDynSystem.abort();
Log.d(TAG, "Called DynamicSystemManager.abort(), result = " + aborted);
return null;
}
GsiProgress progress = mDynSystem.getInstallationProgress();
installedSize = progress.bytes_processed;
if (installedSize > reportedInstalledSize + minStepToReport) {
publishProgress(installedSize);
reportedInstalledSize = installedSize;
}
Thread.sleep(10);
}
if (mInstallationSession == null) {
throw new IOException("Failed to start installation with requested size: "
+ (mSystemSize + mUserdataSize));
}
installedSize = mUserdataSize;
byte[] bytes = new byte[READ_BUFFER_SIZE];
int numBytesRead;
Log.d(TAG, "Start installation loop");
while ((numBytesRead = mStream.read(bytes, 0, READ_BUFFER_SIZE)) != -1) {
if (isCancelled()) {
break;
}
byte[] writeBuffer = numBytesRead == READ_BUFFER_SIZE
? bytes : Arrays.copyOf(bytes, numBytesRead);
if (!mInstallationSession.write(writeBuffer)) {
throw new IOException("Failed write() to DynamicSystem");
}
installedSize += numBytesRead;
if (installedSize > reportedInstalledSize + minStepToReport) {
publishProgress(installedSize);
reportedInstalledSize = installedSize;
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
return e;
} finally {
close();
}
}
@Override
protected void onCancelled() {
Log.d(TAG, "onCancelled(), URL: " + mUrl);
mListener.onCancelled();
}
@Override
protected void onPostExecute(Throwable detail) {
if (detail == null) {
mResult = RESULT_OK;
} else if (detail instanceof IOException) {
mResult = RESULT_ERROR_IO;
} else if (detail instanceof InvalidImageUrlException) {
mResult = RESULT_ERROR_INVALID_URL;
} else {
mResult = RESULT_ERROR_EXCEPTION;
}
Log.d(TAG, "onPostExecute(), URL: " + mUrl + ", result: " + mResult);
mListener.onResult(mResult, detail);
}
@Override
protected void onProgressUpdate(Long... values) {
long progress = values[0];
mListener.onProgressUpdate(progress);
}
private void initInputStream() throws IOException, InvalidImageUrlException {
if (URLUtil.isNetworkUrl(mUrl) || URLUtil.isFileUrl(mUrl)) {
mStream = new BufferedInputStream(new GZIPInputStream(new URL(mUrl).openStream()));
} else if (URLUtil.isContentUrl(mUrl)) {
Uri uri = Uri.parse(mUrl);
mStream = new BufferedInputStream(new GZIPInputStream(
mContext.getContentResolver().openInputStream(uri)));
} else {
throw new InvalidImageUrlException(
String.format(Locale.US, "Unsupported file source: %s", mUrl));
}
}
private void close() {
try {
if (mStream != null) {
mStream.close();
mStream = null;
}
} catch (IOException e) {
// ignore
}
}
int getResult() {
return mResult;
}
boolean commit() {
if (mInstallationSession == null) {
return false;
}
return mInstallationSession.commit();
}
}
|