2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
import android.app.DownloadManager
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
import jp.juggler.subwaytooter.util.showToast
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class DownloadReceiver : BroadcastReceiver() {
|
|
|
|
override fun onReceive(context : Context, intent : Intent?) {
|
2018-01-18 19:09:36 +01:00
|
|
|
intent ?: return
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
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")
|
2018-01-10 16:47:35 +01:00
|
|
|
|
2018-01-18 19:09:36 +01:00
|
|
|
val query = DownloadManager.Query().setFilterById(id)
|
2018-01-04 19:52:25 +01:00
|
|
|
downloadManager.query(query)?.use { cursor ->
|
|
|
|
if(cursor.moveToFirst()) {
|
|
|
|
val idx_status = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
|
|
|
|
val idx_title = cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)
|
|
|
|
|
|
|
|
val title = cursor.getString(idx_title)
|
|
|
|
|
|
|
|
if(DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(idx_status)) {
|
|
|
|
/*
|
|
|
|
ダウンロード完了通知がシステムからのものと重複することがある
|
|
|
|
|
|
|
|
- (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.
|
|
|
|
|
|
|
|
重複を回避する方法はなさそうだ…
|
|
|
|
*/
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
showToast(context, false, context.getString(R.string.download_complete, title))
|
2018-01-04 19:52:25 +01:00
|
|
|
} else {
|
2018-01-21 13:46:36 +01:00
|
|
|
showToast(context, false, context.getString(R.string.download_failed, title))
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|