2018-05-06 11:07:10 +02:00
|
|
|
/* Copyright 2018 Jeremiasz Nelz <remi6397(a)gmail.com>
|
|
|
|
* Copyright 2017 Andrew Dawson
|
2017-04-28 05:29:42 +02:00
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2020-05-12 18:46:49 +02:00
|
|
|
package com.keylesspalace.tusky.components.notifications;
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
import static com.keylesspalace.tusky.BuildConfig.APPLICATION_ID;
|
2022-04-16 09:44:05 +02:00
|
|
|
import static com.keylesspalace.tusky.util.StatusParsingHelper.parseAsMastodonHtml;
|
2022-04-14 19:39:30 +02:00
|
|
|
import static com.keylesspalace.tusky.viewdata.PollViewDataKt.buildDescription;
|
|
|
|
|
2017-10-18 22:18:07 +02:00
|
|
|
import android.app.NotificationChannel;
|
2018-02-03 22:45:14 +01:00
|
|
|
import android.app.NotificationChannelGroup;
|
2018-02-12 22:03:08 +01:00
|
|
|
import android.app.NotificationManager;
|
2017-04-28 05:29:42 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
2017-11-03 10:09:09 +01:00
|
|
|
import android.graphics.BitmapFactory;
|
2017-04-28 05:29:42 +02:00
|
|
|
import android.os.Build;
|
2023-05-13 16:00:28 +02:00
|
|
|
import android.os.Bundle;
|
2017-04-28 05:29:42 +02:00
|
|
|
import android.provider.Settings;
|
2023-05-13 16:00:28 +02:00
|
|
|
import android.service.notification.StatusBarNotification;
|
2019-06-22 21:55:03 +02:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2023-06-29 18:37:27 +02:00
|
|
|
import androidx.annotation.StringRes;
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.app.RemoteInput;
|
|
|
|
import androidx.core.app.TaskStackBuilder;
|
2020-05-12 18:46:49 +02:00
|
|
|
import androidx.work.Constraints;
|
|
|
|
import androidx.work.NetworkType;
|
2023-05-13 16:00:28 +02:00
|
|
|
import androidx.work.OneTimeWorkRequest;
|
|
|
|
import androidx.work.OutOfQuotaPolicy;
|
2020-05-12 18:46:49 +02:00
|
|
|
import androidx.work.PeriodicWorkRequest;
|
|
|
|
import androidx.work.WorkManager;
|
|
|
|
import androidx.work.WorkRequest;
|
2019-05-02 19:44:35 +02:00
|
|
|
|
2019-04-16 21:39:12 +02:00
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
|
|
|
import com.bumptech.glide.request.FutureTarget;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.MainActivity;
|
|
|
|
import com.keylesspalace.tusky.R;
|
2022-03-09 20:50:23 +01:00
|
|
|
import com.keylesspalace.tusky.components.compose.ComposeActivity;
|
2018-02-03 22:45:14 +01:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2017-04-28 05:29:42 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
2019-05-02 19:44:35 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Poll;
|
|
|
|
import com.keylesspalace.tusky.entity.PollOption;
|
2018-05-06 11:07:10 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
|
|
|
import com.keylesspalace.tusky.receiver.SendStatusBroadcastReceiver;
|
2020-11-04 18:21:41 +01:00
|
|
|
import com.keylesspalace.tusky.util.StringUtils;
|
2019-06-22 21:55:03 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.PollViewDataKt;
|
2023-06-11 13:17:30 +02:00
|
|
|
import com.keylesspalace.tusky.worker.NotificationWorker;
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2017-10-18 22:18:07 +02:00
|
|
|
import java.util.ArrayList;
|
2018-05-06 11:07:10 +02:00
|
|
|
import java.util.Collections;
|
2023-05-13 16:00:28 +02:00
|
|
|
import java.util.HashMap;
|
2018-05-06 11:07:10 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2017-10-18 22:18:07 +02:00
|
|
|
import java.util.List;
|
2023-05-13 16:00:28 +02:00
|
|
|
import java.util.Map;
|
2022-03-09 20:50:23 +01:00
|
|
|
import java.util.Set;
|
2019-04-16 21:39:12 +02:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2020-05-12 18:46:49 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-10-18 22:18:07 +02:00
|
|
|
|
2018-02-12 22:03:08 +01:00
|
|
|
public class NotificationHelper {
|
2018-02-03 22:45:14 +01:00
|
|
|
|
2023-06-29 18:37:27 +02:00
|
|
|
/** ID of notification shown when fetching notifications */
|
|
|
|
public static final int NOTIFICATION_ID_FETCH_NOTIFICATION = 0;
|
|
|
|
/** ID of notification shown when pruning the cache */
|
|
|
|
public static final int NOTIFICATION_ID_PRUNE_CACHE = 1;
|
|
|
|
/** Dynamic notification IDs start here */
|
|
|
|
private static int notificationId = NOTIFICATION_ID_PRUNE_CACHE + 1;
|
2018-05-06 11:07:10 +02:00
|
|
|
|
2018-02-12 22:03:08 +01:00
|
|
|
private static final String TAG = "NotificationHelper";
|
2017-05-23 21:34:31 +02:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
public static final String REPLY_ACTION = "REPLY_ACTION";
|
|
|
|
|
|
|
|
public static final String KEY_REPLY = "KEY_REPLY";
|
|
|
|
|
|
|
|
public static final String KEY_SENDER_ACCOUNT_ID = "KEY_SENDER_ACCOUNT_ID";
|
|
|
|
|
|
|
|
public static final String KEY_SENDER_ACCOUNT_IDENTIFIER = "KEY_SENDER_ACCOUNT_IDENTIFIER";
|
|
|
|
|
|
|
|
public static final String KEY_SENDER_ACCOUNT_FULL_NAME = "KEY_SENDER_ACCOUNT_FULL_NAME";
|
|
|
|
|
|
|
|
public static final String KEY_NOTIFICATION_ID = "KEY_NOTIFICATION_ID";
|
|
|
|
|
|
|
|
public static final String KEY_CITED_STATUS_ID = "KEY_CITED_STATUS_ID";
|
|
|
|
|
|
|
|
public static final String KEY_VISIBILITY = "KEY_VISIBILITY";
|
|
|
|
|
|
|
|
public static final String KEY_SPOILER = "KEY_SPOILER";
|
|
|
|
|
|
|
|
public static final String KEY_MENTIONS = "KEY_MENTIONS";
|
|
|
|
|
2018-03-09 22:02:32 +01:00
|
|
|
/**
|
|
|
|
* notification channels used on Android O+
|
|
|
|
**/
|
2018-05-06 11:07:10 +02:00
|
|
|
public static final String CHANNEL_MENTION = "CHANNEL_MENTION";
|
|
|
|
public static final String CHANNEL_FOLLOW = "CHANNEL_FOLLOW";
|
2020-03-19 22:02:10 +01:00
|
|
|
public static final String CHANNEL_FOLLOW_REQUEST = "CHANNEL_FOLLOW_REQUEST";
|
2018-05-06 11:07:10 +02:00
|
|
|
public static final String CHANNEL_BOOST = "CHANNEL_BOOST";
|
|
|
|
public static final String CHANNEL_FAVOURITE = "CHANNEL_FAVOURITE";
|
2019-05-02 19:44:35 +02:00
|
|
|
public static final String CHANNEL_POLL = "CHANNEL_POLL";
|
2020-12-23 12:52:39 +01:00
|
|
|
public static final String CHANNEL_SUBSCRIPTIONS = "CHANNEL_SUBSCRIPTIONS";
|
2022-04-14 19:39:30 +02:00
|
|
|
public static final String CHANNEL_SIGN_UP = "CHANNEL_SIGN_UP";
|
2022-04-19 11:10:13 +02:00
|
|
|
public static final String CHANNEL_UPDATES = "CHANNEL_UPDATES";
|
2022-12-01 20:11:55 +01:00
|
|
|
public static final String CHANNEL_REPORT = "CHANNEL_REPORT";
|
2023-06-29 18:37:27 +02:00
|
|
|
public static final String CHANNEL_BACKGROUND_TASKS = "CHANNEL_BACKGROUND_TASKS";
|
2020-05-12 18:46:49 +02:00
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
/**
|
2020-05-12 18:46:49 +02:00
|
|
|
* WorkManager Tag
|
2018-11-12 21:09:39 +01:00
|
|
|
*/
|
2020-05-12 18:46:49 +02:00
|
|
|
private static final String NOTIFICATION_PULL_TAG = "pullNotifications";
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
/** Tag for the summary notification */
|
|
|
|
private static final String GROUP_SUMMARY_TAG = APPLICATION_ID + ".notification.group_summary";
|
|
|
|
|
|
|
|
/** The name of the account that caused the notification, for use in a summary */
|
|
|
|
private static final String EXTRA_ACCOUNT_NAME = APPLICATION_ID + ".notification.extra.account_name";
|
|
|
|
|
|
|
|
/** The notification's type (string representation of a Notification.Type) */
|
|
|
|
private static final String EXTRA_NOTIFICATION_TYPE = APPLICATION_ID + ".notification.extra.notification_type";
|
|
|
|
|
2017-07-28 04:03:45 +02:00
|
|
|
/**
|
2023-05-13 16:00:28 +02:00
|
|
|
* Takes a given Mastodon notification and creates a new Android notification or updates the
|
|
|
|
* existing Android notification.
|
|
|
|
* <p>
|
|
|
|
* The Android notification has it's tag set to the Mastodon notification ID, and it's ID set
|
|
|
|
* to the ID of the account that received the notification.
|
2017-07-28 04:03:45 +02:00
|
|
|
*
|
2018-03-09 22:02:32 +01:00
|
|
|
* @param context to access application preferences and services
|
|
|
|
* @param body a new Mastodon notification
|
|
|
|
* @param account the account for which the notification should be shown
|
2023-05-13 16:00:28 +02:00
|
|
|
* @return the new notification
|
2017-07-28 04:03:45 +02:00
|
|
|
*/
|
2023-05-13 16:00:28 +02:00
|
|
|
@NonNull
|
|
|
|
public static android.app.Notification make(final Context context, NotificationManager notificationManager, Notification body, AccountEntity account, boolean isFirstOfBatch) {
|
2020-12-23 12:52:39 +01:00
|
|
|
body = body.rewriteToStatusTypeIfNeeded(account.getAccountId());
|
2023-05-13 16:00:28 +02:00
|
|
|
String mastodonNotificationId = body.getId();
|
|
|
|
int accountId = (int) account.getId();
|
|
|
|
|
|
|
|
// Check for an existing notification with this Mastodon Notification ID
|
|
|
|
android.app.Notification existingAndroidNotification = null;
|
|
|
|
StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications();
|
|
|
|
for (StatusBarNotification androidNotification : activeNotifications) {
|
|
|
|
if (mastodonNotificationId.equals(androidNotification.getTag()) && accountId == androidNotification.getId()) {
|
|
|
|
existingAndroidNotification = androidNotification.getNotification();
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
// Notification group member
|
|
|
|
// =========================
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
notificationId++;
|
2023-05-13 16:00:28 +02:00
|
|
|
// Create the notification -- either create a new one, or use the existing one.
|
|
|
|
NotificationCompat.Builder builder;
|
|
|
|
if (existingAndroidNotification == null) {
|
|
|
|
builder = newAndroidNotification(context, body, account);
|
|
|
|
} else {
|
|
|
|
builder = new NotificationCompat.Builder(context, existingAndroidNotification);
|
|
|
|
}
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2020-11-04 18:21:41 +01:00
|
|
|
builder.setContentTitle(titleForType(context, body, account))
|
2023-01-12 19:40:01 +01:00
|
|
|
.setContentText(bodyForType(body, context, account.getAlwaysOpenSpoiler()));
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2019-05-02 19:44:35 +02:00
|
|
|
if (body.getType() == Notification.Type.MENTION || body.getType() == Notification.Type.POLL) {
|
2018-05-06 11:07:10 +02:00
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
2023-01-12 19:40:01 +01:00
|
|
|
.bigText(bodyForType(body, context, account.getAlwaysOpenSpoiler())));
|
2018-05-06 11:07:10 +02:00
|
|
|
}
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
//load the avatar synchronously
|
|
|
|
Bitmap accountAvatar;
|
|
|
|
try {
|
2019-04-16 21:39:12 +02:00
|
|
|
FutureTarget<Bitmap> target = Glide.with(context)
|
|
|
|
.asBitmap()
|
2018-05-06 11:07:10 +02:00
|
|
|
.load(body.getAccount().getAvatar())
|
2019-04-16 21:39:12 +02:00
|
|
|
.transform(new RoundedCorners(20))
|
|
|
|
.submit();
|
|
|
|
|
|
|
|
accountAvatar = target.get();
|
|
|
|
} catch (ExecutionException | InterruptedException e) {
|
2018-05-06 11:07:10 +02:00
|
|
|
Log.d(TAG, "error loading account avatar", e);
|
|
|
|
accountAvatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.avatar_default);
|
|
|
|
}
|
2017-10-18 22:18:07 +02:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
builder.setLargeIcon(accountAvatar);
|
2018-02-03 22:45:14 +01:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
// Reply to mention action; RemoteInput is available from KitKat Watch, but buttons are available from Nougat
|
|
|
|
if (body.getType() == Notification.Type.MENTION
|
|
|
|
&& android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
RemoteInput replyRemoteInput = new RemoteInput.Builder(KEY_REPLY)
|
|
|
|
.setLabel(context.getString(R.string.label_quick_reply))
|
|
|
|
.build();
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2022-03-09 20:50:23 +01:00
|
|
|
PendingIntent quickReplyPendingIntent = getStatusReplyIntent(context, body, account);
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
NotificationCompat.Action quickReplyAction =
|
|
|
|
new NotificationCompat.Action.Builder(R.drawable.ic_reply_24dp,
|
2022-03-09 20:50:23 +01:00
|
|
|
context.getString(R.string.action_quick_reply),
|
|
|
|
quickReplyPendingIntent)
|
2018-05-06 11:07:10 +02:00
|
|
|
.addRemoteInput(replyRemoteInput)
|
|
|
|
.build();
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
builder.addAction(quickReplyAction);
|
|
|
|
|
2022-03-09 20:50:23 +01:00
|
|
|
PendingIntent composeIntent = getStatusComposeIntent(context, body, account);
|
2018-05-06 11:07:10 +02:00
|
|
|
|
|
|
|
NotificationCompat.Action composeAction =
|
|
|
|
new NotificationCompat.Action.Builder(R.drawable.ic_reply_24dp,
|
2022-03-09 20:50:23 +01:00
|
|
|
context.getString(R.string.action_compose_shortcut),
|
|
|
|
composeIntent)
|
|
|
|
.setShowsUserInterface(true)
|
2018-05-06 11:07:10 +02:00
|
|
|
.build();
|
|
|
|
|
|
|
|
builder.addAction(composeAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.setSubText(account.getFullName());
|
|
|
|
builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
|
|
|
|
builder.setOnlyAlertOnce(true);
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
Bundle extras = new Bundle();
|
|
|
|
// Add the sending account's name, so it can be used when summarising this notification
|
|
|
|
extras.putString(EXTRA_ACCOUNT_NAME, body.getAccount().getName());
|
|
|
|
extras.putString(EXTRA_NOTIFICATION_TYPE, body.getType().toString());
|
|
|
|
builder.addExtras(extras);
|
|
|
|
|
|
|
|
// Only alert for the first notification of a batch to avoid multiple alerts at once
|
2018-06-25 14:08:36 +02:00
|
|
|
if(!isFirstOfBatch) {
|
|
|
|
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
|
|
|
|
}
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the summary notifications for each notification group.
|
|
|
|
* <p>
|
|
|
|
* Notifications are sent to channels. Within each channel they may be grouped, and the group
|
|
|
|
* may have a summary.
|
|
|
|
* <p>
|
|
|
|
* Tusky uses N notification channels for each account, each channel corresponds to a type
|
|
|
|
* of notification (follow, reblog, mention, etc). Therefore each channel also has exactly
|
|
|
|
* 0 or 1 summary notifications along with its regular notifications.
|
|
|
|
* <p>
|
|
|
|
* The group key is the same as the channel ID.
|
|
|
|
* <p>
|
|
|
|
* Regnerates the summary notifications for all active Tusky notifications for `account`.
|
|
|
|
* This may delete the summary notification if there are no active notifications for that
|
|
|
|
* account in a group.
|
|
|
|
*
|
|
|
|
* @see <a href="https://developer.android.com/develop/ui/views/notifications/group">Create a
|
|
|
|
* notification group</a>
|
|
|
|
* @param context to access application preferences and services
|
|
|
|
* @param notificationManager the system's NotificationManager
|
|
|
|
* @param account the account for which the notification should be shown
|
|
|
|
*/
|
|
|
|
public static void updateSummaryNotifications(Context context, NotificationManager notificationManager, AccountEntity account) {
|
|
|
|
// Map from the channel ID to a list of notifications in that channel. Those are the
|
|
|
|
// notifications that will be summarised.
|
|
|
|
Map<String, List<StatusBarNotification>> channelGroups = new HashMap<>();
|
|
|
|
int accountId = (int) account.getId();
|
|
|
|
|
|
|
|
// Initialise the map with all channel IDs.
|
2023-08-02 09:04:24 +02:00
|
|
|
for (Notification.Type ty : Notification.Type.getEntries()) {
|
2023-05-13 16:00:28 +02:00
|
|
|
channelGroups.put(getChannelId(account, ty), new ArrayList<>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch all existing notifications. Add them to the map, ignoring notifications that:
|
|
|
|
// - belong to a different account
|
|
|
|
// - are summary notifications
|
|
|
|
for (StatusBarNotification sn : notificationManager.getActiveNotifications()) {
|
|
|
|
if (sn.getId() != accountId) continue;
|
|
|
|
|
|
|
|
String channelId = sn.getNotification().getGroup();
|
|
|
|
String summaryTag = GROUP_SUMMARY_TAG + "." + channelId;
|
|
|
|
if (summaryTag.equals(sn.getTag())) continue;
|
|
|
|
|
|
|
|
// TODO: API 26 supports getting the channel ID directly (sn.getNotification().getChannelId()).
|
|
|
|
// This works here because the channelId and the groupKey are the same.
|
|
|
|
List<StatusBarNotification> members = channelGroups.get(channelId);
|
|
|
|
if (members == null) { // can't happen, but just in case...
|
|
|
|
Log.e(TAG, "members == null for channel ID " + channelId);
|
|
|
|
continue;
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
2023-05-13 16:00:28 +02:00
|
|
|
members.add(sn);
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
// Create, update, or cancel the summary notifications for each group.
|
|
|
|
for (Map.Entry<String, List<StatusBarNotification>> channelGroup : channelGroups.entrySet()) {
|
|
|
|
String channelId = channelGroup.getKey();
|
|
|
|
List<StatusBarNotification> members = channelGroup.getValue();
|
|
|
|
String summaryTag = GROUP_SUMMARY_TAG + "." + channelId;
|
|
|
|
|
|
|
|
// If there are 0-1 notifications in this group then the additional summary
|
|
|
|
// notification is not needed and can be cancelled.
|
|
|
|
if (members.size() <= 1) {
|
|
|
|
notificationManager.cancel(summaryTag, accountId);
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-03 22:45:14 +01:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
// Create a notification that summarises the other notifications in this group
|
2017-04-28 05:29:42 +02:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
// All notifications in this group have the same type, so get it from the first.
|
2023-08-19 12:31:47 +02:00
|
|
|
Notification.Type notificationType = (Notification.Type)members.get(0).getNotification().extras.getSerializable(EXTRA_NOTIFICATION_TYPE);
|
|
|
|
|
|
|
|
Intent summaryResultIntent = MainActivity.openNotificationIntent(context, accountId, notificationType);
|
2023-05-13 16:00:28 +02:00
|
|
|
|
|
|
|
TaskStackBuilder summaryStackBuilder = TaskStackBuilder.create(context);
|
|
|
|
summaryStackBuilder.addParentStack(MainActivity.class);
|
|
|
|
summaryStackBuilder.addNextIntent(summaryResultIntent);
|
|
|
|
|
|
|
|
PendingIntent summaryResultPendingIntent = summaryStackBuilder.getPendingIntent((int) (notificationId + account.getId() * 10000),
|
|
|
|
pendingIntentFlags(false));
|
|
|
|
|
|
|
|
String title = context.getResources().getQuantityString(R.plurals.notification_title_summary, members.size(), members.size());
|
|
|
|
String text = joinNames(context, members);
|
|
|
|
|
|
|
|
NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context, channelId)
|
|
|
|
.setSmallIcon(R.drawable.ic_notify)
|
|
|
|
.setContentIntent(summaryResultPendingIntent)
|
|
|
|
.setColor(context.getColor(R.color.notification_color))
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setShortcutId(Long.toString(account.getId()))
|
|
|
|
.setDefaults(0) // So it doesn't ring twice, notify only in Target callback
|
|
|
|
.setContentTitle(title)
|
|
|
|
.setContentText(text)
|
|
|
|
.setSubText(account.getFullName())
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_SOCIAL)
|
|
|
|
.setOnlyAlertOnce(true)
|
|
|
|
.setGroup(channelId)
|
|
|
|
.setGroupSummary(true);
|
|
|
|
|
|
|
|
setSoundVibrationLight(account, summaryBuilder);
|
|
|
|
|
|
|
|
// TODO: Use the batch notification API available in NotificationManagerCompat
|
|
|
|
// 1.11 and up (https://developer.android.com/jetpack/androidx/releases/core#1.11.0-alpha01)
|
|
|
|
// when it is released.
|
|
|
|
notificationManager.notify(summaryTag, accountId, summaryBuilder.build());
|
|
|
|
|
|
|
|
// Android will rate limit / drop notifications if they're posted too
|
|
|
|
// quickly. There is no indication to the user that this happened.
|
|
|
|
// See https://github.com/tuskyapp/Tusky/pull/3626#discussion_r1192963664
|
|
|
|
try { Thread.sleep(1000); } catch (InterruptedException ignored) { }
|
2018-05-06 11:07:10 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-01 19:05:47 +01:00
|
|
|
|
2018-02-03 22:45:14 +01:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
private static NotificationCompat.Builder newAndroidNotification(Context context, Notification body, AccountEntity account) {
|
2018-05-06 11:07:10 +02:00
|
|
|
|
2023-08-19 12:31:47 +02:00
|
|
|
Intent eventResultIntent = MainActivity.openNotificationIntent(context, account.getId(), body.getType());
|
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
TaskStackBuilder eventStackBuilder = TaskStackBuilder.create(context);
|
2018-05-08 20:58:06 +02:00
|
|
|
eventStackBuilder.addParentStack(MainActivity.class);
|
2018-05-06 11:07:10 +02:00
|
|
|
eventStackBuilder.addNextIntent(eventResultIntent);
|
|
|
|
|
|
|
|
PendingIntent eventResultPendingIntent = eventStackBuilder.getPendingIntent((int) account.getId(),
|
2022-03-09 20:50:23 +01:00
|
|
|
pendingIntentFlags(false));
|
2018-05-06 11:07:10 +02:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
String channelId = getChannelId(account, body);
|
|
|
|
assert channelId != null;
|
2018-05-06 11:07:10 +02:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
|
2018-05-06 11:07:10 +02:00
|
|
|
.setSmallIcon(R.drawable.ic_notify)
|
2023-05-13 16:00:28 +02:00
|
|
|
.setContentIntent(eventResultPendingIntent)
|
2022-08-04 16:48:26 +02:00
|
|
|
.setColor(context.getColor(R.color.notification_color))
|
2023-05-13 16:00:28 +02:00
|
|
|
.setGroup(channelId)
|
2018-05-08 19:15:10 +02:00
|
|
|
.setAutoCancel(true)
|
2019-10-22 21:18:20 +02:00
|
|
|
.setShortcutId(Long.toString(account.getId()))
|
2018-05-06 11:07:10 +02:00
|
|
|
.setDefaults(0); // So it doesn't ring twice, notify only in Target callback
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
setSoundVibrationLight(account, builder);
|
2018-05-06 11:07:10 +02:00
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2022-03-09 20:50:23 +01:00
|
|
|
private static PendingIntent getStatusReplyIntent(Context context, Notification body, AccountEntity account) {
|
2018-05-06 11:07:10 +02:00
|
|
|
Status status = body.getStatus();
|
|
|
|
|
|
|
|
String inReplyToId = status.getId();
|
|
|
|
Status actionableStatus = status.getActionableStatus();
|
|
|
|
Status.Visibility replyVisibility = actionableStatus.getVisibility();
|
|
|
|
String contentWarning = actionableStatus.getSpoilerText();
|
2021-06-11 20:15:40 +02:00
|
|
|
List<Status.Mention> mentions = actionableStatus.getMentions();
|
2018-05-06 11:07:10 +02:00
|
|
|
List<String> mentionedUsernames = new ArrayList<>();
|
|
|
|
mentionedUsernames.add(actionableStatus.getAccount().getUsername());
|
|
|
|
for (Status.Mention mention : mentions) {
|
|
|
|
mentionedUsernames.add(mention.getUsername());
|
|
|
|
}
|
|
|
|
mentionedUsernames.removeAll(Collections.singleton(account.getUsername()));
|
|
|
|
mentionedUsernames = new ArrayList<>(new LinkedHashSet<>(mentionedUsernames));
|
|
|
|
|
|
|
|
Intent replyIntent = new Intent(context, SendStatusBroadcastReceiver.class)
|
2022-03-09 20:50:23 +01:00
|
|
|
.setAction(REPLY_ACTION)
|
2018-05-06 11:07:10 +02:00
|
|
|
.putExtra(KEY_SENDER_ACCOUNT_ID, account.getId())
|
|
|
|
.putExtra(KEY_SENDER_ACCOUNT_IDENTIFIER, account.getIdentifier())
|
|
|
|
.putExtra(KEY_SENDER_ACCOUNT_FULL_NAME, account.getFullName())
|
|
|
|
.putExtra(KEY_NOTIFICATION_ID, notificationId)
|
|
|
|
.putExtra(KEY_CITED_STATUS_ID, inReplyToId)
|
|
|
|
.putExtra(KEY_VISIBILITY, replyVisibility)
|
|
|
|
.putExtra(KEY_SPOILER, contentWarning)
|
|
|
|
.putExtra(KEY_MENTIONS, mentionedUsernames.toArray(new String[0]));
|
|
|
|
|
|
|
|
return PendingIntent.getBroadcast(context.getApplicationContext(),
|
|
|
|
notificationId,
|
|
|
|
replyIntent,
|
2022-03-09 20:50:23 +01:00
|
|
|
pendingIntentFlags(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static PendingIntent getStatusComposeIntent(Context context, Notification body, AccountEntity account) {
|
|
|
|
Status status = body.getStatus();
|
|
|
|
|
|
|
|
String citedLocalAuthor = status.getAccount().getLocalUsername();
|
2022-04-16 09:44:05 +02:00
|
|
|
String citedText = parseAsMastodonHtml(status.getContent()).toString();
|
2022-03-09 20:50:23 +01:00
|
|
|
String inReplyToId = status.getId();
|
|
|
|
Status actionableStatus = status.getActionableStatus();
|
|
|
|
Status.Visibility replyVisibility = actionableStatus.getVisibility();
|
|
|
|
String contentWarning = actionableStatus.getSpoilerText();
|
|
|
|
List<Status.Mention> mentions = actionableStatus.getMentions();
|
|
|
|
Set<String> mentionedUsernames = new LinkedHashSet<>();
|
|
|
|
mentionedUsernames.add(actionableStatus.getAccount().getUsername());
|
|
|
|
for (Status.Mention mention : mentions) {
|
|
|
|
String mentionedUsername = mention.getUsername();
|
|
|
|
if (!mentionedUsername.equals(account.getUsername())) {
|
|
|
|
mentionedUsernames.add(mention.getUsername());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ComposeActivity.ComposeOptions composeOptions = new ComposeActivity.ComposeOptions();
|
|
|
|
composeOptions.setInReplyToId(inReplyToId);
|
|
|
|
composeOptions.setReplyVisibility(replyVisibility);
|
|
|
|
composeOptions.setContentWarning(contentWarning);
|
|
|
|
composeOptions.setReplyingStatusAuthor(citedLocalAuthor);
|
|
|
|
composeOptions.setReplyingStatusContent(citedText);
|
|
|
|
composeOptions.setMentionedUsernames(mentionedUsernames);
|
|
|
|
composeOptions.setModifiedInitialState(true);
|
2022-08-31 18:53:57 +02:00
|
|
|
composeOptions.setLanguage(actionableStatus.getLanguage());
|
Fix saving changes to statuses when editing (#3103)
* Fix saving changes to statuses when editing
With the previous code backing out of a status editing operation where changes
had been made (whether it was editing an existing status, a scheduled status,
or a draft) would prompt the user to save the changes as a new draft.
See https://github.com/tuskyapp/Tusky/issues/2704 and
https://github.com/tuskyapp/Tusky/issues/2705 for more detail.
The fix:
- Create an enum to represent the four different kinds of edits that can
happen
- Editing a new status (i.e., composing it for the first time)
- Editing a posted status
- Editing a draft
- Editing a scheduled status
- Store this in ComposeOptions, and set it appropriately everywhere
ComposeOptions is created.
- Check the edit kind when backing out of ComposeActivity, and use this to
show one of three different dialogs as appropriate so the user can:
- Save as new draft or discard changes
- Continue editing or discard changes
- Update existing draft or discard changes
Also fix ComposeViewModel.didChange(), which erroneously reported false if the
old text started with the new text (e.g., if the old text was "hello, world"
and it was edited to "hello", didChange() would not consider that to be a
change).
Fixes https://github.com/tuskyapp/Tusky/issues/2704,
https://github.com/tuskyapp/Tusky/issues/2705
* Use orEmpty extension function
2022-12-31 13:04:49 +01:00
|
|
|
composeOptions.setKind(ComposeActivity.ComposeKind.NEW);
|
2022-03-09 20:50:23 +01:00
|
|
|
|
2023-08-19 12:31:47 +02:00
|
|
|
Intent composeIntent = MainActivity.composeIntent(context, composeOptions, account.getId(), body.getId(), (int)account.getId());
|
2022-03-09 20:50:23 +01:00
|
|
|
|
|
|
|
composeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
|
|
|
return PendingIntent.getActivity(context.getApplicationContext(),
|
|
|
|
notificationId,
|
|
|
|
composeIntent,
|
|
|
|
pendingIntentFlags(false));
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 18:37:27 +02:00
|
|
|
/**
|
|
|
|
* Creates a notification channel for notifications for background work that should not
|
|
|
|
* disturb the user.
|
|
|
|
*
|
|
|
|
* @param context context
|
|
|
|
*/
|
|
|
|
public static void createWorkerNotificationChannel(@NonNull Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
|
|
|
|
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
NotificationChannel channel = new NotificationChannel(
|
|
|
|
CHANNEL_BACKGROUND_TASKS,
|
|
|
|
context.getString(R.string.notification_listenable_worker_name),
|
|
|
|
NotificationManager.IMPORTANCE_NONE
|
|
|
|
);
|
|
|
|
|
|
|
|
channel.setDescription(context.getString(R.string.notification_listenable_worker_description));
|
|
|
|
channel.enableLights(false);
|
|
|
|
channel.enableVibration(false);
|
|
|
|
channel.setShowBadge(false);
|
|
|
|
|
|
|
|
notificationManager.createNotificationChannel(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a notification for a background worker.
|
|
|
|
*
|
|
|
|
* @param context context
|
|
|
|
* @param titleResource String resource to use as the notification's title
|
|
|
|
* @return the notification
|
|
|
|
*/
|
|
|
|
@NonNull
|
|
|
|
public static android.app.Notification createWorkerNotification(@NonNull Context context, @StringRes int titleResource) {
|
|
|
|
String title = context.getString(titleResource);
|
|
|
|
return new NotificationCompat.Builder(context, CHANNEL_BACKGROUND_TASKS)
|
|
|
|
.setContentTitle(title)
|
|
|
|
.setTicker(title)
|
|
|
|
.setSmallIcon(R.drawable.ic_notify)
|
|
|
|
.setOngoing(true)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:59:10 +02:00
|
|
|
public static void createNotificationChannelsForAccount(@NonNull AccountEntity account, @NonNull Context context) {
|
2017-11-13 19:05:23 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2017-10-18 22:18:07 +02:00
|
|
|
|
2018-02-12 22:03:08 +01:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2017-10-18 22:18:07 +02:00
|
|
|
|
2018-02-03 22:45:14 +01:00
|
|
|
String[] channelIds = new String[]{
|
2018-03-09 22:02:32 +01:00
|
|
|
CHANNEL_MENTION + account.getIdentifier(),
|
|
|
|
CHANNEL_FOLLOW + account.getIdentifier(),
|
2020-03-19 22:02:10 +01:00
|
|
|
CHANNEL_FOLLOW_REQUEST + account.getIdentifier(),
|
2018-03-09 22:02:32 +01:00
|
|
|
CHANNEL_BOOST + account.getIdentifier(),
|
2019-05-02 19:44:35 +02:00
|
|
|
CHANNEL_FAVOURITE + account.getIdentifier(),
|
|
|
|
CHANNEL_POLL + account.getIdentifier(),
|
2020-12-23 12:52:39 +01:00
|
|
|
CHANNEL_SUBSCRIPTIONS + account.getIdentifier(),
|
2022-04-14 19:39:30 +02:00
|
|
|
CHANNEL_SIGN_UP + account.getIdentifier(),
|
2022-04-19 11:10:13 +02:00
|
|
|
CHANNEL_UPDATES + account.getIdentifier(),
|
2022-12-01 20:11:55 +01:00
|
|
|
CHANNEL_REPORT + account.getIdentifier(),
|
2019-05-02 19:44:35 +02:00
|
|
|
};
|
2017-10-18 22:18:07 +02:00
|
|
|
int[] channelNames = {
|
2019-05-02 19:44:35 +02:00
|
|
|
R.string.notification_mention_name,
|
|
|
|
R.string.notification_follow_name,
|
2020-03-19 22:02:10 +01:00
|
|
|
R.string.notification_follow_request_name,
|
2019-05-02 19:44:35 +02:00
|
|
|
R.string.notification_boost_name,
|
|
|
|
R.string.notification_favourite_name,
|
2020-12-23 12:52:39 +01:00
|
|
|
R.string.notification_poll_name,
|
|
|
|
R.string.notification_subscription_name,
|
2022-04-14 19:39:30 +02:00
|
|
|
R.string.notification_sign_up_name,
|
2022-04-19 11:10:13 +02:00
|
|
|
R.string.notification_update_name,
|
2022-12-01 20:11:55 +01:00
|
|
|
R.string.notification_report_name,
|
2017-10-18 22:18:07 +02:00
|
|
|
};
|
|
|
|
int[] channelDescriptions = {
|
2019-05-02 19:44:35 +02:00
|
|
|
R.string.notification_mention_descriptions,
|
|
|
|
R.string.notification_follow_description,
|
2020-03-19 22:02:10 +01:00
|
|
|
R.string.notification_follow_request_description,
|
2019-05-02 19:44:35 +02:00
|
|
|
R.string.notification_boost_description,
|
|
|
|
R.string.notification_favourite_description,
|
2020-12-23 12:52:39 +01:00
|
|
|
R.string.notification_poll_description,
|
|
|
|
R.string.notification_subscription_description,
|
2022-04-14 19:39:30 +02:00
|
|
|
R.string.notification_sign_up_description,
|
2022-04-19 11:10:13 +02:00
|
|
|
R.string.notification_update_description,
|
2022-12-01 20:11:55 +01:00
|
|
|
R.string.notification_report_description,
|
2017-10-18 22:18:07 +02:00
|
|
|
};
|
|
|
|
|
2020-03-19 22:02:10 +01:00
|
|
|
List<NotificationChannel> channels = new ArrayList<>(6);
|
2017-10-18 22:18:07 +02:00
|
|
|
|
2018-02-03 22:45:14 +01:00
|
|
|
NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(), account.getFullName());
|
|
|
|
|
2018-02-12 22:03:08 +01:00
|
|
|
notificationManager.createNotificationChannelGroup(channelGroup);
|
2018-02-03 22:45:14 +01:00
|
|
|
|
2017-11-13 19:05:23 +01:00
|
|
|
for (int i = 0; i < channelIds.length; i++) {
|
2017-10-18 22:18:07 +02:00
|
|
|
String id = channelIds[i];
|
|
|
|
String name = context.getString(channelNames[i]);
|
|
|
|
String description = context.getString(channelDescriptions[i]);
|
2018-02-12 22:03:08 +01:00
|
|
|
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
2017-10-18 22:18:07 +02:00
|
|
|
NotificationChannel channel = new NotificationChannel(id, name, importance);
|
|
|
|
|
|
|
|
channel.setDescription(description);
|
|
|
|
channel.enableLights(true);
|
2018-02-12 22:03:08 +01:00
|
|
|
channel.setLightColor(0xFF2B90D9);
|
2017-10-18 22:18:07 +02:00
|
|
|
channel.enableVibration(true);
|
|
|
|
channel.setShowBadge(true);
|
2018-02-03 22:45:14 +01:00
|
|
|
channel.setGroup(account.getIdentifier());
|
2017-10-18 22:18:07 +02:00
|
|
|
channels.add(channel);
|
|
|
|
}
|
|
|
|
|
2018-02-12 22:03:08 +01:00
|
|
|
notificationManager.createNotificationChannels(channels);
|
2017-10-18 22:18:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:59:10 +02:00
|
|
|
public static void deleteNotificationChannelsForAccount(@NonNull AccountEntity account, @NonNull Context context) {
|
2018-02-03 22:45:14 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
2018-02-12 22:03:08 +01:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-02-03 22:45:14 +01:00
|
|
|
|
2018-02-12 22:03:08 +01:00
|
|
|
notificationManager.deleteNotificationChannelGroup(account.getIdentifier());
|
2018-02-03 22:45:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:59:10 +02:00
|
|
|
public static boolean areNotificationsEnabled(@NonNull Context context, @NonNull AccountManager accountManager) {
|
2018-03-09 22:02:32 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2018-02-12 22:03:08 +01:00
|
|
|
|
|
|
|
// on Android >= O, notifications are enabled, if at least one channel is enabled
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
2018-03-09 22:02:32 +01:00
|
|
|
if (notificationManager.areNotificationsEnabled()) {
|
|
|
|
for (NotificationChannel channel : notificationManager.getNotificationChannels()) {
|
2022-12-02 19:46:02 +01:00
|
|
|
if (channel != null && channel.getImportance() > NotificationManager.IMPORTANCE_NONE) {
|
2018-02-12 22:03:08 +01:00
|
|
|
Log.d(TAG, "NotificationsEnabled");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.d(TAG, "NotificationsDisabled");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// on Android < O, notifications are enabled, if at least one account has notification enabled
|
2018-07-23 21:59:10 +02:00
|
|
|
return accountManager.areNotificationsEnabled();
|
2018-02-12 22:03:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:46:49 +02:00
|
|
|
public static void enablePullNotifications(Context context) {
|
|
|
|
WorkManager workManager = WorkManager.getInstance(context);
|
|
|
|
workManager.cancelAllWorkByTag(NOTIFICATION_PULL_TAG);
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
// Periodic work requests are supposed to start running soon after being enqueued. In
|
|
|
|
// practice that may not be soon enough, so create and enqueue an expedited one-time
|
|
|
|
// request to get new notifications immediately.
|
|
|
|
WorkRequest fetchNotifications = new OneTimeWorkRequest.Builder(NotificationWorker.class)
|
|
|
|
.setExpedited(OutOfQuotaPolicy.DROP_WORK_REQUEST)
|
|
|
|
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build())
|
|
|
|
.build();
|
|
|
|
workManager.enqueue(fetchNotifications);
|
|
|
|
|
2020-05-12 18:46:49 +02:00
|
|
|
WorkRequest workRequest = new PeriodicWorkRequest.Builder(
|
|
|
|
NotificationWorker.class,
|
|
|
|
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS,
|
|
|
|
PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS
|
|
|
|
)
|
|
|
|
.addTag(NOTIFICATION_PULL_TAG)
|
|
|
|
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build())
|
2023-05-13 16:00:28 +02:00
|
|
|
.setInitialDelay(5, TimeUnit.MINUTES)
|
2020-05-12 18:46:49 +02:00
|
|
|
.build();
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2020-05-12 18:46:49 +02:00
|
|
|
workManager.enqueue(workRequest);
|
|
|
|
|
|
|
|
Log.d(TAG, "enabled notification checks with "+ PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS + "ms interval");
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 18:46:49 +02:00
|
|
|
public static void disablePullNotifications(Context context) {
|
|
|
|
WorkManager.getInstance(context).cancelAllWorkByTag(NOTIFICATION_PULL_TAG);
|
2018-11-12 21:09:39 +01:00
|
|
|
Log.d(TAG, "disabled notification checks");
|
|
|
|
}
|
|
|
|
|
2023-06-01 19:19:18 +02:00
|
|
|
public static void clearNotificationsForAccount(@NonNull Context context, @NonNull AccountEntity account) {
|
2023-05-13 16:00:28 +02:00
|
|
|
int accountId = (int) account.getId();
|
|
|
|
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
for (StatusBarNotification androidNotification : notificationManager.getActiveNotifications()) {
|
|
|
|
if (accountId == androidNotification.getId()) {
|
|
|
|
notificationManager.cancel(androidNotification.getTag(), androidNotification.getId());
|
|
|
|
}
|
2017-11-15 21:18:35 +01:00
|
|
|
}
|
2017-11-13 19:05:23 +01:00
|
|
|
}
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
public static boolean filterNotification(NotificationManager notificationManager, AccountEntity account, @NonNull Notification notification) {
|
|
|
|
return filterNotification(notificationManager, account, notification.getType());
|
2022-05-17 19:32:09 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
public static boolean filterNotification(@NonNull NotificationManager notificationManager, @NonNull AccountEntity account, @NonNull Notification.Type type) {
|
2017-11-13 19:05:23 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2022-05-17 19:32:09 +02:00
|
|
|
String channelId = getChannelId(account, type);
|
2019-05-02 19:44:35 +02:00
|
|
|
if(channelId == null) {
|
|
|
|
// unknown notificationtype
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
|
2022-12-02 19:46:02 +01:00
|
|
|
return channel != null && channel.getImportance() > NotificationManager.IMPORTANCE_NONE;
|
2017-10-18 22:18:07 +02:00
|
|
|
}
|
|
|
|
|
2022-05-17 19:32:09 +02:00
|
|
|
switch (type) {
|
2017-07-02 02:32:35 +02:00
|
|
|
case MENTION:
|
2018-02-03 22:45:14 +01:00
|
|
|
return account.getNotificationsMentioned();
|
2020-12-23 12:52:39 +01:00
|
|
|
case STATUS:
|
|
|
|
return account.getNotificationsSubscriptions();
|
2017-07-02 02:32:35 +02:00
|
|
|
case FOLLOW:
|
2018-02-03 22:45:14 +01:00
|
|
|
return account.getNotificationsFollowed();
|
2020-03-19 22:02:10 +01:00
|
|
|
case FOLLOW_REQUEST:
|
|
|
|
return account.getNotificationsFollowRequested();
|
2017-07-02 02:32:35 +02:00
|
|
|
case REBLOG:
|
2018-02-03 22:45:14 +01:00
|
|
|
return account.getNotificationsReblogged();
|
2017-07-02 02:32:35 +02:00
|
|
|
case FAVOURITE:
|
2018-02-03 22:45:14 +01:00
|
|
|
return account.getNotificationsFavorited();
|
2019-05-02 19:44:35 +02:00
|
|
|
case POLL:
|
|
|
|
return account.getNotificationsPolls();
|
2022-04-14 19:39:30 +02:00
|
|
|
case SIGN_UP:
|
|
|
|
return account.getNotificationsSignUps();
|
2022-04-19 11:10:13 +02:00
|
|
|
case UPDATE:
|
|
|
|
return account.getNotificationsUpdates();
|
2022-12-01 20:11:55 +01:00
|
|
|
case REPORT:
|
|
|
|
return account.getNotificationsReports();
|
2019-03-25 20:35:21 +01:00
|
|
|
default:
|
|
|
|
return false;
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:46:49 +02:00
|
|
|
@Nullable
|
|
|
|
private static String getChannelId(AccountEntity account, Notification notification) {
|
2022-05-17 19:32:09 +02:00
|
|
|
return getChannelId(account, notification.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static String getChannelId(AccountEntity account, Notification.Type type) {
|
|
|
|
switch (type) {
|
2017-11-13 19:05:23 +01:00
|
|
|
case MENTION:
|
2018-03-09 22:02:32 +01:00
|
|
|
return CHANNEL_MENTION + account.getIdentifier();
|
2020-12-23 12:52:39 +01:00
|
|
|
case STATUS:
|
|
|
|
return CHANNEL_SUBSCRIPTIONS + account.getIdentifier();
|
2017-11-13 19:05:23 +01:00
|
|
|
case FOLLOW:
|
2018-03-09 22:02:32 +01:00
|
|
|
return CHANNEL_FOLLOW + account.getIdentifier();
|
2020-03-19 22:02:10 +01:00
|
|
|
case FOLLOW_REQUEST:
|
|
|
|
return CHANNEL_FOLLOW_REQUEST + account.getIdentifier();
|
2017-11-13 19:05:23 +01:00
|
|
|
case REBLOG:
|
2018-03-09 22:02:32 +01:00
|
|
|
return CHANNEL_BOOST + account.getIdentifier();
|
2017-11-13 19:05:23 +01:00
|
|
|
case FAVOURITE:
|
2018-03-09 22:02:32 +01:00
|
|
|
return CHANNEL_FAVOURITE + account.getIdentifier();
|
2019-05-02 19:44:35 +02:00
|
|
|
case POLL:
|
|
|
|
return CHANNEL_POLL + account.getIdentifier();
|
2022-04-14 19:39:30 +02:00
|
|
|
case SIGN_UP:
|
|
|
|
return CHANNEL_SIGN_UP + account.getIdentifier();
|
2022-12-01 20:11:55 +01:00
|
|
|
case UPDATE:
|
|
|
|
return CHANNEL_UPDATES + account.getIdentifier();
|
|
|
|
case REPORT:
|
|
|
|
return CHANNEL_REPORT + account.getIdentifier();
|
2019-05-02 19:44:35 +02:00
|
|
|
default:
|
|
|
|
return null;
|
2017-11-13 19:05:23 +01:00
|
|
|
}
|
2017-10-18 22:18:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
private static void setSoundVibrationLight(AccountEntity account, NotificationCompat.Builder builder) {
|
2017-11-13 19:05:23 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2017-10-18 22:18:07 +02:00
|
|
|
return; //do nothing on Android O or newer, the system uses the channel settings anyway
|
|
|
|
}
|
|
|
|
|
2018-02-03 22:45:14 +01:00
|
|
|
if (account.getNotificationSound()) {
|
2017-04-28 05:29:42 +02:00
|
|
|
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
|
|
|
|
}
|
|
|
|
|
2018-02-03 22:45:14 +01:00
|
|
|
if (account.getNotificationVibration()) {
|
2017-11-13 19:05:23 +01:00
|
|
|
builder.setVibrate(new long[]{500, 500});
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 22:45:14 +01:00
|
|
|
if (account.getNotificationLight()) {
|
2018-02-12 22:03:08 +01:00
|
|
|
builder.setLights(0xFF2B90D9, 300, 1000);
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 16:00:28 +02:00
|
|
|
private static String wrapItemAt(StatusBarNotification notification) {
|
|
|
|
return StringUtils.unicodeWrap(notification.getNotification().extras.getString(EXTRA_ACCOUNT_NAME));//getAccount().getName());
|
2018-05-24 19:00:17 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 05:29:42 +02:00
|
|
|
@Nullable
|
2023-05-13 16:00:28 +02:00
|
|
|
private static String joinNames(Context context, List<StatusBarNotification> notifications) {
|
|
|
|
if (notifications.size() > 3) {
|
|
|
|
int length = notifications.size();
|
|
|
|
//notifications.get(0).getNotification().extras.getString(EXTRA_ACCOUNT_NAME);
|
2017-07-02 02:32:35 +02:00
|
|
|
return String.format(context.getString(R.string.notification_summary_large),
|
2023-05-13 16:00:28 +02:00
|
|
|
wrapItemAt(notifications.get(length - 1)),
|
|
|
|
wrapItemAt(notifications.get(length - 2)),
|
|
|
|
wrapItemAt(notifications.get(length - 3)),
|
2018-05-24 19:00:17 +02:00
|
|
|
length - 3);
|
2023-05-13 16:00:28 +02:00
|
|
|
} else if (notifications.size() == 3) {
|
2017-07-02 02:32:35 +02:00
|
|
|
return String.format(context.getString(R.string.notification_summary_medium),
|
2023-05-13 16:00:28 +02:00
|
|
|
wrapItemAt(notifications.get(2)),
|
|
|
|
wrapItemAt(notifications.get(1)),
|
|
|
|
wrapItemAt(notifications.get(0)));
|
|
|
|
} else if (notifications.size() == 2) {
|
2017-07-02 02:32:35 +02:00
|
|
|
return String.format(context.getString(R.string.notification_summary_small),
|
2023-05-13 16:00:28 +02:00
|
|
|
wrapItemAt(notifications.get(1)),
|
|
|
|
wrapItemAt(notifications.get(0)));
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2020-11-04 18:21:41 +01:00
|
|
|
private static String titleForType(Context context, Notification notification, AccountEntity account) {
|
|
|
|
String accountName = StringUtils.unicodeWrap(notification.getAccount().getName());
|
2018-03-03 13:24:03 +01:00
|
|
|
switch (notification.getType()) {
|
2017-04-28 05:29:42 +02:00
|
|
|
case MENTION:
|
2017-07-02 02:32:35 +02:00
|
|
|
return String.format(context.getString(R.string.notification_mention_format),
|
2018-05-24 19:00:17 +02:00
|
|
|
accountName);
|
2020-12-23 12:52:39 +01:00
|
|
|
case STATUS:
|
|
|
|
return String.format(context.getString(R.string.notification_subscription_format),
|
|
|
|
accountName);
|
2017-04-28 05:29:42 +02:00
|
|
|
case FOLLOW:
|
2017-07-02 02:32:35 +02:00
|
|
|
return String.format(context.getString(R.string.notification_follow_format),
|
2018-05-24 19:00:17 +02:00
|
|
|
accountName);
|
2020-03-19 22:02:10 +01:00
|
|
|
case FOLLOW_REQUEST:
|
|
|
|
return String.format(context.getString(R.string.notification_follow_request_format),
|
|
|
|
accountName);
|
2017-04-28 05:29:42 +02:00
|
|
|
case FAVOURITE:
|
2017-07-02 02:32:35 +02:00
|
|
|
return String.format(context.getString(R.string.notification_favourite_format),
|
2018-05-24 19:00:17 +02:00
|
|
|
accountName);
|
2017-04-28 05:29:42 +02:00
|
|
|
case REBLOG:
|
2017-07-02 02:32:35 +02:00
|
|
|
return String.format(context.getString(R.string.notification_reblog_format),
|
2018-05-24 19:00:17 +02:00
|
|
|
accountName);
|
2019-05-02 19:44:35 +02:00
|
|
|
case POLL:
|
|
|
|
if(notification.getStatus().getAccount().getId().equals(account.getAccountId())) {
|
|
|
|
return context.getString(R.string.poll_ended_created);
|
|
|
|
} else {
|
|
|
|
return context.getString(R.string.poll_ended_voted);
|
|
|
|
}
|
2022-04-14 19:39:30 +02:00
|
|
|
case SIGN_UP:
|
|
|
|
return String.format(context.getString(R.string.notification_sign_up_format), accountName);
|
2022-04-19 11:10:13 +02:00
|
|
|
case UPDATE:
|
|
|
|
return String.format(context.getString(R.string.notification_update_format), accountName);
|
2022-12-01 20:11:55 +01:00
|
|
|
case REPORT:
|
|
|
|
return context.getString(R.string.notification_report_format, account.getDomain());
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-12 19:40:01 +01:00
|
|
|
private static String bodyForType(Notification notification, Context context, Boolean alwaysOpenSpoiler) {
|
2018-03-03 13:24:03 +01:00
|
|
|
switch (notification.getType()) {
|
2017-04-28 05:29:42 +02:00
|
|
|
case FOLLOW:
|
2020-03-19 22:02:10 +01:00
|
|
|
case FOLLOW_REQUEST:
|
2022-04-14 19:39:30 +02:00
|
|
|
case SIGN_UP:
|
2018-03-09 22:02:32 +01:00
|
|
|
return "@" + notification.getAccount().getUsername();
|
2017-04-28 05:29:42 +02:00
|
|
|
case MENTION:
|
|
|
|
case FAVOURITE:
|
|
|
|
case REBLOG:
|
2020-12-23 12:52:39 +01:00
|
|
|
case STATUS:
|
2023-01-12 19:40:01 +01:00
|
|
|
if (!TextUtils.isEmpty(notification.getStatus().getSpoilerText()) && !alwaysOpenSpoiler) {
|
2018-08-13 22:59:30 +02:00
|
|
|
return notification.getStatus().getSpoilerText();
|
|
|
|
} else {
|
2022-04-16 09:44:05 +02:00
|
|
|
return parseAsMastodonHtml(notification.getStatus().getContent()).toString();
|
2018-08-13 22:59:30 +02:00
|
|
|
}
|
2019-05-02 19:44:35 +02:00
|
|
|
case POLL:
|
2023-01-12 19:40:01 +01:00
|
|
|
if (!TextUtils.isEmpty(notification.getStatus().getSpoilerText()) && !alwaysOpenSpoiler) {
|
2019-05-02 19:44:35 +02:00
|
|
|
return notification.getStatus().getSpoilerText();
|
|
|
|
} else {
|
2022-04-16 09:44:05 +02:00
|
|
|
StringBuilder builder = new StringBuilder(parseAsMastodonHtml(notification.getStatus().getContent()));
|
2019-05-02 19:44:35 +02:00
|
|
|
builder.append('\n');
|
|
|
|
Poll poll = notification.getStatus().getPoll();
|
2021-09-17 22:12:17 +02:00
|
|
|
List<PollOption> options = poll.getOptions();
|
|
|
|
for(int i = 0; i < options.size(); ++i) {
|
|
|
|
PollOption option = options.get(i);
|
2019-10-11 17:35:22 +02:00
|
|
|
builder.append(buildDescription(option.getTitle(),
|
2020-03-24 21:06:58 +01:00
|
|
|
PollViewDataKt.calculatePercent(option.getVotesCount(), poll.getVotersCount(), poll.getVotesCount()),
|
2021-09-17 22:12:17 +02:00
|
|
|
poll.getOwnVotes() != null && poll.getOwnVotes().contains(i),
|
2019-10-11 17:35:22 +02:00
|
|
|
context));
|
2019-05-02 19:44:35 +02:00
|
|
|
builder.append('\n');
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2022-12-01 20:11:55 +01:00
|
|
|
case REPORT:
|
|
|
|
return context.getString(
|
|
|
|
R.string.notification_header_report_format,
|
|
|
|
StringUtils.unicodeWrap(notification.getAccount().getName()),
|
|
|
|
StringUtils.unicodeWrap(notification.getReport().getTargetAccount().getName())
|
|
|
|
);
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-18 22:18:07 +02:00
|
|
|
|
2022-03-09 20:50:23 +01:00
|
|
|
public static int pendingIntentFlags(boolean mutable) {
|
|
|
|
if (mutable) {
|
|
|
|
return PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
|
|
|
|
} else {
|
|
|
|
return PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0);
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 05:29:42 +02:00
|
|
|
}
|