Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 17 | package com.android.dynsystem; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 18 | |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 19 | import android.content.Context; |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 20 | import android.gsi.GsiProgress; |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 21 | import android.net.Uri; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 22 | import android.os.AsyncTask; |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 23 | import android.os.MemoryFile; |
| 24 | import android.os.ParcelFileDescriptor; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 25 | import android.os.image.DynamicSystemManager; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | import android.webkit.URLUtil; |
| 28 | |
| 29 | import java.io.BufferedInputStream; |
| 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | import java.net.URL; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 33 | import java.util.Locale; |
| 34 | import java.util.zip.GZIPInputStream; |
| 35 | |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 36 | class InstallationAsyncTask extends AsyncTask<String, Long, Throwable> { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 37 | |
| 38 | private static final String TAG = "InstallationAsyncTask"; |
| 39 | |
Po-Chien Hsueh | 22c1f9a | 2019-05-17 15:08:20 +0800 | [diff] [blame] | 40 | private static final int READ_BUFFER_SIZE = 1 << 13; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 41 | |
| 42 | private class InvalidImageUrlException extends RuntimeException { |
| 43 | private InvalidImageUrlException(String message) { |
| 44 | super(message); |
| 45 | } |
| 46 | } |
| 47 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 48 | /** Not completed, including being cancelled */ |
| 49 | static final int NO_RESULT = 0; |
| 50 | static final int RESULT_OK = 1; |
| 51 | static final int RESULT_ERROR_IO = 2; |
| 52 | static final int RESULT_ERROR_INVALID_URL = 3; |
| 53 | static final int RESULT_ERROR_EXCEPTION = 6; |
| 54 | |
| 55 | interface InstallStatusListener { |
| 56 | void onProgressUpdate(long installedSize); |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 57 | void onResult(int resultCode, Throwable detail); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 58 | void onCancelled(); |
| 59 | } |
| 60 | |
| 61 | private final String mUrl; |
| 62 | private final long mSystemSize; |
| 63 | private final long mUserdataSize; |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 64 | private final Context mContext; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 65 | private final DynamicSystemManager mDynSystem; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 66 | private final InstallStatusListener mListener; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 67 | private DynamicSystemManager.Session mInstallationSession; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 68 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 69 | private int mResult = NO_RESULT; |
| 70 | |
| 71 | private InputStream mStream; |
| 72 | |
| 73 | |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 74 | InstallationAsyncTask(String url, long systemSize, long userdataSize, Context context, |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 75 | DynamicSystemManager dynSystem, InstallStatusListener listener) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 76 | mUrl = url; |
| 77 | mSystemSize = systemSize; |
| 78 | mUserdataSize = userdataSize; |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 79 | mContext = context; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 80 | mDynSystem = dynSystem; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 81 | mListener = listener; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void onPreExecute() { |
| 86 | mListener.onProgressUpdate(0); |
| 87 | } |
| 88 | |
| 89 | @Override |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 90 | protected Throwable doInBackground(String... voids) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 91 | Log.d(TAG, "Start doInBackground(), URL: " + mUrl); |
| 92 | |
| 93 | try { |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 94 | long installedSize = 0; |
| 95 | long reportedInstalledSize = 0; |
| 96 | |
| 97 | long minStepToReport = (mSystemSize + mUserdataSize) / 100; |
| 98 | |
| 99 | // init input stream before calling startInstallation(), which takes 90 seconds. |
| 100 | initInputStream(); |
| 101 | |
| 102 | Thread thread = new Thread(() -> { |
| 103 | mInstallationSession = |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 104 | mDynSystem.startInstallation(mSystemSize, mUserdataSize); |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 105 | }); |
| 106 | |
| 107 | |
| 108 | thread.start(); |
| 109 | |
| 110 | while (thread.isAlive()) { |
| 111 | if (isCancelled()) { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 112 | boolean aborted = mDynSystem.abort(); |
| 113 | Log.d(TAG, "Called DynamicSystemManager.abort(), result = " + aborted); |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 114 | return null; |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 115 | } |
| 116 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 117 | GsiProgress progress = mDynSystem.getInstallationProgress(); |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 118 | installedSize = progress.bytes_processed; |
| 119 | |
| 120 | if (installedSize > reportedInstalledSize + minStepToReport) { |
| 121 | publishProgress(installedSize); |
| 122 | reportedInstalledSize = installedSize; |
| 123 | } |
| 124 | |
| 125 | Thread.sleep(10); |
| 126 | } |
| 127 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 128 | if (mInstallationSession == null) { |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 129 | throw new IOException( |
| 130 | "Failed to start installation with requested size: " |
| 131 | + (mSystemSize + mUserdataSize)); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 134 | installedSize = mUserdataSize; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 135 | |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 136 | MemoryFile memoryFile = new MemoryFile("dsu", READ_BUFFER_SIZE); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 137 | byte[] bytes = new byte[READ_BUFFER_SIZE]; |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 138 | mInstallationSession.setAshmem( |
| 139 | new ParcelFileDescriptor(memoryFile.getFileDescriptor()), READ_BUFFER_SIZE); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 140 | int numBytesRead; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 141 | Log.d(TAG, "Start installation loop"); |
| 142 | while ((numBytesRead = mStream.read(bytes, 0, READ_BUFFER_SIZE)) != -1) { |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 143 | memoryFile.writeBytes(bytes, 0, 0, numBytesRead); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 144 | if (isCancelled()) { |
| 145 | break; |
| 146 | } |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 147 | if (!mInstallationSession.submitFromAshmem(numBytesRead)) { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 148 | throw new IOException("Failed write() to DynamicSystem"); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 149 | } |
| 150 | |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 151 | installedSize += numBytesRead; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 152 | |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 153 | if (installedSize > reportedInstalledSize + minStepToReport) { |
| 154 | publishProgress(installedSize); |
| 155 | reportedInstalledSize = installedSize; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 156 | } |
| 157 | } |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 158 | return null; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 159 | |
| 160 | } catch (Exception e) { |
| 161 | e.printStackTrace(); |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 162 | return e; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 163 | } finally { |
| 164 | close(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void onCancelled() { |
| 170 | Log.d(TAG, "onCancelled(), URL: " + mUrl); |
| 171 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 172 | mListener.onCancelled(); |
| 173 | } |
| 174 | |
| 175 | @Override |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 176 | protected void onPostExecute(Throwable detail) { |
| 177 | if (detail == null) { |
| 178 | mResult = RESULT_OK; |
| 179 | } else if (detail instanceof IOException) { |
| 180 | mResult = RESULT_ERROR_IO; |
| 181 | } else if (detail instanceof InvalidImageUrlException) { |
| 182 | mResult = RESULT_ERROR_INVALID_URL; |
| 183 | } else { |
| 184 | mResult = RESULT_ERROR_EXCEPTION; |
| 185 | } |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 186 | |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 187 | Log.d(TAG, "onPostExecute(), URL: " + mUrl + ", result: " + mResult); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 188 | |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 189 | mListener.onResult(mResult, detail); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | protected void onProgressUpdate(Long... values) { |
| 194 | long progress = values[0]; |
| 195 | mListener.onProgressUpdate(progress); |
| 196 | } |
| 197 | |
| 198 | private void initInputStream() throws IOException, InvalidImageUrlException { |
| 199 | if (URLUtil.isNetworkUrl(mUrl) || URLUtil.isFileUrl(mUrl)) { |
| 200 | mStream = new BufferedInputStream(new GZIPInputStream(new URL(mUrl).openStream())); |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 201 | } else if (URLUtil.isContentUrl(mUrl)) { |
| 202 | Uri uri = Uri.parse(mUrl); |
| 203 | mStream = new BufferedInputStream(new GZIPInputStream( |
| 204 | mContext.getContentResolver().openInputStream(uri))); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 205 | } else { |
| 206 | throw new InvalidImageUrlException( |
| 207 | String.format(Locale.US, "Unsupported file source: %s", mUrl)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private void close() { |
| 212 | try { |
| 213 | if (mStream != null) { |
| 214 | mStream.close(); |
| 215 | mStream = null; |
| 216 | } |
| 217 | } catch (IOException e) { |
| 218 | // ignore |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | int getResult() { |
| 223 | return mResult; |
| 224 | } |
| 225 | |
| 226 | boolean commit() { |
| 227 | if (mInstallationSession == null) { |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return mInstallationSession.commit(); |
| 232 | } |
| 233 | } |