Commit ce1a143c authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

add cancel downloading notification button

parent 78fe02f2
......@@ -36,7 +36,7 @@ private const val TIMEOUT_SECONDS=120L
private const val WRITE_SECONDS=120L
private const val READ_SECONDS=120L
val INTERCEPT_LOG_LEVEL = HttpLoggingInterceptor.Level.BODY
val INTERCEPT_LOG_LEVEL = HttpLoggingInterceptor.Level.NONE
@Module
class RetrofitModule{
......
......@@ -47,6 +47,8 @@ private const val META_PREDICTION="/tourMeta_"
private const val META_FILE_TYPE=".json"
const val DOWNLOAD_MANAGER_COMMAND_KEY = "TOURS_DOWNLOAD_MANAGER_COMMAND"
const val DOWNLOAD_MANAGER_ADD_IDS_TO_LOAD_COMMAND = "ADD_TOUR_IDS_TO_QUEUE"
const val DOWNLOAD_MANAGER_STOP_COMMAND = "STOP_SERVICE"
@Singleton
class DownloadManagerService @Inject constructor(
......@@ -117,7 +119,24 @@ class DownloadManagerService @Inject constructor(
return null
}
override fun onStart(intent: Intent?, startId: Int) {
Timber.d("Got intent: $intent")
val v = intent?.extras?.getString(DOWNLOAD_MANAGER_COMMAND_KEY)
Timber.d("Got intent: $v")
if (v == DOWNLOAD_MANAGER_STOP_COMMAND){
Timber.d("Got DOWNLOAD_MANAGER_STOP_COMMAND")
disposable.clear()
stopSelf()
}
super.onStart(intent, startId)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val v = intent?.extras?.getString(DOWNLOAD_MANAGER_COMMAND_KEY)
if (v == DOWNLOAD_MANAGER_STOP_COMMAND){
disposable.clear()
stopSelf()
}
// Timber.d(" GOT INTENT $intent with action ${intent?.action}")
// if (intent?.action == NOTIFICATION_INTENT_STOP_SERVICE_ACTION)
// {
......
......@@ -2,6 +2,8 @@ package com.biganto.visual.roompark.data.service.notification
import android.annotation.TargetApi
import android.app.*
import android.app.PendingIntent.FLAG_ONE_SHOT
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
......@@ -11,6 +13,9 @@ 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.roompark.data.service.download.DOWNLOAD_MANAGER_COMMAND_KEY
import com.biganto.visual.roompark.data.service.download.DOWNLOAD_MANAGER_STOP_COMMAND
import com.biganto.visual.roompark.data.service.download.DownloadManagerService
import com.biganto.visual.roomparkvr.data.repository.db.requery.model.DownloadState
import javax.inject.Inject
import javax.inject.Singleton
......@@ -135,7 +140,7 @@ class NotificationCenter @Inject constructor(val context: Application) : INotif
val pendingIntent = PendingIntent.getActivity(context
, PENDING_REQUEST_CODE
, toDownloadsIntent
, PendingIntent.FLAG_ONE_SHOT
, FLAG_ONE_SHOT
)
......@@ -160,36 +165,77 @@ class NotificationCenter @Inject constructor(val context: Application) : INotif
, noty)
}
private val cancelServicePendingIntent: NotificationCompat.Action
by lazy {
val stopServiceIntent = Intent(context, DownloadManagerService::class.java)
stopServiceIntent.putExtra(DOWNLOAD_MANAGER_COMMAND_KEY, DOWNLOAD_MANAGER_STOP_COMMAND)
val pendingStopServiceIntent = PendingIntent.getService(context
, 546
, stopServiceIntent
, FLAG_UPDATE_CURRENT
)
NotificationCompat.Action.Builder(
R.drawable.ic_back,
"Отмена",
pendingStopServiceIntent).build()
}
override fun donwloadServiceProgressNotfication(progress:Int
, progressMax:Int
, indeterminate:Boolean
, message: String){
val pendingIntent = PendingIntent.getActivity(context
val b = (if (indeterminate) updateProgressNotificationIndeterminate
else updateProgressNotification)
b.setContentText(message)
b.setProgress(progress, progressMax,indeterminate)
actualNotifyManager.notify(
(if (indeterminate) DOWNLOAD_SERVICE_ID else TOUR_INFO_SERVICE_ID)
, b.build()
)
}
private val defPendingIntent by lazy {
PendingIntent.getActivity(context
, PENDING_REQUEST_CODE
, toDownloadsIntent
, PendingIntent.FLAG_ONE_SHOT
, FLAG_UPDATE_CURRENT
)
}
val notification =
(if (indeterminate) builder else toursNotyBuilder)
// builder
private val updateProgressNotificationIndeterminate by lazy {
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)
.setContentIntent(defPendingIntent)
.addAction(cancelServicePendingIntent)
.setAutoCancel(true)
.build()
}
actualNotifyManager.notify(
(if (indeterminate) DOWNLOAD_SERVICE_ID else TOUR_INFO_SERVICE_ID)
, notification)
private val updateProgressNotification by lazy {
toursNotyBuilder
.setOnlyAlertOnce(true)
.setContentTitle(context.getString(R.string.notification_content_title))
.setSmallIcon(R.mipmap.ic_launcher)
.setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE)
.setColor(notificationSystemColor)
.setLargeIcon(icon)
.setContentIntent(defPendingIntent)
.addAction(cancelServicePendingIntent)
.setAutoCancel(true)
}
@TargetApi(Build.VERSION_CODES.O)
......
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