Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions app/src/main/java/org/obd/graphs/activity/Components.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<TextView>(R.id.dialog_screen_lock_message_id)
func(dialogTitle)
setView(dialogView)
lockScreenDialog = create()
lockScreenDialog.show()
}
}
18 changes: 5 additions & 13 deletions app/src/main/java/org/obd/graphs/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -157,7 +158,7 @@ class MainActivity :
it.visibility = View.GONE
}

setupLockScreenDialog()
screenLockManager.setup()
supportActionBar?.hide()
setupMetricsProcessors()
backupManager = BackupManager(this)
Expand Down Expand Up @@ -189,6 +190,7 @@ class MainActivity :
sendBroadcastEvent(MAIN_ACTIVITY_EVENT_DESTROYED)
}


private fun setupExceptionHandler() {
Thread.setDefaultUncaughtExceptionHandler(ExceptionHandler())
}
Expand All @@ -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(
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/org/obd/graphs/activity/Receivers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")) {
Expand Down
79 changes: 79 additions & 0 deletions app/src/main/java/org/obd/graphs/activity/ScreenLockManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright 2019-2026, Tomasz Żebrowski
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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<Button>(R.id.dialog_screen_lock_cancel_btn)
cancelButton.setOnClickListener {
dismiss()
onCancelAction?.invoke()
}

setView(dialogView)
lockScreenDialog = create()
}
}

fun show(
message: String,
onCancel: (() -> Unit)? = null,
) {
this.onCancelAction = onCancel

lockScreenDialog?.let { dialog ->
val dialogTitle = dialog.findViewById<TextView>(R.id.dialog_screen_lock_message_id)
if (dialogTitle != null && message.isNotEmpty()) {
dialogTitle.text = message
}
if (!dialog.isShowing) {
dialog.show()
}
}
}

fun dismiss() {
if (lockScreenDialog?.isShowing == true) {
lockScreenDialog?.dismiss()
}
onCancelAction = null
}

override fun onDestroy(owner: LifecycleOwner) {
dismiss()
lockScreenDialog = null
super.onDestroy(owner)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ package org.obd.graphs.preferences.dtc

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ProgressBar
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.obd.graphs.R
import org.obd.graphs.SCREEN_LOCK_PROGRESS_EVENT
import org.obd.graphs.SCREEN_UNLOCK_PROGRESS_EVENT
import org.obd.graphs.bl.datalogger.DATA_LOGGER_DTC_ACTION_COMPLETED
import org.obd.graphs.bl.datalogger.DataLoggerRepository
import org.obd.graphs.bl.datalogger.VehicleCapabilitiesManager
import org.obd.graphs.bl.datalogger.dataLoggerSettings
import org.obd.graphs.preferences.CoreDialogFragment
import org.obd.graphs.registerReceiver
import org.obd.graphs.sendBroadcastEvent
import org.obd.graphs.ui.common.toast
import org.obd.graphs.ui.withDataLogger
import org.obd.metrics.api.model.DiagnosticTroubleCode
Expand All @@ -41,9 +47,20 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
private lateinit var clearButton: Button
private lateinit var refreshButton: Button
private lateinit var shareButton: Button
private lateinit var progressBar: ProgressBar
private lateinit var recyclerView: RecyclerView

private val handler = Handler(Looper.getMainLooper())
private val timeoutRunnable =
Runnable {

setLoadingState(false)

if (isAdded) {
setLoadingState(false)
Toast.makeText(requireContext(), "Timeout waiting for vehicle response", Toast.LENGTH_SHORT).show()
}
}

private val dtcNotificationsReceiver =
object : android.content.BroadcastReceiver() {
override fun onReceive(
Expand All @@ -66,7 +83,6 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
val root = inflater.inflate(R.layout.dialog_dtc, container, false)
val sortedDtcList = diagnosticTroubleCodes()

progressBar = root.findViewById(R.id.progress_bar)
recyclerView = root.findViewById(R.id.recycler_view)

adapter = DiagnosticTroubleCodeViewAdapter(context)
Expand Down Expand Up @@ -103,6 +119,7 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
refreshButton.setOnClickListener {
if (DataLoggerRepository.isRunning()) {
setLoadingState(true)
startTimeoutTimer()
withDataLogger {
scheduleDTCRead()
}
Expand All @@ -120,6 +137,7 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
).setPositiveButton("Clear Codes") { dialog, _ ->
if (DataLoggerRepository.isRunning()) {
setLoadingState(true)
startTimeoutTimer()
withDataLogger {
scheduleDTCCleanup()
}
Expand All @@ -135,18 +153,21 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
}
}

private fun setLoadingState(isLoading: Boolean) {
private fun setLoadingState(isLoading: Boolean) =
if (isLoading) {
progressBar.visibility = View.VISIBLE
recyclerView.alpha = 0.5f
refreshButton.isEnabled = false
clearButton.isEnabled = false
sendBroadcastEvent(
SCREEN_LOCK_PROGRESS_EVENT,
context?.getText(R.string.pref_dtc_screen_lock) as String,
)
} else {
progressBar.visibility = View.GONE
recyclerView.alpha = 1.0f
refreshButton.isEnabled = true
clearButton.isEnabled = true
sendBroadcastEvent(
SCREEN_UNLOCK_PROGRESS_EVENT,
)
}

private fun startTimeoutTimer() {
handler.removeCallbacks(timeoutRunnable)
handler.postDelayed(timeoutRunnable, 5000)
}

private fun shareDtcReport(dtcList: List<DiagnosticTroubleCode>) {
Expand Down Expand Up @@ -178,7 +199,7 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
reportBuilder.append("Status: $activeStatuses (Hex: $hex)\n")

val snapshot = code.snapshot
if (snapshot != null) {
if (snapshot != null && dataLoggerSettings.instance().adapter.dtcReadSnapshots) {
reportBuilder.append("Snapshot (Record ${snapshot.size}):\n")
snapshot.forEach { did ->
val value = did.decodedValue ?: "N/A"
Expand All @@ -188,7 +209,7 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
}
}

reportBuilder.append("\n") // Blank line between codes
reportBuilder.append("\n")
}

val sendIntent: Intent =
Expand Down Expand Up @@ -247,9 +268,14 @@ internal class DiagnosticTroubleCodePreferenceDialogFragment : CoreDialogFragmen
override fun onPause() {
super.onPause()
requireContext().unregisterReceiver(dtcNotificationsReceiver)
handler.removeCallbacks(timeoutRunnable)

setLoadingState(false)
}

private fun handleDTCChangedNotification() {
handler.removeCallbacks(timeoutRunnable)

setLoadingState(false)

val newCodes = diagnosticTroubleCodes()
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/res/layout/dialog_dtc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header_divider" />

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toTopOf="@+id/recycler_view"
app:layout_constraintBottom_toBottomOf="@+id/recycler_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<LinearLayout
android:id="@+id/button_container"
android:layout_width="0dp"
Expand Down
41 changes: 30 additions & 11 deletions app/src/main/res/layout/dialog_screen_lock.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:orientation="vertical"
android:padding="20dp">
<ProgressBar
android:layout_width="0dp"

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp" />

<TextView
android:id="@+id/dialog_screen_lock_message_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dialog.screen.lock.message" />
</LinearLayout>

<Button
android:id="@+id/dialog_screen_lock_cancel_btn"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:layout_gravity="end"
android:layout_marginTop="16dp"
android:text="Cancel"
android:textColor="@color/rainbow_indigo" />

<TextView
android:id="@+id/dialog_screen_lock_message_id"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="@string/dialog.screen.lock.message" />
</LinearLayout>
Loading
Loading