Commit 696b8464 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

add notification center

parent d044c9c2
package com.biganto.visual.roompark.data.service.notification
import android.annotation.TargetApi
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roomparkvr.data.repository.db.requery.model.DownloadState
import javax.inject.Inject
import javax.inject.Singleton
/**
* Created by Vladislav Bogdashkin on 13.04.2020.
*/
const val ANDROID_CHANNEL_ID = "com.biganto.visual.androidplayer.data.services.downloader.DownloadManagerService.CHANNEL_ID"
const val TOURS_CHANNEL_ID = "com.biganto.visual.androidplayer.data.services.downloader.DownloadManagerService.TOURS_CHANNEL_ID"
const val DOWNLOAD_SERVICE_ID = 7897
const val TOUR_INFO_SERVICE_ID = 7899
const val NOTIFICATION_INTENT="NOTIFICATION_INTENT_KEY"
const val NOTIFICATION_START_SCREEN="NOTIFICATION_START_SCREEN_KEY"
const val NOTIFICATION_INTENT_SCREEN_TYPE="NOTIFICATION_SHOW_SCREEN"
const val NOTIFICATION_INTENT_STOP_SERVICE_ACTION="STOP_DOWNLOADS_SERVICE"
const val PENDING_REQUEST_CODE=0
@Singleton
class NotificationCenter @Inject constructor(val context: Context) {
private val updateProgressNotificationDelay_Milliseconds= 333
var lastTimeProgressNotificationUpdated = 0L
val actualNotifyManager:NotificationManager
get()= context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
createToursNotificationChannel()
}
}
private val builder = NotificationCompat.Builder(context, ANDROID_CHANNEL_ID)
private val toursNotyBuilder = NotificationCompat.Builder(context, TOURS_CHANNEL_ID)
private val notificationSystemColor = ContextCompat.getColor(context, R.color.colorAccent)
private val icon = BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher_round);
//Заготовка под интерфейсы для равзедения каналов нотификаций по разным инстансам
val foregroundDownloadServiceChannelId = DOWNLOAD_SERVICE_ID
val foregroundDownloadServiceNotification: Notification =
builder
.setContentTitle(context.getString(R.string.notification_content_title))//getString(R.string.app_name))
.setContentText(context.getString(R.string.notification_content_text))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.mipmap.ic_launcher)
.setColor(notificationSystemColor)
.build()
//Заготовка под переход в определенный экран приложения по нажатию на нотификацию
private val toDownloadsIntent =
Intent(context, RoomParkMainActivity::class.java)
.putExtra(NOTIFICATION_INTENT,NOTIFICATION_START_SCREEN)
// .putExtra(NOTIFICATION_INTENT_SCREEN_TYPE,R.id.tab_downloads)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
// private val stopServiceIntent=
// Intent(context, DownloadManagerService::class.java)
// .setAction(NOTIFICATION_INTENT_STOP_SERVICE_ACTION)
fun indeterminateProgressMessage(progress:Int=0
,progressMax:Int=0
,message: String) {
if (progress!=0)
if (System.currentTimeMillis()-lastTimeProgressNotificationUpdated
<updateProgressNotificationDelay_Milliseconds)
return
lastTimeProgressNotificationUpdated=System.currentTimeMillis()
donwloadServiceProgressNotfication(progress, progressMax, true, message)
}
fun completeProgressMessage(message: String){
donwloadServiceProgressNotfication(indeterminate = false,message = message)
}
fun donwloadServiceProgressNotfication(progress:Int=0
,progressMax:Int=0
,indeterminate:Boolean=true
,message: String){
val pendingIntent = PendingIntent.getActivity(context
, PENDING_REQUEST_CODE
, toDownloadsIntent
, PendingIntent.FLAG_ONE_SHOT
)
val notification =
(if (indeterminate) builder else toursNotyBuilder)
// builder
.setOnlyAlertOnce(true)
.setContentTitle(context.getString(R.string.notification_content_title))
.setContentText(message)
.setProgress(progress, progressMax,indeterminate)
.setSmallIcon(R.mipmap.ic_launcher)
.setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE)
.setColor(notificationSystemColor)
.setLargeIcon(icon)
.setContentIntent(pendingIntent)
.setAutoCancel(false)
.build()
actualNotifyManager.notify(
(if (indeterminate) DOWNLOAD_SERVICE_ID else TOUR_INFO_SERVICE_ID)
, notification)
}
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(): String {
val channelId = ANDROID_CHANNEL_ID
val channelName = "Biganto Visual Download Service"
val chan = NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_DEFAULT)
chan.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
chan.enableVibration(false)
chan.lightColor = R.color.colorPrimary;
val service = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(chan)
return channelId
}
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(Build.VERSION_CODES.O)
private fun createToursNotificationChannel(): String {
val channelId = TOURS_CHANNEL_ID
val channelName = "Biganto Visual Tour info update"
val chan = NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_DEFAULT)
chan.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
chan.enableVibration(true)
chan.lightColor = R.color.colorPrimary;
val service = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(chan)
return channelId
}
private fun DownloadState.notyName() =
when(this){
DownloadState.Downloaded ->context.getString(R.string.state_downloaded_notify_message)
DownloadState.Crushed -> context.getString(R.string.state_crushed_notify_message)
DownloadState.Suspended -> context.getString(R.string.state_suspended_notify_message)
DownloadState.Downloading -> context.getString(R.string.state_downloading_notify_message)
else -> context.getString(R.string.state_else_notify_message)
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="178dp"
android:height="91dp"
android:viewportWidth="178"
android:viewportHeight="91">
<group>
<clip-path android:pathData="M73,0L105,0L105,3.156L73,3.156ZM82.617,9.531L95.535,9.531L95.535,12.734L82.617,12.734ZM92.375,12.734L95.535,12.734L95.535,31.781L92.375,31.781ZM82.617,12.734L85.953,12.734L85.953,31.781L82.617,31.781ZM82.617,31.781L95.535,31.781L95.535,34.879L82.617,34.879ZM73,3.156L76.156,3.156L76.156,34.879L73,34.879ZM73,34.879L85.922,34.879L85.922,38.145L73,38.145ZM101.844,3.156L105,3.156L105,41.25L101.844,41.25ZM82.613,38.145L85.922,38.145L85.922,41.25L82.613,41.25ZM82.613,41.25L105,41.25L105,44.379L82.613,44.379ZM82.613,44.379L85.77,44.379L85.77,57.145L82.613,57.145ZM82.613,44.379 M 0,0"/>
<path
android:pathData="M68,-5L110,-5L110,62.145L68,62.145ZM68,-5"
android:fillColor="#40A19B"
android:fillAlpha="1"
android:fillType="nonZero"
android:strokeColor="#00000000"/>
</group>
</vector>
...@@ -100,6 +100,17 @@ ...@@ -100,6 +100,17 @@
<string name="download_tour_cancel_text">Отмена</string> <string name="download_tour_cancel_text">Отмена</string>
<!-- region notifications-->
<string name="notification_content_title">Румянцево Парк</string>
<string name="notification_content_text">Загрузчик</string>
<string name="state_downloaded_notify_message">загружен</string>
<string name="state_crushed_notify_message">произошла ошибка</string>
<string name="state_suspended_notify_message">загрузка приостановлена</string>
<string name="state_downloading_notify_message">загружается</string>
<string name="state_else_notify_message" />
<!--endregion-->
<string name="game_view_content_description" /> <string name="game_view_content_description" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment