feat: checks plugin version
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package com.stardust.scriptdroid.build;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
|
||||
import com.stardust.BuildConfig;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.scriptdroid.BuildConfig;
|
||||
import com.stardust.scriptdroid.ui.build.BuildActivity;
|
||||
import com.stardust.util.DeveloperUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -20,7 +22,7 @@ public class ApkBuilderPluginHelper {
|
||||
private static final String PLUGIN_PACKAGE_NAME = "org.autojs.apkbuilderplugin";
|
||||
private static final String TEMPLATE_APK_PATH = "template.apk";
|
||||
|
||||
public static boolean checkPlugin(Context context) {
|
||||
public static boolean isPluginAvailable(Context context) {
|
||||
return DeveloperUtils.checkSignature(context, PLUGIN_PACKAGE_NAME);
|
||||
}
|
||||
|
||||
@@ -37,4 +39,17 @@ public class ApkBuilderPluginHelper {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getPluginVersion(Context context) {
|
||||
try {
|
||||
PackageInfo info = context.getPackageManager().getPackageInfo(PLUGIN_PACKAGE_NAME, 0);
|
||||
return info.versionCode;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getSuitablePluginVersion() {
|
||||
return BuildConfig.VERSION_CODE - 200;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ import android.support.design.widget.TextInputEditText;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.autojs.project.ProjectConfig;
|
||||
import com.stardust.scriptdroid.BuildConfig;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.build.AutoJsApkBuilder;
|
||||
import com.stardust.scriptdroid.build.ApkBuilderPluginHelper;
|
||||
@@ -35,6 +35,7 @@ import org.androidannotations.annotations.ViewById;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
@@ -82,25 +83,41 @@ public class BuildActivity extends BaseActivity implements AutoJsApkBuilder.Prog
|
||||
if (sourcePath != null) {
|
||||
setupWithSourceFile(new ScriptFile(sourcePath));
|
||||
}
|
||||
showPluginDownloadDialogIfNeeded();
|
||||
checkApkBuilderPlugin();
|
||||
}
|
||||
|
||||
private void showPluginDownloadDialogIfNeeded() {
|
||||
if (ApkBuilderPluginHelper.checkPlugin(this)) {
|
||||
private void checkApkBuilderPlugin() {
|
||||
if (!ApkBuilderPluginHelper.isPluginAvailable(this)) {
|
||||
showPluginDownloadDialog(R.string.no_apk_builder_plugin, true);
|
||||
return;
|
||||
}
|
||||
int version = ApkBuilderPluginHelper.getPluginVersion(this);
|
||||
if (version < 0) {
|
||||
showPluginDownloadDialog(R.string.no_apk_builder_plugin, true);
|
||||
return;
|
||||
}
|
||||
if (version < ApkBuilderPluginHelper.getSuitablePluginVersion()) {
|
||||
showPluginDownloadDialog(R.string.apk_builder_plugin_version_too_low, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void showPluginDownloadDialog(int msgRes, boolean finishIfCanceled) {
|
||||
new ThemeColorMaterialDialogBuilder(this)
|
||||
.content(R.string.no_apk_builder_plugin)
|
||||
.content(msgRes)
|
||||
.positiveText(R.string.ok)
|
||||
.negativeText(R.string.text_exit)
|
||||
.onPositive((dialog, which) -> downloadPlugin())
|
||||
.onNegative((dialog, which) -> finish())
|
||||
.onNegative((dialog, which) -> {
|
||||
if (finishIfCanceled) finish();
|
||||
})
|
||||
.show();
|
||||
|
||||
}
|
||||
|
||||
private void downloadPlugin() {
|
||||
IntentUtil.browse(this, "https://www.autojs.org/assets/autojs/plugin.apk");
|
||||
IntentUtil.browse(this, String.format(Locale.getDefault(),
|
||||
"https://i.autojs.org/autojs/plugin/%d.apk", ApkBuilderPluginHelper.getSuitablePluginVersion()));
|
||||
}
|
||||
|
||||
private void setupWithSourceFile(ScriptFile file) {
|
||||
@@ -165,7 +182,7 @@ public class BuildActivity extends BaseActivity implements AutoJsApkBuilder.Prog
|
||||
|
||||
@Click(R.id.fab)
|
||||
void buildApk() {
|
||||
if (!ApkBuilderPluginHelper.checkPlugin(this)) {
|
||||
if (!ApkBuilderPluginHelper.isPluginAvailable(this)) {
|
||||
Toast.makeText(this, R.string.text_apk_builder_plugin_unavailable, Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -350,4 +350,5 @@
|
||||
<string name="text_size_current_value" formatted="true">字体大小: %d</string>
|
||||
<string name="error_regex_find_prev">正则表达式不能向前查找</string>
|
||||
<string name="text_notification">通知</string>
|
||||
<string name="apk_builder_plugin_version_too_low">打包插件版本过低,打包的应用可能无法正常运行。是否立即下载对应版本的打包插件?</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user