-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new notification action for deleting the recording
The new Delete action will show up alongside the current Open and Share actions, but unlike those other two actions, Delete will also dismiss the notification. Fixes: #179 Signed-off-by: Andrew Gunnerson <[email protected]>
- Loading branch information
1 parent
62d969c
commit df042ba
Showing
7 changed files
with
145 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
app/src/main/java/com/chiller3/bcr/NotificationActionService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.chiller3.bcr | ||
|
||
import android.app.NotificationManager | ||
import android.app.Service | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Handler | ||
import android.os.IBinder | ||
import android.os.Looper | ||
import android.util.Log | ||
|
||
class NotificationActionService : Service() { | ||
companion object { | ||
private val TAG = NotificationActionService::class.java.simpleName | ||
|
||
private val ACTION_DELETE_URI = "${NotificationActionService::class.java.canonicalName}.delete_uri" | ||
private const val EXTRA_REDACTED = "redacted" | ||
private const val EXTRA_NOTIFICATION_ID = "notification_id" | ||
|
||
private fun intentFromFile(context: Context, file: OutputFile): Intent = | ||
Intent(context, NotificationActionService::class.java).apply { | ||
setDataAndType(file.uri, file.mimeType) | ||
putExtra(EXTRA_REDACTED, file.redacted) | ||
} | ||
|
||
fun createDeleteUriIntent(context: Context, file: OutputFile, notificationId: Int): Intent = | ||
intentFromFile(context, file).apply { | ||
action = ACTION_DELETE_URI | ||
putExtra(EXTRA_NOTIFICATION_ID, notificationId) | ||
} | ||
} | ||
|
||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
private fun parseFileFromIntent(intent: Intent): OutputFile = | ||
OutputFile( | ||
intent.data!!, | ||
intent.getStringExtra(EXTRA_REDACTED)!!, | ||
intent.type!!, | ||
) | ||
|
||
private fun parseDeleteUriIntent(intent: Intent): Pair<OutputFile, Int> { | ||
val file = parseFileFromIntent(intent) | ||
|
||
val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1) | ||
if (notificationId < 0) { | ||
throw IllegalArgumentException("Invalid notification ID: $notificationId") | ||
} | ||
|
||
return Pair(file, notificationId) | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder? = null | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int = | ||
try { | ||
when (intent?.action) { | ||
ACTION_DELETE_URI -> { | ||
val (file, notificationId) = parseDeleteUriIntent(intent) | ||
val documentFile = file.toDocumentFile(this) | ||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
|
||
Thread { | ||
Log.d(TAG, "Deleting: ${file.redacted}") | ||
try { | ||
documentFile.delete() | ||
} catch (e: Exception) { | ||
Log.w(TAG, "Failed to delete ${file.redacted}", e) | ||
} | ||
|
||
handler.post { | ||
notificationManager.cancel(notificationId) | ||
stopSelf(startId) | ||
} | ||
}.start() | ||
} | ||
else -> throw IllegalArgumentException("Invalid action: ${intent.action}") | ||
} | ||
|
||
START_REDELIVER_INTENT | ||
} catch (e: Exception) { | ||
val redactedIntent = intent?.let { Intent(it) }?.apply { | ||
setDataAndType(Uri.fromParts("redacted", "", ""), type) | ||
} | ||
|
||
Log.w(TAG, "Failed to handle intent: $redactedIntent", e) | ||
stopSelf(startId) | ||
|
||
START_NOT_STICKY | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,29 @@ | ||
package com.chiller3.bcr | ||
|
||
import android.content.ContentResolver | ||
import android.content.Context | ||
import android.net.Uri | ||
import androidx.core.net.toFile | ||
import androidx.documentfile.provider.DocumentFile | ||
|
||
data class OutputFile( | ||
/** | ||
* URI to a single file, which may have a [ContentResolver.SCHEME_FILE] or | ||
* [ContentResolver.SCHEME_CONTENT] scheme. | ||
*/ | ||
val uri: Uri, | ||
|
||
/** String representation of [uri] with private information redacted. */ | ||
val redacted: String, | ||
|
||
/** MIME type of [uri]'s contents. */ | ||
val mimeType: String, | ||
) | ||
) { | ||
fun toDocumentFile(context: Context): DocumentFile = | ||
when (uri.scheme) { | ||
ContentResolver.SCHEME_FILE -> DocumentFile.fromFile(uri.toFile()) | ||
// Only returns null on API <19 | ||
ContentResolver.SCHEME_CONTENT -> DocumentFile.fromSingleUri(context, uri)!! | ||
else -> throw IllegalArgumentException("Invalid URI scheme: $redacted") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters