Skip to content

Commit

Permalink
add error notification
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Jul 7, 2024
1 parent 544e375 commit e366c23
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "de.felixnuesse.disky"
minSdk = 30
targetSdk = 34
versionCode = 6
versionName = "1.1.0"
versionCode = 7
versionName = "1.2.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
84 changes: 76 additions & 8 deletions app/src/main/java/de/felixnuesse/disky/background/ScanService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package de.felixnuesse.disky.background

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.app.usage.StorageStatsManager
import android.content.BroadcastReceiver
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
Expand Down Expand Up @@ -49,7 +54,11 @@ class ScanService: Service(), ScannerCallback {
val SCAN_REFRESH_REQUESTED = "SCAN_REFRESH_REQUESTED"
val SCAN_PROGRESSED = "SCAN_PROGRESSED"
private val NOTIFICATION_CHANNEL_ID = "general_notification_channel"
private val ERROR_NOTIFICATION_CHANNEL_ID = "error_notification_channel"
private val ERROR_COPY_INTENT_ACTION = "de.felixnuesse.disky.background.ACTION_COPY_ERROR"
private val NOTIFICATION_ID = 5691
private val ERROR_NOTIFICATION_ID = 5692
private val CLIPBOARD_INTENT_ID = 5693
private var storageResult: StorageResult? = null

/**
Expand Down Expand Up @@ -101,17 +110,25 @@ class ScanService: Service(), ScannerCallback {
Log.e(tag(), "No valid storage name was provided!")
return@launch
}
val result = scan(storageToScan, serviceRunId, subfolder)
Log.e(tag(), "Scanning took: ${System.currentTimeMillis()-now}ms ${wasStopped(thisServiceRunId)}")
if(wasStopped(thisServiceRunId)) {

try {
val result = scan(storageToScan, serviceRunId, subfolder)
Log.e(tag(), "Scanning took: ${System.currentTimeMillis()-now}ms ${wasStopped(thisServiceRunId)}")
if(wasStopped(thisServiceRunId)) {
val resultIntent = Intent(SCAN_ABORTED)
LocalBroadcastManager.getInstance(context).sendBroadcast(resultIntent)
Log.e(tag(), "Scan was prematurely stopped!")
} else {
storageResult = result
val resultIntent = Intent(SCAN_COMPLETE)
LocalBroadcastManager.getInstance(context).sendBroadcast(resultIntent)
}
} catch (e: Exception) {
showErrorNotification(e.message.toString())
val resultIntent = Intent(SCAN_ABORTED)
LocalBroadcastManager.getInstance(context).sendBroadcast(resultIntent)
Log.e(tag(), "Scan was prematurely stopped!")
} else {
storageResult = result
val resultIntent = Intent(SCAN_COMPLETE)
LocalBroadcastManager.getInstance(context).sendBroadcast(resultIntent)
}

finishService()
}
return super.onStartCommand(intent, flags, startId)
Expand All @@ -129,6 +146,14 @@ class ScanService: Service(), ScannerCallback {
)
channel.setSound(null, null)
notificationManager.createNotificationChannel(channel)

val error_channel = NotificationChannel(
ERROR_NOTIFICATION_CHANNEL_ID,
getString(R.string.error_notification_channel_name),
NotificationManager.IMPORTANCE_LOW
)
notificationManager.createNotificationChannel(error_channel)

}

private fun getNotification(message: String, bigmessage: String): NotificationCompat.Builder {
Expand All @@ -142,6 +167,49 @@ class ScanService: Service(), ScannerCallback {
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
}

//Todo: Migrate this out into its own class
private fun showErrorNotification(bigmessage: String) {
val error = NotificationCompat.Builder(this, ERROR_NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.error_notification_title))
.setContentText(getString(R.string.error_notification_channel_message))
.setStyle(NotificationCompat.BigTextStyle().bigText(bigmessage))
.setSmallIcon(R.drawable.round_running_with_errors_24)


val clipboardReciever = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
copyToClipboard("Error Message", bigmessage)
}
}

val intentFilter = IntentFilter(ERROR_COPY_INTENT_ACTION)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(clipboardReciever, intentFilter, RECEIVER_EXPORTED)
} else {
registerReceiver(clipboardReciever, intentFilter)
}

val copyIntent = PendingIntent.getBroadcast(
this,
CLIPBOARD_INTENT_ID,
Intent(ERROR_COPY_INTENT_ACTION),
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

error.addAction(
R.drawable.icon_copy,
getString(R.string.error_notification_action_copy),
copyIntent
)

notificationManager.notify(ERROR_NOTIFICATION_ID, error.build())
}

private fun copyToClipboard(label: String, content: String) {
val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setPrimaryClip(ClipData.newPlainText(label, content))
}

private fun scan(storage: String, id: Long, subpath: String): StorageResult? {
val selectedStorage = findStorageByNameOrUUID(storage)
val rootElement: StoragePrototype?
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/icon_copy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M16,1L4,1c-1.1,0 -2,0.9 -2,2v14h2L4,3h12L16,1zM19,5L8,5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2L21,7c0,-1.1 -0.9,-2 -2,-2zM19,21L8,21L8,7h11v14z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/round_running_with_errors_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M21,18c-0.55,0 -1,-0.45 -1,-1v-6c0,-0.55 0.45,-1 1,-1s1,0.45 1,1v6C22,17.55 21.55,18 21,18zM21,20c-0.55,0 -1,0.45 -1,1s0.45,1 1,1s1,-0.45 1,-1S21.55,20 21,20zM18,17.29C16.53,18.95 14.39,20 12,20c-4.41,0 -8,-3.59 -8,-8c0,-4.41 3.59,-8 8,-8v9l7.55,-7.55C17.72,3.34 15.02,2 12,2C6.48,2 2,6.48 2,12c0,5.52 4.48,10 10,10c2.25,0 4.33,-0.74 6,-2V17.29z"/>

</vector>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@
<string name="faq_answer_accuracy">No. We can calculate your files and apps properly, but not your android system. That means you will likely get different results for the system each time you refresh the scan. This is a limitation of android itself.</string>
<string name="faq_label_androidos">What is \"Android OS\"?</string>
<string name="faq_answer_androidos">This is all the used space that we can\'t access. That includes other profiles and their apps and the system itself. The calculated value is therefore only an estimation of its true size.</string>
<string name="error_notification_channel_name">Errors</string>
<string name="error_notification_channel_message">The Scan-Service encountered an error.</string>
<string name="error_notification_title">Scanning was not successful</string>
<string name="error_notification_action_copy">Copy Error</string>
</resources>

0 comments on commit e366c23

Please sign in to comment.