Skip to content

Commit

Permalink
New feature: random time & isolated flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lhwdev committed Aug 26, 2021
1 parent 80e7397 commit de2fc52
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 24 deletions.
9 changes: 9 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions api/build.gradle

This file was deleted.

55 changes: 55 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<MavenPublication>("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")
}
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<activity android:name=".FirstActivity"
android:theme="@style/AppTheme.Dialog" />

<activity android:name=".DebugActivity" />

<receiver android:name=".AlarmReceiver" />

<receiver
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/lhwdev/selfTestMacro/DebugActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.lhwdev.selfTestMacro

import android.app.Activity
import android.app.AlertDialog
import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import android.widget.Toast
import androidx.core.content.getSystemService


class DebugActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val clipboard = getSystemService<ClipboardManager>()!!
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ class FirstActivity : AppCompatActivity() {
pref.firstState = 1
}
} catch(e: Throwable) {
onError(e, "잘못된 학생 정보입니다.", forceShow = true)
e.printStackTrace()
showToastSuspendAsync("잘못된 학생 정보입니다.")
}

}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/lhwdev/selfTestMacro/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.lhwdev.selfTestMacro

import android.app.Application
import android.content.Intent
import kotlinx.coroutines.runBlocking


class MainApplication : Application() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lhwdev/selfTestMacro/debugUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ suspend inline fun <R : Any> 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)
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/com/lhwdev/selfTestMacro/selfTestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }
Expand All @@ -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")
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/lhwdev/selfTestMacro/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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<String>
get() = pref.getStringSet("shownNotices", setOf())!!
set(value) = pref.edit {
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Switch
android:id="@+id/switch_random"
android:padding="12dp"
android:text="렌덤 시간(설정한 시간 ±5분)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Switch
android:id="@+id/switch_isolation"
android:padding="12dp"
android:text="자가격리자 여부"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:id="@+id/time"
Expand Down

0 comments on commit de2fc52

Please sign in to comment.