Guest User

Untitled

a guest
Nov 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. package com.greatdealcompare.app.model.repository.pricealert
  2.  
  3. import android.annotation.SuppressLint
  4. import android.app.Service
  5. import android.content.Intent
  6. import android.os.*
  7. import com.greatdealcompare.app.extension.subscribeIgnoreResult
  8. import com.greatdealcompare.app.model.repository.product.ProductRepository
  9. import javax.inject.Inject
  10. import android.app.NotificationManager
  11. import android.app.PendingIntent
  12. import android.app.TaskStackBuilder
  13. import android.content.Context
  14. import android.support.v4.app.NotificationCompat
  15. import com.greatdealcompare.app.BuildConfig
  16. import com.greatdealcompare.app.R
  17. import com.greatdealcompare.app.entity.search.Offer
  18. import com.greatdealcompare.app.entity.search.Product
  19. import com.greatdealcompare.app.toothpick.DI
  20. import com.greatdealcompare.app.toothpick.module.AppModule
  21. import com.greatdealcompare.app.toothpick.module.DatabaseModule
  22. import com.greatdealcompare.app.toothpick.module.MainActivityModule
  23. import com.greatdealcompare.app.toothpick.module.ServerModule
  24. import com.greatdealcompare.app.ui.launch.MainActivity
  25. import toothpick.Toothpick
  26.  
  27. @SuppressLint("Registered")
  28. class PriceAlertService : Service() {
  29. private val timeToCheck = 1000 * 30
  30. private var mServiceLooper: Looper? = null
  31. private var mServiceHandler: ServiceHandler? = null
  32.  
  33. @Inject lateinit var priceAlertRepository: PriceAlertRepository
  34. @Inject lateinit var productRepository: ProductRepository
  35.  
  36. private inner class ServiceHandler(looper: Looper) : Handler(looper) {
  37. override fun handleMessage(msg: Message) {
  38. var time = System.currentTimeMillis()
  39. while (true) {
  40. synchronized(this) {
  41. try {
  42. if (System.currentTimeMillis() - time > timeToCheck) {
  43. time = System.currentTimeMillis()
  44.  
  45. priceAlertRepository.getPriceAlerts()
  46. .doOnSuccess { priceAlert ->
  47. priceAlert.forEach { product ->
  48. if (product.upc.isNotEmpty()) {
  49. productRepository.getProduct(
  50. upc = product.upc
  51. )
  52. .doOnSuccess { updateProduct ->
  53. if (updateProduct.minPrice <= updateProduct.minPrice) {
  54. sendPush(updateProduct)
  55. }
  56. }
  57. .subscribeIgnoreResult()
  58. }
  59. else if (product.offerId.isNotEmpty()) {
  60. productRepository.getOffer(
  61. offerId = product.offerId
  62. )
  63. .doOnSuccess { updateProduct ->
  64. if (updateProduct.basePrice <= updateProduct.basePrice) {
  65. sendPush(updateProduct)
  66. }
  67. }
  68. .subscribeIgnoreResult()
  69. }
  70. }
  71. }
  72. .subscribeIgnoreResult()
  73. }
  74. } catch (e: Exception) { }
  75. }
  76. }
  77. }
  78. }
  79.  
  80. override fun onCreate() {
  81. val thread = HandlerThread("ServiceStartArguments",
  82. Process.THREAD_PRIORITY_BACKGROUND)
  83. thread.start()
  84.  
  85. Toothpick.openScopes(DI.DATA_SCOPE, DI.PRICE_ALERT_SERVICE_SCOPE).apply {
  86. Toothpick.inject(this@PriceAlertService, this)
  87. }
  88.  
  89. mServiceLooper = thread.looper
  90. mServiceHandler = ServiceHandler(mServiceLooper!!)
  91. }
  92.  
  93. override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
  94. val msg = mServiceHandler!!.obtainMessage()
  95. msg.arg1 = startId
  96. mServiceHandler!!.sendMessage(msg)
  97.  
  98. return START_STICKY
  99. }
  100.  
  101. override fun onBind(intent: Intent): IBinder? {
  102. return null
  103. }
  104.  
  105. override fun onDestroy() {
  106. }
  107.  
  108. private fun sendPush(product: Product) {
  109. val mBuilder = NotificationCompat.Builder(this)
  110. .setSmallIcon(R.drawable.ic_alert_green)
  111. .setContentTitle("New price: " + product.minPrice.toString() + "!")
  112. .setContentText(product.name)
  113. val notifyID = 1
  114. val resultIntent = Intent(this, MainActivity::class.java).putExtra("deepLink_product", product.upcs[0].upc)
  115.  
  116. val stackBuilder = TaskStackBuilder.create(this)
  117. stackBuilder.addParentStack(MainActivity::class.java)
  118. stackBuilder.addNextIntent(resultIntent)
  119. val resultPendingIntent = stackBuilder.getPendingIntent(
  120. 0,
  121. PendingIntent.FLAG_UPDATE_CURRENT
  122. )
  123. mBuilder.setContentIntent(resultPendingIntent)
  124. val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  125. mNotificationManager.notify(notifyID, mBuilder.build())
  126. }
  127.  
  128. private fun sendPush(offer: Offer) {
  129. val mBuilder = NotificationCompat.Builder(this)
  130. .setSmallIcon(R.drawable.ic_alert_green)
  131. .setContentTitle("New price: " + offer.basePrice.toString() + "!")
  132. .setContentText(offer.name)
  133. val notifyID = 1
  134. val resultIntent = Intent(this, MainActivity::class.java).putExtra("deepLink_offer", offer.id)
  135.  
  136. val stackBuilder = TaskStackBuilder.create(this)
  137. stackBuilder.addParentStack(MainActivity::class.java)
  138. stackBuilder.addNextIntent(resultIntent)
  139. val resultPendingIntent = stackBuilder.getPendingIntent(
  140. 0,
  141. PendingIntent.FLAG_UPDATE_CURRENT
  142. )
  143. mBuilder.setContentIntent(resultPendingIntent)
  144. val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  145. mNotificationManager.notify(notifyID, mBuilder.build())
  146. }
  147. }
Add Comment
Please, Sign In to add comment