From 52db9a54c3a867ebcac93b35109f42c101e82581 Mon Sep 17 00:00:00 2001 From: crimean Date: Mon, 11 Jun 2018 17:12:03 +0300 Subject: [PATCH] Telegram - simple notifications --- .../res/drawable-hdpi/ic_action_view.png | Bin 0 -> 1505 bytes .../res/drawable-mdpi/ic_action_view.png | Bin 0 -> 1281 bytes .../res/drawable-xhdpi/ic_action_view.png | Bin 0 -> 1632 bytes .../res/drawable-xxhdpi/ic_action_view.png | Bin 0 -> 2113 bytes OsmAnd-telegram/res/values/strings.xml | 2 + .../net/osmand/telegram/TelegramSettings.kt | 2 + .../NotificationDismissReceiver.kt | 39 ---- .../notifications/NotificationHelper.kt | 100 +-------- .../ShareLocationNotification.kt | 163 +++------------ .../notifications/ShowLocationNotification.kt | 45 ++++ .../notifications/TelegramNotification.kt | 164 ++++++--------- .../telegram/services/MyLocationService.kt | 194 +++++++++--------- .../telegram/services/UserLocationService.kt | 8 +- 13 files changed, 241 insertions(+), 476 deletions(-) create mode 100644 OsmAnd-telegram/res/drawable-hdpi/ic_action_view.png create mode 100644 OsmAnd-telegram/res/drawable-mdpi/ic_action_view.png create mode 100644 OsmAnd-telegram/res/drawable-xhdpi/ic_action_view.png create mode 100644 OsmAnd-telegram/res/drawable-xxhdpi/ic_action_view.png delete mode 100644 OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationDismissReceiver.kt create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/notifications/ShowLocationNotification.kt diff --git a/OsmAnd-telegram/res/drawable-hdpi/ic_action_view.png b/OsmAnd-telegram/res/drawable-hdpi/ic_action_view.png new file mode 100644 index 0000000000000000000000000000000000000000..a49475137830f48601375ca7a7babacfb4086de4 GIT binary patch literal 1505 zcmaJ>eQeZZ81LrT2V^Q=W(;v%QG&2uKi2lvX196VZSDrQjoXxg!O)MlT)FjHdL7ph z)X4+|t9J=OmH0kyW;%M`Pu)~wcKGs+tPa!Z;3V4un_ z1p&ZI4a-k}LI`@|9i^t$to6_1L``+@aUF-H>NXmJ3X3hB7el~=1+Y{JyO8ULzd~R| zav=+7KknB(AgGkBG(h#riW+fcNMt3XxCkz^INN{x^RTET*$Cp&fdq5F$5k~ zF+(n7gjB7+3ifCQfN2MVia0^RPS!!tjFWZd!K5u?cmYP@D9&9K^^m70z;^B)?KBS7z$-^ zibYvgB2fkqfEEP`$byux^C&#wcC+qMs&q~%T|f{eR6&8qGsj62WU1SWvv__2>kCIr zJ}iQSZ^iaKiY<98mh%{ZH#MV1)9MmkP!-fnEfUmp*y9^{iOPalfHDP?(@p~IL@AM!Wswva#_2>x@zO*28|hQ~ z#EwHx(BXJdq0Q2{u4lpp|w3M50K0s$m~l0-t}c?7d5V)52_sFe~&c7MjV4+g^) z9*hHe0KF4-Qy;}sYShMRvPwu2G#rd{1rWebaP{&m6E@pSf*xF zvOgU%PwjFKEG``SaCUdcWPRL`UkAqZ2;ihD%}l*E?`G1$Yw9`L)cDJ$ZSc*?s zePHbo^lD4?P+Q%eoy1E;%JJ->9-}{!TbMQZ{FP)nf7zlzjf!PlZD{Yvu{Pb=5c;{# zWyz_#c3<9hEai(!jb}F;$~m`e`@Q4p_B&iw=+smD+OJe9PJjB1RXc)bTH9XfGGq6< z&DgulVIY!f~Ox?oz z?AT#A^FrW2cQJjpqH)Fmq1+sV-eQ_VedEjCc6BFR?5D46?s8A-!@o=&Lri*ox5v|w d+w$)-OQ4%+?}7BrG%5bm^_5h3yXGue{U6izB8LC~ literal 0 HcmV?d00001 diff --git a/OsmAnd-telegram/res/drawable-mdpi/ic_action_view.png b/OsmAnd-telegram/res/drawable-mdpi/ic_action_view.png new file mode 100644 index 0000000000000000000000000000000000000000..12ae65d881c0b126df87565931bcf6d2b1beb10e GIT binary patch literal 1281 zcmaJ>TWs4@7iHE8XgoYTGy#Uq3rDCXp2ee6ufmIGcFswc{6iuSFsq`MKMc*HD;T^Obb^6oJNh9OrWD8=ws-ZwU zmX*h063Xh#>1(hP!@P@HD($4>PfNha`sI?2Kc6)b8pArf^QH_Yp@S=MT+_qE?;oxc zxTc1Q(O{g8n-Msn4b58c6jw$ zvWhbqCU!}s;|V-sSP&2Tc@oeJhYO;g3G#v{bl@zKDLO>4G)aq6Fd)$^UU>-Q%~CT` zGCEN4g`_Ys;W(y5QMp{sp9}a6Yn)<4QKV>=Vp$StkoJu3$azw?TdNkL&<2)fI+~&5 zC5y6ROgmu$G2IOzYsTYyhIPA=D3mfPFPjwOr>ShV6jxc>c9QUa8vAP7sTmVeNoX6> z7C`mLv{u0=clUNwQbcT|5lcfwk*A^tn9f4oiABQ%y78-;Dse$oRTQ2lS)S)ek>*5F z6jhewAp?Uz;b2Blt9JIovkb@40w3xd2!5NV)2G+o-0iM$Ue+RHogiqtMcxgX(}~ zWbtyIC2b!L0^o#<%&}xf0U?qPaRN$0P#`(LW-@>UJTC}D1+U(fzui7XCnyfHN=K3b z2+AywNd*cr$uN*36_#g7g@G!=sel2pOi+j-Rcf8PTB&-3`m?m%8w@1ujTY3=JXvUP zo@zOtz%Wl^EZUpOZz}&3i``z*)978Q8YOKDnI=|E0uwJvZp7^^tb@NrX-c2p~ ze)HabrC4~OHStIK`bUqic?;{s)!Lq4&z-sI9zFNVmp#vV3MYC#uY2>jyS455F8Y`> zH|JA8^Y_BiXO|Y5rj86@w-0nIzwO?^M_U$aKk03|=xKlXfv%qGk*n@wgWtCI-TL#5 zvn`9u$G5)vW<}o!G}nFS-r4@?-^TVYmLfkiuKBLaT{%41eqrgxTVEGO+-sK>Zf*ZH zzZMGJx3UoVx!^hY=?(Vb4fD|JbJ%j?NL&1wE|+`awT4rv#0MS9u>{<#Iobysh z@#hZ}M&@_g8s4kf`grI5vF3MH58SMo$Gq5?M;?6X*h9t5(oZSYKOB9xZ|ueY0O2aI AhyVZp literal 0 HcmV?d00001 diff --git a/OsmAnd-telegram/res/drawable-xhdpi/ic_action_view.png b/OsmAnd-telegram/res/drawable-xhdpi/ic_action_view.png new file mode 100644 index 0000000000000000000000000000000000000000..9f4493c58571fb2bd917c61de44b00a84d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Z`Qw|N#%|$NR(WQdFXZc|xV2HU}^A>vHHalL@ Uxg9FMYyGK9BjdyugOsmAnd logo You need to install free or paid version of OsmAnd first Install OsmAnd + Show users on map + Active chats yd ft diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 1ad4824403..e3d30dc18b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -80,6 +80,8 @@ class TelegramSettings(private val app: TelegramApplication) { fun getShareLocationChats() = ArrayList(shareLocationChats) fun getShowOnMapChats() = ArrayList(showOnMapChats) + fun getShowOnMapChatsCount() = showOnMapChats.size + fun save() { val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE) val edit = prefs.edit() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationDismissReceiver.kt b/OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationDismissReceiver.kt deleted file mode 100644 index 555cb95952..0000000000 --- a/OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationDismissReceiver.kt +++ /dev/null @@ -1,39 +0,0 @@ -package net.osmand.telegram.notifications - -import android.app.PendingIntent -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.text.TextUtils - -import net.osmand.telegram.TelegramApplication -import net.osmand.telegram.notifications.TelegramNotification.NotificationType - -class NotificationDismissReceiver : BroadcastReceiver() { - - override fun onReceive(context: Context, intent: Intent) { - val helper = (context.applicationContext as TelegramApplication).notificationHelper - val notificationTypeStr = intent.extras!!.getString(NOTIFICATION_TYPE_KEY_NAME) - if (!TextUtils.isEmpty(notificationTypeStr)) { - try { - val notificationType = NotificationType.valueOf(notificationTypeStr) - helper.onNotificationDismissed(notificationType) - } catch (e: Exception) { - //ignored - } - - } - } - - companion object { - - const val NOTIFICATION_TYPE_KEY_NAME = "net.osmand.telegram.notifications.NotificationType" - - fun createIntent(context: Context, notificationType: NotificationType): PendingIntent { - val intent = Intent(context, NotificationDismissReceiver::class.java) - intent.putExtra(NOTIFICATION_TYPE_KEY_NAME, notificationType.name) - return PendingIntent.getBroadcast(context.applicationContext, - 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) - } - } -} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationHelper.kt index 9f9f9533d9..5f87676f29 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/notifications/NotificationHelper.kt @@ -13,56 +13,13 @@ import java.util.* class NotificationHelper(private val app: TelegramApplication) { - private var shareLocationNotification: ShareLocationNotification? = null - private val all = ArrayList() + val shareLocationNotification = ShareLocationNotification(app) + val showLocationNotification = ShowLocationNotification(app) - init { - init() - } + private val all = listOf(shareLocationNotification, showLocationNotification) - private fun init() { - val shareLocationNotification = ShareLocationNotification(app) - this.shareLocationNotification = shareLocationNotification - all.add(shareLocationNotification) - } - - fun buildTopNotification(): Notification? { - val notification = acquireTopNotification() - if (notification != null) { - removeNotification(notification.type) - setTopNotification(notification) - val notificationBuilder = notification.buildNotification(false) - return notificationBuilder?.build() - } - return null - } - - private fun acquireTopNotification(): TelegramNotification? { - var notification: TelegramNotification? = null - if (shareLocationNotification!!.isEnabled && shareLocationNotification!!.isActive) { - notification = shareLocationNotification - } - return notification - } - - fun updateTopNotification() { - val notification = acquireTopNotification() - setTopNotification(notification) - } - - private fun setTopNotification(notification: TelegramNotification?) { - for (n in all) { - n.isTop = n === notification - } - } - - fun showNotifications() { - if (!hasAnyTopNotification()) { - removeTopNotification() - } - for (notification in all) { - notification.showNotification() - } + fun buildNotification(telegramNotification: TelegramNotification): Notification { + return telegramNotification.buildNotification(false).build() } fun refreshNotification(notificationType: NotificationType) { @@ -74,53 +31,6 @@ class NotificationHelper(private val app: TelegramApplication) { } } - fun onNotificationDismissed(notificationType: NotificationType) { - for (notification in all) { - if (notification.type == notificationType) { - notification.onNotificationDismissed() - break - } - } - } - - fun hasAnyTopNotification(): Boolean { - for (notification in all) { - if (notification.isTop) { - return true - } - } - return false - } - - fun refreshNotifications() { - if (!hasAnyTopNotification()) { - removeTopNotification() - } - for (notification in all) { - notification.refreshNotification() - } - } - - fun removeTopNotification() { - val notificationManager = NotificationManagerCompat.from(app) - notificationManager.cancel(TelegramNotification.TOP_NOTIFICATION_SERVICE_ID) - } - - fun removeNotification(notificationType: NotificationType) { - for (notification in all) { - if (notification.type == notificationType) { - notification.removeNotification() - break - } - } - } - - fun removeNotifications() { - for (notification in all) { - notification.removeNotification() - } - } - @TargetApi(26) fun createNotificationChannel() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/notifications/ShareLocationNotification.kt b/OsmAnd-telegram/src/net/osmand/telegram/notifications/ShareLocationNotification.kt index f642076106..e52c82386d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/notifications/ShareLocationNotification.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/notifications/ShareLocationNotification.kt @@ -1,10 +1,5 @@ package net.osmand.telegram.notifications -import android.app.PendingIntent -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.content.IntentFilter import android.support.v4.app.NotificationCompat import android.support.v4.content.ContextCompat import net.osmand.telegram.R @@ -14,139 +9,43 @@ import net.osmand.util.Algorithms class ShareLocationNotification(app: TelegramApplication) : TelegramNotification(app, GROUP_NAME) { - companion object { + companion object { + const val GROUP_NAME = "share_location" + } - const val OSMAND_START_LOCATION_SHARING_SERVICE_ACTION = "osmand_start_location_sharing_service_action" - const val OSMAND_PAUSE_LOCATION_SHARING_SERVICE_ACTION = "osmand_pause_location_sharing_service_action" - const val OSMAND_STOP_LOCATION_SHARING_SERVICE_ACTION = "osmand_stop_location_sharing_service_action" - const val GROUP_NAME = "share_location" - } + override val type: TelegramNotification.NotificationType + get() = TelegramNotification.NotificationType.SHARE_LOCATION - private var wasNoDataDismissed: Boolean = false - private var lastBuiltNoData: Boolean = false + override val priority: Int + get() = NotificationCompat.PRIORITY_DEFAULT - override val type: TelegramNotification.NotificationType - get() = TelegramNotification.NotificationType.SHARE_LOCATION + override val isActive: Boolean + get() { + val service = app.myLocationService + return isEnabled && service != null + } - override val priority: Int - get() = NotificationCompat.PRIORITY_DEFAULT + override val isEnabled: Boolean + get() = app.settings.hasAnyChatToShareLocation() - override val isActive: Boolean - get() { - val service = app.myLocationService - return isEnabled && service != null - } + override val telegramNotificationId: Int + get() = TelegramNotification.SHARE_LOCATION_NOTIFICATION_SERVICE_ID - override val isEnabled: Boolean - get() = app.settings.hasAnyChatToShareLocation() + override val telegramWearableNotificationId: Int + get() = TelegramNotification.WEAR_SHARE_LOCATION_NOTIFICATION_SERVICE_ID - override val osmandNotificationId: Int - get() = TelegramNotification.SHARE_LOCATION_NOTIFICATION_SERVICE_ID + override fun buildNotification(wearable: Boolean): NotificationCompat.Builder { + icon = R.drawable.ic_action_polygom_dark + val shareLocationHelper = app.shareLocationHelper + val sharedDistance = shareLocationHelper.distance.toFloat() + color = ContextCompat.getColor(app, R.color.osmand_orange) + val notificationTitle = (app.getString(R.string.sharing_location) + " • " + + Algorithms.formatDuration((shareLocationHelper.duration / 1000).toInt(), true)) + val notificationText = (app.getString(R.string.shared_string_distance) + + ": " + OsmandFormatter.getFormattedDistance(sharedDistance, app)) - override val osmandWearableNotificationId: Int - get() = TelegramNotification.WEAR_SHARE_LOCATION_NOTIFICATION_SERVICE_ID - - init { - app.registerReceiver(object : BroadcastReceiver() { - - override fun onReceive(context: Context, intent: Intent) { - app.shareLocationHelper.startSharingLocation() - } - }, IntentFilter(OSMAND_START_LOCATION_SHARING_SERVICE_ACTION)) - - app.registerReceiver(object : BroadcastReceiver() { - - override fun onReceive(context: Context, intent: Intent) { - app.shareLocationHelper.pauseSharingLocation() - } - }, IntentFilter(OSMAND_PAUSE_LOCATION_SHARING_SERVICE_ACTION)) - - app.registerReceiver(object : BroadcastReceiver() { - - override fun onReceive(context: Context, intent: Intent) { - app.shareLocationHelper.stopSharingLocation() - } - }, IntentFilter(OSMAND_STOP_LOCATION_SHARING_SERVICE_ACTION)) - } - - override fun onNotificationDismissed() { - if (!wasNoDataDismissed) { - wasNoDataDismissed = lastBuiltNoData - } - } - - override fun buildNotification(wearable: Boolean): NotificationCompat.Builder? { - if (!isEnabled) { - return null - } - val notificationTitle: String - val notificationText: String - color = 0 - icon = R.drawable.ic_action_polygom_dark - val shareLocationHelper = app.shareLocationHelper - val isSharingLocation = shareLocationHelper.sharingLocation - val sharedDistance = shareLocationHelper.distance.toFloat() - ongoing = true - lastBuiltNoData = false - if (isSharingLocation) { - color = ContextCompat.getColor(app, R.color.osmand_orange) - notificationTitle = (app.getString(R.string.sharing_location) + " • " - + Algorithms.formatDuration((shareLocationHelper.duration / 1000).toInt(), true)) - notificationText = (app.getString(R.string.shared_string_distance) - + ": " + OsmandFormatter.getFormattedDistance(sharedDistance, app)) - } else { - if (sharedDistance > 0) { - notificationTitle = (app.getString(R.string.shared_string_paused) + " • " - + Algorithms.formatDuration((shareLocationHelper.duration / 1000).toInt(), true)) - notificationText = (app.getString(R.string.shared_string_distance) - + ": " + OsmandFormatter.getFormattedDistance(sharedDistance, app)) - } else { - ongoing = false - notificationTitle = app.getString(R.string.share_location) - notificationText = app.getString(R.string.shared_string_no_data) - lastBuiltNoData = true - } - } - - if ((wasNoDataDismissed || !app.settings.showNotificationAlways) && !ongoing) { - return null - } - - val notificationBuilder = createBuilder(wearable) - .setContentTitle(notificationTitle) - .setStyle(NotificationCompat.BigTextStyle().bigText(notificationText)) - - val stopIntent = Intent(OSMAND_STOP_LOCATION_SHARING_SERVICE_ACTION) - val stopPendingIntent = PendingIntent.getBroadcast(app, 0, stopIntent, - PendingIntent.FLAG_UPDATE_CURRENT) - if (isSharingLocation) { - if (app.shareLocationHelper.distance > 0) { - val pauseIntent = Intent(OSMAND_PAUSE_LOCATION_SHARING_SERVICE_ACTION) - val pausePendingIntent = PendingIntent.getBroadcast(app, 0, pauseIntent, - PendingIntent.FLAG_UPDATE_CURRENT) - notificationBuilder.addAction(R.drawable.ic_pause, - app.getString(R.string.shared_string_pause), pausePendingIntent) - notificationBuilder.addAction(R.drawable.ic_action_rec_stop, - app.getString(R.string.shared_string_stop), stopPendingIntent) - } else { - notificationBuilder.addAction(R.drawable.ic_action_rec_stop, - app.getString(R.string.shared_string_stop), stopPendingIntent) - } - } else { - val startIntent = Intent(OSMAND_START_LOCATION_SHARING_SERVICE_ACTION) - val startPendingIntent = PendingIntent.getBroadcast(app, 0, startIntent, - PendingIntent.FLAG_UPDATE_CURRENT) - if (sharedDistance > 0) { - notificationBuilder.addAction(R.drawable.ic_action_rec_start, - app.getString(R.string.shared_string_continue), startPendingIntent) - notificationBuilder.addAction(R.drawable.ic_action_rec_stop, - app.getString(R.string.shared_string_stop), stopPendingIntent) - } else { - notificationBuilder.addAction(R.drawable.ic_action_rec_start, - app.getString(R.string.shared_string_start), startPendingIntent) - } - } - - return notificationBuilder - } + return createBuilder(wearable) + .setContentTitle(notificationTitle) + .setStyle(NotificationCompat.BigTextStyle().bigText(notificationText)) + } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/notifications/ShowLocationNotification.kt b/OsmAnd-telegram/src/net/osmand/telegram/notifications/ShowLocationNotification.kt new file mode 100644 index 0000000000..f1e38eb02d --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/notifications/ShowLocationNotification.kt @@ -0,0 +1,45 @@ +package net.osmand.telegram.notifications + +import android.support.v4.app.NotificationCompat +import net.osmand.telegram.R +import net.osmand.telegram.TelegramApplication + +class ShowLocationNotification(app: TelegramApplication) : TelegramNotification(app, GROUP_NAME) { + + companion object { + + const val GROUP_NAME = "show_location" + } + + override val type: TelegramNotification.NotificationType + get() = NotificationType.SHOW_LOCATION + + override val priority: Int + get() = NotificationCompat.PRIORITY_DEFAULT + + override val isActive: Boolean + get() { + val service = app.userLocationService + return isEnabled && service != null + } + + override val isEnabled: Boolean + get() = app.settings.hasAnyChatToShowOnMap() + + override val telegramNotificationId: Int + get() = TelegramNotification.SHOW_LOCATION_NOTIFICATION_SERVICE_ID + + override val telegramWearableNotificationId: Int + get() = TelegramNotification.WEAR_SHOW_LOCATION_NOTIFICATION_SERVICE_ID + + override fun buildNotification(wearable: Boolean): NotificationCompat.Builder { + val notificationTitle: String = app.getString(R.string.show_users_on_map) + val notificationText: String = app.getString(R.string.active_chats) + ": " + app.settings.getShowOnMapChatsCount() + color = 0 + icon = R.drawable.ic_action_view + + return createBuilder(wearable) + .setContentTitle(notificationTitle) + .setStyle(NotificationCompat.BigTextStyle().bigText(notificationText)) + } +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/notifications/TelegramNotification.kt b/OsmAnd-telegram/src/net/osmand/telegram/notifications/TelegramNotification.kt index c333fb3471..135d671c8d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/notifications/TelegramNotification.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/notifications/TelegramNotification.kt @@ -1,134 +1,90 @@ package net.osmand.telegram.notifications import android.annotation.SuppressLint -import android.app.Notification import android.app.PendingIntent -import android.content.Context import android.content.Intent import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationManagerCompat - import net.osmand.telegram.MainActivity import net.osmand.telegram.TelegramApplication abstract class TelegramNotification(protected var app: TelegramApplication, val groupName: String) { - protected var ongoing = true - protected var color: Int = 0 - protected var icon: Int = 0 - var isTop: Boolean = false - abstract val type: NotificationType + companion object { - abstract val osmandNotificationId: Int + const val SHARE_LOCATION_NOTIFICATION_SERVICE_ID = 6 + const val SHOW_LOCATION_NOTIFICATION_SERVICE_ID = 7 - abstract val osmandWearableNotificationId: Int + const val WEAR_SHARE_LOCATION_NOTIFICATION_SERVICE_ID = 1006 + const val WEAR_SHOW_LOCATION_NOTIFICATION_SERVICE_ID = 1006 + } - abstract val priority: Int + protected var ongoing = true + protected var color: Int = 0 + protected var icon: Int = 0 - abstract val isActive: Boolean + abstract val type: NotificationType - abstract val isEnabled: Boolean + abstract val telegramNotificationId: Int - enum class NotificationType { - SHARE_LOCATION - } + abstract val telegramWearableNotificationId: Int - @SuppressLint("InlinedApi") - protected fun createBuilder(wearable: Boolean): NotificationCompat.Builder { - val contentIntent = Intent(app, MainActivity::class.java) - val contentPendingIntent = PendingIntent.getActivity(app, 0, contentIntent, - PendingIntent.FLAG_UPDATE_CURRENT) - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - app.notificationHelper.createNotificationChannel() - } - val builder = NotificationCompat.Builder(app, NotificationHelper.NOTIFICATION_CHANEL_ID) - .setVisibility(android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC) - .setPriority(if (isTop) NotificationCompat.PRIORITY_HIGH else priority) - .setOngoing(ongoing && !wearable) - .setContentIntent(contentPendingIntent) - .setDeleteIntent(NotificationDismissReceiver.createIntent(app, type)) - .setGroup(groupName).setGroupSummary(!wearable) + abstract val priority: Int - if (color != 0) { - builder.color = color - } - if (icon != 0) { - builder.setSmallIcon(icon) - } + abstract val isActive: Boolean - return builder - } + abstract val isEnabled: Boolean - abstract fun buildNotification(wearable: Boolean): NotificationCompat.Builder? + enum class NotificationType { + SHARE_LOCATION, + SHOW_LOCATION + } - fun setupNotification(notification: Notification) {} + @SuppressLint("InlinedApi") + protected fun createBuilder(wearable: Boolean): NotificationCompat.Builder { + val contentIntent = Intent(app, MainActivity::class.java) + val contentPendingIntent = PendingIntent.getActivity(app, 0, contentIntent, + PendingIntent.FLAG_UPDATE_CURRENT) + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + app.notificationHelper.createNotificationChannel() + } + val builder = NotificationCompat.Builder(app, NotificationHelper.NOTIFICATION_CHANEL_ID) + .setVisibility(android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC) + .setPriority(priority) + .setOngoing(ongoing && !wearable) + .setContentIntent(contentPendingIntent) + .setGroup(groupName).setGroupSummary(!wearable) - open fun onNotificationDismissed() {} + if (color != 0) { + builder.color = color + } + if (icon != 0) { + builder.setSmallIcon(icon) + } - private fun notifyWearable(notificationManager: NotificationManagerCompat) { - val wearNotificationBuilder = buildNotification(true) - if (wearNotificationBuilder != null) { - val wearNotification = wearNotificationBuilder.build() - notificationManager.notify(osmandWearableNotificationId, wearNotification) - } - } + return builder + } - fun showNotification(): Boolean { - val notificationManager = NotificationManagerCompat.from(app) - if (isEnabled) { - val notificationBuilder = buildNotification(false) - if (notificationBuilder != null) { - val notification = notificationBuilder.build() - setupNotification(notification) - notificationManager.notify(if (isTop) TOP_NOTIFICATION_SERVICE_ID else osmandNotificationId, notification) - notifyWearable(notificationManager) - return true - } - } - return false - } + abstract fun buildNotification(wearable: Boolean): NotificationCompat.Builder - fun refreshNotification(): Boolean { - val notificationManager = NotificationManagerCompat.from(app) - if (isEnabled) { - val notificationBuilder = buildNotification(false) - if (notificationBuilder != null) { - val notification = notificationBuilder.build() - setupNotification(notification) - if (isTop) { - notificationManager.cancel(osmandNotificationId) - notificationManager.notify(TOP_NOTIFICATION_SERVICE_ID, notification) - } else { - notificationManager.notify(osmandNotificationId, notification) - } - notifyWearable(notificationManager) - return true - } else { - notificationManager.cancel(osmandNotificationId) - } - } else { - notificationManager.cancel(osmandNotificationId) - } - return false - } + private fun notifyWearable(notificationManager: NotificationManagerCompat) { + val wearNotificationBuilder = buildNotification(true) + val wearNotification = wearNotificationBuilder.build() + notificationManager.notify(telegramWearableNotificationId, wearNotification) + } - fun removeNotification() { - val notificationManager = NotificationManagerCompat.from(app) - notificationManager.cancel(osmandNotificationId) - notificationManager.cancel(osmandWearableNotificationId) - } - - fun closeSystemDialogs(context: Context) { - val it = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) - context.sendBroadcast(it) - } - - companion object { - - const val SHARE_LOCATION_NOTIFICATION_SERVICE_ID = 6 - const val TOP_NOTIFICATION_SERVICE_ID = 100 - - const val WEAR_SHARE_LOCATION_NOTIFICATION_SERVICE_ID = 1006 - } + fun refreshNotification(): Boolean { + val notificationManager = NotificationManagerCompat.from(app) + if (isEnabled) { + val notificationBuilder = buildNotification(false) + val notification = notificationBuilder.build() + notificationManager.notify(telegramNotificationId, notification) + notifyWearable(notificationManager) + return true + } else { + notificationManager.cancel(telegramNotificationId) + } + return false + } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/services/MyLocationService.kt b/OsmAnd-telegram/src/net/osmand/telegram/services/MyLocationService.kt index 8369910450..5390ee6cb2 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/services/MyLocationService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/services/MyLocationService.kt @@ -12,132 +12,122 @@ import android.os.Handler import android.os.IBinder import android.util.Log import android.widget.Toast -import net.osmand.telegram.notifications.TelegramNotification import net.osmand.PlatformUtil import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication class MyLocationService : Service(), LocationListener { - private val binder = LocationServiceBinder() - private fun app() = application as TelegramApplication + private val binder = LocationServiceBinder() + private fun app() = application as TelegramApplication - var handler: Handler? = null + var handler: Handler? = null - class LocationServiceBinder : Binder() + class LocationServiceBinder : Binder() - override fun onBind(intent: Intent): IBinder? { - return binder - } + override fun onBind(intent: Intent): IBinder? { + return binder + } - fun stopIfNeeded(ctx: Context) { - val serviceIntent = Intent(ctx, MyLocationService::class.java) - ctx.stopService(serviceIntent) - } + fun stopIfNeeded(ctx: Context) { + val serviceIntent = Intent(ctx, MyLocationService::class.java) + ctx.stopService(serviceIntent) + } - override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { - handler = Handler() - val app = app() + override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { + handler = Handler() + val app = app() - app.myLocationService = this + app.myLocationService = this - // requesting - // request location updates - val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager - try { - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, this@MyLocationService) - } catch (e: SecurityException) { - Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show() - Log.d(PlatformUtil.TAG, "Location service permission not granted") - } catch (e: IllegalArgumentException) { - Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show() - Log.d(PlatformUtil.TAG, "GPS location provider not available") - } + // requesting + // request location updates + val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager + try { + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, this@MyLocationService) + } catch (e: SecurityException) { + Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show() + Log.d(PlatformUtil.TAG, "Location service permission not granted") + } catch (e: IllegalArgumentException) { + Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show() + Log.d(PlatformUtil.TAG, "GPS location provider not available") + } - val notification = app.notificationHelper.buildTopNotification() - if (notification != null) { - startForeground(TelegramNotification.TOP_NOTIFICATION_SERVICE_ID, notification) - app.notificationHelper.refreshNotification(TelegramNotification.NotificationType.SHARE_LOCATION) - //app.notificationHelper.refreshNotifications() - } - return Service.START_REDELIVER_INTENT - } + val shareLocationNotification = app.notificationHelper.shareLocationNotification + val notification = app.notificationHelper.buildNotification(shareLocationNotification) + startForeground(shareLocationNotification.telegramNotificationId, notification) + app.notificationHelper.refreshNotification(shareLocationNotification.type) + return Service.START_REDELIVER_INTENT + } - override fun onDestroy() { - super.onDestroy() - val app = app() - app.myLocationService = null + override fun onDestroy() { + super.onDestroy() + val app = app() + app.myLocationService = null - // remove updates - val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager - try { - locationManager.removeUpdates(this) - } catch (e: SecurityException) { - Log.d(PlatformUtil.TAG, "Location service permission not granted") - } + // remove updates + val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager + try { + locationManager.removeUpdates(this) + } catch (e: SecurityException) { + Log.d(PlatformUtil.TAG, "Location service permission not granted") + } - // remove notification - stopForeground(java.lang.Boolean.TRUE) - app.notificationHelper.updateTopNotification() + // remove notification + stopForeground(java.lang.Boolean.TRUE) + } - app.runInUIThread({ - app.notificationHelper.refreshNotification(TelegramNotification.NotificationType.SHARE_LOCATION) - //app.notificationHelper.refreshNotifications() - }, 500) - } + override fun onLocationChanged(l: Location?) { + if (l != null) { + val location = convertLocation(l) + app().shareLocationHelper.updateLocation(location) + } + } - override fun onLocationChanged(l: Location?) { - if (l != null) { - val location = convertLocation(l) - app().shareLocationHelper.updateLocation(location) - } - } - - override fun onProviderDisabled(provider: String) { - Toast.makeText(this, getString(R.string.location_service_no_gps_available), Toast.LENGTH_LONG).show() - } + override fun onProviderDisabled(provider: String) { + Toast.makeText(this, getString(R.string.location_service_no_gps_available), Toast.LENGTH_LONG).show() + } - override fun onProviderEnabled(provider: String) {} + override fun onProviderEnabled(provider: String) {} - override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {} + override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {} - override fun onTaskRemoved(rootIntent: Intent) { - val app = app() - app.notificationHelper.removeNotifications() - if (app.myLocationService != null) { - // Do not stop service after UI task was dismissed - //this@MyLocationService.stopSelf() - } - } + override fun onTaskRemoved(rootIntent: Intent) { + val app = app() + if (app.myLocationService != null) { + // Do not stop service after UI task was dismissed + //this@MyLocationService.stopSelf() + } + } - companion object { + companion object { - fun convertLocation(l: Location?): net.osmand.Location? { - if (l == null) { - return null - } - val r = net.osmand.Location(l.provider) - r.latitude = l.latitude - r.longitude = l.longitude - r.time = l.time - if (l.hasAccuracy()) { - r.accuracy = l.accuracy - } - if (l.hasSpeed()) { - r.speed = l.speed - } - if (l.hasAltitude()) { - r.altitude = l.altitude - } - if (l.hasBearing()) { - r.bearing = l.bearing - } - if (l.hasAltitude()) { - r.altitude = l.altitude - } - return r - } - } + fun convertLocation(l: Location?): net.osmand.Location? { + if (l == null) { + return null + } + val r = net.osmand.Location(l.provider) + r.latitude = l.latitude + r.longitude = l.longitude + r.time = l.time + if (l.hasAccuracy()) { + r.accuracy = l.accuracy + } + if (l.hasSpeed()) { + r.speed = l.speed + } + if (l.hasAltitude()) { + r.altitude = l.altitude + } + if (l.hasBearing()) { + r.bearing = l.bearing + } + if (l.hasAltitude()) { + r.altitude = l.altitude + } + return r + } + } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/services/UserLocationService.kt b/OsmAnd-telegram/src/net/osmand/telegram/services/UserLocationService.kt index ca205cc9c6..a7afdece16 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/services/UserLocationService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/services/UserLocationService.kt @@ -9,7 +9,6 @@ import android.os.Handler import android.os.IBinder import net.osmand.telegram.TelegramApplication import net.osmand.telegram.helpers.TelegramHelper.TelegramIncomingMessagesListener -import net.osmand.telegram.notifications.TelegramNotification import org.drinkless.td.libcore.telegram.TdApi import java.util.concurrent.Executors @@ -46,9 +45,10 @@ class UserLocationService : Service(), TelegramIncomingMessagesListener { app.userLocationService = this app.telegramHelper.incomingMessagesListener = this - //val notification = app.notificationHelper.buildTopNotification() - //startForeground(TelegramNotification.TOP_NOTIFICATION_SERVICE_ID, notification) - + val showLocationNotification = app.notificationHelper.showLocationNotification + val notification = app.notificationHelper.buildNotification(showLocationNotification) + startForeground(showLocationNotification.telegramNotificationId, notification) + app.notificationHelper.refreshNotification(showLocationNotification.type) return Service.START_REDELIVER_INTENT }