From de2fc52392d28ef6a59c7e2eaef62b1e68f18c00 Mon Sep 17 00:00:00 2001 From: lhwdev Date: Fri, 27 Aug 2021 00:02:52 +0900 Subject: [PATCH] New feature: random time & isolated flag --- .idea/discord.xml | 9 +++ api/build.gradle | 14 ----- api/build.gradle.kts | 55 +++++++++++++++++++ app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 2 + .../com/lhwdev/selfTestMacro/DebugActivity.kt | 23 ++++++++ .../com/lhwdev/selfTestMacro/FirstActivity.kt | 2 +- .../com/lhwdev/selfTestMacro/MainActivity.kt | 15 +++++ .../lhwdev/selfTestMacro/MainApplication.kt | 2 + .../com/lhwdev/selfTestMacro/debugUtils.kt | 4 +- .../com/lhwdev/selfTestMacro/selfTestUtils.kt | 16 ++++-- .../java/com/lhwdev/selfTestMacro/utils.kt | 8 ++- app/src/main/res/layout/content_main.xml | 14 +++++ 13 files changed, 144 insertions(+), 24 deletions(-) create mode 100644 .idea/discord.xml delete mode 100644 api/build.gradle create mode 100644 api/build.gradle.kts create mode 100644 app/src/main/java/com/lhwdev/selfTestMacro/DebugActivity.kt diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 00000000..eadfa445 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/api/build.gradle b/api/build.gradle deleted file mode 100644 index cb840767..00000000 --- a/api/build.gradle +++ /dev/null @@ -1,14 +0,0 @@ -plugins { - id "kotlin" - id "kotlinx-serialization" -} - -dependencies { - implementation project(":api-base") - implementation project(":transkey") - - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2" - implementation "org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0" - implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0" -} diff --git a/api/build.gradle.kts b/api/build.gradle.kts new file mode 100644 index 00000000..62749ab0 --- /dev/null +++ b/api/build.gradle.kts @@ -0,0 +1,55 @@ +import java.util.Properties + + +plugins { + `maven-publish` + id("com.github.johnrengelman.shadow") version "7.0.0" + id("kotlin") + id("kotlinx-serialization") +} + + +publishing { + val config = Properties() + val file = rootProject.file("local.properties") + if(file.exists()) file.inputStream().use { config.load(it) } + val keyPrefix = "publishMaven.api." + + publications { + create("covid-hcs") { + groupId = "com.lhwdev.covid-hcs" + artifactId = "hcs-api" + version = "1.0.0" + + pom { + licenses { + license { + name.set("Apache License 2.0") + url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") + } + } + } + + from(components["java"]) + } + } + + repositories { + maven { + url = uri(config[keyPrefix + "url"].toString()) + credentials { + username = config[keyPrefix + "username"].toString() + password = config[keyPrefix + "password"].toString() + } + } + } +} + +dependencies { + implementation(project(":api-base")) + implementation(project(":transkey")) + + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0") +} diff --git a/app/build.gradle b/app/build.gradle index 0ad433ae..2370aac5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -14,8 +14,8 @@ android { applicationId "com.lhwdev.selfTestMacro" minSdkVersion 19 targetSdkVersion 31 - versionCode 1011 - versionName "2.11" + versionCode 1012 + versionName "2.12" multiDexEnabled true diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9731b7bd..40894af6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -33,6 +33,8 @@ + + ()!! + val error = intent.getStringExtra("error") + AlertDialog.Builder(this).setTitle("오류 발생").setMessage(error).setPositiveButton("오류 내용 복사") { _, _ -> + clipboard.setPrimaryClip(ClipData.newPlainText("오류 로그", error)) + Toast.makeText(this, "오류 로그를 복사했습니다.", Toast.LENGTH_LONG).show() + }.show() + } +} diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt b/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt index 0e857922..c53046b0 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt @@ -228,8 +228,8 @@ class FirstActivity : AppCompatActivity() { pref.firstState = 1 } } catch(e: Throwable) { + onError(e, "잘못된 학생 정보입니다.", forceShow = true) e.printStackTrace() - showToastSuspendAsync("잘못된 학생 정보입니다.") } } diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt b/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt index 46636c05..87397cf3 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt @@ -127,6 +127,21 @@ class MainActivity : AppCompatActivity() { } } + switch_random.isChecked = pref.isRandomEnabled + + switch_random.setOnCheckedChangeListener { _, isChecked -> + pref.isRandomEnabled = isChecked + update() + } + + + switch_isolation.isChecked = pref.isIsolated + + switch_isolation.setOnCheckedChangeListener { _, isChecked -> + pref.isIsolated = isChecked + } + + time.setOnClickListener { pickTime() } diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/MainApplication.kt b/app/src/main/java/com/lhwdev/selfTestMacro/MainApplication.kt index 0a28ec01..629b17bb 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/MainApplication.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/MainApplication.kt @@ -1,6 +1,8 @@ package com.lhwdev.selfTestMacro import android.app.Application +import android.content.Intent +import kotlinx.coroutines.runBlocking class MainApplication : Application() { diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/debugUtils.kt b/app/src/main/java/com/lhwdev/selfTestMacro/debugUtils.kt index a8aff1cf..947a0ce0 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/debugUtils.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/debugUtils.kt @@ -30,10 +30,10 @@ suspend inline fun Context.catchErrorThanToast(description: String = " } -suspend fun Context.onError(error: Throwable, description: String = "???") { +suspend fun Context.onError(error: Throwable, description: String = "???", forceShow: Boolean = false) { Log.e("ERROR", description, error) val info = getErrorInfo(error, description) - if(isDebugEnabled) withContext(Dispatchers.Main) { + if(forceShow || isDebugEnabled) withContext(Dispatchers.Main) { showErrorInfo(info) } writeErrorLog(info) diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/selfTestUtils.kt b/app/src/main/java/com/lhwdev/selfTestMacro/selfTestUtils.kt index 4f508afa..88435dc8 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/selfTestUtils.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/selfTestUtils.kt @@ -12,6 +12,7 @@ import net.gotev.cookiestore.InMemoryCookieStore import java.net.CookieManager import java.net.CookiePolicy import java.util.Calendar +import kotlin.random.Random fun selfTestSession(context: Context): Session { @@ -42,6 +43,8 @@ suspend fun Context.submitSuspend(session: Session, notification: Boolean = true val loginInfo: UserLoginInfo = preferenceState.user!! // (not valid ->) // note: `preferenceStte.user` may change after val user = ... + val isIsolated = preferenceState.isIsolated + // val user = loginInfo.ensureTokenValid( // session, institute, // onUpdate = { preferenceState.user = it } @@ -60,7 +63,7 @@ suspend fun Context.submitSuspend(session: Session, notification: Boolean = true val result = session.registerSurvey( preferenceState.institute!!, user, - SurveyData(userToken = user.token, upperUserName = user.name) + SurveyData(userToken = user.token, upperUserName = user.name, rspns09 = if(isIsolated) "1" else "0") ) println("selfTestMacro: submitSuspend=success") @@ -76,25 +79,30 @@ suspend fun Context.submitSuspend(session: Session, notification: Boolean = true } fun Context.updateTime(intent: PendingIntent) { + val preferenceState = preferenceState val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.cancel(intent) if(preferenceState.isSchedulingEnabled) - scheduleNextAlarm(intent, preferenceState.hour, preferenceState.min) + scheduleNextAlarm(intent, preferenceState.hour, preferenceState.min, isRandom = preferenceState.isRandomEnabled) } +private val random = Random + @SuppressLint("NewApi") fun Context.scheduleNextAlarm( intent: PendingIntent, hour: Int, min: Int, - nextDay: Boolean = false + isRandom: Boolean, + nextDay: Boolean = false, ) { (getSystemService(Context.ALARM_SERVICE) as AlarmManager).setExact( AlarmManager.RTC_WAKEUP, Calendar.getInstance().run { + val newMin = if(isRandom) (min + random.nextInt(-5, 6)).coerceIn(0, 59) else min val new = clone() as Calendar new[Calendar.HOUR_OF_DAY] = hour - new[Calendar.MINUTE] = min + new[Calendar.MINUTE] = newMin new[Calendar.SECOND] = 0 new[Calendar.MILLISECOND] = 0 if(nextDay || new <= this) new.add(Calendar.DAY_OF_YEAR, 1) diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt b/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt index cca0b5c8..93d745b7 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt @@ -7,6 +7,7 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.content.res.Resources +import android.os.Build import android.os.Handler import android.view.View import android.view.inputmethod.EditorInfo @@ -123,6 +124,9 @@ class PreferenceState(val pref: SharedPreferences) { var firstState by pref.preferenceInt("first", 0) var isSchedulingEnabled by pref.preferenceBoolean("isSchedulingEnabled", false) + var isRandomEnabled by pref.preferenceBoolean("isRandomEnabled", false) + var isIsolated by pref.preferenceBoolean("isIsolated", false) + var hour by pref.preferenceInt("hour", -1) var min by pref.preferenceInt("min", 0) @@ -133,6 +137,8 @@ class PreferenceState(val pref: SharedPreferences) { var setting: UserSetting? by pref.preferenceSerialized("userSetting", UserSetting.serializer()) + var lastQuestion: String? by pref.preferenceString("lastQuestion") + var shownNotices: Set get() = pref.getStringSet("shownNotices", setOf())!! set(value) = pref.edit { @@ -230,7 +236,7 @@ fun Context.prefMain() = getSharedPreferences("main", AppCompatActivity.MODE_PRI fun Context.createIntent() = PendingIntent.getBroadcast( this, AlarmReceiver.REQUEST_CODE, Intent(this, AlarmReceiver::class.java), - PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_UPDATE_CURRENT or (if(Build.VERSION.SDK_INT >= 31) PendingIntent.FLAG_IMMUTABLE else 0) ) fun Int.toPx() = (this * Resources.getSystem().displayMetrics.density).toInt() diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 5a0ca083..e031766f 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -26,6 +26,20 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> + + + +