Author: Adam Pioterek <adam.pioterek@protonmail.ch>
Oreo notification channel
app/src/main/java/ml/adamsprogs/bimba/NotificationChannels.kt | 26 ++++ app/src/main/java/ml/adamsprogs/bimba/TimetableDownloader.kt | 29 ++++
diff --git a/app/src/main/java/ml/adamsprogs/bimba/NotificationChannels.kt b/app/src/main/java/ml/adamsprogs/bimba/NotificationChannels.kt new file mode 100644 index 0000000000000000000000000000000000000000..8ed8000409fe4705c3161668f926bb0d8d87272d --- /dev/null +++ b/app/src/main/java/ml/adamsprogs/bimba/NotificationChannels.kt @@ -0,0 +1,26 @@ +package ml.adamsprogs.bimba + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.os.Build +import android.support.annotation.RequiresApi + + +class NotificationChannels { + companion object { + val CHANNEL_UPDATES = "updates" + + @RequiresApi(Build.VERSION_CODES.O) + fun makeChannel(id: String, name: String, manager: NotificationManager) { + try { + manager.getNotificationChannel(id) + } catch (e: RuntimeException) { + val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_MIN) + channel.enableLights(false) + channel.enableVibration(false) + channel.setShowBadge(false) + manager.createNotificationChannel(channel) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/adamsprogs/bimba/TimetableDownloader.kt b/app/src/main/java/ml/adamsprogs/bimba/TimetableDownloader.kt index f49ae019176280aa383a4de8a6b17fd249dd1f60..b5829717ca489d28aaa7b7a52dac4ce83d89371f 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/TimetableDownloader.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/TimetableDownloader.kt @@ -1,6 +1,8 @@ package ml.adamsprogs.bimba +import android.annotation.TargetApi import android.app.IntentService +import android.app.Notification import android.content.Context import android.content.Intent import android.support.v4.app.NotificationCompat @@ -11,6 +13,7 @@ import java.io.* import java.security.MessageDigest import kotlin.experimental.and import android.app.NotificationManager +import android.os.Build import ml.adamsprogs.bimba.models.Timetable @@ -25,6 +28,7 @@ val RESULT_UP_TO_DATE = "up-to-date" val RESULT_DOWNLOADED = "downloaded" val RESULT_VALIDITY_FAILED = "validity failed" } + private lateinit var notificationManager: NotificationManager private var size: Int = 0 @@ -39,7 +43,7 @@ return } val metadataUrl = URL("https://adamsprogs.ml/w/_media/programmes/bimba/timetable.db.meta") var httpCon = metadataUrl.openConnection() as HttpURLConnection - if (httpCon.responseCode != HttpURLConnection.HTTP_OK){ + if (httpCon.responseCode != HttpURLConnection.HTTP_OK) { sendResult(RESULT_NO_CONNECTIVITY) return } @@ -63,7 +67,7 @@ notify(0) val xzDbUrl = URL("https://adamsprogs.ml/w/_media/programmes/bimba/$dbFilename") httpCon = xzDbUrl.openConnection() as HttpURLConnection - if (httpCon.responseCode != HttpURLConnection.HTTP_OK){ + if (httpCon.responseCode != HttpURLConnection.HTTP_OK) { sendResult(RESULT_NO_CONNECTIVITY) return } @@ -94,7 +98,14 @@ sendBroadcast(broadcastIntent) } private fun notify(progress: Int) { - //todo create channel + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) + notifyCompat(progress) + else + notifyStandard(progress) + } + + @Suppress("DEPRECATION") + private fun notifyCompat(progress: Int) { val builder = NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_download) .setContentTitle(getString(R.string.timetable_downloading)) @@ -102,7 +113,19 @@ .setContentText("$progress KiB/$size KiB") .setCategory(NotificationCompat.CATEGORY_PROGRESS) .setOngoing(true) .setProgress(size, progress, false) + notificationManager.notify(42, builder.build()) + } + @TargetApi(Build.VERSION_CODES.O) + private fun notifyStandard(progress: Int) { + NotificationChannels.makeChannel(NotificationChannels.CHANNEL_UPDATES, "Updates", notificationManager) + val builder = Notification.Builder(this, NotificationChannels.CHANNEL_UPDATES) + .setSmallIcon(R.drawable.ic_download) + .setContentTitle(getString(R.string.timetable_downloading)) + .setContentText("$progress KiB/$size KiB") + .setCategory(Notification.CATEGORY_PROGRESS) + .setOngoing(true) + .setProgress(size, progress, false) notificationManager.notify(42, builder.build()) }