Commit e8ecd39c authored by jo's avatar jo
Browse files

Don't repeat

parent af84e5ff
......@@ -4,13 +4,9 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import io.eelo.appinstaller.application.model.data.FullData
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants
import java.io.IOException
import java.lang.Exception
import java.net.SocketTimeoutException
import java.net.URL
import javax.net.ssl.HttpsURLConnection
import io.eelo.appinstaller.utils.Error
class AppDetailRequest(private val id: String) {
......@@ -20,11 +16,8 @@ class AppDetailRequest(private val id: String) {
fun request(callback: (Error?, FullData?) -> Unit) {
try {
val url = URL(Constants.BASE_URL + "apps?action=app_detail&id=$id")
val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.requestMethod = Constants.REQUEST_METHOD
urlConnection.connectTimeout = Constants.CONNECT_TIMEOUT
urlConnection.readTimeout = Constants.READ_TIMEOUT
val url = Constants.BASE_URL + "apps?action=app_detail&id=$id"
val urlConnection = Common.createConnection(url)
val result = reader.readValue<Result>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result.app)
......
......@@ -11,6 +11,7 @@ import io.eelo.appinstaller.application.model.data.FullData
import io.eelo.appinstaller.categories.model.Category
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.utils.ApplicationParser
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants
import java.io.IOException
import java.lang.Exception
......@@ -26,11 +27,8 @@ class HomeRequest {
fun request(callback: (Error?, HomeResult?) -> Unit) {
try {
val url = URL(Constants.BASE_URL + "apps?action=list_home")
val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.requestMethod = Constants.REQUEST_METHOD
urlConnection.connectTimeout = Constants.CONNECT_TIMEOUT
urlConnection.readTimeout = Constants.READ_TIMEOUT
val url = Constants.BASE_URL + "apps?action=list_home"
val urlConnection = Common.createConnection(url)
val result = reader.readValue<HomeResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
......
......@@ -5,17 +5,13 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import io.eelo.appinstaller.application.model.Application
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.application.model.data.FullData
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.utils.ApplicationParser
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants
import java.io.IOException
import java.lang.Exception
import java.net.SocketTimeoutException
import java.net.URL
import io.eelo.appinstaller.utils.Error
import java.net.URLEncoder
import javax.net.ssl.HttpsURLConnection
class ListApplicationsRequest(private val category: String, private val page: Int, private val resultsPerPage: Int) {
......@@ -25,11 +21,8 @@ class ListApplicationsRequest(private val category: String, private val page: In
fun request(callback: (Error?, ListApplicationsResult?) -> Unit) {
try {
val url = URL(Constants.BASE_URL + "apps?action=list_apps&category=${URLEncoder.encode(category, "utf-8")}&nres=$resultsPerPage&page=$page")
val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.requestMethod = Constants.REQUEST_METHOD
urlConnection.connectTimeout = Constants.CONNECT_TIMEOUT
urlConnection.readTimeout = Constants.READ_TIMEOUT
val url = Constants.BASE_URL + "apps?action=list_apps&category=${URLEncoder.encode(category, "utf-8")}&nres=$resultsPerPage&page=$page"
val urlConnection = Common.createConnection(url)
val result = reader.readValue<ListApplicationsResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
......
......@@ -3,6 +3,7 @@ package io.eelo.appinstaller.api
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.utils.Constants
import java.io.IOException
......@@ -19,11 +20,8 @@ class ListCategoriesRequest {
fun request(callback: (Error?, ListCategoriesResult?) -> Unit) {
try {
val url = URL(Constants.BASE_URL + "apps?action=list_cat")
val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.requestMethod = Constants.REQUEST_METHOD
urlConnection.connectTimeout = Constants.CONNECT_TIMEOUT
urlConnection.readTimeout = Constants.READ_TIMEOUT
val url = Constants.BASE_URL + "apps?action=list_cat"
val urlConnection = Common.createConnection(url)
val result = reader.readValue<ListCategoriesResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
......
......@@ -4,13 +4,10 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import io.eelo.appinstaller.application.model.data.BasicData
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants
import java.io.IOException
import java.net.SocketTimeoutException
import java.net.URL
import io.eelo.appinstaller.utils.Error
import java.net.URLEncoder
import javax.net.ssl.HttpsURLConnection
class PackageNameSearchRequest(private val packageName: String) {
......@@ -20,11 +17,8 @@ class PackageNameSearchRequest(private val packageName: String) {
fun request(callback: (Error?, SearchResult?) -> Unit) {
try {
val url = URL(Constants.BASE_URL + "apps?action=search&keyword=${URLEncoder.encode(packageName, "utf-8")}&by=package_name")
val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.requestMethod = Constants.REQUEST_METHOD
urlConnection.connectTimeout = Constants.CONNECT_TIMEOUT
urlConnection.readTimeout = Constants.READ_TIMEOUT
val url = Constants.BASE_URL + "apps?action=search&keyword=${URLEncoder.encode(packageName, "utf-8")}&by=package_name"
val urlConnection = Common.createConnection(url)
val result = reader.readValue<SearchResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
......
......@@ -9,6 +9,7 @@ import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.application.model.data.BasicData
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.utils.ApplicationParser
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants
import java.io.IOException
import java.lang.Exception
......@@ -25,11 +26,8 @@ class SearchRequest(private val keyword: String, private val page: Int, private
fun request(callback: (Error?, SearchResult?) -> Unit) {
try {
val url = URL(Constants.BASE_URL + "apps?action=search&keyword=${URLEncoder.encode(keyword, "utf-8")}&page=$page&nres=$resultsPerPage")
val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.requestMethod = Constants.REQUEST_METHOD
urlConnection.connectTimeout = Constants.CONNECT_TIMEOUT
urlConnection.readTimeout = Constants.READ_TIMEOUT
val url = Constants.BASE_URL + "apps?action=search&keyword=${URLEncoder.encode(keyword, "utf-8")}&page=$page&nres=$resultsPerPage"
val urlConnection = Common.createConnection(url)
val result = reader.readValue<SearchResult>(urlConnection.inputStream)
urlConnection.disconnect()
callback.invoke(null, result)
......
......@@ -3,9 +3,11 @@ package io.eelo.appinstaller.utils
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import java.net.URL
import java.util.concurrent.Executors
import javax.net.ssl.HttpsURLConnection
import kotlin.math.roundToInt
import android.net.ConnectivityManager
object Common {
......@@ -22,6 +24,14 @@ object Common {
return inMiB.times(10.0).roundToInt().div(10.0)
}
fun createConnection(url: String): HttpsURLConnection {
val connection = URL(url).openConnection() as HttpsURLConnection
connection.requestMethod = Constants.REQUEST_METHOD
connection.connectTimeout = Constants.CONNECT_TIMEOUT
connection.readTimeout = Constants.READ_TIMEOUT
return connection
}
fun isSystemApp(packageManager: PackageManager, packageName: String): Boolean {
try {
// Get package information for the app
......
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