From 72f81e5af3200ef49f2a9dca1de2ed4c0ed6606e Mon Sep 17 00:00:00 2001 From: idirin Date: Mon, 4 Jan 2021 00:00:13 +0100 Subject: [PATCH] version 3 --- .idea/compiler.xml | 3 ++ .idea/gradle.xml | 1 + .idea/misc.xml | 2 +- app/build.gradle | 10 ++-- build.gradle | 6 +++ idceptor/build.gradle | 17 +++++-- .../main/java/com/idirin/idceptor/IDCeptor.kt | 22 +-------- .../java/com/idirin/idceptor/di/ApiModule.kt | 12 ----- .../idceptor/models/network/AppInitRequest.kt | 18 +++++++ .../idceptor/models/network/PostApiRequest.kt | 21 ++++---- .../network/AlgClientIdInterceptor.kt | 10 ++-- .../idirin/idceptor/network/IdApiInterface.kt | 7 +-- .../idirin/idceptor/network/UploadHelper.kt | 49 ++++++++++--------- .../java/com/idirin/idceptor/utils/AppUtil.kt | 31 ++++++++++-- .../com/idirin/idceptor/utils/DeviceUtil.kt | 6 +-- 15 files changed, 123 insertions(+), 92 deletions(-) create mode 100644 idceptor/src/main/java/com/idirin/idceptor/models/network/AppInitRequest.kt diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 7fb671c..1af78e9 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,5 +1,8 @@ + + + diff --git a/.idea/gradle.xml b/.idea/gradle.xml index e3fa8b1..88748aa 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -15,6 +15,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index b816a97..579222b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -62,7 +62,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index ef1ef74..cd86e48 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,15 +3,15 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 29 - buildToolsVersion "29.0.2" + compileSdkVersion 30 + buildToolsVersion "30.0.2" defaultConfig { applicationId "com.idirin.idceptor" minSdkVersion 16 - targetSdkVersion 29 - versionCode 2 - versionName "0.0.2" + targetSdkVersion 30 + versionCode 3 + versionName "0.0.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/build.gradle b/build.gradle index 3aa3a03..90b0ce2 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,12 @@ allprojects { } } +ext { + targetSdkVersion = 30 + minSdkVersion = 16 + buildToolsVersion = '30.0.2' +} + task clean(type: Delete) { delete rootProject.buildDir } diff --git a/idceptor/build.gradle b/idceptor/build.gradle index 3a26dc3..ed89a7d 100644 --- a/idceptor/build.gradle +++ b/idceptor/build.gradle @@ -8,12 +8,12 @@ group='com.idirin.idceptor' version='0.0.1' android { - compileSdkVersion 29 - buildToolsVersion "29.0.2" + compileSdkVersion 30 + buildToolsVersion "30.0.2" defaultConfig { minSdkVersion 16 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName "1.0" @@ -32,6 +32,13 @@ android { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + + buildConfigField 'String', 'REST_ENDPOINT', '"https://data.lasercateyes.com/api/"' + buildConfigField 'String', 'CLIENT_ID', '"8D0B85DF-E212-4126-9C3A-AC4EEB98B0B1"' + } + debug { + buildConfigField 'String', 'REST_ENDPOINT', '"https://data-beta.lasercateyes.com/api/"' + buildConfigField 'String', 'CLIENT_ID', '"424F5699-9FBA-4161-AE79-49839455E04A"' } } @@ -49,8 +56,8 @@ dependencies { kapt "androidx.room:room-compiler:2.2.5" //Koin - implementation "org.koin:koin-core:2.0.1" - implementation "org.koin:koin-android:2.0.1" + implementation "org.koin:koin-core:2.1.1" + implementation "org.koin:koin-android:2.1.1" //Interceptor implementation "com.squareup.okhttp3:okhttp:3.11.0" diff --git a/idceptor/src/main/java/com/idirin/idceptor/IDCeptor.kt b/idceptor/src/main/java/com/idirin/idceptor/IDCeptor.kt index ded92f5..77a3e2a 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/IDCeptor.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/IDCeptor.kt @@ -15,10 +15,7 @@ import okhttp3.internal.http.HttpHeaders import okio.Buffer import okio.BufferedSource import org.koin.android.ext.koin.androidContext -import org.koin.core.context.GlobalContext -import org.koin.core.context.loadKoinModules -import org.koin.core.context.startKoin -import org.koin.core.context.unloadKoinModules +import org.koin.core.context.* import java.nio.charset.Charset import java.nio.charset.UnsupportedCharsetException import java.util.* @@ -30,7 +27,7 @@ class IDCeptor(private val application: Application, apiKey: String): Intercepto IDCeptor.apiKey = apiKey val modules = listOf(idApiModule, idDbModule, idPrefModule, idConcurrencyModule) - if (GlobalContext.getOrNull() == null) { + if (KoinContextHandler.getOrNull() == null) { startKoin { androidContext(application) modules(modules) @@ -44,24 +41,9 @@ class IDCeptor(private val application: Application, apiKey: String): Intercepto } companion object { - private val DEFAULT_RETENTION: Period = Period.ONE_WEEK private val UTF8 = Charset.forName("UTF-8") lateinit var apiKey: String - - enum class Period { - /** Retain data for the last hour. */ - ONE_HOUR, - - /** Retain data for the last day. */ - ONE_DAY, - - /** Retain data for the last week. */ - ONE_WEEK, - - /** Retain data forever. */ - FOREVER - } } private val maxContentLength = 250000L diff --git a/idceptor/src/main/java/com/idirin/idceptor/di/ApiModule.kt b/idceptor/src/main/java/com/idirin/idceptor/di/ApiModule.kt index 46dd7f1..4d0c1b5 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/di/ApiModule.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/di/ApiModule.kt @@ -7,10 +7,7 @@ import com.idirin.idceptor.IDCeptor import com.idirin.idceptor.network.AlgClientIdInterceptor import com.idirin.idceptor.network.IdApiInterface import com.idirin.idceptor.network.IdTypeAdapterDate -import com.idirin.idceptor.utils.DeviceUtil -import com.idirin.idceptor.utils.USER_AGENT import okhttp3.Cache -import okhttp3.Interceptor import okhttp3.logging.HttpLoggingInterceptor import org.koin.android.ext.koin.androidApplication import org.koin.core.module.Module @@ -34,20 +31,11 @@ inline fun getApi(application: Application, apiKey: String): T { val cacheSize: Long = 10 * 1024 * 1024 //10mb cache size val cache = Cache(application.cacheDir, cacheSize) - val userAgentInterceptor = Interceptor { chain -> - val request = chain.request() - val requestWithUserAgent = request.newBuilder() - .header(USER_AGENT, DeviceUtil.userAgent) - .build() - chain.proceed(requestWithUserAgent) - } - val okHttpClient = okhttp3.OkHttpClient.Builder() .connectTimeout(BuildConfig.CONNECTION_TIMEOUT.toLong(), TimeUnit.MILLISECONDS) .readTimeout(BuildConfig.CONNECTION_TIMEOUT.toLong(), TimeUnit.MILLISECONDS) .writeTimeout(BuildConfig.CONNECTION_UPLOAD_TIMEOUT.toLong(), TimeUnit.MILLISECONDS) .cache(cache) - .addInterceptor(userAgentInterceptor) .addInterceptor(AlgClientIdInterceptor(apiKey)) if (BuildConfig.DEBUG) { diff --git a/idceptor/src/main/java/com/idirin/idceptor/models/network/AppInitRequest.kt b/idceptor/src/main/java/com/idirin/idceptor/models/network/AppInitRequest.kt new file mode 100644 index 0000000..b9ad707 --- /dev/null +++ b/idceptor/src/main/java/com/idirin/idceptor/models/network/AppInitRequest.kt @@ -0,0 +1,18 @@ +package com.idirin.idceptor.models.network + +data class AppInitRequest( + val operatingSystem: String, + val name: String?, + val environment: String?, + val version: String, + val buildNumber: String, + val device: DeviceModel +) + +data class DeviceModel( + val name: String, + val userFriendlyName: String, + val uuid: String, + val operatingSystem: String, + val osVersion: String +) \ No newline at end of file diff --git a/idceptor/src/main/java/com/idirin/idceptor/models/network/PostApiRequest.kt b/idceptor/src/main/java/com/idirin/idceptor/models/network/PostApiRequest.kt index f699803..56eee73 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/models/network/PostApiRequest.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/models/network/PostApiRequest.kt @@ -1,25 +1,22 @@ package com.idirin.idceptor.models.network import com.idirin.idceptor.utils.AppUtil -import com.idirin.idceptor.utils.DeviceUtil import java.util.* - data class PostApiRequest( - val applicationId: String = AppUtil.appId, - val connectionId: String, - val deviceId: String = DeviceUtil.deviceId, - val requestPackage: RequestPackageModel? = null, - val responsePackage: ResponsePackageModel? = null, - val sender: String? = null, - val tags: List? = null, - val text: String? = null + val connectionId: String, + val deviceUUID: String = AppUtil.getDeviceId(), + val requestPackage: RequestPackageModel? = null, + val responsePackage: ResponsePackageModel? = null, + val sender: String? = null, + val tags: List? = null, + val text: String? = null ) data class RequestPackageModel( val id: String, val body: String? = null, - val header: String, + val headers: List, val methodType: String, val timeStamp: Date, val url: String @@ -28,7 +25,7 @@ data class RequestPackageModel( data class ResponsePackageModel( val id: String, val body: String?, - val header: String, + val headers: List, val statusCode: Int, val timeStamp: Date ) \ No newline at end of file diff --git a/idceptor/src/main/java/com/idirin/idceptor/network/AlgClientIdInterceptor.kt b/idceptor/src/main/java/com/idirin/idceptor/network/AlgClientIdInterceptor.kt index 46104ba..45c1352 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/network/AlgClientIdInterceptor.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/network/AlgClientIdInterceptor.kt @@ -1,5 +1,7 @@ package com.idirin.idceptor.network +import com.idirin.idceptor.BuildConfig +import com.idirin.idceptor.utils.AppUtil.getDeviceId import okhttp3.Interceptor import okhttp3.Response import org.koin.core.KoinComponent @@ -10,16 +12,16 @@ import java.io.IOException * idirin on 01.04.2020... */ -class AlgClientIdInterceptor(private val apiKey: String) : Interceptor, KoinComponent { - - //private const val client_secret = "424F5699-9FBA-4161-AE79-49839455E04A" +class AlgClientIdInterceptor(private val appKey: String) : Interceptor, KoinComponent { @Throws(IOException::class) override fun intercept(chain: Interceptor.Chain): Response { val original = chain.request() val request = original.newBuilder() - .addHeader("Alg-Client-Id", apiKey) + .addHeader("Alg-Client-Id", BuildConfig.CLIENT_ID) + .addHeader("Alg-App-Key", appKey) + .addHeader("Alg-Device-UUId", getDeviceId()) .method(original.method(), original.body()) .build() diff --git a/idceptor/src/main/java/com/idirin/idceptor/network/IdApiInterface.kt b/idceptor/src/main/java/com/idirin/idceptor/network/IdApiInterface.kt index f5e0a79..5460846 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/network/IdApiInterface.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/network/IdApiInterface.kt @@ -1,5 +1,6 @@ package com.idirin.idceptor.network +import com.idirin.idceptor.models.network.AppInitRequest import com.idirin.idceptor.models.network.PostApiRequest import kotlinx.coroutines.Deferred import okhttp3.ResponseBody @@ -13,12 +14,12 @@ import retrofit2.http.* interface IdApiInterface { - @POST("Data/Test") + @POST("Data/SendPackage") fun postApi(@Body request: PostApiRequest): Call - - + @PUT("App/UpdateSubApp") + fun initApp(@Body request: AppInitRequest): Call } diff --git a/idceptor/src/main/java/com/idirin/idceptor/network/UploadHelper.kt b/idceptor/src/main/java/com/idirin/idceptor/network/UploadHelper.kt index b671d27..52a4c03 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/network/UploadHelper.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/network/UploadHelper.kt @@ -1,14 +1,13 @@ package com.idirin.idceptor.network -import com.google.gson.Gson +import android.app.Application +import android.os.Build import com.idirin.idceptor.db.DbHelper import com.idirin.idceptor.models.HttpTransaction import com.idirin.idceptor.models.isRequest -import com.idirin.idceptor.models.network.PostApiRequest -import com.idirin.idceptor.models.network.RequestPackageModel -import com.idirin.idceptor.models.network.ResponsePackageModel -import com.idirin.idceptor.utils.AppUtil -import com.idirin.idceptor.utils.DeviceUtil +import com.idirin.idceptor.models.network.* +import com.idirin.idceptor.utils.* +import com.idirin.idceptor.utils.AppUtil.getDeviceId import kotlinx.coroutines.* import org.koin.core.KoinComponent import org.koin.core.inject @@ -18,19 +17,18 @@ import java.util.concurrent.atomic.AtomicBoolean object UploadHelper: KoinComponent { + private val app: Application by inject() private val api: IdApiInterface by inject() private val scope: CoroutineScope by inject() private val syncThread: ExecutorCoroutineDispatcher by inject() private var isRequesting: AtomicBoolean = AtomicBoolean(true) - private val gson by lazy { Gson() } - private const val RETRY_DELAY = 60_000L fun init() { isRequesting.set(true) - postDeviceInfo() + postAppInfo() DbHelper.clean() scope.launch(Dispatchers.Main) { @@ -67,13 +65,11 @@ object UploadHelper: KoinComponent { val uploadTime = System.currentTimeMillis() val request = PostApiRequest( connectionId = transaction.transactionId, - applicationId = AppUtil.appId, - deviceId = DeviceUtil.deviceId, requestPackage = RequestPackageModel( id = transaction.transactionId, timeStamp = transaction.requestDate!!, url = transaction.url!!, - header = gson.toJson(transaction.getRequestHeaders()), + headers = transaction.getRequestHeaders().map { "${it.name} - ${it.value}" }, body = transaction.requestBody, methodType = transaction.method!! ), @@ -85,7 +81,7 @@ object UploadHelper: KoinComponent { id = transaction.transactionId, timeStamp = transaction.responseDate!!, statusCode = transaction.responseCode!!, - header = gson.toJson(transaction.getResponseHeaders()), + headers = transaction.getResponseHeaders().map { "${it.name} - ${it.value}" }, body = transaction.responseBody ) } @@ -104,17 +100,24 @@ object UploadHelper: KoinComponent { private fun postAppInfo() { coroutine(async = { - // TODO post AppInfo - // api.postApi(request).await() - upload() - }) - } - private fun postDeviceInfo() { - coroutine(async = { - // TODO post Device Info - // api.postApi(request).await() - postAppInfo() + val request = AppInitRequest( + operatingSystem = "Android", + name = "", + environment = "", + version = getVersionName(app), + buildNumber = getVersionCode(app).toString(), + device = DeviceModel( + name = Build.MODEL, + userFriendlyName = Build.PRODUCT, + operatingSystem = "Android", + uuid = getDeviceId(), + osVersion = Build.VERSION.SDK_INT.toString() + ) + ) + + api.initApp(request).await() + upload() }) } diff --git a/idceptor/src/main/java/com/idirin/idceptor/utils/AppUtil.kt b/idceptor/src/main/java/com/idirin/idceptor/utils/AppUtil.kt index 9e700ef..bf7dd5e 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/utils/AppUtil.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/utils/AppUtil.kt @@ -1,18 +1,41 @@ package com.idirin.idceptor.utils +import android.annotation.SuppressLint +import android.app.Application +import android.content.Context +import android.content.pm.PackageInfo +import android.os.Build +import android.provider.Settings import org.koin.core.KoinComponent import org.koin.core.inject +import java.nio.charset.Charset import java.util.* - object AppUtil: KoinComponent { + private val app: Application by inject() private val sPref: SecurePref by inject() val appId = sPref.getString(PREF_DEVICE_ID, null) ?: UUID.randomUUID().toString() - - - + @SuppressLint("HardwareIds") + fun getDeviceId(): String { + val deviceId = Settings.Secure.getString(app.contentResolver, Settings.Secure.ANDROID_ID) + return UUID.nameUUIDFromBytes(deviceId.toByteArray(Charset.forName("UTF-8"))).toString() + } + +} + +fun getVersionCode(context: Context): Long { + val pInfo: PackageInfo = context.packageManager.getPackageInfo(context.packageName, 0) + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + pInfo.longVersionCode + } else { + pInfo.versionCode.toLong() + } +} + +fun getVersionName(context: Context): String { + return context.packageManager.getPackageInfo(context.packageName, 0).versionName } \ No newline at end of file diff --git a/idceptor/src/main/java/com/idirin/idceptor/utils/DeviceUtil.kt b/idceptor/src/main/java/com/idirin/idceptor/utils/DeviceUtil.kt index b3407d9..d57a332 100644 --- a/idceptor/src/main/java/com/idirin/idceptor/utils/DeviceUtil.kt +++ b/idceptor/src/main/java/com/idirin/idceptor/utils/DeviceUtil.kt @@ -23,9 +23,9 @@ object DeviceUtil: KoinComponent { val deviceId = sPref.getString(PREF_DEVICE_ID, null) ?: UUID.randomUUID().toString() - val userAgent: String - get() = ("Android/" + BuildConfig.APPLICATION_ID + "/" + BuildConfig.VERSION_CODE + "/(" - + Build.MANUFACTURER + ":" + Build.VERSION.RELEASE + ":" + Build.MODEL + ")") +// val userAgent: String +// get() = ("Android/" + BuildConfig.APPLICATION_ID + "/" + BuildConfig.VERSION_CODE + "/(" +// + Build.MANUFACTURER + ":" + Build.VERSION.RELEASE + ":" + Build.MODEL + ")") fun bytesToHuman(size: Long): String { val Kb: Long = 1024