From 6c5b39093684c8bb0584463c788f776a2e5d407a Mon Sep 17 00:00:00 2001 From: zt515 Date: Mon, 25 Dec 2017 23:59:15 +0800 Subject: [PATCH] Setup: Rewritten in Kotlin --- .../connection/AssetsFileConnection.java | 21 -------- .../setup/connection/AssetsFileConnection.kt | 21 ++++++++ .../connection/BackupFileConnection.java | 15 ------ .../setup/connection/BackupFileConnection.kt | 10 ++++ .../setup/connection/LocalFileConnection.java | 15 ------ .../setup/connection/LocalFileConnection.kt | 10 ++++ .../setup/connection/NetworkConnection.java | 54 ------------------- .../setup/connection/NetworkConnection.kt | 47 ++++++++++++++++ .../setup/connection/OfflineConnection.java | 48 ----------------- .../setup/connection/OfflineConnection.kt | 49 +++++++++++++++++ .../connection/OfflineUriConnection.java | 26 --------- .../setup/connection/OfflineUriConnection.kt | 19 +++++++ .../java/io/neoterm/ui/setup/SetupActivity.kt | 4 +- 13 files changed, 158 insertions(+), 181 deletions(-) delete mode 100644 app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.java create mode 100644 app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.kt delete mode 100644 app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.java create mode 100644 app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.kt delete mode 100644 app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.java create mode 100644 app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.kt delete mode 100644 app/src/main/java/io/neoterm/setup/connection/NetworkConnection.java create mode 100644 app/src/main/java/io/neoterm/setup/connection/NetworkConnection.kt delete mode 100644 app/src/main/java/io/neoterm/setup/connection/OfflineConnection.java create mode 100644 app/src/main/java/io/neoterm/setup/connection/OfflineConnection.kt delete mode 100644 app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.java create mode 100644 app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.kt diff --git a/app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.java b/app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.java deleted file mode 100644 index dddeeae..0000000 --- a/app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.java +++ /dev/null @@ -1,21 +0,0 @@ -package io.neoterm.setup.connection; - -import java.io.IOException; -import java.io.InputStream; - -import io.neoterm.App; -import io.neoterm.setup.SetupHelper; -import io.neoterm.utils.AssetsUtils; - -/** - * @author kiva - */ - -public class AssetsFileConnection extends OfflineConnection { - @Override - protected InputStream openInputStream() throws IOException { - String arch = SetupHelper.INSTANCE.determineArchName(); - String fileName = "offline_setup/" + arch + ".zip"; - return AssetsUtils.INSTANCE.openAssetsFile(App.Companion.get(), fileName); - } -} diff --git a/app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.kt b/app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.kt new file mode 100644 index 0000000..8ab191b --- /dev/null +++ b/app/src/main/java/io/neoterm/setup/connection/AssetsFileConnection.kt @@ -0,0 +1,21 @@ +package io.neoterm.setup.connection + +import java.io.IOException +import java.io.InputStream + +import io.neoterm.App +import io.neoterm.setup.SetupHelper +import io.neoterm.utils.AssetsUtils + +/** + * @author kiva + */ + +class AssetsFileConnection : OfflineConnection() { + @Throws(IOException::class) + override fun openInputStream(): InputStream { + val arch = SetupHelper.determineArchName() + val fileName = "offline_setup/$arch.zip" + return AssetsUtils.openAssetsFile(App.get(), fileName) + } +} diff --git a/app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.java b/app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.java deleted file mode 100644 index d05030f..0000000 --- a/app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.neoterm.setup.connection; - -import android.content.Context; -import android.net.Uri; - -/** - * @author kiva - */ - -public class BackupFileConnection extends OfflineUriConnection { - - public BackupFileConnection(Context context, Uri uri) { - super(context, uri); - } -} diff --git a/app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.kt b/app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.kt new file mode 100644 index 0000000..063a70b --- /dev/null +++ b/app/src/main/java/io/neoterm/setup/connection/BackupFileConnection.kt @@ -0,0 +1,10 @@ +package io.neoterm.setup.connection + +import android.content.Context +import android.net.Uri + +/** + * @author kiva + */ + +class BackupFileConnection(context: Context, uri: Uri) : OfflineUriConnection(context, uri) diff --git a/app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.java b/app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.java deleted file mode 100644 index a95a7fc..0000000 --- a/app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.neoterm.setup.connection; - -import android.content.Context; -import android.net.Uri; - -/** - * @author kiva - */ - -public class LocalFileConnection extends OfflineUriConnection { - - public LocalFileConnection(Context context, Uri uri) { - super(context, uri); - } -} diff --git a/app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.kt b/app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.kt new file mode 100644 index 0000000..172e89b --- /dev/null +++ b/app/src/main/java/io/neoterm/setup/connection/LocalFileConnection.kt @@ -0,0 +1,10 @@ +package io.neoterm.setup.connection + +import android.content.Context +import android.net.Uri + +/** + * @author kiva + */ + +class LocalFileConnection(context: Context, uri: Uri) : OfflineUriConnection(context, uri) diff --git a/app/src/main/java/io/neoterm/setup/connection/NetworkConnection.java b/app/src/main/java/io/neoterm/setup/connection/NetworkConnection.java deleted file mode 100644 index 1f528a2..0000000 --- a/app/src/main/java/io/neoterm/setup/connection/NetworkConnection.java +++ /dev/null @@ -1,54 +0,0 @@ -package io.neoterm.setup.connection; - -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; - -import io.neoterm.setup.SetupHelper; -import io.neoterm.setup.SourceConnection; - -/** - * @author kiva - */ - -public class NetworkConnection implements SourceConnection { - private final String sourceUrl; - private HttpURLConnection connection = null; - - public NetworkConnection(String sourceUrl) { - this.sourceUrl = sourceUrl; - } - - @Override - public InputStream getInputStream() throws IOException { - if (connection == null) { - connection = openHttpConnection(); - connection.setConnectTimeout(8000); - connection.setReadTimeout(8000); - } - return connection.getInputStream(); - } - - @Override - public int getSize() { - if (connection != null) { - return connection.getContentLength(); - } - - return 0; - } - - @Override - public void close() { - if (connection != null) { - connection.disconnect(); - } - } - - private HttpURLConnection openHttpConnection() throws IOException { - String arch = SetupHelper.INSTANCE.determineArchName(); - - return (HttpURLConnection) new URL(sourceUrl + "/boot/" + arch + ".zip").openConnection(); - } -} diff --git a/app/src/main/java/io/neoterm/setup/connection/NetworkConnection.kt b/app/src/main/java/io/neoterm/setup/connection/NetworkConnection.kt new file mode 100644 index 0000000..110b188 --- /dev/null +++ b/app/src/main/java/io/neoterm/setup/connection/NetworkConnection.kt @@ -0,0 +1,47 @@ +package io.neoterm.setup.connection + +import java.io.IOException +import java.io.InputStream +import java.net.HttpURLConnection +import java.net.URL + +import io.neoterm.setup.SetupHelper +import io.neoterm.setup.SourceConnection + +/** + * @author kiva + */ + +class NetworkConnection(private val sourceUrl: String) : SourceConnection { + private var connection: HttpURLConnection? = null + + @Throws(IOException::class) + override fun getInputStream(): InputStream { + if (connection == null) { + connection = openHttpConnection() + connection!!.connectTimeout = 8000 + connection!!.readTimeout = 8000 + } + return connection!!.inputStream + } + + override fun getSize(): Int { + return if (connection != null) { + connection!!.contentLength + } else 0 + + } + + override fun close() { + if (connection != null) { + connection!!.disconnect() + } + } + + @Throws(IOException::class) + private fun openHttpConnection(): HttpURLConnection { + val arch = SetupHelper.determineArchName() + + return URL("$sourceUrl/boot/$arch.zip").openConnection() as HttpURLConnection + } +} diff --git a/app/src/main/java/io/neoterm/setup/connection/OfflineConnection.java b/app/src/main/java/io/neoterm/setup/connection/OfflineConnection.java deleted file mode 100644 index 09caad2..0000000 --- a/app/src/main/java/io/neoterm/setup/connection/OfflineConnection.java +++ /dev/null @@ -1,48 +0,0 @@ -package io.neoterm.setup.connection; - -import java.io.IOException; -import java.io.InputStream; - -import io.neoterm.setup.SourceConnection; - -/** - * @author kiva - */ - -public abstract class OfflineConnection implements SourceConnection { - private InputStream inputStream; - - protected abstract InputStream openInputStream() throws IOException; - - @Override - public InputStream getInputStream() throws IOException { - if (inputStream == null) { - inputStream = openInputStream(); - } - return inputStream; - } - - @Override - public int getSize() { - if (inputStream != null) { - try { - return inputStream.available(); - } catch (IOException e) { - e.printStackTrace(); - return 0; - } - } - return 0; - } - - @Override - public void close() { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException ignore) { - ignore.printStackTrace(); - } - } - } -} diff --git a/app/src/main/java/io/neoterm/setup/connection/OfflineConnection.kt b/app/src/main/java/io/neoterm/setup/connection/OfflineConnection.kt new file mode 100644 index 0000000..916c385 --- /dev/null +++ b/app/src/main/java/io/neoterm/setup/connection/OfflineConnection.kt @@ -0,0 +1,49 @@ +package io.neoterm.setup.connection + +import java.io.IOException +import java.io.InputStream + +import io.neoterm.setup.SourceConnection + +/** + * @author kiva + */ + +abstract class OfflineConnection : SourceConnection { + private var inputStream: InputStream? = null + + @Throws(IOException::class) + protected abstract fun openInputStream(): InputStream + + @Throws(IOException::class) + override fun getInputStream(): InputStream { + if (inputStream == null) { + inputStream = openInputStream() + } + return inputStream!! + } + + override fun getSize(): Int { + if (inputStream != null) { + try { + return inputStream!!.available() + } catch (e: IOException) { + e.printStackTrace() + return 0 + } + + } + return 0 + } + + override fun close() { + if (inputStream != null) { + try { + inputStream!!.close() + } catch (ignore: IOException) { + ignore.printStackTrace() + } + + } + } +} diff --git a/app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.java b/app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.java deleted file mode 100644 index 524360e..0000000 --- a/app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.neoterm.setup.connection; - -import android.content.Context; -import android.net.Uri; - -import java.io.IOException; -import java.io.InputStream; - -/** - * @author kiva - */ - -public class OfflineUriConnection extends OfflineConnection { - private final Context context; - private final Uri uri; - - public OfflineUriConnection(Context context, Uri uri) { - this.context = context; - this.uri = uri; - } - - @Override - protected InputStream openInputStream() throws IOException { - return context.getContentResolver().openInputStream(uri); - } -} diff --git a/app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.kt b/app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.kt new file mode 100644 index 0000000..0bb7813 --- /dev/null +++ b/app/src/main/java/io/neoterm/setup/connection/OfflineUriConnection.kt @@ -0,0 +1,19 @@ +package io.neoterm.setup.connection + +import android.content.Context +import android.net.Uri + +import java.io.IOException +import java.io.InputStream + +/** + * @author kiva + */ + +open class OfflineUriConnection(private val context: Context, private val uri: Uri) : OfflineConnection() { + + @Throws(IOException::class) + override fun openInputStream(): InputStream { + return context.contentResolver.openInputStream(uri) + } +} 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 2f7b9de..8637930 100644 --- a/app/src/main/java/io/neoterm/ui/setup/SetupActivity.kt +++ b/app/src/main/java/io/neoterm/ui/setup/SetupActivity.kt @@ -140,10 +140,10 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener private fun createSourceConnection(id: Int, parameter: String, parameterUri: Uri?): SourceConnection { return when (id) { - R.id.setup_method_local -> LocalFileConnection(this, parameterUri) + R.id.setup_method_local -> LocalFileConnection(this, parameterUri!!) R.id.setup_method_online -> NetworkConnection(parameter) R.id.setup_method_assets -> AssetsFileConnection() - R.id.setup_method_backup -> BackupFileConnection(this, parameterUri) + R.id.setup_method_backup -> BackupFileConnection(this, parameterUri!!) else -> throw IllegalArgumentException("Unexpected setup method!") } }