mirror of
https://codeberg.org/timelimit/timelimit-android.git
synced 2025-10-06 03:50:23 +02:00
Add watchdog
This commit is contained in:
parent
ae8509c8b8
commit
f3a2f29aa8
6 changed files with 80 additions and 1 deletions
|
@ -72,6 +72,8 @@ abstract class PlatformIntegration(
|
|||
// this function requires the device owner permission and a recent android version
|
||||
abstract fun setForceNetworkTime(enable: Boolean)
|
||||
|
||||
abstract fun restartApp()
|
||||
|
||||
var installedAppsChangeListener: Runnable? = null
|
||||
var systemClockChangeListener: Runnable? = null
|
||||
}
|
||||
|
|
|
@ -39,17 +39,20 @@ import androidx.core.app.NotificationManagerCompat
|
|||
import androidx.lifecycle.LiveData
|
||||
import io.timelimit.android.BuildConfig
|
||||
import io.timelimit.android.R
|
||||
import io.timelimit.android.async.Threads
|
||||
import io.timelimit.android.coroutines.runAsyncExpectForever
|
||||
import io.timelimit.android.data.model.App
|
||||
import io.timelimit.android.data.model.AppActivity
|
||||
import io.timelimit.android.integration.platform.*
|
||||
import io.timelimit.android.integration.platform.android.foregroundapp.ForegroundAppHelper
|
||||
import io.timelimit.android.ui.MainActivity
|
||||
import io.timelimit.android.ui.homescreen.HomescreenActivity
|
||||
import io.timelimit.android.ui.lock.LockActivity
|
||||
import io.timelimit.android.ui.manipulation.AnnoyActivity
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.consumeEach
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
|
||||
class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectionLevel) {
|
||||
|
@ -503,4 +506,20 @@ class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectio
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun restartApp() {
|
||||
Threads.mainThreadHandler.post {
|
||||
if (lastAppStatusMessage != null) {
|
||||
LockActivity.start(context, BuildConfig.APPLICATION_ID, null)
|
||||
|
||||
if (!BackgroundService.isBackgroundActivityRestricted(context)) {
|
||||
context.startService(Intent(context, BackgroundActionService::class.java))
|
||||
}
|
||||
}
|
||||
|
||||
Threads.mainThreadHandler.post {
|
||||
exitProcess(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ class BackgroundService: Service() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun isBackgroundActivityRestricted(context: Context): Boolean {
|
||||
fun isBackgroundActivityRestricted(context: Context): Boolean {
|
||||
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
|
|
|
@ -170,4 +170,6 @@ class DummyIntegration(
|
|||
|
||||
override fun setEnableCustomHomescreen(enable: Boolean) = Unit
|
||||
override fun setForceNetworkTime(enable: Boolean) = Unit
|
||||
|
||||
override fun restartApp() = Unit
|
||||
}
|
||||
|
|
|
@ -96,6 +96,7 @@ class AppLogic(
|
|||
|
||||
init {
|
||||
SyncInstalledAppsLogic(this)
|
||||
WatchdogLogic(this)
|
||||
}
|
||||
|
||||
val manipulationLogic = ManipulationLogic(this)
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* TimeLimit Copyright <C> 2019 - 2020 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.timelimit.android.logic
|
||||
|
||||
import android.widget.Toast
|
||||
import io.timelimit.android.async.Threads
|
||||
import io.timelimit.android.coroutines.runAsyncExpectForever
|
||||
import kotlinx.coroutines.delay
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
data class WatchdogLogic(private val logic: AppLogic) {
|
||||
init {
|
||||
runAsyncExpectForever {
|
||||
val boolean = AtomicBoolean(false)
|
||||
val runnable = Runnable {
|
||||
logic.database.runInUnobservedTransaction {
|
||||
logic.database.config().getOwnDeviceIdSync()
|
||||
}
|
||||
|
||||
boolean.compareAndSet(false, true)
|
||||
}
|
||||
|
||||
while (true) {
|
||||
Threads.database.execute(runnable)
|
||||
|
||||
for (i in 1..15) {
|
||||
delay(1000)
|
||||
}
|
||||
|
||||
if (!boolean.getAndSet(false)) {
|
||||
Toast.makeText(logic.context, "TimeLimit: watchdog triggered", Toast.LENGTH_SHORT).show()
|
||||
|
||||
while (true) {
|
||||
delay(3000)
|
||||
logic.platformIntegration.restartApp()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue