Setup: Rewritten in Kotlin

This commit is contained in:
zt515 2017-12-25 23:55:56 +08:00
parent 001e628dc0
commit 81887a6ed1
10 changed files with 100 additions and 87 deletions

View File

@ -1,8 +0,0 @@
package io.neoterm.setup;
/**
* @author kiva
*/
public interface ResultListener {
void onResult(Exception error);
}

View File

@ -0,0 +1,8 @@
package io.neoterm.setup
/**
* @author kiva
*/
interface ResultListener {
fun onResult(error: Exception?)
}

View File

@ -1,66 +0,0 @@
package io.neoterm.setup;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Build;
import java.io.File;
import java.util.Arrays;
import io.neoterm.R;
import io.neoterm.frontend.config.NeoTermPath;
/**
* @author kiva
*/
public final class SetupHelper {
public static boolean needSetup() {
final File PREFIX_FILE = new File(NeoTermPath.USR_PATH);
return !PREFIX_FILE.isDirectory();
}
public static void setup(final Activity activity, final SourceConnection connection,
final ResultListener resultListener) {
if (!needSetup()) {
resultListener.onResult(null);
return;
}
final File prefixFile = new File(NeoTermPath.USR_PATH);
final ProgressDialog progress = makeProgressDialog(activity);
progress.setMax(100);
progress.show();
new SetupThread(activity, connection, prefixFile, resultListener, progress);
}
private static ProgressDialog makeProgressDialog(Context context) {
return makeProgressDialog(context, context.getString(R.string.installer_message));
}
public static ProgressDialog makeProgressDialog(Context context, String message) {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setMessage(message);
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
return dialog;
}
public static String determineArchName() {
for (String androidArch : Build.SUPPORTED_ABIS) {
switch (androidArch) {
case "arm64-v8a":
return "aarch64";
case "armeabi-v7a":
return "arm";
case "x86_64":
return "x86_64";
}
}
throw new RuntimeException("Unable to determine arch from Build.SUPPORTED_ABIS = " +
Arrays.toString(Build.SUPPORTED_ABIS));
}
}

View File

@ -0,0 +1,79 @@
package io.neoterm.setup
import android.app.Activity
import android.app.AlertDialog
import android.app.ProgressDialog
import android.content.Context
import android.content.DialogInterface
import android.os.Build
import java.io.File
import java.util.Arrays
import io.neoterm.App
import io.neoterm.R
import io.neoterm.frontend.config.NeoTermPath
/**
* @author kiva
*/
object SetupHelper {
fun needSetup(): Boolean {
val PREFIX_FILE = File(NeoTermPath.USR_PATH)
return !PREFIX_FILE.isDirectory
}
fun setup(activity: Activity, connection: SourceConnection,
resultListener: ResultListener) {
if (!needSetup()) {
resultListener.onResult(null)
return
}
val prefixFile = File(NeoTermPath.USR_PATH)
val progress = makeProgressDialog(activity)
progress.max = 100
progress.show()
SetupThread(activity, connection, prefixFile, resultListener, progress)
}
private fun makeProgressDialog(context: Context): ProgressDialog {
return makeProgressDialog(context, context.getString(R.string.installer_message))
}
fun makeProgressDialog(context: Context, message: String): ProgressDialog {
val dialog = ProgressDialog(context)
dialog.setMessage(message)
dialog.isIndeterminate = false
dialog.setCancelable(false)
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
return dialog
}
fun makeErrorDialog(context: Context, messageId: Int): AlertDialog {
return makeErrorDialog(context, context.getString(messageId))
}
fun makeErrorDialog(context: Context, message: String): AlertDialog {
return AlertDialog.Builder(context)
.setTitle(R.string.error)
.setMessage(message)
.setPositiveButton(android.R.string.yes, null)
.setNeutralButton(R.string.show_help) { _, _ -> App.get().openHelpLink() }
.create()
}
fun determineArchName(): String {
for (androidArch in Build.SUPPORTED_ABIS) {
when (androidArch) {
"arm64-v8a" -> return "aarch64"
"armeabi-v7a" -> return "arm"
"x86_64" -> return "x86_64"
}
}
throw RuntimeException("Unable to determine arch from Build.SUPPORTED_ABIS = "
+ Arrays.toString(Build.SUPPORTED_ABIS))
}
}

View File

