Swallow exceptions in ScreenshotObserver

pull/1195/head
Andrew 1 year ago committed by GitHub
commit 84004d2fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,10 @@ import android.os.Build
import android.os.Handler import android.os.Handler
import android.provider.MediaStore import android.provider.MediaStore
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer
private const val TAG = "ScreenshotObserver"
class ScreenshotObserver(private val context: Context, handler: Handler, private val screenshotTriggered: ()->Unit): ContentObserver(handler) { class ScreenshotObserver(private val context: Context, handler: Handler, private val screenshotTriggered: ()->Unit): ContentObserver(handler) {
@ -31,22 +35,26 @@ class ScreenshotObserver(private val context: Context, handler: Handler, private
val projection = arrayOf( val projection = arrayOf(
MediaStore.Images.Media.DATA MediaStore.Images.Media.DATA
) )
context.contentResolver.query( try {
uri, context.contentResolver.query(
projection, uri,
null, projection,
null, null,
null null,
)?.use { cursor -> null
val dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA) )?.use { cursor ->
while (cursor.moveToNext()) { val dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
val path = cursor.getString(dataColumn) while (cursor.moveToNext()) {
if (path.contains("screenshot", true)) { val path = cursor.getString(dataColumn)
if (cache.add(uri.hashCode())) { if (path.contains("screenshot", true)) {
screenshotTriggered() if (cache.add(uri.hashCode())) {
screenshotTriggered()
}
} }
} }
} }
} catch (e: SecurityException) {
Log.e(TAG, e)
} }
} }
@ -56,28 +64,32 @@ class ScreenshotObserver(private val context: Context, handler: Handler, private
MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.RELATIVE_PATH MediaStore.Images.Media.RELATIVE_PATH
) )
context.contentResolver.query(
uri, try {
projection, context.contentResolver.query(
null, uri,
null, projection,
null null,
)?.use { cursor -> null,
val relativePathColumn = null
cursor.getColumnIndex(MediaStore.Images.Media.RELATIVE_PATH) )?.use { cursor ->
val displayNameColumn = val relativePathColumn =
cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME) cursor.getColumnIndex(MediaStore.Images.Media.RELATIVE_PATH)
while (cursor.moveToNext()) { val displayNameColumn =
val name = cursor.getString(displayNameColumn) cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)
val relativePath = cursor.getString(relativePathColumn) while (cursor.moveToNext()) {
if (name.contains("screenshot", true) or val name = cursor.getString(displayNameColumn)
relativePath.contains("screenshot", true)) { val relativePath = cursor.getString(relativePathColumn)
if (cache.add(uri.hashCode())) { if (name.contains("screenshot", true) or
screenshotTriggered() relativePath.contains("screenshot", true)) {
if (cache.add(uri.hashCode())) {
screenshotTriggered()
}
} }
} }
} }
} catch (e: IllegalStateException) {
Log.e(TAG, e)
} }
} }
}
}

Loading…
Cancel
Save