Setup: Rewritten in Kotlin

This commit is contained in:
zt515 2017-12-25 23:59:15 +08:00
parent 81887a6ed1
commit 6c5b390936
13 changed files with 158 additions and 181 deletions

View File

@ -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);
}
}

View File

@ -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)
}
}

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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();
}
}

View File

@ -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
}
}

View File

@ -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();
}
}
}
}

View File

@ -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()
}
}
}
}

View File

@ -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);
}
}

View File

@ -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)
}
}

View File

@ -140,10 +140,10 @@ class SetupActivity : AppCompatActivity(), View.OnClickListener, ResultListener
private fun createSourceConnection(id: Int, parameter: String, parameterUri: Uri?): SourceConnection { private fun createSourceConnection(id: Int, parameter: String, parameterUri: Uri?): SourceConnection {
return when (id) { 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_online -> NetworkConnection(parameter)
R.id.setup_method_assets -> AssetsFileConnection() 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!") else -> throw IllegalArgumentException("Unexpected setup method!")
} }
} }