Commit f50c6d46 authored by jo's avatar jo
Browse files

Don't repeat same things

parent 89dc87e2
......@@ -28,12 +28,8 @@ class AppDetailRequest(private val id: String) {
val result = reader.readValue<Result>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result.app)
} catch (e: SocketTimeoutException) {
callback.invoke(Error.REQUEST_TIMEOUT, null)
} catch (e: IOException) {
callback.invoke(Error.SERVER_UNAVAILABLE, null)
} catch (e: Exception) {
callback.invoke(Error.UNKNOWN, null)
callback.invoke(Error.findError(e), null)
}
}
......
......@@ -34,12 +34,8 @@ class HomeRequest {
val result = reader.readValue<HomeResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
} catch (e: SocketTimeoutException) {
callback.invoke(Error.REQUEST_TIMEOUT, null)
} catch (e: IOException) {
callback.invoke(Error.SERVER_UNAVAILABLE, null)
} catch (e: Exception) {
callback.invoke(Error.UNKNOWN, null)
callback.invoke(Error.findError(e), null)
}
}
......
......@@ -33,12 +33,8 @@ class ListApplicationsRequest(private val category: String, private val page: In
val result = reader.readValue<ListApplicationsResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
} catch (e: SocketTimeoutException) {
callback.invoke(Error.REQUEST_TIMEOUT, null)
} catch (e: IOException) {
callback.invoke(Error.SERVER_UNAVAILABLE, null)
} catch (e: Exception) {
callback.invoke(Error.UNKNOWN, null)
callback.invoke(Error.findError(e), null)
}
}
......
......@@ -27,12 +27,8 @@ class ListCategoriesRequest {
val result = reader.readValue<ListCategoriesResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
} catch (e: SocketTimeoutException) {
callback.invoke(Error.REQUEST_TIMEOUT, null)
} catch (e: IOException) {
callback.invoke(Error.SERVER_UNAVAILABLE, null)
} catch (e: Exception) {
callback.invoke(Error.UNKNOWN, null)
callback.invoke(Error.findError(e), null)
}
}
......
......@@ -28,12 +28,8 @@ class PackageNameSearchRequest(private val packageName: String) {
val result = reader.readValue<SearchResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
} catch (e: SocketTimeoutException) {
callback.invoke(Error.REQUEST_TIMEOUT, null)
} catch (e: IOException) {
callback.invoke(Error.SERVER_UNAVAILABLE, null)
} catch (e: Exception) {
callback.invoke(Error.UNKNOWN, null)
callback.invoke(Error.findError(e), null)
}
}
......
......@@ -33,12 +33,8 @@ class SearchRequest(private val keyword: String, private val page: Int, private
val result = reader.readValue<SearchResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
} catch (e: SocketTimeoutException) {
callback.invoke(Error.REQUEST_TIMEOUT, null)
} catch (e: IOException) {
callback.invoke(Error.SERVER_UNAVAILABLE, null)
} catch (e: Exception) {
callback.invoke(Error.UNKNOWN, null)
callback.invoke(Error.findError(e), null)
}
}
......
......@@ -3,7 +3,6 @@ package io.eelo.appinstaller.utils
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import io.eelo.appinstaller.R
import java.util.concurrent.Executors
import kotlin.math.roundToInt
import android.net.ConnectivityManager
......@@ -23,24 +22,7 @@ object Common {
return inMiB.times(10.0).roundToInt().div(10.0)
}
fun getScreenErrorDescriptionId(screenError: Error) = when (screenError) {
Error.NO_INTERNET ->
R.string.error_no_internet
Error.SERVER_UNAVAILABLE ->
R.string.error_server_unavailable
Error.REQUEST_TIMEOUT ->
R.string.error_request_timeout
Error.UNKNOWN ->
R.string.error_unknown
Error.NO_RESULTS ->
R.string.error_no_results
Error.APK_UNAVAILABLE ->
R.string.error_apk_unavailable
Error.APK_CORRUPT ->
R.string.error_apk_corrupt
Error.INSTALL_FAILED ->
R.string.error_install_failed
}
fun getScreenErrorDescriptionId(screenError: Error) = screenError.description
fun isSystemApp(packageManager: PackageManager, packageName: String): Boolean {
try {
......
package io.eelo.appinstaller.utils
enum class Error {
NO_RESULTS,
NO_INTERNET,
SERVER_UNAVAILABLE,
REQUEST_TIMEOUT,
UNKNOWN,
APK_UNAVAILABLE,
APK_CORRUPT,
INSTALL_FAILED
import io.eelo.appinstaller.R
import java.io.IOException
import java.net.SocketTimeoutException
enum class Error(val description: Int) {
NO_RESULTS(R.string.error_no_results),
NO_INTERNET(R.string.error_no_internet),
SERVER_UNAVAILABLE(R.string.error_server_unavailable),
REQUEST_TIMEOUT(R.string.error_request_timeout),
UNKNOWN(R.string.error_unknown),
APK_UNAVAILABLE(R.string.error_apk_unavailable),
APK_CORRUPT(R.string.error_apk_corrupt),
INSTALL_FAILED(R.string.error_install_failed);
companion object {
fun findError(e: Exception): Error {
return when (e::class) {
CustomException::class -> (e as CustomException).error
SocketTimeoutException::class -> return REQUEST_TIMEOUT
IOException::class -> return SERVER_UNAVAILABLE
else -> UNKNOWN
}
}
}
class CustomException(val error: Error) : Exception(error.name.toLowerCase())
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment