diff --git a/app/src/main/java/org/obd/graphs/activity/Components.kt b/app/src/main/java/org/obd/graphs/activity/Components.kt index 0d949768..d3ba116e 100644 --- a/app/src/main/java/org/obd/graphs/activity/Components.kt +++ b/app/src/main/java/org/obd/graphs/activity/Components.kt @@ -16,11 +16,8 @@ */ package org.obd.graphs.activity -import android.view.View import android.widget.Chronometer import android.widget.ProgressBar -import android.widget.TextView -import androidx.appcompat.app.AlertDialog import androidx.navigation.NavController import androidx.navigation.fragment.NavHostFragment import com.google.android.material.bottomappbar.BottomAppBar @@ -46,21 +43,3 @@ fun MainActivity.leftAppBar(func: (p: NavigationView) -> Unit) { fun MainActivity.navController(func: (p: NavController) -> Unit) { func((supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment).navController) } - -fun MainActivity.lockScreenDialogShow(func: (dialogTitle: TextView) -> Unit) { - lockScreenDialog?.let { - if (it.isShowing) { - it.dismiss() - } - } - - AlertDialog.Builder(this).run { - setCancelable(false) - val dialogView: View = layoutInflater.inflate(R.layout.dialog_screen_lock, null) - val dialogTitle = dialogView.findViewById(R.id.dialog_screen_lock_message_id) - func(dialogTitle) - setView(dialogView) - lockScreenDialog = create() - lockScreenDialog.show() - } -} diff --git a/app/src/main/java/org/obd/graphs/activity/MainActivity.kt b/app/src/main/java/org/obd/graphs/activity/MainActivity.kt index 1a84ce16..7278aaa7 100644 --- a/app/src/main/java/org/obd/graphs/activity/MainActivity.kt +++ b/app/src/main/java/org/obd/graphs/activity/MainActivity.kt @@ -25,7 +25,6 @@ import android.os.StrictMode import android.os.StrictMode.ThreadPolicy import android.os.StrictMode.VmPolicy import android.view.View -import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.view.GravityCompat import androidx.drawerlayout.widget.DrawerLayout @@ -59,7 +58,9 @@ const val LOG_TAG = "MainActivity" class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks { - lateinit var lockScreenDialog: AlertDialog + + internal val screenLockManager = ScreenLockManager(this) + internal lateinit var backupManager: BackupManager internal var activityBroadcastReceiver = @@ -157,7 +158,7 @@ class MainActivity : it.visibility = View.GONE } - setupLockScreenDialog() + screenLockManager.setup() supportActionBar?.hide() setupMetricsProcessors() backupManager = BackupManager(this) @@ -189,6 +190,7 @@ class MainActivity : sendBroadcastEvent(MAIN_ACTIVITY_EVENT_DESTROYED) } + private fun setupExceptionHandler() { Thread.setDefaultUncaughtExceptionHandler(ExceptionHandler()) } @@ -207,16 +209,6 @@ class MainActivity : cacheManager.initCache(cache) } - private fun setupLockScreenDialog() { - AlertDialog.Builder(this).run { - setCancelable(false) - - val dialogView: View = this@MainActivity.layoutInflater.inflate(R.layout.dialog_screen_lock, null) - setView(dialogView) - lockScreenDialog = create() - } - } - private fun setupStrictMode() { if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy( diff --git a/app/src/main/java/org/obd/graphs/activity/Receivers.kt b/app/src/main/java/org/obd/graphs/activity/Receivers.kt index bbcda612..f979724d 100644 --- a/app/src/main/java/org/obd/graphs/activity/Receivers.kt +++ b/app/src/main/java/org/obd/graphs/activity/Receivers.kt @@ -144,14 +144,13 @@ internal fun MainActivity.receive(intent: Intent?) { toast(org.obd.graphs.commons.R.string.main_activity_toast_connection_wifi_incorrect_ssid) } + SCREEN_UNLOCK_PROGRESS_EVENT -> screenLockManager.dismiss() SCREEN_LOCK_PROGRESS_EVENT -> { - lockScreenDialogShow { dialogTitle -> - var msg = intent.getExtraParam() - if (msg.isEmpty()) { - msg = getText(R.string.dialog_screen_lock_message) as String - } - dialogTitle.text = msg + var msg = intent.getExtraParam() + if (msg.isEmpty()) { + msg = getText(R.string.dialog_screen_lock_message) as String } + screenLockManager.show(msg) } AA_EDIT_PREF_SCREEN -> navigateToPreferencesScreen("pref.aa") @@ -169,7 +168,7 @@ internal fun MainActivity.receive(intent: Intent?) { toast(R.string.pref_usb_device_attached, usbDevice.productName!!) } - SCREEN_UNLOCK_PROGRESS_EVENT -> lockScreenDialog.dismiss() + DATA_LOGGER_DTC_AVAILABLE -> if (Prefs.isEnabled("pref.dtc.show_notification")) { diff --git a/app/src/main/java/org/obd/graphs/activity/ScreenLockManager.kt b/app/src/main/java/org/obd/graphs/activity/ScreenLockManager.kt new file mode 100644 index 00000000..4a14f75f --- /dev/null +++ b/app/src/main/java/org/obd/graphs/activity/ScreenLockManager.kt @@ -0,0 +1,79 @@ + /** + * Copyright 2019-2026, Tomasz Żebrowski + * + *

Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + *

http://www.apache.org/licenses/LICENSE-2.0 + * + *

Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.obd.graphs.activity + +import android.app.Activity +import android.view.View +import android.widget.Button +import android.widget.TextView +import androidx.appcompat.app.AlertDialog +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import org.obd.graphs.R + +class ScreenLockManager( + private val activity: Activity, +) : DefaultLifecycleObserver { + private var lockScreenDialog: AlertDialog? = null + private var onCancelAction: (() -> Unit)? = null + + fun setup() { + AlertDialog.Builder(activity).run { + setCancelable(false) + val dialogView: View = activity.layoutInflater.inflate(R.layout.dialog_screen_lock, null) + + val cancelButton = dialogView.findViewById