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 0000000000..a494751378 Binary files /dev/null and b/OsmAnd-telegram/res/drawable-hdpi/ic_action_view.png differ 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 0000000000..12ae65d881 Binary files /dev/null and b/OsmAnd-telegram/res/drawable-mdpi/ic_action_view.png differ 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 0000000000..9f4493c585 Binary files /dev/null and b/OsmAnd-telegram/res/drawable-xhdpi/ic_action_view.png differ diff --git a/OsmAnd-telegram/res/drawable-xxhdpi/ic_action_view.png b/OsmAnd-telegram/res/drawable-xxhdpi/ic_action_view.png new file mode 100644 index 0000000000..9db1626258 Binary files /dev/null and b/OsmAnd-telegram/res/drawable-xxhdpi/ic_action_view.png differ diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index dcb2637d44..ca33f3ca50 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -31,6 +31,8 @@ OsmAnd 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 }