aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt
blob: 7360c191d25b47dadcae33075def708612557b86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
125
126
127
128
129
130
131
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.services

import android.app.job.JobParameters
import androidx.core.app.NotificationManagerCompat
import ca.allanwang.kau.utils.string
import com.pitchedapps.frost.BuildConfig
import com.pitchedapps.frost.R
import com.pitchedapps.frost.dbflow.CookieModel
import com.pitchedapps.frost.dbflow.loadFbCookiesSync
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.frostEvent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * Created by Allan Wang on 2017-06-14.
 *
 * Service to manage notifications
 * Will periodically check through all accounts in the db and send notifications when appropriate
 *
 * All fetching is done through parsers
 */
class NotificationService : BaseJobService() {

    override fun onStopJob(params: JobParameters?): Boolean {
        super.onStopJob(params)
        prepareFinish(true)
        return false
    }

    private var preparedFinish = false

    private fun prepareFinish(abrupt: Boolean) {
        if (preparedFinish)
            return
        preparedFinish = true
        val time = System.currentTimeMillis() - startTime
        L.i { "Notification service has ${if (abrupt) "finished abruptly" else "finished"} in $time ms" }
        frostEvent(
            "NotificationTime",
            "Type" to (if (abrupt) "Service force stop" else "Service"),
            "IM Included" to Prefs.notificationsInstantMessages,
            "Duration" to time
        )
    }

    override fun onStartJob(params: JobParameters?): Boolean {
        super.onStartJob(params)
        L.i { "Fetching notifications" }
        launch {
            try {
                sendNotifications(params)
            } finally {
                if (!isActive)
                    prepareFinish(false)
                jobFinished(params, false)
            }
        }
        return true
    }

    private suspend fun sendNotifications(params: JobParameters?): Unit = withContext(Dispatchers.Default) {
        val currentId = Prefs.userId
        val cookies = loadFbCookiesSync()
        if (!isActive) return@withContext
        val jobId = params?.extras?.getInt(NOTIFICATION_PARAM_ID, -1) ?: -1
        var notifCount = 0
        for (cookie in cookies) {
            if (!isActive) break
            val current = cookie.id == currentId
            if (Prefs.notificationsGeneral &&
                (current || Prefs.notificationAllAccounts)
            )
                notifCount += fetch(jobId, NotificationType.GENERAL, cookie)
            if (Prefs.notificationsInstantMessages &&
                (current || Prefs.notificationsImAllAccounts)
            )
                notifCount += fetch(jobId, NotificationType.MESSAGE, cookie)
        }

        L.i { "Sent $notifCount notifications" }
        if (notifCount == 0 && jobId == NOTIFICATION_JOB_NOW)
            generalNotification(665, R.string.no_new_notifications, BuildConfig.DEBUG)
    }

    /**
     * Implemented fetch to also notify when an error occurs
     * Also normalized the output to return the number of notifications received
     */
    private fun fetch(jobId: Int, type: NotificationType, cookie: CookieModel): Int {
        val count = type.fetch(this, cookie)
        if (count < 0) {
            if (jobId == NOTIFICATION_JOB_NOW)
                generalNotification(666, R.string.error_notification, BuildConfig.DEBUG)
            return 0
        }
        return count
    }

    private fun logNotif(text: String): NotificationContent? {
        L.eThrow("NotificationService: $text")
        return null
    }

    private fun generalNotification(id: Int, textRes: Int, withDefaults: Boolean) {
        val notifBuilder = frostNotification(NOTIF_CHANNEL_GENERAL)
            .setFrostAlert(withDefaults, Prefs.notificationRingtone)
            .setContentTitle(string(R.string.frost_name))
            .setContentText(string(textRes))
        NotificationManagerCompat.from(this).notify(id, notifBuilder.build())
    }
}