forked from firka/flutter
Refactor dynamic patching to use clearer naming and structure. (flutter/engine#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.
This commit is contained in:
committed by
GitHub
parent
1bdc80b8d6
commit
077b1ebf23
@@ -33,6 +33,7 @@ import io.flutter.view.FlutterNativeView;
|
|||||||
import io.flutter.view.FlutterRunArguments;
|
import io.flutter.view.FlutterRunArguments;
|
||||||
import io.flutter.view.FlutterView;
|
import io.flutter.view.FlutterView;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -343,8 +344,9 @@ public final class FlutterActivityDelegate
|
|||||||
if (!flutterView.getFlutterNativeView().isApplicationRunning()) {
|
if (!flutterView.getFlutterNativeView().isApplicationRunning()) {
|
||||||
FlutterRunArguments args = new FlutterRunArguments();
|
FlutterRunArguments args = new FlutterRunArguments();
|
||||||
ArrayList<String> bundlePaths = new ArrayList<>();
|
ArrayList<String> bundlePaths = new ArrayList<>();
|
||||||
if (FlutterMain.getUpdateInstallationPath() != null) {
|
if (FlutterMain.getResourceUpdater() != null) {
|
||||||
bundlePaths.add(FlutterMain.getUpdateInstallationPath());
|
File patchFile = FlutterMain.getResourceUpdater().getPatch();
|
||||||
|
bundlePaths.add(patchFile.getPath());
|
||||||
}
|
}
|
||||||
bundlePaths.add(appBundlePath);
|
bundlePaths.add(appBundlePath);
|
||||||
args.bundlePaths = bundlePaths.toArray(new String[0]);
|
args.bundlePaths = bundlePaths.toArray(new String[0]);
|
||||||
|
|||||||
@@ -360,8 +360,13 @@ public class FlutterMain {
|
|||||||
return appBundle.exists() ? appBundle.getPath() : null;
|
return appBundle.exists() ? appBundle.getPath() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUpdateInstallationPath() {
|
/**
|
||||||
return sResourceUpdater == null ? null : sResourceUpdater.getUpdateInstallationPath();
|
* Returns the main internal interface for the dynamic patching subsystem.
|
||||||
|
*
|
||||||
|
* If this is null, it means that dynamic patching is disabled in this app.
|
||||||
|
*/
|
||||||
|
public static ResourceUpdater getResourceUpdater() {
|
||||||
|
return sResourceUpdater;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -123,17 +123,6 @@ class ResourceExtractor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean filesMatch() {
|
|
||||||
JSONObject updateManifest = readUpdateManifest();
|
|
||||||
if (!validateUpdateManifest(updateManifest)) {
|
|
||||||
updateManifest = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final File dataDir = new File(PathUtils.getDataDirectory(mContext));
|
|
||||||
final String timestamp = checkTimestamp(dataDir, updateManifest);
|
|
||||||
return (timestamp == null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String[] getExistingTimestamps(File dataDir) {
|
private String[] getExistingTimestamps(File dataDir) {
|
||||||
return dataDir.list(new FilenameFilter() {
|
return dataDir.list(new FilenameFilter() {
|
||||||
@Override
|
@Override
|
||||||
@@ -160,7 +149,6 @@ class ResourceExtractor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Returns true if successfully unpacked APK resources,
|
/// Returns true if successfully unpacked APK resources,
|
||||||
/// otherwise deletes all resources and returns false.
|
/// otherwise deletes all resources and returns false.
|
||||||
private boolean extractAPK(File dataDir) {
|
private boolean extractAPK(File dataDir) {
|
||||||
@@ -208,11 +196,12 @@ class ResourceExtractor {
|
|||||||
/// Returns true if successfully unpacked update resources or if there is no update,
|
/// Returns true if successfully unpacked update resources or if there is no update,
|
||||||
/// otherwise deletes all resources and returns false.
|
/// otherwise deletes all resources and returns false.
|
||||||
private boolean extractUpdate(File dataDir) {
|
private boolean extractUpdate(File dataDir) {
|
||||||
if (FlutterMain.getUpdateInstallationPath() == null) {
|
ResourceUpdater resourceUpdater = FlutterMain.getResourceUpdater();
|
||||||
|
if (resourceUpdater == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final File updateFile = new File(FlutterMain.getUpdateInstallationPath());
|
File updateFile = resourceUpdater.getPatch();
|
||||||
if (!updateFile.exists()) {
|
if (!updateFile.exists()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -298,11 +287,14 @@ class ResourceExtractor {
|
|||||||
if (!buildNumber.equals(Long.toString(getVersionCode(packageInfo)))) {
|
if (!buildNumber.equals(Long.toString(getVersionCode(packageInfo)))) {
|
||||||
Log.w(TAG, "Outdated update file for " + getVersionCode(packageInfo));
|
Log.w(TAG, "Outdated update file for " + getVersionCode(packageInfo));
|
||||||
} else {
|
} else {
|
||||||
final File updateFile = new File(FlutterMain.getUpdateInstallationPath());
|
ResourceUpdater resourceUpdater = FlutterMain.getResourceUpdater();
|
||||||
|
assert resourceUpdater != null;
|
||||||
|
File patchFile = resourceUpdater.getPatch();
|
||||||
|
assert patchFile.exists();
|
||||||
if (patchNumber != null) {
|
if (patchNumber != null) {
|
||||||
expectedTimestamp += "-" + patchNumber + "-" + updateFile.lastModified();
|
expectedTimestamp += "-" + patchNumber + "-" + patchFile.lastModified();
|
||||||
} else {
|
} else {
|
||||||
expectedTimestamp += "-" + updateFile.lastModified();
|
expectedTimestamp += "-" + patchFile.lastModified();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -365,11 +357,12 @@ class ResourceExtractor {
|
|||||||
|
|
||||||
/// Returns null if no update manifest is found.
|
/// Returns null if no update manifest is found.
|
||||||
private JSONObject readUpdateManifest() {
|
private JSONObject readUpdateManifest() {
|
||||||
if (FlutterMain.getUpdateInstallationPath() == null) {
|
ResourceUpdater resourceUpdater = FlutterMain.getResourceUpdater();
|
||||||
|
if (resourceUpdater == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
File updateFile = new File(FlutterMain.getUpdateInstallationPath());
|
File updateFile = resourceUpdater.getPatch();
|
||||||
if (!updateFile.exists()) {
|
if (!updateFile.exists()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,12 +58,12 @@ public final class ResourceUpdater {
|
|||||||
IMMEDIATE
|
IMMEDIATE
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class DownloadTask extends AsyncTask<String, String, Void> {
|
private class DownloadTask extends AsyncTask<String, String, Void> {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(String... args) {
|
protected Void doInBackground(String... unused) {
|
||||||
try {
|
try {
|
||||||
URL unresolvedURL = new URL(args[0]);
|
URL unresolvedURL = new URL(buildUpdateDownloadURL());
|
||||||
File localFile = new File(args[1]);
|
File localFile = getPatch();
|
||||||
|
|
||||||
long startMillis = new Date().getTime();
|
long startMillis = new Date().getTime();
|
||||||
Log.i(TAG, "Checking for updates at " + unresolvedURL);
|
Log.i(TAG, "Checking for updates at " + unresolvedURL);
|
||||||
@@ -71,10 +71,10 @@ public final class ResourceUpdater {
|
|||||||
HttpURLConnection connection =
|
HttpURLConnection connection =
|
||||||
(HttpURLConnection)unresolvedURL.openConnection();
|
(HttpURLConnection)unresolvedURL.openConnection();
|
||||||
|
|
||||||
long lastModified = localFile.lastModified();
|
long lastDownloadTime = localFile.lastModified();
|
||||||
if (lastModified != 0) {
|
if (lastDownloadTime != 0) {
|
||||||
Log.i(TAG, "Active update timestamp " + lastModified);
|
Log.i(TAG, "Active update timestamp " + lastDownloadTime);
|
||||||
connection.setIfModifiedSince(lastModified);
|
connection.setIfModifiedSince(lastDownloadTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (InputStream input = connection.getInputStream()) {
|
try (InputStream input = connection.getInputStream()) {
|
||||||
@@ -108,7 +108,6 @@ public final class ResourceUpdater {
|
|||||||
long totalMillis = new Date().getTime() - startMillis;
|
long totalMillis = new Date().getTime() - startMillis;
|
||||||
Log.i(TAG, "Update downloaded in " + totalMillis / 100 / 10. + "s");
|
Log.i(TAG, "Update downloaded in " + totalMillis / 100 / 10. + "s");
|
||||||
|
|
||||||
output.flush();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,7 +126,7 @@ public final class ResourceUpdater {
|
|||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAPKVersion() {
|
private String getAPKVersion() {
|
||||||
try {
|
try {
|
||||||
PackageManager packageManager = context.getPackageManager();
|
PackageManager packageManager = context.getPackageManager();
|
||||||
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
||||||
@@ -138,11 +137,11 @@ public final class ResourceUpdater {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUpdateInstallationPath() {
|
public File getPatch() {
|
||||||
return context.getFilesDir().toString() + "/patch.zip";
|
return new File(context.getFilesDir().toString() + "/patch.zip");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String buildUpdateDownloadURL() {
|
private String buildUpdateDownloadURL() {
|
||||||
Bundle metaData;
|
Bundle metaData;
|
||||||
try {
|
try {
|
||||||
metaData = context.getPackageManager().getApplicationInfo(
|
metaData = context.getPackageManager().getApplicationInfo(
|
||||||
@@ -168,7 +167,7 @@ public final class ResourceUpdater {
|
|||||||
return uri.normalize().toString();
|
return uri.normalize().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public DownloadMode getDownloadMode() {
|
DownloadMode getDownloadMode() {
|
||||||
Bundle metaData;
|
Bundle metaData;
|
||||||
try {
|
try {
|
||||||
metaData = context.getPackageManager().getApplicationInfo(
|
metaData = context.getPackageManager().getApplicationInfo(
|
||||||
@@ -195,7 +194,7 @@ public final class ResourceUpdater {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public InstallMode getInstallMode() {
|
InstallMode getInstallMode() {
|
||||||
Bundle metaData;
|
Bundle metaData;
|
||||||
try {
|
try {
|
||||||
metaData = context.getPackageManager().getApplicationInfo(
|
metaData = context.getPackageManager().getApplicationInfo(
|
||||||
@@ -222,21 +221,21 @@ public final class ResourceUpdater {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startUpdateDownloadOnce() {
|
void startUpdateDownloadOnce() {
|
||||||
if (downloadTask != null ) {
|
if (downloadTask != null ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
downloadTask = new DownloadTask();
|
downloadTask = new DownloadTask();
|
||||||
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
|
downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
buildUpdateDownloadURL(), getUpdateInstallationPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void waitForDownloadCompletion() {
|
void waitForDownloadCompletion() {
|
||||||
if (downloadTask == null) {
|
if (downloadTask == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
downloadTask.get();
|
downloadTask.get();
|
||||||
|
downloadTask = null;
|
||||||
} catch (CancellationException e) {
|
} catch (CancellationException e) {
|
||||||
Log.w(TAG, "Download cancelled: " + e.getMessage());
|
Log.w(TAG, "Download cancelled: " + e.getMessage());
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user