Skip to content

Commit 844d27c

Browse files
authored
Refactor dynamic patching to use clearer naming and structure. (#7426)
This is a no-op change, except for fixing a bug where download task reference wasn't cleared after download was completed. This change also removes call to output stream flush(), which is not necessary according to Java spec. The rest of the change deals with requiring the code to work directly with ResourceUpdater object instead of having FlutterMain be a facade that forwards some of ResourceUpdater's methods. This simplifies the other (more essential) upcoming changes that will be landing in the followings few PRs.
1 parent 99fa390 commit 844d27c

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed

shell/platform/android/io/flutter/app/FlutterActivityDelegate.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.flutter.view.FlutterRunArguments;
3434
import io.flutter.view.FlutterView;
3535

36+
import java.io.File;
3637
import java.util.ArrayList;
3738

3839
/**
@@ -343,8 +344,9 @@ private void runBundle(String appBundlePath) {
343344
if (!flutterView.getFlutterNativeView().isApplicationRunning()) {
344345
FlutterRunArguments args = new FlutterRunArguments();
345346
ArrayList<String> bundlePaths = new ArrayList<>();
346-
if (FlutterMain.getUpdateInstallationPath() != null) {
347-
bundlePaths.add(FlutterMain.getUpdateInstallationPath());
347+
if (FlutterMain.getResourceUpdater() != null) {
348+
File patchFile = FlutterMain.getResourceUpdater().getPatch();
349+
bundlePaths.add(patchFile.getPath());
348350
}
349351
bundlePaths.add(appBundlePath);
350352
args.bundlePaths = bundlePaths.toArray(new String[0]);

shell/platform/android/io/flutter/view/FlutterMain.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,13 @@ public static String findAppBundlePath(Context applicationContext) {
360360
return appBundle.exists() ? appBundle.getPath() : null;
361361
}
362362

363-
public static String getUpdateInstallationPath() {
364-
return sResourceUpdater == null ? null : sResourceUpdater.getUpdateInstallationPath();
363+
/**
364+
* Returns the main internal interface for the dynamic patching subsystem.
365+
*
366+
* If this is null, it means that dynamic patching is disabled in this app.
367+
*/
368+
public static ResourceUpdater getResourceUpdater() {
369+
return sResourceUpdater;
365370
}
366371

367372
/**

shell/platform/android/io/flutter/view/ResourceExtractor.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,6 @@ void waitForCompletion() {
123123
}
124124
}
125125

126-
boolean filesMatch() {
127-
JSONObject updateManifest = readUpdateManifest();
128-
if (!validateUpdateManifest(updateManifest)) {
129-
updateManifest = null;
130-
}
131-
132-
final File dataDir = new File(PathUtils.getDataDirectory(mContext));
133-
final String timestamp = checkTimestamp(dataDir, updateManifest);
134-
return (timestamp == null);
135-
}
136-
137126
private String[] getExistingTimestamps(File dataDir) {
138127
return dataDir.list(new FilenameFilter() {
139128
@Override
@@ -160,7 +149,6 @@ private void deleteFiles() {
160149
}
161150
}
162151

163-
164152
/// Returns true if successfully unpacked APK resources,
165153
/// otherwise deletes all resources and returns false.
166154
private boolean extractAPK(File dataDir) {
@@ -208,11 +196,12 @@ private boolean extractAPK(File dataDir) {
208196
/// Returns true if successfully unpacked update resources or if there is no update,
209197
/// otherwise deletes all resources and returns false.
210198
private boolean extractUpdate(File dataDir) {
211-
if (FlutterMain.getUpdateInstallationPath() == null) {
199+
ResourceUpdater resourceUpdater = FlutterMain.getResourceUpdater();
200+
if (resourceUpdater == null) {
212201
return true;
213202
}
214203

215-
final File updateFile = new File(FlutterMain.getUpdateInstallationPath());
204+
File updateFile = resourceUpdater.getPatch();
216205
if (!updateFile.exists()) {
217206
return true;
218207
}
@@ -298,11 +287,14 @@ private String checkTimestamp(File dataDir, JSONObject updateManifest) {
298287
if (!buildNumber.equals(Long.toString(getVersionCode(packageInfo)))) {
299288
Log.w(TAG, "Outdated update file for " + getVersionCode(packageInfo));
300289
} else {
301-
final File updateFile = new File(FlutterMain.getUpdateInstallationPath());
290+
ResourceUpdater resourceUpdater = FlutterMain.getResourceUpdater();
291+
assert resourceUpdater != null;
292+
File patchFile = resourceUpdater.getPatch();
293+
assert patchFile.exists();
302294
if (patchNumber != null) {
303-
expectedTimestamp += "-" + patchNumber + "-" + updateFile.lastModified();
295+
expectedTimestamp += "-" + patchNumber + "-" + patchFile.lastModified();
304296
} else {
305-
expectedTimestamp += "-" + updateFile.lastModified();
297+
expectedTimestamp += "-" + patchFile.lastModified();
306298
}
307299
}
308300
}
@@ -365,11 +357,12 @@ private boolean validateUpdateManifest(JSONObject updateManifest) {
365357

366358
/// Returns null if no update manifest is found.
367359
private JSONObject readUpdateManifest() {
368-
if (FlutterMain.getUpdateInstallationPath() == null) {
360+
ResourceUpdater resourceUpdater = FlutterMain.getResourceUpdater();
361+
if (resourceUpdater == null) {
369362
return null;
370363
}
371364

372-
File updateFile = new File(FlutterMain.getUpdateInstallationPath());
365+
File updateFile = resourceUpdater.getPatch();
373366
if (!updateFile.exists()) {
374367
return null;
375368
}

shell/platform/android/io/flutter/view/ResourceUpdater.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,23 @@ enum InstallMode {
5858
IMMEDIATE
5959
}
6060

61-
private static class DownloadTask extends AsyncTask<String, String, Void> {
61+
private class DownloadTask extends AsyncTask<String, String, Void> {
6262
@Override
63-
protected Void doInBackground(String... args) {
63+
protected Void doInBackground(String... unused) {
6464
try {
65-
URL unresolvedURL = new URL(args[0]);
66-
File localFile = new File(args[1]);
65+
URL unresolvedURL = new URL(buildUpdateDownloadURL());
66+
File localFile = getPatch();
6767

6868
long startMillis = new Date().getTime();
6969
Log.i(TAG, "Checking for updates at " + unresolvedURL);
7070

7171
HttpURLConnection connection =
7272
(HttpURLConnection)unresolvedURL.openConnection();
7373

74-
long lastModified = localFile.lastModified();
75-
if (lastModified != 0) {
76-
Log.i(TAG, "Active update timestamp " + lastModified);
77-
connection.setIfModifiedSince(lastModified);
74+
long lastDownloadTime = localFile.lastModified();
75+
if (lastDownloadTime != 0) {
76+
Log.i(TAG, "Active update timestamp " + lastDownloadTime);
77+
connection.setIfModifiedSince(lastDownloadTime);
7878
}
7979

8080
try (InputStream input = connection.getInputStream()) {
@@ -108,7 +108,6 @@ protected Void doInBackground(String... args) {
108108
long totalMillis = new Date().getTime() - startMillis;
109109
Log.i(TAG, "Update downloaded in " + totalMillis / 100 / 10. + "s");
110110

111-
output.flush();
112111
return null;
113112
}
114113
}
@@ -127,7 +126,7 @@ public ResourceUpdater(Context context) {
127126
this.context = context;
128127
}
129128

130-
public String getAPKVersion() {
129+
private String getAPKVersion() {
131130
try {
132131
PackageManager packageManager = context.getPackageManager();
133132
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
@@ -138,11 +137,11 @@ public String getAPKVersion() {
138137
}
139138
}
140139

141-
public String getUpdateInstallationPath() {
142-
return context.getFilesDir().toString() + "/patch.zip";
140+
public File getPatch() {
141+
return new File(context.getFilesDir().toString() + "/patch.zip");
143142
}
144143

145-
public String buildUpdateDownloadURL() {
144+
private String buildUpdateDownloadURL() {
146145
Bundle metaData;
147146
try {
148147
metaData = context.getPackageManager().getApplicationInfo(
@@ -168,7 +167,7 @@ public String buildUpdateDownloadURL() {
168167
return uri.normalize().toString();
169168
}
170169

171-
public DownloadMode getDownloadMode() {
170+
DownloadMode getDownloadMode() {
172171
Bundle metaData;
173172
try {
174173
metaData = context.getPackageManager().getApplicationInfo(
@@ -195,7 +194,7 @@ public DownloadMode getDownloadMode() {
195194
}
196195
}
197196

198-
public InstallMode getInstallMode() {
197+
InstallMode getInstallMode() {
199198
Bundle metaData;
200199
try {
201200
metaData = context.getPackageManager().getApplicationInfo(
@@ -222,21 +221,21 @@ public InstallMode getInstallMode() {
222221
}
223222
}
224223

225-
public void startUpdateDownloadOnce() {
224+
void startUpdateDownloadOnce() {
226225
if (downloadTask != null ) {
227226
return;
228227
}
229228
downloadTask = new DownloadTask();
230-
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
231-
buildUpdateDownloadURL(), getUpdateInstallationPath());
229+
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
232230
}
233231

234-
public void waitForDownloadCompletion() {
232+
void waitForDownloadCompletion() {
235233
if (downloadTask == null) {
236234
return;
237235
}
238236
try {
239237
downloadTask.get();
238+
downloadTask = null;
240239
} catch (CancellationException e) {
241240
Log.w(TAG, "Download cancelled: " + e.getMessage());
242241
return;

0 commit comments

Comments
 (0)