@ -6,7 +6,6 @@ import java.io.InputStream;
/**
* @author kiva
*/
public interface SourceConnection {
InputStream getInputStream() throws IOException;

View File

@ -14,7 +14,7 @@ import io.neoterm.utils.AssetsUtils;
public class AssetsFileConnection extends OfflineConnection {
@Override
protected InputStream openInputStream() throws IOException {
String arch = SetupHelper.determineArchName();
String arch = SetupHelper.INSTANCE.determineArchName();
String fileName = "offline_setup/" + arch + ".zip";
return AssetsUtils.INSTANCE.openAssetsFile(App.Companion.get(), fileName);
}

View File

@ -47,7 +47,7 @@ public class NetworkConnection implements SourceConnection {
}
private HttpURLConnection openHttpConnection() throws IOException {
String arch = SetupHelper.determineArchName();
String arch = SetupHelper.INSTANCE.determineArchName();
return (HttpURLConnection) new URL(sourceUrl + "/boot/" + arch + ".zip").openConnection();
}

View File

@ -9,6 +9,7 @@ import android.view.View
import android.widget.*
import io.neoterm.App
import io.neoterm.R
import io.neoterm.frontend.config.NeoTermPath
import io.neoterm.setup.ResultListener
import io.neoterm.setup.SetupHelper
import io.neoterm.setup.SourceConnection
@ -17,7 +18,6 @@ import io.neoterm.setup.connection.BackupFileConnection
import io.neoterm.setup.connection.LocalFileConnection
import io.neoterm.setup.connection.NetworkConnection
import io.neoterm.setup.helper.URLAvailability
import io.neoterm.frontend.config.NeoTermPath
import io.neoterm.utils.MediaUtils
import io.neoterm.utils.PackageUtils
import java.io.File
@ -101,7 +101,8 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener
when (id) {
R.id.setup_method_backup,
R.id.setup_method_local -> {
// TODO: Report as an error
SetupHelper.makeErrorDialog(this, R.string.setup_error_parameter_null)
.show()
return
}
}
@ -109,17 +110,15 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener
val dialog = SetupHelper.makeProgressDialog(this, getString(R.string.setup_preparing))
dialog.show()
Thread {
val errorMessage = validateParameter(id, setupParameter)
SetupActivity@ this.runOnUiThread {
runOnUiThread {
dialog.dismiss()
editor.error = errorMessage
if (errorMessage != null) {
AlertDialog.Builder(SetupActivity@ this)
.setMessage(errorMessage)
.setPositiveButton(android.R.string.yes, null)
.show()
SetupHelper.makeErrorDialog(this, errorMessage).show()
return@runOnUiThread
}
@ -199,7 +198,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener
}
private fun doSetup(connection: SourceConnection) {
SetupHelper.setup(this@SetupActivity, connection, this)
SetupHelper.setup(this, connection, this)
}
override fun onResult(error: Exception?) {
@ -209,7 +208,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener
executeAptUpdate()
} else {
AlertDialog.Builder(this@SetupActivity)
AlertDialog.Builder(this)
.setTitle(R.string.error)
.setMessage(error.toString())
.setNegativeButton(R.string.use_system_shell, { _, _ ->

View File

@ -77,7 +77,7 @@
<string name="setup_assets">从软件自带 Zip 文件安装</string>
<string name="setup_backup">从 NeoTerm 备份中恢复</string>
<string name="setup_source_parameter">配置参数</string>
<string name="setup_input_source_parameter">输入配置地址</string>
<string name="setup_input_source_parameter">输入配置参数</string>
<string name="setup_hint_online">含有启动文件的软件源地址</string>
<string name="setup_hint_local">启动文件路径</string>
<string name="setup_hint_assets">不可用</string>
@ -86,6 +86,7 @@
<string name="setup_error_invalid_url">非法的 URL</string>
<string name="setup_error_no_internet">无网络连接</string>
<string name="setup_error_connection_failed">无法连接到服务器</string>
<string name="setup_error_parameter_null">配置参数为空</string>
<string name="setup_preparing">准备中…</string>
<string name="setup_confirm">最后一步</string>
<string name="setup_confirm_text">目前为止一切顺利,继续?</string>

View File

@ -30,6 +30,7 @@
<string name="setup_error_invalid_url">Invalid URL</string>
<string name="setup_error_no_internet">No Internet connection</string>
<string name="setup_error_connection_failed">Failed to connect to server</string>
<string name="setup_error_parameter_null">Setup parameter is null</string>
<string name="setup_preparing">Preparing…</string>
<string name="setup_confirm">Finish Setup</string>
<string name="setup_confirm_text">Everything goes well till now, continue?</string>