GenericNotificationClass.kt 6.8 KB
Newer Older
1 2
package it.lavermicocca.giacomo.beautifulnotification.notifications

3
import android.app.*
4 5 6 7 8 9 10 11
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.v4.app.NotificationCompat
import android.widget.RemoteViews
import it.lavermicocca.giacomo.beautifulnotification.MainActivity
import it.lavermicocca.giacomo.beautifulnotification.R
import it.lavermicocca.giacomo.beautifulnotification.SwitchButtonListener
12
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
13

14
class GenericNotificationClass(val service: Service, val text : String, val status:Short) {
15

16 17 18
    private val builder:NotificationCompat.Builder
    private val notificationManager:NotificationManager
    private val notification:Notification
19
    private val CHANNEL_ID = "my_channel"
20

21
    enum class STATUS {none, garage, cancello, all, portone, brightness, led, garageCancello, lastCommand}
22

23
    init {
24
        val ns = Context.NOTIFICATION_SERVICE
25
        notificationManager = service.getSystemService(ns) as NotificationManager
26

27
        builder = NotificationCompat.Builder(service.applicationContext, CHANNEL_ID)
28 29 30 31 32
                .setContentTitle("TITLE")
                .setContentText("BODY")
                .setSmallIcon(R.drawable.ic_stat_notification)
                .setAutoCancel(true)
                .setTicker("Ticker TEXT")
33 34 35 36 37 38
                .setOnlyAlertOnce(true)
        notification = builder.build()
    }

    fun startNotification() {
        when (status){
39 40
            Helper.FULL_ON -> fullOn(false)
            Helper.FULL_ON_STOP_PROGRESS -> fullOn(true)
41
            Helper.FULL_OFF -> fullOff()
42
            Helper.FULL_OFF_ERROR -> fullOffError()
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        }
    }

    private fun fullOff() {
        val notificationView = RemoteViews(service.packageName, R.layout.mynotification_off)

        notificationView.setTextViewText(R.id.text_response, text)

        //the intent that is started when the notification is clicked (works)
        val notificationIntent = Intent(service, MainActivity::class.java)
        val pendingNotificationIntent = PendingIntent.getActivity(service, 0, notificationIntent, 0)

        notification.contentView = notificationView
        notification.contentIntent = pendingNotificationIntent
        notification.flags = notification.flags or Notification.FLAG_NO_CLEAR

59 60 61 62 63
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, "Channel beauty notification", NotificationManager.IMPORTANCE_HIGH)
            channel.lockscreenVisibility = Notification.VISIBILITY_SECRET
            notificationManager.createNotificationChannel(channel)
        }
64

65 66
        service.startForeground(1, notification)
    }
67

68 69
    private fun fullOffError() {
        val notificationView = RemoteViews(service.packageName, R.layout.mynotification_off_error)
70

71
        notificationView.setTextViewText(R.id.text_response, text)
72

73 74 75 76 77 78 79
        //the intent that is started when the notification is clicked (works)
        val notificationIntent = Intent(service, MainActivity::class.java)
        val pendingNotificationIntent = PendingIntent.getActivity(service, 0, notificationIntent, 0)

        notification.contentView = notificationView
        notification.contentIntent = pendingNotificationIntent
        notification.flags = notification.flags or Notification.FLAG_NO_CLEAR
80 81

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
82 83
            val channel = NotificationChannel(CHANNEL_ID, "Channel beauty notification", NotificationManager.IMPORTANCE_HIGH)
            channel.lockscreenVisibility = Notification.VISIBILITY_SECRET
84 85 86 87 88 89
            notificationManager.createNotificationChannel(channel)
        }

        service.startForeground(1, notification)
    }

90 91 92
    fun fullOn(stopProgress: Boolean){

        var notificationView = RemoteViews(service.packageName, R.layout.mynotification)
93

94 95 96
        if(stopProgress) {
            notificationView = RemoteViews(service.packageName, R.layout.mynotification_stop_progress)
        }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

        notificationView.setTextViewText(R.id.text_response, text)

        //the intent that is started when the notification is clicked (works)
        val notificationIntent = Intent(service, MainActivity::class.java)
        val pendingNotificationIntent = PendingIntent.getActivity(service, 0, notificationIntent, 0)

        notification.contentView = notificationView
        notification.contentIntent = pendingNotificationIntent
        notification.flags = notification.flags or Notification.FLAG_NO_CLEAR

        //this is the intent that is supposed to be called when the
        //button is clicked

        val switchIntent = Intent(service, SwitchButtonListener::class.java)

        prepareData(switchIntent, STATUS.lastCommand)
        val lastCommand = PendingIntent.getBroadcast(service, 0, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)

        prepareData(switchIntent, STATUS.garage)
        val openCloseGarage = PendingIntent.getBroadcast(service, STATUS.garage.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)

        prepareData(switchIntent, STATUS.cancello)
        val openCloseCancello = PendingIntent.getBroadcast(service, STATUS.cancello.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)

        prepareData(switchIntent, STATUS.portone)
        val openClosePortone = PendingIntent.getBroadcast(service, STATUS.portone.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)

125 126 127
        prepareData(switchIntent, STATUS.led)
        val brightness = PendingIntent.getBroadcast(service, STATUS.led.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)

128 129 130
        prepareData(switchIntent, STATUS.garageCancello)
        val garageCancello = PendingIntent.getBroadcast(service, STATUS.garageCancello.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)

131 132 133 134
        notificationView.setOnClickPendingIntent(R.id.last_command, lastCommand)
        notificationView.setOnClickPendingIntent(R.id.open_close_garage, openCloseGarage)
        notificationView.setOnClickPendingIntent(R.id.open_close_cancello, openCloseCancello)
        notificationView.setOnClickPendingIntent(R.id.open_close_portone, openClosePortone)
135
        notificationView.setOnClickPendingIntent(R.id.brightness, brightness)
136
        notificationView.setOnClickPendingIntent(R.id.garage_cancello, garageCancello)
137 138

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
139 140
            val channel = NotificationChannel(CHANNEL_ID, "Channel beauty notification", NotificationManager.IMPORTANCE_HIGH)
            channel.lockscreenVisibility = Notification.VISIBILITY_SECRET
141 142 143 144 145 146
            notificationManager.createNotificationChannel(channel)
        }

        service.startForeground(1, notification)
    }

147 148 149 150 151
    companion object {
        val COMMAND = "COMMAND"
    }

    fun prepareData(i: Intent, status: STATUS){
152 153
        i.putExtra(COMMAND, status.ordinal)
    }
154
}