2017-04-28 13:29:42 +10:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* 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>. */
|
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
package com.keylesspalace.tusky.util;
|
2017-04-28 13:29:42 +10:00
|
|
|
|
2017-10-19 07:18:07 +11:00
|
|
|
import android.app.NotificationChannel;
|
2018-02-04 08:45:14 +11:00
|
|
|
import android.app.NotificationChannelGroup;
|
2018-02-13 08:03:08 +11:00
|
|
|
import android.app.NotificationManager;
|
2017-04-28 13:29:42 +10:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
2017-11-03 20:09:09 +11:00
|
|
|
import android.graphics.BitmapFactory;
|
2017-04-28 13:29:42 +10:00
|
|
|
import android.os.Build;
|
|
|
|
import android.provider.Settings;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
import android.support.v4.app.TaskStackBuilder;
|
2017-10-19 07:18:07 +11:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-05-24 05:34:31 +10:00
|
|
|
import android.util.Log;
|
2017-04-28 13:29:42 +10:00
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.MainActivity;
|
|
|
|
import com.keylesspalace.tusky.R;
|
2018-02-04 08:45:14 +11:00
|
|
|
import com.keylesspalace.tusky.TuskyApplication;
|
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2017-04-28 13:29:42 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
2017-05-15 20:05:10 +10:00
|
|
|
import com.keylesspalace.tusky.receiver.NotificationClearBroadcastReceiver;
|
|
|
|
import com.keylesspalace.tusky.view.RoundedTransformation;
|
2017-04-28 13:29:42 +10:00
|
|
|
import com.squareup.picasso.Picasso;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
2017-11-03 20:09:09 +11:00
|
|
|
import java.io.IOException;
|
2017-10-19 07:18:07 +11:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
public class NotificationHelper {
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
/**
|
|
|
|
* constants used in Intents
|
|
|
|
*/
|
2018-02-04 08:45:14 +11:00
|
|
|
public static final String ACCOUNT_ID = "account_id";
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
private static final String TAG = "NotificationHelper";
|
2017-05-24 05:34:31 +10:00
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
/**
|
|
|
|
* notification channels used on Android O+
|
|
|
|
**/
|
2017-10-19 07:18:07 +11:00
|
|
|
private static final String CHANNEL_MENTION = "CHANNEL_MENTION";
|
|
|
|
private static final String CHANNEL_FOLLOW = "CHANNEL_FOLLOW";
|
|
|
|
private static final String CHANNEL_BOOST = "CHANNEL_BOOST";
|
2017-11-14 05:05:23 +11:00
|
|
|
private static final String CHANNEL_FAVOURITE = " CHANNEL_FAVOURITE";
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2017-07-28 12:03:45 +10:00
|
|
|
/**
|
|
|
|
* Takes a given Mastodon notification and either creates a new Android notification or updates
|
|
|
|
* the state of the existing notification to reflect the new interaction.
|
|
|
|
*
|
2018-03-10 08:02:32 +11: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
|
2017-07-28 12:03:45 +10:00
|
|
|
*/
|
2017-04-28 13:29:42 +10:00
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
public static void make(final Context context, Notification body, AccountEntity account) {
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
if (!filterNotification(account, body, context)) {
|
2017-04-28 13:29:42 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
String rawCurrentNotifications = account.getActiveNotifications();
|
2017-04-28 13:29:42 +10:00
|
|
|
JSONArray currentNotifications;
|
|
|
|
|
|
|
|
try {
|
|
|
|
currentNotifications = new JSONArray(rawCurrentNotifications);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
currentNotifications = new JSONArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean alreadyContains = false;
|
|
|
|
|
2017-11-14 05:05:23 +11:00
|
|
|
for (int i = 0; i < currentNotifications.length(); i++) {
|
2017-04-28 13:29:42 +10:00
|
|
|
try {
|
2018-03-03 23:24:03 +11:00
|
|
|
if (currentNotifications.getString(i).equals(body.getAccount().getName())) {
|
2017-04-28 13:29:42 +10:00
|
|
|
alreadyContains = true;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2017-05-24 05:34:31 +10:00
|
|
|
Log.d(TAG, Log.getStackTraceString(e));
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alreadyContains) {
|
2018-03-03 23:24:03 +11:00
|
|
|
currentNotifications.put(body.getAccount().getName());
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
account.setActiveNotifications(currentNotifications.toString());
|
|
|
|
|
|
|
|
//no need to save account, this will be done in the calling function
|
2017-04-28 13:29:42 +10:00
|
|
|
|
|
|
|
Intent resultIntent = new Intent(context, MainActivity.class);
|
2018-02-04 08:45:14 +11:00
|
|
|
resultIntent.putExtra(ACCOUNT_ID, account.getId());
|
2017-04-28 13:29:42 +10:00
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
|
|
|
|
stackBuilder.addParentStack(MainActivity.class);
|
|
|
|
stackBuilder.addNextIntent(resultIntent);
|
2018-03-10 08:02:32 +11:00
|
|
|
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent((int) account.getId(),
|
2017-07-02 10:32:35 +10:00
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
2017-04-28 13:29:42 +10:00
|
|
|
|
|
|
|
Intent deleteIntent = new Intent(context, NotificationClearBroadcastReceiver.class);
|
2018-02-04 08:45:14 +11:00
|
|
|
deleteIntent.putExtra(ACCOUNT_ID, account.getId());
|
2018-03-10 08:02:32 +11:00
|
|
|
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, (int) account.getId(), deleteIntent,
|
2018-02-04 08:45:14 +11:00
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
2017-04-28 13:29:42 +10:00
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, getChannelId(account, body))
|
2017-04-28 13:29:42 +10:00
|
|
|
.setSmallIcon(R.drawable.ic_notify)
|
|
|
|
.setContentIntent(resultPendingIntent)
|
|
|
|
.setDeleteIntent(deletePendingIntent)
|
2017-10-19 07:18:07 +11:00
|
|
|
.setColor(ContextCompat.getColor(context, (R.color.primary)))
|
2017-04-28 13:29:42 +10:00
|
|
|
.setDefaults(0); // So it doesn't ring twice, notify only in Target callback
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
setupPreferences(account, builder);
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2017-04-28 13:29:42 +10:00
|
|
|
if (currentNotifications.length() == 1) {
|
|
|
|
builder.setContentTitle(titleForType(context, body))
|
2018-02-04 08:45:14 +11:00
|
|
|
.setContentText(bodyForType(body));
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
if (body.getType() == Notification.Type.MENTION) {
|
2018-02-04 08:45:14 +11:00
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(bodyForType(body)));
|
|
|
|
}
|
2017-04-28 13:29:42 +10:00
|
|
|
|
2017-11-03 20:09:09 +11:00
|
|
|
//load the avatar synchronously
|
|
|
|
Bitmap accountAvatar;
|
|
|
|
try {
|
|
|
|
accountAvatar = Picasso.with(context)
|
2018-03-03 23:24:03 +11:00
|
|
|
.load(body.getAccount().getAvatar())
|
2018-03-30 19:36:19 +11:00
|
|
|
.transform(new RoundedTransformation(20))
|
2017-11-03 20:09:09 +11:00
|
|
|
.get();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.d(TAG, "error loading account avatar", e);
|
|
|
|
accountAvatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.avatar_default);
|
|
|
|
}
|
2017-04-28 13:29:42 +10:00
|
|
|
|
2017-11-03 20:09:09 +11:00
|
|
|
builder.setLargeIcon(accountAvatar);
|
2017-04-28 13:29:42 +10:00
|
|
|
|
|
|
|
} else {
|
|
|
|
try {
|
2018-03-02 05:05:47 +11:00
|
|
|
String title = context.getString(R.string.notification_title_summary, currentNotifications.length());
|
2018-02-04 08:45:14 +11:00
|
|
|
String text = joinNames(context, currentNotifications);
|
2017-07-02 10:32:35 +10:00
|
|
|
builder.setContentTitle(title)
|
|
|
|
.setContentText(text);
|
2017-04-28 13:29:42 +10:00
|
|
|
} catch (JSONException e) {
|
2017-05-24 05:34:31 +10:00
|
|
|
Log.d(TAG, Log.getStackTraceString(e));
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
builder.setSubText(account.getFullName());
|
|
|
|
|
|
|
|
builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
|
2017-04-28 13:29:42 +10:00
|
|
|
|
2018-03-02 05:05:47 +11:00
|
|
|
builder.setOnlyAlertOnce(true);
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2017-11-14 05:05:23 +11:00
|
|
|
//noinspection ConstantConditions
|
2018-03-10 08:02:32 +11:00
|
|
|
notificationManager.notify((int) account.getId(), builder.build());
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
public static void createNotificationChannelsForAccount(AccountEntity account, Context context) {
|
2017-11-14 05:05:23 +11:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
String[] channelIds = new String[]{
|
2018-03-10 08:02:32 +11:00
|
|
|
CHANNEL_MENTION + account.getIdentifier(),
|
|
|
|
CHANNEL_FOLLOW + account.getIdentifier(),
|
|
|
|
CHANNEL_BOOST + account.getIdentifier(),
|
|
|
|
CHANNEL_FAVOURITE + account.getIdentifier()};
|
2017-10-19 07:18:07 +11:00
|
|
|
int[] channelNames = {
|
|
|
|
R.string.notification_channel_mention_name,
|
|
|
|
R.string.notification_channel_follow_name,
|
|
|
|
R.string.notification_channel_boost_name,
|
|
|
|
R.string.notification_channel_favourite_name
|
|
|
|
};
|
|
|
|
int[] channelDescriptions = {
|
|
|
|
R.string.notification_channel_mention_descriptions,
|
|
|
|
R.string.notification_channel_follow_description,
|
|
|
|
R.string.notification_channel_boost_description,
|
|
|
|
R.string.notification_channel_favourite_description
|
|
|
|
};
|
|
|
|
|
|
|
|
List<NotificationChannel> channels = new ArrayList<>(4);
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(), account.getFullName());
|
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
2018-02-13 08:03:08 +11:00
|
|
|
notificationManager.createNotificationChannelGroup(channelGroup);
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2017-11-14 05:05:23 +11:00
|
|
|
for (int i = 0; i < channelIds.length; i++) {
|
2017-10-19 07:18:07 +11:00
|
|
|
String id = channelIds[i];
|
|
|
|
String name = context.getString(channelNames[i]);
|
|
|
|
String description = context.getString(channelDescriptions[i]);
|
2018-02-13 08:03:08 +11:00
|
|
|
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
2017-10-19 07:18:07 +11:00
|
|
|
NotificationChannel channel = new NotificationChannel(id, name, importance);
|
|
|
|
|
|
|
|
channel.setDescription(description);
|
|
|
|
channel.enableLights(true);
|
2018-02-13 08:03:08 +11:00
|
|
|
channel.setLightColor(0xFF2B90D9);
|
2017-10-19 07:18:07 +11:00
|
|
|
channel.enableVibration(true);
|
|
|
|
channel.setShowBadge(true);
|
2018-02-04 08:45:14 +11:00
|
|
|
channel.setGroup(account.getIdentifier());
|
2017-10-19 07:18:07 +11:00
|
|
|
channels.add(channel);
|
|
|
|
}
|
|
|
|
|
2017-11-14 05:05:23 +11:00
|
|
|
//noinspection ConstantConditions
|
2018-02-13 08:03:08 +11:00
|
|
|
notificationManager.createNotificationChannels(channels);
|
2017-10-19 07:18:07 +11:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
public static void deleteNotificationChannelsForAccount(AccountEntity account, Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
2018-02-13 08:03:08 +11:00
|
|
|
notificationManager.deleteNotificationChannelGroup(account.getIdentifier());
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void deleteLegacyNotificationChannels(Context context) {
|
|
|
|
// delete the notification channels that where used before the multi account mode was introduced to avoid confusion
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
2018-02-13 08:03:08 +11:00
|
|
|
notificationManager.deleteNotificationChannel(CHANNEL_MENTION);
|
|
|
|
notificationManager.deleteNotificationChannel(CHANNEL_FAVOURITE);
|
|
|
|
notificationManager.deleteNotificationChannel(CHANNEL_BOOST);
|
|
|
|
notificationManager.deleteNotificationChannel(CHANNEL_FOLLOW);
|
2018-02-04 08:45:14 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
public static boolean areNotificationsEnabled(Context context) {
|
2018-03-10 08:02:32 +11:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2018-02-13 08:03:08 +11:00
|
|
|
|
|
|
|
// on Android >= O, notifications are enabled, if at least one channel is enabled
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
2018-03-10 08:02:32 +11:00
|
|
|
if (notificationManager.areNotificationsEnabled()) {
|
|
|
|
for (NotificationChannel channel : notificationManager.getNotificationChannels()) {
|
|
|
|
if (channel.getImportance() > NotificationManager.IMPORTANCE_NONE) {
|
2018-02-13 08:03:08 +11: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-03-10 08:02:32 +11:00
|
|
|
return TuskyApplication.getInstance(context).getServiceLocator()
|
|
|
|
.get(AccountManager.class).areNotificationsEnabled();
|
2018-02-13 08:03:08 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
public static void clearNotificationsForActiveAccount(Context context) {
|
2018-03-10 08:02:32 +11:00
|
|
|
AccountManager accountManager = TuskyApplication.getInstance(context).getServiceLocator()
|
|
|
|
.get(AccountManager.class);
|
2018-02-04 08:45:14 +11:00
|
|
|
AccountEntity account = accountManager.getActiveAccount();
|
|
|
|
if (account != null) {
|
|
|
|
account.setActiveNotifications("[]");
|
|
|
|
accountManager.saveAccount(account);
|
2017-11-16 07:18:35 +11:00
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2017-11-16 07:18:35 +11:00
|
|
|
//noinspection ConstantConditions
|
2018-03-10 08:02:32 +11:00
|
|
|
notificationManager.cancel((int) account.getId());
|
2017-11-16 07:18:35 +11:00
|
|
|
}
|
2017-11-14 05:05:23 +11:00
|
|
|
}
|
|
|
|
|
2018-02-13 08:03:08 +11:00
|
|
|
private static boolean filterNotification(AccountEntity account, Notification notification,
|
|
|
|
Context context) {
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2017-11-14 05:05:23 +11:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2018-02-13 08:03:08 +11:00
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
NotificationChannel channel = notificationManager.getNotificationChannel(getChannelId(account, notification));
|
|
|
|
return channel.getImportance() > NotificationManager.IMPORTANCE_NONE;
|
2017-10-19 07:18:07 +11:00
|
|
|
}
|
|
|
|
|
2018-03-03 23:24:03 +11:00
|
|
|
switch (notification.getType()) {
|
2017-04-28 13:29:42 +10:00
|
|
|
default:
|
2017-07-02 10:32:35 +10:00
|
|
|
case MENTION:
|
2018-02-04 08:45:14 +11:00
|
|
|
return account.getNotificationsMentioned();
|
2017-07-02 10:32:35 +10:00
|
|
|
case FOLLOW:
|
2018-02-04 08:45:14 +11:00
|
|
|
return account.getNotificationsFollowed();
|
2017-07-02 10:32:35 +10:00
|
|
|
case REBLOG:
|
2018-02-04 08:45:14 +11:00
|
|
|
return account.getNotificationsReblogged();
|
2017-07-02 10:32:35 +10:00
|
|
|
case FAVOURITE:
|
2018-02-04 08:45:14 +11:00
|
|
|
return account.getNotificationsFavorited();
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
private static String getChannelId(AccountEntity account, Notification notification) {
|
2018-03-03 23:24:03 +11:00
|
|
|
switch (notification.getType()) {
|
2017-11-14 05:05:23 +11:00
|
|
|
default:
|
|
|
|
case MENTION:
|
2018-03-10 08:02:32 +11:00
|
|
|
return CHANNEL_MENTION + account.getIdentifier();
|
2017-11-14 05:05:23 +11:00
|
|
|
case FOLLOW:
|
2018-03-10 08:02:32 +11:00
|
|
|
return CHANNEL_FOLLOW + account.getIdentifier();
|
2017-11-14 05:05:23 +11:00
|
|
|
case REBLOG:
|
2018-03-10 08:02:32 +11:00
|
|
|
return CHANNEL_BOOST + account.getIdentifier();
|
2017-11-14 05:05:23 +11:00
|
|
|
case FAVOURITE:
|
2018-03-10 08:02:32 +11:00
|
|
|
return CHANNEL_FAVOURITE + account.getIdentifier();
|
2017-11-14 05:05:23 +11:00
|
|
|
}
|
2017-10-19 07:18:07 +11:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
private static void setupPreferences(AccountEntity account,
|
2017-11-14 05:05:23 +11:00
|
|
|
NotificationCompat.Builder builder) {
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2017-11-14 05:05:23 +11:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2017-10-19 07:18:07 +11:00
|
|
|
return; //do nothing on Android O or newer, the system uses the channel settings anyway
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
if (account.getNotificationSound()) {
|
2017-04-28 13:29:42 +10:00
|
|
|
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
|
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
if (account.getNotificationVibration()) {
|
2017-11-14 05:05:23 +11:00
|
|
|
builder.setVibrate(new long[]{500, 500});
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
if (account.getNotificationLight()) {
|
2018-02-13 08:03:08 +11:00
|
|
|
builder.setLights(0xFF2B90D9, 300, 1000);
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static String joinNames(Context context, JSONArray array) throws JSONException {
|
|
|
|
if (array.length() > 3) {
|
2018-03-02 05:05:47 +11:00
|
|
|
int length = array.length();
|
2017-07-02 10:32:35 +10:00
|
|
|
return String.format(context.getString(R.string.notification_summary_large),
|
2018-03-10 08:02:32 +11:00
|
|
|
array.get(length - 1), array.get(length - 2), array.get(length - 3), length - 3);
|
2017-04-28 13:29:42 +10:00
|
|
|
} else if (array.length() == 3) {
|
2017-07-02 10:32:35 +10:00
|
|
|
return String.format(context.getString(R.string.notification_summary_medium),
|
2018-03-02 05:05:47 +11:00
|
|
|
array.get(2), array.get(1), array.get(0));
|
2017-04-28 13:29:42 +10:00
|
|
|
} else if (array.length() == 2) {
|
2017-07-02 10:32:35 +10:00
|
|
|
return String.format(context.getString(R.string.notification_summary_small),
|
2018-03-02 05:05:47 +11:00
|
|
|
array.get(1), array.get(0));
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static String titleForType(Context context, Notification notification) {
|
2018-03-03 23:24:03 +11:00
|
|
|
switch (notification.getType()) {
|
2017-04-28 13:29:42 +10:00
|
|
|
case MENTION:
|
2017-07-02 10:32:35 +10:00
|
|
|
return String.format(context.getString(R.string.notification_mention_format),
|
2018-03-03 23:24:03 +11:00
|
|
|
notification.getAccount().getName());
|
2017-04-28 13:29:42 +10:00
|
|
|
case FOLLOW:
|
2017-07-02 10:32:35 +10:00
|
|
|
return String.format(context.getString(R.string.notification_follow_format),
|
2018-03-03 23:24:03 +11:00
|
|
|
notification.getAccount().getName());
|
2017-04-28 13:29:42 +10:00
|
|
|
case FAVOURITE:
|
2017-07-02 10:32:35 +10:00
|
|
|
return String.format(context.getString(R.string.notification_favourite_format),
|
2018-03-03 23:24:03 +11:00
|
|
|
notification.getAccount().getName());
|
2017-04-28 13:29:42 +10:00
|
|
|
case REBLOG:
|
2017-07-02 10:32:35 +10:00
|
|
|
return String.format(context.getString(R.string.notification_reblog_format),
|
2018-03-03 23:24:03 +11:00
|
|
|
notification.getAccount().getName());
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static String bodyForType(Notification notification) {
|
2018-03-03 23:24:03 +11:00
|
|
|
switch (notification.getType()) {
|
2017-04-28 13:29:42 +10:00
|
|
|
case FOLLOW:
|
2018-03-10 08:02:32 +11:00
|
|
|
return "@" + notification.getAccount().getUsername();
|
2017-04-28 13:29:42 +10:00
|
|
|
case MENTION:
|
|
|
|
case FAVOURITE:
|
|
|
|
case REBLOG:
|
2018-03-03 23:24:03 +11:00
|
|
|
return notification.getStatus().getContent().toString();
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-19 07:18:07 +11:00
|
|
|
|
2017-04-28 13:29:42 +10:00
|
|
|
}
|