aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt7
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt12
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt14
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt3
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt3
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt42
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt4
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt50
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt194
10 files changed, 309 insertions, 22 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
index c43c31a2..d6f1abe7 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
@@ -104,6 +104,7 @@ import com.pitchedapps.frost.utils.setFrostColors
import com.pitchedapps.frost.views.BadgedIcon
import com.pitchedapps.frost.views.FrostVideoViewer
import com.pitchedapps.frost.views.FrostViewPager
+import com.pitchedapps.frost.widgets.NotificationWidget
import kotlinx.android.synthetic.main.activity_frame_wrapper.*
import kotlinx.android.synthetic.main.view_main_fab.*
import kotlinx.android.synthetic.main.view_main_toolbar.*
@@ -450,7 +451,11 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract,
Runtime.getRuntime().exit(0)
return
}
- if (resultCode and REQUEST_RESTART > 0) return restart()
+ if (resultCode and REQUEST_RESTART > 0) {
+ NotificationWidget.forceUpdate(this)
+ restart()
+ return
+ }
/*
* These results can be stacked
*/
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
index e1b1d4c4..67372e23 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
@@ -20,6 +20,7 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
+import com.pitchedapps.frost.BuildConfig
import org.koin.dsl.module.module
import org.koin.standalone.StandAloneContext
@@ -69,15 +70,22 @@ class FrostDatabase(private val privateDb: FrostPrivateDatabase, private val pub
}
companion object {
+
+ private fun <T : RoomDatabase> RoomDatabase.Builder<T>.frostBuild() = if (BuildConfig.DEBUG) {
+ fallbackToDestructiveMigration().build()
+ } else {
+ build()
+ }
+
fun create(context: Context): FrostDatabase {
val privateDb = Room.databaseBuilder(
context, FrostPrivateDatabase::class.java,
FrostPrivateDatabase.DATABASE_NAME
- ).build()
+ ).frostBuild()
val publicDb = Room.databaseBuilder(
context, FrostPublicDatabase::class.java,
FrostPublicDatabase.DATABASE_NAME
- ).build()
+ ).frostBuild()
return FrostDatabase(privateDb, publicDb)
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt
index 8936d682..d4b51c1e 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt
@@ -64,7 +64,8 @@ data class NotificationEntity(
val timestamp: Long,
val profileUrl: String?,
// Type essentially refers to channel
- val type: String
+ val type: String,
+ val unread: Boolean
) {
constructor(
type: String,
@@ -77,7 +78,8 @@ data class NotificationEntity(
content.text,
content.timestamp,
content.profileUrl,
- type
+ type,
+ content.unread
)
}
@@ -94,7 +96,8 @@ data class NotificationContentEntity(
title = notif.title,
text = notif.text,
timestamp = notif.timestamp,
- profileUrl = notif.profileUrl
+ profileUrl = notif.profileUrl,
+ unread = notif.unread
)
}
@@ -134,8 +137,11 @@ interface NotificationDao {
suspend fun NotificationDao.deleteAll() = dao { _deleteAll() }
-suspend fun NotificationDao.selectNotifications(userId: Long, type: String): List<NotificationContent> = dao {
+fun NotificationDao.selectNotificationsSync(userId: Long, type: String): List<NotificationContent> =
_selectNotifications(userId, type).map { it.toNotifContent() }
+
+suspend fun NotificationDao.selectNotifications(userId: Long, type: String): List<NotificationContent> = dao {
+ selectNotificationsSync(userId, type)
}
/**
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt
index 80ed8ee8..9979cd2b 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt
@@ -64,7 +64,8 @@ data class FrostMessages(
title = title,
text = content ?: "",
timestamp = time,
- profileUrl = img
+ profileUrl = img,
+ unread = unread
)
}
}.toList()
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt
index 199fc685..c22524ad 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt
@@ -53,7 +53,8 @@ data class FrostNotifs(
title = null,
text = content,
timestamp = time,
- profileUrl = img
+ profileUrl = img,
+ unread = unread
)
}
}.toList()
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt
index 6f039784..1c37bc29 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt
@@ -61,7 +61,7 @@ private val _40_DP = 40.dpToPx
* Enum to handle notification creations
*/
enum class NotificationType(
- private val channelId: String,
+ val channelId: String,
private val overlayContext: OverlayContext,
private val fbItem: FbItem,
private val parser: FrostParser<ParseNotification>,
@@ -95,8 +95,8 @@ enum class NotificationType(
*/
internal open fun bindRequest(content: NotificationContent, cookie: String): (BaseBundle.() -> Unit)? = null
- private fun bindRequest(intent: Intent, content: NotificationContent, cookie: String?) {
- cookie ?: return
+ private fun bindRequest(intent: Intent, content: NotificationContent) {
+ val cookie = content.data.cookie ?: return
val binder = bindRequest(content, cookie) ?: return
val bundle = Bundle()
bundle.binder()
@@ -181,23 +181,40 @@ enum class NotificationType(
"Debug Notif",
"Test 123",
System.currentTimeMillis() / 1000,
- "https://www.iconexperience.com/_img/v_collection_png/256x256/shadow/dog.png"
+ "https://www.iconexperience.com/_img/v_collection_png/256x256/shadow/dog.png",
+ false
)
createNotification(context, content).notify(context)
}
/**
+ * Attach content related data to an intent
+ */
+ fun putContentExtra(intent: Intent, content: NotificationContent): Intent {
+ // We will show the notification page for dependent urls. We can trigger a click next time
+ intent.data = Uri.parse(if (content.href.isIndependent) content.href else FbItem.NOTIFICATIONS.url)
+ bindRequest(intent, content)
+ return intent
+ }
+
+ /**
+ * Create a generic content for the provided type and user id.
+ * No content related data is added
+ */
+ fun createCommonIntent(context: Context, userId: Long): Intent {
+ val intent = Intent(context, FrostWebActivity::class.java)
+ intent.putExtra(ARG_USER_ID, userId)
+ overlayContext.put(intent)
+ return intent
+ }
+
+ /**
* Create and submit a new notification with the given [content]
*/
private fun createNotification(context: Context, content: NotificationContent): FrostNotification =
with(content) {
- val intent = Intent(context, FrostWebActivity::class.java)
- // TODO temp fix; we will show notification page for dependent urls. We can trigger a click next time
- intent.data = Uri.parse(if (href.isIndependent) href else FbItem.NOTIFICATIONS.url)
- intent.putExtra(ARG_USER_ID, data.id)
- overlayContext.put(intent)
- bindRequest(intent, content, data.cookie)
-
+ val intent = createCommonIntent(context, content.data.id)
+ putContentExtra(intent, content)
val group = "${groupPrefix}_${data.id}"
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val notifBuilder = context.frostNotification(channelId)
@@ -266,7 +283,8 @@ data class NotificationContent(
val title: String? = null, // defaults to frost title
val text: String,
val timestamp: Long,
- val profileUrl: String?
+ val profileUrl: String?,
+ val unread: Boolean
) {
val notifId = Math.abs(id.toInt())
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt
index 760c681a..0eee5558 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt
@@ -27,6 +27,7 @@ import com.pitchedapps.frost.db.selectAll
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.frostEvent
+import com.pitchedapps.frost.widgets.NotificationWidget
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
@@ -105,6 +106,9 @@ class NotificationService : BaseJobService() {
L.i { "Sent $notifCount notifications" }
if (notifCount == 0 && jobId == NOTIFICATION_JOB_NOW)
generalNotification(665, R.string.no_new_notifications, BuildConfig.DEBUG)
+ if (notifCount > 0) {
+ NotificationWidget.forceUpdate(this@NotificationService)
+ }
}
/**
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt
new file mode 100644
index 00000000..c0feab1e
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019 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.utils
+
+import android.content.Context
+import ca.allanwang.kau.utils.string
+import com.pitchedapps.frost.R
+import java.text.DateFormat
+import java.text.SimpleDateFormat
+import java.util.Calendar
+import java.util.Date
+import java.util.Locale
+
+/**
+ * Converts time in millis to readable date,
+ * eg Apr 24 at 7:32 PM
+ *
+ * With regards to date modifications in calendars,
+ * it appears to respect calendar rules;
+ * see https://stackoverflow.com/a/43227817/4407321
+ */
+fun Long.toReadableTime(context: Context): String {
+ val cal = Calendar.getInstance()
+ cal.timeInMillis = this
+ val timeFormatter = SimpleDateFormat.getTimeInstance(DateFormat.SHORT)
+ val time = timeFormatter.format(Date(this))
+ val day = when {
+ cal >= Calendar.getInstance().apply { add(Calendar.DAY_OF_MONTH, -1) } -> context.string(R.string.today)
+ cal >= Calendar.getInstance().apply { add(Calendar.DAY_OF_MONTH, -2) } -> context.string(R.string.yesterday)
+ else -> {
+ val dayFormatter = SimpleDateFormat("MMM dd", Locale.getDefault())
+ dayFormatter.format(Date(this))
+ }
+ }
+ return context.getString(R.string.time_template, day, time)
+}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
index 711d7e18..76ffd8cd 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
@@ -186,7 +186,7 @@ fun MaterialDialog.Builder.theme(): MaterialDialog.Builder {
}
fun Activity.setFrostTheme(forceTransparent: Boolean = false) {
- val isTransparent = (Color.alpha(Prefs.bgColor) != 255) || forceTransparent
+ val isTransparent = (Color.alpha(Prefs.bgColor) != 255) || (Color.alpha(Prefs.headerColor) != 255) || forceTransparent
if (Prefs.bgColor.isColorDark)
setTheme(if (isTransparent) R.style.FrostTheme_Transparent else R.style.FrostTheme)
else
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt
new file mode 100644
index 00000000..594da00a
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2019 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.widgets
+
+import android.app.PendingIntent
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.graphics.Canvas
+import android.graphics.Paint
+import android.graphics.PorterDuff
+import android.graphics.PorterDuffColorFilter
+import android.graphics.drawable.Icon
+import android.os.Build
+import android.widget.RemoteViews
+import android.widget.RemoteViewsService
+import androidx.annotation.ColorInt
+import androidx.annotation.DrawableRes
+import androidx.annotation.IdRes
+import ca.allanwang.kau.utils.dimenPixelSize
+import ca.allanwang.kau.utils.withAlpha
+import com.pitchedapps.frost.R
+import com.pitchedapps.frost.activities.MainActivity
+import com.pitchedapps.frost.db.NotificationDao
+import com.pitchedapps.frost.db.selectNotificationsSync
+import com.pitchedapps.frost.glide.FrostGlide
+import com.pitchedapps.frost.glide.GlideApp
+import com.pitchedapps.frost.services.NotificationContent
+import com.pitchedapps.frost.services.NotificationType
+import com.pitchedapps.frost.utils.Prefs
+import com.pitchedapps.frost.utils.toReadableTime
+import org.koin.standalone.KoinComponent
+import org.koin.standalone.inject
+
+class NotificationWidget : AppWidgetProvider() {
+
+ override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
+ super.onUpdate(context, appWidgetManager, appWidgetIds)
+ val type = NotificationType.GENERAL
+ val userId = Prefs.userId
+ val intent = NotificationWidgetService.createIntent(context, type, userId)
+ for (id in appWidgetIds) {
+ val views = RemoteViews(context.packageName, R.layout.widget_notifications)
+
+ views.setBackgroundColor(R.id.widget_layout_toolbar, Prefs.headerColor)
+ views.setIcon(R.id.img_frost, context, R.drawable.frost_f_24, Prefs.iconColor)
+ views.setOnClickPendingIntent(
+ R.id.img_frost,
+ PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0)
+ )
+
+ views.setBackgroundColor(R.id.widget_notification_list, Prefs.bgColor)
+ views.setRemoteAdapter(R.id.widget_notification_list, intent)
+
+ val pendingIntentTemplate = PendingIntent.getActivity(
+ context,
+ 0,
+ type.createCommonIntent(context, userId),
+ PendingIntent.FLAG_UPDATE_CURRENT
+ )
+
+ views.setPendingIntentTemplate(R.id.widget_notification_list, pendingIntentTemplate)
+
+ appWidgetManager.updateAppWidget(id, views)
+ }
+ appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_notification_list)
+ }
+
+ companion object {
+ fun forceUpdate(context: Context) {
+ val manager = AppWidgetManager.getInstance(context)
+ val ids = manager.getAppWidgetIds(ComponentName(context, NotificationWidget::class.java))
+ val intent = Intent().apply {
+ action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
+ putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
+ }
+ context.sendBroadcast(intent)
+ }
+ }
+}
+
+private const val NOTIF_WIDGET_TYPE = "notif_widget_type"
+private const val NOTIF_WIDGET_USER_ID = "notif_widget_user_id"
+
+private fun RemoteViews.setBackgroundColor(@IdRes viewId: Int, @ColorInt color: Int) {
+ setInt(viewId, "setBackgroundColor", color)
+}
+
+/**
+ * Adds backward compatibility to setting tinted icons
+ */
+private fun RemoteViews.setIcon(@IdRes viewId: Int, context: Context, @DrawableRes res: Int, @ColorInt color: Int) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ val icon = Icon.createWithResource(context, res).setTint(color).setTintMode(PorterDuff.Mode.SRC_IN)
+ setImageViewIcon(viewId, icon)
+ } else {
+ val bitmap = BitmapFactory.decodeResource(context.resources, res)
+ if (bitmap != null) {
+ val paint = Paint()
+ paint.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
+ val result = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
+ val canvas = Canvas(result)
+ canvas.drawBitmap(bitmap, 0f, 0f, paint)
+ setImageViewBitmap(viewId, result)
+ } else {
+ // Fallback to just icon
+ setImageViewResource(viewId, res)
+ }
+ }
+}
+
+class NotificationWidgetService : RemoteViewsService() {
+ override fun onGetViewFactory(intent: Intent): RemoteViewsFactory = NotificationWidgetDataProvider(this, intent)
+
+ companion object {
+ fun createIntent(context: Context, type: NotificationType, userId: Long): Intent =
+ Intent(context, NotificationWidgetService::class.java)
+ .putExtra(NOTIF_WIDGET_TYPE, type.name)
+ .putExtra(NOTIF_WIDGET_USER_ID, userId)
+ }
+}
+
+class NotificationWidgetDataProvider(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory,
+ KoinComponent {
+
+ private val notifDao: NotificationDao by inject()
+ @Volatile
+ private var content: List<NotificationContent> = emptyList()
+
+ private val type = NotificationType.valueOf(intent.getStringExtra(NOTIF_WIDGET_TYPE))
+
+ private val userId = intent.getLongExtra(NOTIF_WIDGET_USER_ID, -1)
+
+ private val avatarSize = context.dimenPixelSize(R.dimen.avatar_image_size)
+
+ private val glide = GlideApp.with(context).asBitmap()
+
+ private fun loadNotifications() {
+ content = notifDao.selectNotificationsSync(userId, type.channelId)
+ }
+
+ override fun onCreate() {
+ }
+
+ override fun onDataSetChanged() {
+ loadNotifications()
+ }
+
+ override fun getLoadingView(): RemoteViews? = null
+
+ override fun getItemId(position: Int): Long = content[position].id
+
+ override fun hasStableIds(): Boolean = true
+
+ override fun getViewAt(position: Int): RemoteViews {
+ val views = RemoteViews(context.packageName, R.layout.widget_notification_item)
+ val notif = content[position]
+ views.setBackgroundColor(R.id.item_frame, Prefs.nativeBgColor(notif.unread))
+ views.setTextColor(R.id.item_content, Prefs.textColor)
+ views.setTextViewText(R.id.item_content, notif.text)
+ views.setTextColor(R.id.item_date, Prefs.textColor.withAlpha(150))
+ views.setTextViewText(R.id.item_date, notif.timestamp.toReadableTime(context))
+
+ val avatar = glide.load(notif.profileUrl).transform(FrostGlide.circleCrop).submit(avatarSize, avatarSize).get()
+ views.setImageViewBitmap(R.id.item_avatar, avatar)
+ views.setOnClickFillInIntent(R.id.item_frame, type.putContentExtra(Intent(), notif))
+ return views
+ }
+
+ override fun getCount(): Int = content.size
+
+ override fun getViewTypeCount(): Int = 1
+
+ override fun onDestroy() {
+ }
+}