Skip to content

Commit

Permalink
fix notifications for older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jan 9, 2016
1 parent c5b275f commit 12e7736
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
applicationId "org.blitzortung.android.app"
minSdkVersion 8
targetSdkVersion 23
versionCode 154
versionCode 155
versionName "1.4.0"
multiDexEnabled true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.media.AudioAttributes
import android.media.AudioManager
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Vibrator
import android.util.Log
import org.blitzortung.android.alert.AlertParameters
Expand All @@ -27,6 +28,7 @@ import org.blitzortung.android.location.LocationEvent
import org.blitzortung.android.location.LocationHandler
import org.blitzortung.android.protocol.Event
import org.blitzortung.android.util.MeasurementSystem
import org.blitzortung.android.util.isAtLeast


class AlertHandler(
Expand Down Expand Up @@ -219,7 +221,11 @@ class AlertHandler(
alertSignal.soundSignal?.let { signal ->
RingtoneManager.getRingtone(context, signal)?.let { ringtone ->
if (!ringtone.isPlaying) {
ringtone.audioAttributes = AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_NOTIFICATION).build()
if (isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
ringtone.audioAttributes = AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_NOTIFICATION).build()
} else {
ringtone.streamType = AudioManager.STREAM_NOTIFICATION
}
ringtone.play()
}
Log.v(Main.LOG_TAG, "playing " + ringtone.getTitle(context))
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/org/blitzortung/android/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import org.blitzortung.android.map.overlay.StrikesOverlay
import org.blitzortung.android.map.overlay.color.ParticipantColorHandler
import org.blitzortung.android.map.overlay.color.StrikeColorHandler
import org.blitzortung.android.util.TabletAwareView
import org.blitzortung.android.util.isAtLeast

class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
private val androidIdsForExtendedFunctionality = setOf("44095eb4f9f1a6a6", "f2be4516e5843964")
Expand Down Expand Up @@ -316,7 +317,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
}

private fun openQuickSettingsDialog() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (isAtLeast(Build.VERSION_CODES.HONEYCOMB)) {
val dialog = QuickSettingsDialog()
dialog.show(fragmentManager, "QuickSettingsDialog")
}
Expand Down Expand Up @@ -584,7 +585,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
private fun configureMenuAccess() {
val config = ViewConfiguration.get(this)

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
if (isAtLeast(Build.VERSION_CODES.ICE_CREAM_SANDWICH) &&
!config.hasPermanentMenuKey()) {
val menuButton = findViewById(R.id.menu) as ImageButton
menuButton.visibility = View.VISIBLE
Expand All @@ -594,7 +595,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
}

private fun hideActionBar() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (isAtLeast(Build.VERSION_CODES.HONEYCOMB)) {
actionBar?.hide()
}
}
Expand All @@ -604,5 +605,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
val LOG_TAG = "BO_ANDROID"

val REQUEST_GPS = 1


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build

import org.blitzortung.android.app.Main
import org.blitzortung.android.app.R
import org.blitzortung.android.util.isAtLeast

class NotificationHandler(private val context: Context) {

Expand All @@ -24,12 +26,32 @@ class NotificationHandler(private val context: Context) {
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
val contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)

val notificationBuilder = Notification.Builder(context).setSmallIcon(R.drawable.icon).setContentTitle(context.resources.getText(R.string.app_name)).setContentText(notificationText).setContentIntent(contentIntent).setAutoCancel(true)
val notification = if (isAtLeast(Build.VERSION_CODES.HONEYCOMB)) {
createNotification(contentIntent, notificationText)
} else {
createLegacyNotification(contentIntent, notificationText)
}

notificationService.notify(R.id.alarm_notification_id, notificationBuilder.notification)
notificationService.notify(R.id.alarm_notification_id, notification)
}
}

private fun createNotification(contentIntent: PendingIntent?, notificationText: String): Notification? {
return Notification.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle(context.resources.getText(R.string.app_name))
.setContentText(notificationText)
.setContentIntent(contentIntent)
.setAutoCancel(true).build()
}

private fun createLegacyNotification(contentIntent: PendingIntent?, notificationText: String): Notification {
val notification = Notification(R.drawable.icon, notificationText, System.currentTimeMillis())
val setLatestEventInfo = Notification::class.java.getDeclaredMethod("setLatestEventInfo", Context::class.java, CharSequence::class.java, CharSequence::class.java, PendingIntent::class.java)
setLatestEventInfo.invoke(notification, context, context.resources.getText(R.string.app_name), notificationText, contentIntent)
return notification
}

fun clearNotification() {
notificationService?.cancel(R.id.alarm_notification_id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class TabletAwareView(context: Context, attrs: AttributeSet?, defStyle: Int
companion object {
fun isTablet(context: Context): Boolean {

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
if (isAtLeast(Build.VERSION_CODES.HONEYCOMB_MR2)) {
return context.resources.configuration.smallestScreenWidthDp >= 600
} else {
return false
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/org/blitzortung/android/util/Version.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.blitzortung.android.util

fun isAtLeast(versionCode: Int) : Boolean {
return android.os.Build.VERSION.SDK_INT >= versionCode
}

0 comments on commit 12e7736

Please sign in to comment.