From 096a8770dbd1055b38f131e99099e61fb01882c8 Mon Sep 17 00:00:00 2001 From: tateisu Date: Fri, 15 Nov 2019 13:20:35 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=83=83=E3=82=B7=E3=83=A5?= =?UTF-8?q?=E3=83=AC=E3=83=9D=E3=83=BC=E3=83=88=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../juggler/subwaytooter/DownloadReceiver.kt | 88 ++++++++++--------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/jp/juggler/subwaytooter/DownloadReceiver.kt b/app/src/main/java/jp/juggler/subwaytooter/DownloadReceiver.kt index 2472c49c..9ff08ebe 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/DownloadReceiver.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/DownloadReceiver.kt @@ -4,56 +4,60 @@ import android.app.DownloadManager import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import jp.juggler.util.LogCategory import jp.juggler.util.getIntOrNull import jp.juggler.util.getStringOrNull import jp.juggler.util.showToast class DownloadReceiver : BroadcastReceiver() { + + companion object { + private val log = LogCategory("DownloadReceiver") + } + override fun onReceive(context : Context, intent : Intent?) { - intent ?: return - - val action = intent.action ?: return - - if(DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) { - val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L) - - val downloadManager = - context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager? - ?: throw NotImplementedError("missing DownloadManager system service") - - val query = DownloadManager.Query().setFilterById(id) - downloadManager.query(query)?.use { cursor -> - if(cursor.moveToFirst()) { - - val title = cursor.getStringOrNull(DownloadManager.COLUMN_TITLE) - - if(DownloadManager.STATUS_SUCCESSFUL == cursor.getIntOrNull(DownloadManager.COLUMN_STATUS)) { - /* - ダウンロード完了通知がシステムからのものと重複することがある + when(intent?.action) { + DownloadManager.ACTION_DOWNLOAD_COMPLETE -> { + + val downloadManager = + context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager + + try { + val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L) + val query = DownloadManager.Query().setFilterById(id) - - (Aubee elm. Android 5.1) don't shows toast. - - (Samsung Galaxy S8+ Android 7.0) don't show toast. - - (Kyocera AndroidOne Android 8.0 S2) don't show toast. - - (LGE LGL24 Android 5.0.2) SHOWS toast. - - (LGE LGV32 Android 6.0) SHOWS toast. - - maybe it depends on customization by device maker. not depends on OS version. - - 重複を回避する方法はなさそうだ… - */ - - showToast( - context, - false, - context.getString(R.string.download_complete, title) - ) - } else { - showToast( - context, - false, - context.getString(R.string.download_failed, title) - ) + downloadManager.query(query)?.use { cursor -> + if(! cursor.moveToFirst()) { + log.e("cursor.moveToFirst() failed.") + } else { + val title = cursor.getStringOrNull(DownloadManager.COLUMN_TITLE) + val status = cursor.getIntOrNull(DownloadManager.COLUMN_STATUS) + showToast( + context, + false, + if(status == DownloadManager.STATUS_SUCCESSFUL) { + context.getString(R.string.download_complete, title) + } else { + context.getString(R.string.download_failed, title) + } + ) + /* + ダウンロード完了通知がシステムからのものと重複することがある + + - (Aubee elm. Android 5.1) don't shows toast. + - (Samsung Galaxy S8+ Android 7.0) don't show toast. + - (Kyocera AndroidOne Android 8.0 S2) don't show toast. + - (LGE LGL24 Android 5.0.2) SHOWS toast. + - (LGE LGV32 Android 6.0) SHOWS toast. + + maybe it depends on customization by device maker. not depends on OS version. + + 重複を回避する方法はなさそうだ… + */ + } } + } catch(ex : Throwable) { + log.e(ex, "downloadManager.query() failed.") } } }