2017-02-16 12:07:39 +01:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2017-08-25 13:59:43 +02:00
|
|
|
package org.mariotaku.twidere.util.notification
|
2017-02-16 12:07:39 +01:00
|
|
|
|
2017-08-25 18:58:47 +02:00
|
|
|
import android.accounts.AccountManager
|
2017-02-17 09:31:29 +01:00
|
|
|
import android.annotation.SuppressLint
|
2017-02-16 12:07:39 +01:00
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.media.AudioManager
|
|
|
|
import android.net.Uri
|
|
|
|
import android.support.v4.app.NotificationCompat
|
|
|
|
import org.mariotaku.kpreferences.get
|
2017-04-06 17:27:47 +02:00
|
|
|
import org.mariotaku.ktextension.forEachRow
|
2017-02-17 09:31:29 +01:00
|
|
|
import org.mariotaku.ktextension.isEmpty
|
2017-03-05 09:08:09 +01:00
|
|
|
import org.mariotaku.library.objectcursor.ObjectCursor
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.microblog.library.twitter.model.Activity
|
2017-03-21 14:36:06 +01:00
|
|
|
import org.mariotaku.microblog.library.twitter.model.Status
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.sqliteqb.library.*
|
2017-02-17 14:21:07 +01:00
|
|
|
import org.mariotaku.sqliteqb.library.Columns.Column
|
2017-04-08 16:06:04 +02:00
|
|
|
import org.mariotaku.twidere.BuildConfig
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.twidere.R
|
|
|
|
import org.mariotaku.twidere.TwidereConstants.*
|
|
|
|
import org.mariotaku.twidere.activity.HomeActivity
|
2017-03-21 14:36:06 +01:00
|
|
|
import org.mariotaku.twidere.activity.LinkHandlerActivity
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.twidere.annotation.CustomTabType
|
|
|
|
import org.mariotaku.twidere.annotation.NotificationType
|
|
|
|
import org.mariotaku.twidere.constant.IntentConstants
|
|
|
|
import org.mariotaku.twidere.constant.iWantMyStarsBackKey
|
|
|
|
import org.mariotaku.twidere.constant.nameFirstKey
|
2017-08-25 18:58:47 +02:00
|
|
|
import org.mariotaku.twidere.extension.model.*
|
2017-05-05 09:10:42 +02:00
|
|
|
import org.mariotaku.twidere.extension.model.api.formattedTextWithIndices
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.twidere.extension.rawQuery
|
|
|
|
import org.mariotaku.twidere.model.*
|
2017-08-25 13:59:43 +02:00
|
|
|
import org.mariotaku.twidere.model.notification.NotificationChannelSpec
|
2017-08-25 18:58:47 +02:00
|
|
|
import org.mariotaku.twidere.model.util.AccountUtils
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.twidere.model.util.ParcelableActivityUtils
|
2017-04-08 16:06:04 +02:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.*
|
2017-02-17 09:31:29 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Messages.Conversations
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.twidere.receiver.NotificationReceiver
|
2017-04-08 16:06:04 +02:00
|
|
|
import org.mariotaku.twidere.service.LengthyOperationsService
|
2017-08-25 13:59:43 +02:00
|
|
|
import org.mariotaku.twidere.util.*
|
|
|
|
import org.mariotaku.twidere.util.Utils
|
2017-02-16 12:07:39 +01:00
|
|
|
import org.mariotaku.twidere.util.database.FilterQueryBuilder
|
|
|
|
import org.oshkimaadziig.george.androidutils.SpanFormatter
|
|
|
|
import java.io.IOException
|
|
|
|
|
|
|
|
class ContentNotificationManager(
|
|
|
|
val context: Context,
|
|
|
|
val activityTracker: ActivityTracker,
|
|
|
|
val userColorNameManager: UserColorNameManager,
|
|
|
|
val notificationManager: NotificationManagerWrapper,
|
|
|
|
val preferences: SharedPreferences
|
|
|
|
) {
|
|
|
|
|
|
|
|
private var nameFirst: Boolean = false
|
|
|
|
private var useStarForLikes: Boolean = false
|
|
|
|
|
2017-02-17 15:27:53 +01:00
|
|
|
init {
|
|
|
|
updatePreferences()
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:51:49 +01:00
|
|
|
fun showTimeline(pref: AccountPreferences, minPositionKey: Long) {
|
|
|
|
val accountKey = pref.accountKey
|
|
|
|
val resources = context.resources
|
|
|
|
val selection = Expression.and(Expression.equalsArgs(Statuses.ACCOUNT_KEY),
|
2017-04-23 16:00:54 +02:00
|
|
|
Expression.greaterThan(Statuses.POSITION_KEY, minPositionKey),
|
|
|
|
Expression.notEquals(Statuses.IS_GAP, 1)
|
|
|
|
)
|
2017-04-10 11:35:35 +02:00
|
|
|
val selectionArgs = arrayOf(accountKey.toString())
|
2017-02-16 14:51:49 +01:00
|
|
|
val filteredSelection = buildStatusFilterWhereClause(preferences, Statuses.TABLE_NAME,
|
|
|
|
selection)
|
|
|
|
val userProjection = arrayOf(Statuses.USER_KEY, Statuses.USER_NAME, Statuses.USER_SCREEN_NAME)
|
|
|
|
val statusProjection = arrayOf(Statuses.POSITION_KEY)
|
2017-02-17 09:31:29 +01:00
|
|
|
|
|
|
|
@SuppressLint("Recycle")
|
2017-02-16 14:51:49 +01:00
|
|
|
val statusCursor = context.contentResolver.query(Statuses.CONTENT_URI, statusProjection,
|
2017-02-17 09:31:29 +01:00
|
|
|
filteredSelection.sql, selectionArgs, Statuses.DEFAULT_SORT_ORDER)
|
2017-02-16 14:51:49 +01:00
|
|
|
|
2017-02-17 09:31:29 +01:00
|
|
|
@SuppressLint("Recycle")
|
2017-02-16 14:51:49 +01:00
|
|
|
val userCursor = context.contentResolver.rawQuery(SQLQueryBuilder.select(Columns(*userProjection))
|
|
|
|
.from(Table(Statuses.TABLE_NAME))
|
|
|
|
.where(filteredSelection)
|
2017-02-17 14:21:07 +01:00
|
|
|
.groupBy(Column(Statuses.USER_KEY))
|
2017-02-17 09:31:29 +01:00
|
|
|
.orderBy(OrderBy(Statuses.DEFAULT_SORT_ORDER)).buildSQL(), selectionArgs)
|
2017-02-16 14:51:49 +01:00
|
|
|
|
|
|
|
try {
|
2017-02-17 09:31:29 +01:00
|
|
|
if (statusCursor == null || userCursor == null) return
|
2017-02-16 14:51:49 +01:00
|
|
|
val usersCount = userCursor.count
|
|
|
|
val statusesCount = statusCursor.count
|
|
|
|
if (statusesCount == 0 || usersCount == 0) return
|
2017-03-05 09:08:09 +01:00
|
|
|
val statusIndices = ObjectCursor.indicesFrom(statusCursor, ParcelableStatus::class.java)
|
|
|
|
val userIndices = ObjectCursor.indicesFrom(userCursor, ParcelableStatus::class.java)
|
2017-04-06 17:27:47 +02:00
|
|
|
val positionKey = if (statusCursor.moveToFirst()) {
|
|
|
|
statusCursor.getLong(statusIndices[Statuses.POSITION_KEY])
|
|
|
|
} else {
|
|
|
|
-1L
|
|
|
|
}
|
2017-02-16 14:51:49 +01:00
|
|
|
val notificationTitle = resources.getQuantityString(R.plurals.N_new_statuses,
|
|
|
|
statusesCount, statusesCount)
|
|
|
|
val notificationContent: String
|
|
|
|
userCursor.moveToFirst()
|
2017-03-05 09:08:09 +01:00
|
|
|
val displayName = userColorNameManager.getDisplayName(userCursor.getString(userIndices[Statuses.USER_KEY]),
|
|
|
|
userCursor.getString(userIndices[Statuses.USER_NAME]), userCursor.getString(userIndices[Statuses.USER_SCREEN_NAME]),
|
2017-02-16 14:51:49 +01:00
|
|
|
nameFirst)
|
|
|
|
if (usersCount == 1) {
|
|
|
|
notificationContent = context.getString(R.string.from_name, displayName)
|
|
|
|
} else if (usersCount == 2) {
|
|
|
|
userCursor.moveToPosition(1)
|
2017-03-05 09:08:09 +01:00
|
|
|
val othersName = userColorNameManager.getDisplayName(userCursor.getString(userIndices[Statuses.USER_KEY]),
|
|
|
|
userCursor.getString(userIndices[Statuses.USER_NAME]), userCursor.getString(userIndices[Statuses.USER_SCREEN_NAME]),
|
2017-02-16 14:51:49 +01:00
|
|
|
nameFirst)
|
|
|
|
notificationContent = resources.getString(R.string.from_name_and_name, displayName, othersName)
|
|
|
|
} else {
|
|
|
|
notificationContent = resources.getString(R.string.from_name_and_N_others, displayName, usersCount - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup notification
|
2017-08-25 18:58:47 +02:00
|
|
|
val builder = NotificationChannelSpec.contentUpdates.accountNotificationBuilder(context,
|
|
|
|
accountKey)
|
2017-02-16 14:51:49 +01:00
|
|
|
builder.setAutoCancel(true)
|
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_twitter)
|
|
|
|
builder.setTicker(notificationTitle)
|
|
|
|
builder.setContentTitle(notificationTitle)
|
|
|
|
builder.setContentText(notificationContent)
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL)
|
|
|
|
builder.setContentIntent(getContentIntent(context, CustomTabType.HOME_TIMELINE,
|
|
|
|
NotificationType.HOME_TIMELINE, accountKey, positionKey))
|
|
|
|
builder.setDeleteIntent(getMarkReadDeleteIntent(context, NotificationType.HOME_TIMELINE,
|
|
|
|
accountKey, positionKey, false))
|
|
|
|
builder.setNumber(statusesCount)
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL)
|
|
|
|
applyNotificationPreferences(builder, pref, pref.homeTimelineNotificationType)
|
|
|
|
try {
|
2017-02-17 09:31:29 +01:00
|
|
|
val notificationId = Utils.getNotificationId(NOTIFICATION_ID_HOME_TIMELINE, accountKey)
|
|
|
|
notificationManager.notify("home", notificationId, builder.build())
|
2017-02-16 14:51:49 +01:00
|
|
|
Utils.sendPebbleNotification(context, null, notificationContent)
|
|
|
|
} catch (e: SecurityException) {
|
|
|
|
// Silently ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
2017-02-17 09:31:29 +01:00
|
|
|
statusCursor?.close()
|
|
|
|
userCursor?.close()
|
2017-02-16 14:51:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:19:04 +01:00
|
|
|
fun showInteractions(pref: AccountPreferences, position: Long) {
|
2017-08-25 18:58:47 +02:00
|
|
|
val am = AccountManager.get(context)
|
2017-02-16 12:07:39 +01:00
|
|
|
val cr = context.contentResolver
|
|
|
|
val accountKey = pref.accountKey
|
2017-08-25 18:58:47 +02:00
|
|
|
val account = AccountUtils.getAccountDetails(am, accountKey, false) ?: return
|
2017-02-16 12:07:39 +01:00
|
|
|
val where = Expression.and(
|
|
|
|
Expression.equalsArgs(Activities.ACCOUNT_KEY),
|
2017-04-23 16:00:54 +02:00
|
|
|
Expression.greaterThan(Activities.POSITION_KEY, position),
|
|
|
|
Expression.notEquals(Activities.IS_GAP, 1)
|
2017-02-16 12:07:39 +01:00
|
|
|
).sql
|
2017-04-10 11:35:35 +02:00
|
|
|
val whereArgs = arrayOf(accountKey.toString())
|
2017-02-17 09:31:29 +01:00
|
|
|
@SuppressLint("Recycle")
|
2017-02-16 12:07:39 +01:00
|
|
|
val c = cr.query(Activities.AboutMe.CONTENT_URI, Activities.COLUMNS, where, whereArgs,
|
|
|
|
OrderBy(Activities.TIMESTAMP, false).sql) ?: return
|
2017-08-25 18:58:47 +02:00
|
|
|
val builder = NotificationChannelSpec.contentInteractions.accountNotificationBuilder(context,
|
|
|
|
accountKey)
|
2017-02-16 12:07:39 +01:00
|
|
|
val pebbleNotificationStringBuilder = StringBuilder()
|
|
|
|
try {
|
|
|
|
val count = c.count
|
|
|
|
if (count == 0) return
|
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_notification)
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL)
|
|
|
|
applyNotificationPreferences(builder, pref, pref.mentionsNotificationType)
|
|
|
|
|
|
|
|
val resources = context.resources
|
2017-08-25 18:58:47 +02:00
|
|
|
val accountName = userColorNameManager.getDisplayName(account.user, nameFirst)
|
2017-02-16 12:07:39 +01:00
|
|
|
builder.setContentText(accountName)
|
|
|
|
val style = NotificationCompat.InboxStyle()
|
|
|
|
builder.setStyle(style)
|
|
|
|
builder.setAutoCancel(true)
|
|
|
|
style.setSummaryText(accountName)
|
2017-03-05 09:08:09 +01:00
|
|
|
val ci = ObjectCursor.indicesFrom(c, ParcelableActivity::class.java)
|
2017-02-16 12:07:39 +01:00
|
|
|
|
2017-04-06 17:27:47 +02:00
|
|
|
var newMaxPositionKey = -1L
|
2017-04-12 14:58:08 +02:00
|
|
|
val filteredUserKeys = DataStoreUtils.getFilteredUserKeys(context)
|
2017-03-03 15:56:29 +01:00
|
|
|
var consumed = 0
|
2017-03-21 14:36:06 +01:00
|
|
|
val remaining = c.forEachRow(5) { cur, _ ->
|
2017-03-03 15:56:29 +01:00
|
|
|
val activity = ci.newObject(cur)
|
2017-04-06 17:27:47 +02:00
|
|
|
|
|
|
|
if (newMaxPositionKey == -1L) {
|
|
|
|
newMaxPositionKey = activity.position_key
|
|
|
|
}
|
|
|
|
|
2017-02-17 09:31:29 +01:00
|
|
|
if (pref.isNotificationMentionsOnly && activity.action !in Activity.Action.MENTION_ACTIONS) {
|
|
|
|
return@forEachRow false
|
2017-02-16 12:07:39 +01:00
|
|
|
}
|
2017-04-28 15:44:45 +02:00
|
|
|
if (FilterQueryBuilder.isFiltered(cr, activity)) {
|
2017-02-17 09:31:29 +01:00
|
|
|
return@forEachRow false
|
2017-02-16 12:07:39 +01:00
|
|
|
}
|
2017-04-29 08:55:11 +02:00
|
|
|
val sources = ParcelableActivityUtils.filterSources(activity.sources_lite,
|
|
|
|
filteredUserKeys, pref.isNotificationFollowingOnly) ?: activity.sources_lite
|
|
|
|
?: return@forEachRow false
|
2017-02-17 09:31:29 +01:00
|
|
|
|
|
|
|
if (sources.isEmpty()) return@forEachRow false
|
|
|
|
|
|
|
|
val message = ActivityTitleSummaryMessage.get(context, userColorNameManager,
|
|
|
|
activity, sources, 0, useStarForLikes, nameFirst) ?: return@forEachRow false
|
|
|
|
val summary = message.summary
|
|
|
|
if (summary.isNullOrEmpty()) {
|
|
|
|
style.addLine(message.title)
|
|
|
|
pebbleNotificationStringBuilder.append(message.title)
|
|
|
|
pebbleNotificationStringBuilder.append("\n")
|
|
|
|
} else {
|
|
|
|
style.addLine(SpanFormatter.format(resources.getString(R.string.title_summary_line_format),
|
|
|
|
message.title, summary))
|
|
|
|
pebbleNotificationStringBuilder.append(message.title)
|
|
|
|
pebbleNotificationStringBuilder.append(": ")
|
|
|
|
pebbleNotificationStringBuilder.append(summary)
|
|
|
|
pebbleNotificationStringBuilder.append("\n")
|
2017-02-16 12:07:39 +01:00
|
|
|
}
|
2017-03-03 15:56:29 +01:00
|
|
|
consumed++
|
2017-02-17 09:31:29 +01:00
|
|
|
return@forEachRow true
|
|
|
|
}
|
|
|
|
if (remaining < 0) return
|
|
|
|
if (remaining > 0) {
|
2017-03-03 15:56:29 +01:00
|
|
|
style.addLine(resources.getString(R.string.and_N_more, remaining))
|
|
|
|
pebbleNotificationStringBuilder.append(resources.getString(R.string.and_N_more, remaining))
|
2017-02-16 12:07:39 +01:00
|
|
|
}
|
2017-03-03 15:56:29 +01:00
|
|
|
val displayCount = consumed + remaining
|
|
|
|
if (displayCount <= 0) return
|
2017-02-16 12:07:39 +01:00
|
|
|
val title = resources.getQuantityString(R.plurals.N_new_interactions,
|
|
|
|
displayCount, displayCount)
|
|
|
|
builder.setContentTitle(title)
|
|
|
|
style.setBigContentTitle(title)
|
|
|
|
builder.setNumber(displayCount)
|
|
|
|
builder.setContentIntent(getContentIntent(context, CustomTabType.NOTIFICATIONS_TIMELINE,
|
2017-04-24 12:54:09 +02:00
|
|
|
NotificationType.INTERACTIONS, accountKey, newMaxPositionKey))
|
2017-04-06 17:27:47 +02:00
|
|
|
builder.setDeleteIntent(getMarkReadDeleteIntent(context, NotificationType.INTERACTIONS,
|
|
|
|
accountKey, newMaxPositionKey, false))
|
2017-02-16 12:07:39 +01:00
|
|
|
} catch (e: IOException) {
|
|
|
|
return
|
|
|
|
} finally {
|
|
|
|
c.close()
|
|
|
|
}
|
|
|
|
val notificationId = Utils.getNotificationId(NOTIFICATION_ID_INTERACTIONS_TIMELINE, accountKey)
|
|
|
|
notificationManager.notify("interactions", notificationId, builder.build())
|
2017-02-16 14:51:49 +01:00
|
|
|
Utils.sendPebbleNotification(context, context.getString(R.string.interactions), pebbleNotificationStringBuilder.toString())
|
2017-02-16 12:07:39 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 17:34:36 +01:00
|
|
|
fun showMessages(pref: AccountPreferences) {
|
2017-02-17 14:21:07 +01:00
|
|
|
val resources = context.resources
|
2017-02-17 09:31:29 +01:00
|
|
|
val accountKey = pref.accountKey
|
|
|
|
val cr = context.contentResolver
|
2017-02-17 14:21:07 +01:00
|
|
|
val projection = (Conversations.COLUMNS + Conversations.UNREAD_COUNT).map {
|
|
|
|
TwidereQueryBuilder.mapConversationsProjection(it)
|
|
|
|
}.toTypedArray()
|
2017-04-10 11:35:35 +02:00
|
|
|
val unreadHaving = Expression.greaterThan(Conversations.UNREAD_COUNT, 0)
|
|
|
|
val cur = cr.getUnreadMessagesEntriesCursor(projection, arrayOf(accountKey),
|
|
|
|
extraHaving = unreadHaving) ?: return
|
2017-02-17 09:31:29 +01:00
|
|
|
try {
|
|
|
|
if (cur.isEmpty) return
|
2017-02-17 14:21:07 +01:00
|
|
|
|
2017-03-05 09:08:09 +01:00
|
|
|
val indices = ObjectCursor.indicesFrom(cur, ParcelableMessageConversation::class.java)
|
2017-02-17 14:21:07 +01:00
|
|
|
|
|
|
|
var messageSum: Int = 0
|
2017-04-06 17:27:47 +02:00
|
|
|
var newLastReadTimestamp = -1L
|
2017-08-25 13:59:43 +02:00
|
|
|
cur.forEachRow { c, _ ->
|
|
|
|
val unreadCount = c.getInt(indices[Conversations.UNREAD_COUNT])
|
2017-04-10 11:35:35 +02:00
|
|
|
if (unreadCount <= 0) return@forEachRow false
|
2017-04-06 17:27:47 +02:00
|
|
|
if (newLastReadTimestamp != -1L) {
|
2017-08-25 13:59:43 +02:00
|
|
|
newLastReadTimestamp = c.getLong(indices[Conversations.LAST_READ_TIMESTAMP])
|
2017-04-06 17:27:47 +02:00
|
|
|
}
|
2017-04-10 11:35:35 +02:00
|
|
|
messageSum += unreadCount
|
2017-02-17 14:21:07 +01:00
|
|
|
return@forEachRow true
|
|
|
|
}
|
2017-04-10 11:35:35 +02:00
|
|
|
if (messageSum == 0) return
|
2017-02-17 14:21:07 +01:00
|
|
|
|
2017-08-25 18:58:47 +02:00
|
|
|
val builder = NotificationChannelSpec.contentMessages.accountNotificationBuilder(context,
|
|
|
|
accountKey)
|
2017-02-17 09:31:29 +01:00
|
|
|
applyNotificationPreferences(builder, pref, pref.directMessagesNotificationType)
|
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_message)
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL)
|
|
|
|
builder.setAutoCancel(true)
|
|
|
|
val style = NotificationCompat.InboxStyle(builder)
|
2017-02-17 14:21:07 +01:00
|
|
|
style.setSummaryText(DataStoreUtils.getAccountDisplayName(context, accountKey, nameFirst))
|
|
|
|
|
|
|
|
val notificationTitle = resources.getQuantityString(R.plurals.N_new_messages, messageSum,
|
|
|
|
messageSum)
|
|
|
|
builder.setTicker(notificationTitle)
|
|
|
|
builder.setContentTitle(notificationTitle)
|
2017-03-19 09:35:19 +01:00
|
|
|
builder.setContentIntent(getContentIntent(context, CustomTabType.DIRECT_MESSAGES,
|
|
|
|
NotificationType.DIRECT_MESSAGES, accountKey, 0))
|
2017-04-06 17:27:47 +02:00
|
|
|
builder.setDeleteIntent(getMarkReadDeleteIntent(context, NotificationType.DIRECT_MESSAGES,
|
|
|
|
accountKey, newLastReadTimestamp, false))
|
|
|
|
|
2017-08-25 13:59:43 +02:00
|
|
|
val remaining = cur.forEachRow(5) { c, pos ->
|
|
|
|
val conversation = indices.newObject(c)
|
2017-02-17 17:07:57 +01:00
|
|
|
if (conversation.notificationDisabled) return@forEachRow false
|
2017-02-22 02:08:22 +01:00
|
|
|
val title = conversation.getTitle(context, userColorNameManager, nameFirst)
|
2017-02-17 09:31:29 +01:00
|
|
|
val summary = conversation.getSummaryText(context, userColorNameManager, nameFirst)
|
2017-02-17 14:21:07 +01:00
|
|
|
val line = SpanFormatter.format(context.getString(R.string.title_summary_line_format),
|
|
|
|
title.first, summary)
|
2017-02-17 09:31:29 +01:00
|
|
|
if (pos == 0) {
|
2017-02-17 14:21:07 +01:00
|
|
|
builder.setContentText(line)
|
2017-02-17 09:31:29 +01:00
|
|
|
}
|
2017-02-17 14:21:07 +01:00
|
|
|
style.addLine(line)
|
2017-02-17 09:31:29 +01:00
|
|
|
return@forEachRow true
|
|
|
|
}
|
2017-02-17 17:07:57 +01:00
|
|
|
if (remaining < 0) return
|
2017-02-17 09:31:29 +01:00
|
|
|
if (remaining > 0) {
|
|
|
|
style.addLine(context.getString(R.string.and_N_more, remaining))
|
|
|
|
}
|
|
|
|
val notificationId = Utils.getNotificationId(NOTIFICATION_ID_DIRECT_MESSAGES, accountKey)
|
|
|
|
notificationManager.notify("direct_messages", notificationId, builder.build())
|
|
|
|
} finally {
|
|
|
|
cur.close()
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 17:34:36 +01:00
|
|
|
|
2017-03-13 06:35:11 +01:00
|
|
|
|
2017-04-06 17:27:47 +02:00
|
|
|
fun showUserNotification(accountKey: UserKey, status: Status, userKey: UserKey) {
|
|
|
|
// Build favorited user notifications
|
|
|
|
val userDisplayName = userColorNameManager.getDisplayName(status.user,
|
|
|
|
preferences[nameFirstKey])
|
|
|
|
val statusUri = LinkCreator.getTwidereStatusLink(accountKey, status.id)
|
2017-08-25 18:58:47 +02:00
|
|
|
val builder = NotificationChannelSpec.contentSubscriptions.accountNotificationBuilder(context,
|
|
|
|
accountKey)
|
2017-04-06 17:27:47 +02:00
|
|
|
builder.color = userColorNameManager.getUserColor(userKey)
|
|
|
|
builder.setAutoCancel(true)
|
|
|
|
builder.setWhen(status.createdAt?.time ?: 0)
|
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_twitter)
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL)
|
|
|
|
if (status.isRetweetedByMe) {
|
|
|
|
builder.setContentTitle(context.getString(R.string.notification_title_new_retweet_by_user, userDisplayName))
|
2017-05-05 09:10:42 +02:00
|
|
|
builder.setContentText(status.retweetedStatus.formattedTextWithIndices().text)
|
2017-04-06 17:27:47 +02:00
|
|
|
} else {
|
|
|
|
builder.setContentTitle(context.getString(R.string.notification_title_new_status_by_user, userDisplayName))
|
2017-05-05 09:10:42 +02:00
|
|
|
builder.setContentText(status.formattedTextWithIndices().text)
|
2017-02-17 09:31:29 +01:00
|
|
|
}
|
2017-04-06 17:27:47 +02:00
|
|
|
builder.setContentIntent(PendingIntent.getActivity(context, 0, Intent(Intent.ACTION_VIEW, statusUri).apply {
|
|
|
|
setClass(context, LinkHandlerActivity::class.java)
|
|
|
|
}, PendingIntent.FLAG_UPDATE_CURRENT))
|
|
|
|
|
|
|
|
val tag = "$accountKey:$userKey:${status.id}"
|
|
|
|
notificationManager.notify(tag, NOTIFICATION_ID_USER_NOTIFICATION, builder.build())
|
|
|
|
}
|
|
|
|
|
2017-04-08 16:06:04 +02:00
|
|
|
fun showDraft(draftUri: Uri): Long {
|
|
|
|
val draftId = draftUri.lastPathSegment.toLongOrNull() ?: return -1
|
|
|
|
val where = Expression.equals(Drafts._ID, draftId)
|
|
|
|
val c = context.contentResolver.query(Drafts.CONTENT_URI, Drafts.COLUMNS, where.sql,
|
|
|
|
null, null) ?: return -1
|
|
|
|
val i = ObjectCursor.indicesFrom(c, Draft::class.java)
|
|
|
|
val item: Draft
|
|
|
|
try {
|
|
|
|
if (!c.moveToFirst()) return -1
|
|
|
|
item = i.newObject(c)
|
|
|
|
} catch (e: IOException) {
|
|
|
|
return -1
|
|
|
|
} finally {
|
|
|
|
c.close()
|
|
|
|
}
|
|
|
|
val title = context.getString(R.string.status_not_updated)
|
|
|
|
val message = context.getString(R.string.status_not_updated_summary)
|
|
|
|
val intent = Intent()
|
|
|
|
intent.`package` = BuildConfig.APPLICATION_ID
|
|
|
|
val uriBuilder = Uri.Builder()
|
|
|
|
uriBuilder.scheme(SCHEME_TWIDERE)
|
|
|
|
uriBuilder.authority(AUTHORITY_DRAFTS)
|
|
|
|
intent.data = uriBuilder.build()
|
2017-08-25 18:58:47 +02:00
|
|
|
val nb = NotificationChannelSpec.appNotices.notificationBuilder(context)
|
2017-04-08 16:06:04 +02:00
|
|
|
nb.setTicker(message)
|
|
|
|
nb.setContentTitle(title)
|
|
|
|
nb.setContentText(item.text)
|
|
|
|
nb.setAutoCancel(true)
|
|
|
|
nb.setWhen(System.currentTimeMillis())
|
|
|
|
nb.setSmallIcon(R.drawable.ic_stat_draft)
|
|
|
|
val discardIntent = Intent(context, LengthyOperationsService::class.java)
|
|
|
|
discardIntent.action = INTENT_ACTION_DISCARD_DRAFT
|
|
|
|
discardIntent.data = draftUri
|
|
|
|
nb.addAction(R.drawable.ic_action_delete, context.getString(R.string.discard),
|
|
|
|
PendingIntent.getService(context, 0, discardIntent, PendingIntent.FLAG_ONE_SHOT))
|
|
|
|
|
|
|
|
val sendIntent = Intent(context, LengthyOperationsService::class.java)
|
|
|
|
sendIntent.action = INTENT_ACTION_SEND_DRAFT
|
|
|
|
sendIntent.data = draftUri
|
|
|
|
nb.addAction(R.drawable.ic_action_send, context.getString(R.string.action_send),
|
|
|
|
PendingIntent.getService(context, 0, sendIntent, PendingIntent.FLAG_ONE_SHOT))
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
|
|
|
|
nb.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT))
|
|
|
|
notificationManager.notify(draftUri.toString(), NOTIFICATION_ID_DRAFTS, nb.build())
|
|
|
|
return draftId
|
|
|
|
}
|
|
|
|
|
2017-04-06 17:27:47 +02:00
|
|
|
fun updatePreferences() {
|
|
|
|
nameFirst = preferences[nameFirstKey]
|
|
|
|
useStarForLikes = preferences[iWantMyStarsBackKey]
|
2017-02-16 17:34:36 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 12:07:39 +01:00
|
|
|
private fun applyNotificationPreferences(builder: NotificationCompat.Builder, pref: AccountPreferences, defaultFlags: Int) {
|
|
|
|
var notificationDefaults = 0
|
|
|
|
if (AccountPreferences.isNotificationHasLight(defaultFlags)) {
|
|
|
|
notificationDefaults = notificationDefaults or NotificationCompat.DEFAULT_LIGHTS
|
|
|
|
}
|
|
|
|
if (isNotificationAudible()) {
|
|
|
|
if (AccountPreferences.isNotificationHasVibration(defaultFlags)) {
|
|
|
|
notificationDefaults = notificationDefaults or NotificationCompat.DEFAULT_VIBRATE
|
|
|
|
} else {
|
|
|
|
notificationDefaults = notificationDefaults and NotificationCompat.DEFAULT_VIBRATE.inv()
|
|
|
|
}
|
|
|
|
if (AccountPreferences.isNotificationHasRingtone(defaultFlags)) {
|
|
|
|
builder.setSound(pref.notificationRingtone, AudioManager.STREAM_NOTIFICATION)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notificationDefaults = notificationDefaults and (NotificationCompat.DEFAULT_VIBRATE or NotificationCompat.DEFAULT_SOUND).inv()
|
|
|
|
}
|
|
|
|
builder.color = pref.notificationLightColor
|
|
|
|
builder.setDefaults(notificationDefaults)
|
|
|
|
builder.setOnlyAlertOnce(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun isNotificationAudible(): Boolean {
|
|
|
|
return !activityTracker.isHomeActivityStarted
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getContentIntent(context: Context, @CustomTabType type: String,
|
|
|
|
@NotificationType notificationType: String, accountKey: UserKey?, readPosition: Long): PendingIntent {
|
|
|
|
// Setup click intent
|
|
|
|
val homeIntent = Intent(context, HomeActivity::class.java)
|
|
|
|
val homeLinkBuilder = Uri.Builder()
|
|
|
|
homeLinkBuilder.scheme(SCHEME_TWIDERE)
|
|
|
|
homeLinkBuilder.authority(type)
|
|
|
|
if (accountKey != null)
|
|
|
|
homeLinkBuilder.appendQueryParameter(QUERY_PARAM_ACCOUNT_KEY, accountKey.toString())
|
|
|
|
homeLinkBuilder.appendQueryParameter(QUERY_PARAM_FROM_NOTIFICATION, true.toString())
|
|
|
|
homeLinkBuilder.appendQueryParameter(QUERY_PARAM_TIMESTAMP, System.currentTimeMillis().toString())
|
|
|
|
homeLinkBuilder.appendQueryParameter(QUERY_PARAM_NOTIFICATION_TYPE, notificationType)
|
|
|
|
if (readPosition > 0) {
|
|
|
|
homeLinkBuilder.appendQueryParameter(QUERY_PARAM_READ_POSITION, readPosition.toString())
|
|
|
|
}
|
|
|
|
homeIntent.data = homeLinkBuilder.build()
|
|
|
|
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
|
|
|
return PendingIntent.getActivity(context, 0, homeIntent, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getMarkReadDeleteIntent(context: Context, @NotificationType type: String,
|
|
|
|
accountKey: UserKey?, position: Long,
|
|
|
|
extraUserFollowing: Boolean): PendingIntent {
|
2017-04-06 17:27:47 +02:00
|
|
|
return getMarkReadDeleteIntent(context, type, accountKey, position)
|
2017-02-16 12:07:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun getMarkReadDeleteIntent(context: Context, @NotificationType type: String,
|
2017-04-06 17:27:47 +02:00
|
|
|
accountKey: UserKey?, position: Long): PendingIntent {
|
2017-02-16 12:07:39 +01:00
|
|
|
// Setup delete intent
|
|
|
|
val intent = Intent(context, NotificationReceiver::class.java)
|
|
|
|
intent.action = IntentConstants.BROADCAST_NOTIFICATION_DELETED
|
|
|
|
val linkBuilder = Uri.Builder()
|
|
|
|
linkBuilder.scheme(SCHEME_TWIDERE)
|
2017-04-06 17:27:47 +02:00
|
|
|
linkBuilder.authority(AUTHORITY_NOTIFICATIONS)
|
2017-02-16 12:07:39 +01:00
|
|
|
linkBuilder.appendPath(type)
|
|
|
|
if (accountKey != null) {
|
|
|
|
linkBuilder.appendQueryParameter(QUERY_PARAM_ACCOUNT_KEY, accountKey.toString())
|
|
|
|
}
|
|
|
|
linkBuilder.appendQueryParameter(QUERY_PARAM_READ_POSITION, position.toString())
|
|
|
|
linkBuilder.appendQueryParameter(QUERY_PARAM_NOTIFICATION_TYPE, type)
|
|
|
|
|
|
|
|
intent.data = linkBuilder.build()
|
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, 0)
|
|
|
|
}
|
|
|
|
}
|