From cb12df3ee85257c0edf60c365346120ccd8904de Mon Sep 17 00:00:00 2001 From: zt515 Date: Mon, 18 Dec 2017 00:09:22 +0800 Subject: [PATCH] Setup: More friendly --- app/src/main/AndroidManifest.xml | 2 +- .../neoterm/ui/pm/PackageManagerActivity.kt | 10 +-- .../ui/settings/GeneralSettingsActivity.kt | 2 +- .../java/io/neoterm/ui/setup/SetupActivity.kt | 85 +++++++++++-------- .../java/io/neoterm/utils/PackageUtils.kt | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 6 ++ app/src/main/res/values/strings.xml | 8 +- app/src/main/res/xml/settings_main.xml | 2 +- 8 files changed, 72 insertions(+), 45 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f7780f8..a51eb45 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -133,7 +133,7 @@ android:label="@string/error" android:theme="@style/AppTheme.NoActionBar.Dark" /> + PackageUtils.apt(this, "update", null, { exitStatus, dialog -> if (exitStatus != 0) { dialog.setTitle(getString(R.string.error)) - return@executeApt + return@apt } Toast.makeText(this, R.string.apt_update_ok, Toast.LENGTH_SHORT).show() dialog.dismiss() @@ -171,14 +171,14 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen } private fun executeAptUpgrade() { - PackageUtils.executeApt(this, "update", null, { exitStatus, dialog -> + PackageUtils.apt(this, "update", null, { exitStatus, dialog -> if (exitStatus != 0) { dialog.setTitle(getString(R.string.error)) - return@executeApt + return@apt } dialog.dismiss() - PackageUtils.executeApt(this, "upgrade", arrayOf("-y"), out@ { exitStatus, dialog -> + PackageUtils.apt(this, "upgrade", arrayOf("-y"), out@ { exitStatus, dialog -> if (exitStatus != 0) { dialog.setTitle(getString(R.string.error)) return@out diff --git a/app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt b/app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt index 6aa4048..b035a83 100644 --- a/app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt +++ b/app/src/main/java/io/neoterm/ui/settings/GeneralSettingsActivity.kt @@ -40,7 +40,7 @@ class GeneralSettingsActivity : BasePreferenceActivity() { .setTitle(getString(R.string.shell_not_found, shellName)) .setMessage(R.string.shell_not_found_message) .setPositiveButton(R.string.install, { _, _ -> - PackageUtils.executeApt(this, "install", arrayOf("-y", shellName), { exitStatus, dialog -> + PackageUtils.apt(this, "install", arrayOf("-y", shellName), { exitStatus, dialog -> if (exitStatus == 0) { dialog.dismiss() postChangeShell(shellName) diff --git a/app/src/main/java/io/neoterm/ui/setup/SetupActivity.kt b/app/src/main/java/io/neoterm/ui/setup/SetupActivity.kt index 5e0d6e4..33b2a2a 100644 --- a/app/src/main/java/io/neoterm/ui/setup/SetupActivity.kt +++ b/app/src/main/java/io/neoterm/ui/setup/SetupActivity.kt @@ -5,7 +5,9 @@ import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.View import android.widget.* +import io.neoterm.App import io.neoterm.R +import io.neoterm.component.setup.ResultListener import io.neoterm.component.setup.SetupHelper import io.neoterm.component.setup.SourceConnection import io.neoterm.component.setup.connection.AssetsFileConnection @@ -16,12 +18,13 @@ import io.neoterm.component.setup.helper.URLAvailability import io.neoterm.frontend.config.NeoTermPath import io.neoterm.utils.PackageUtils import java.io.File +import java.lang.Exception /** * @author kiva */ -class SetupActivity : AppCompatActivity(), View.OnClickListener { +class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener { private var aptUpdated = false private var setupParameter = "" @@ -38,6 +41,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener { setContentView(R.layout.ui_setup) val parameterEditor = findViewById(R.id.setup_source_parameter) + val tipText = findViewById(R.id.setup_url_tip_text) val onCheckedChangeListener = CompoundButton.OnCheckedChangeListener { button, checked -> if (checked) { @@ -48,6 +52,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener { return@OnCheckedChangeListener } parameterEditor.setHint(hintMapping[index + 1]) + tipText.setText(hintMapping[index + 1]) setDefaultValue(parameterEditor, id) } } @@ -70,11 +75,11 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener { Thread { val errorMessage = validateParameter(id, setupParameter) - SetupActivity@this.runOnUiThread { + SetupActivity@ this.runOnUiThread { dialog.dismiss() editor.error = errorMessage if (errorMessage != null) { - AlertDialog.Builder(SetupActivity@this) + AlertDialog.Builder(SetupActivity@ this) .setMessage(errorMessage) .setPositiveButton(android.R.string.yes, null) .show() @@ -82,7 +87,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener { } val connection = createSourceConnection(id, setupParameter) - setup(connection) + showConfirmDialog(connection) } }.start() } @@ -93,7 +98,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener { R.id.setup_method_online -> NetworkConnection(parameter) R.id.setup_method_assets -> AssetsFileConnection() R.id.setup_method_backup -> BackupFileConnection(parameter) - else -> throw IllegalArgumentException("Unexpected setup method!") + else -> throw IllegalArgumentException("Unexpected showConfirmDialog method!") } } @@ -131,37 +136,49 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener { parameterEditor.setText(setupParameter) } - private fun setup(connection: SourceConnection) { - // TODO: Refactor -// var resultListener: SetupHelper.ResultListener? = null -// resultListener = SetupHelper.ResultListener { error -> -// if (error == null) { -// setResult(Activity.RESULT_OK) -// PackageUtils.syncSource() -// executeAptUpdate() -// } else { -// AlertDialog.Builder(this@SetupActivity) -// .setTitle(R.string.error) -// .setMessage(error.toString()) -// .setNegativeButton(R.string.use_system_shell, { _, _ -> -// setResult(Activity.RESULT_CANCELED) -// finish() -// }) -// .setPositiveButton(R.string.retry, { dialog, _ -> -// dialog.dismiss() -// SetupHelper.setup(this@SetupActivity, resultListener) -// }) -// .setNeutralButton(R.string.show_help, { _, _ -> -// App.get().openHelpLink() -// }) -// .show() -// } -// } -// SetupHelper.setup(this, resultListener) + private fun showConfirmDialog(connection: SourceConnection) { + val needSetup = !SetupHelper.needSetup() + val titleId = if (needSetup) R.string.setup_confirm else R.string.setup_reset_confirm + val messageId = if (needSetup) R.string.setup_confirm_text else R.string.setup_reset_confirm_text + + AlertDialog.Builder(this) + .setTitle(titleId) + .setMessage(messageId) + .setPositiveButton(android.R.string.yes, { _, _ -> + doSetup(connection) + }) + .setNegativeButton(android.R.string.no, null) + .show() + } + + private fun doSetup(connection: SourceConnection) { + SetupHelper.setup(this@SetupActivity, connection, this) + } + + override fun onResult(error: Exception?) { + if (error == null) { + setResult(RESULT_OK) + PackageUtils.syncSource() + executeAptUpdate() + + } else { + AlertDialog.Builder(this@SetupActivity) + .setTitle(R.string.error) + .setMessage(error.toString()) + .setNegativeButton(R.string.use_system_shell, { _, _ -> + setResult(RESULT_CANCELED) + finish() + }) + .setNeutralButton(R.string.show_help, { _, _ -> + App.get().openHelpLink() + }) + .setPositiveButton(android.R.string.yes, null) + .show() + } } private fun executeAptUpdate() { - PackageUtils.executeApt(this, "update", null, { exitStatus, dialog -> + PackageUtils.apt(this, "update", null, { exitStatus, dialog -> if (exitStatus == 0) { dialog.dismiss() aptUpdated = true @@ -173,7 +190,7 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener { } private fun executeAptUpgrade() { - PackageUtils.executeApt(this, "upgrade", arrayOf("-y"), { exitStatus, dialog -> + PackageUtils.apt(this, "upgrade", arrayOf("-y"), { exitStatus, dialog -> if (exitStatus == 0) { dialog.dismiss() finish() diff --git a/app/src/main/java/io/neoterm/utils/PackageUtils.kt b/app/src/main/java/io/neoterm/utils/PackageUtils.kt index 324af17..413c081 100644 --- a/app/src/main/java/io/neoterm/utils/PackageUtils.kt +++ b/app/src/main/java/io/neoterm/utils/PackageUtils.kt @@ -27,7 +27,7 @@ object PackageUtils { .toString() } - fun executeApt(context: Context, subCommand: String, extraArgs: Array?, callback: (Int, TerminalDialog) -> Unit) { + fun apt(context: Context, subCommand: String, extraArgs: Array?, callback: (Int, TerminalDialog) -> Unit) { val argArray = if (extraArgs != null) arrayOf(NeoTermPath.APT_BIN_PATH, subCommand, *extraArgs) else arrayOf(NeoTermPath.APT_BIN_PATH, subCommand) diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index d826021..142475c 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -66,6 +66,7 @@ 软件源,更新,升级 安装字体 安装配色方案 + 你好,NeoTerm 开始! 选择你的安装方式 @@ -84,6 +85,11 @@ 无网络连接 无法连接到服务器 准备中… + 最后一步 + 目前为止一切顺利,继续? + 重置确认 + 这个操作会清空你的所有数据,是否继续? + 设备: %s 应用: %s 堆栈信息 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b795ae3..85d8b4f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -22,8 +22,8 @@ Source Parameter Input source parameter… - URL of mirror that contains setup zip files - File path to setup zip files + URL of mirror that contains showConfirmDialog zip files + File path to showConfirmDialog zip files Not available File path to backup file(*.neobackup) File not found @@ -31,6 +31,10 @@ No Internet connection Failed to connect to server Preparing… + Finish Setup + Everything goes well till now, continue? + Reset Confirm + This operation will clear all your data! Continue? About Settings diff --git a/app/src/main/res/xml/settings_main.xml b/app/src/main/res/xml/settings_main.xml index 0099055..36071f2 100644 --- a/app/src/main/res/xml/settings_main.xml +++ b/app/src/main/res/xml/settings_main.xml @@ -43,7 +43,7 @@ android:key="@string/discovery" android:title="@string/discovery"> -->