Poll notifications (#1229)

* show poll notifications in the app

* show poll notifications in the app

* allow filtering poll notifications in the poll fragment

* show poll notifications in system notifications
This commit is contained in:
Konrad Pozniak 2019-05-02 19:44:35 +02:00 committed by GitHub
commit e735e4843e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 1076 additions and 318 deletions

View file

@ -35,6 +35,8 @@ import androidx.core.app.RemoteInput;
import androidx.core.app.TaskStackBuilder;
import androidx.core.content.ContextCompat;
import androidx.core.text.BidiFormatter;
import android.text.TextUtils;
import android.util.Log;
import com.bumptech.glide.Glide;
@ -48,6 +50,8 @@ import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.db.AccountEntity;
import com.keylesspalace.tusky.db.AccountManager;
import com.keylesspalace.tusky.entity.Notification;
import com.keylesspalace.tusky.entity.Poll;
import com.keylesspalace.tusky.entity.PollOption;
import com.keylesspalace.tusky.entity.Status;
import com.keylesspalace.tusky.receiver.NotificationClearBroadcastReceiver;
import com.keylesspalace.tusky.receiver.SendStatusBroadcastReceiver;
@ -105,6 +109,7 @@ public class NotificationHelper {
public static final String CHANNEL_FOLLOW = "CHANNEL_FOLLOW";
public static final String CHANNEL_BOOST = "CHANNEL_BOOST";
public static final String CHANNEL_FAVOURITE = "CHANNEL_FAVOURITE";
public static final String CHANNEL_POLL = "CHANNEL_POLL";
/**
* time in minutes between notification checks
@ -159,12 +164,12 @@ public class NotificationHelper {
notificationId++;
builder.setContentTitle(titleForType(context, body, bidiFormatter))
.setContentText(bodyForType(body));
builder.setContentTitle(titleForType(context, body, bidiFormatter, account))
.setContentText(bodyForType(body, context));
if (body.getType() == Notification.Type.MENTION) {
if (body.getType() == Notification.Type.MENTION || body.getType() == Notification.Type.POLL) {
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(bodyForType(body)));
.bigText(bodyForType(body, context)));
}
//load the avatar synchronously
@ -337,21 +342,25 @@ public class NotificationHelper {
CHANNEL_MENTION + account.getIdentifier(),
CHANNEL_FOLLOW + account.getIdentifier(),
CHANNEL_BOOST + account.getIdentifier(),
CHANNEL_FAVOURITE + account.getIdentifier()};
CHANNEL_FAVOURITE + account.getIdentifier(),
CHANNEL_POLL + account.getIdentifier(),
};
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
R.string.notification_mention_name,
R.string.notification_follow_name,
R.string.notification_boost_name,
R.string.notification_favourite_name,
R.string.notification_poll_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
R.string.notification_mention_descriptions,
R.string.notification_follow_description,
R.string.notification_boost_description,
R.string.notification_favourite_description,
R.string.notification_poll_description
};
List<NotificationChannel> channels = new ArrayList<>(4);
List<NotificationChannel> channels = new ArrayList<>(5);
NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(), account.getFullName());
@ -472,8 +481,13 @@ public class NotificationHelper {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = getChannelId(account, notification);
if(channelId == null) {
// unknown notificationtype
return false;
}
//noinspection ConstantConditions
NotificationChannel channel = notificationManager.getNotificationChannel(getChannelId(account, notification));
NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
return channel.getImportance() > NotificationManager.IMPORTANCE_NONE;
}
@ -486,14 +500,15 @@ public class NotificationHelper {
return account.getNotificationsReblogged();
case FAVOURITE:
return account.getNotificationsFavorited();
case POLL:
return account.getNotificationsPolls();
default:
return false;
}
}
private static String getChannelId(AccountEntity account, Notification notification) {
private static @Nullable String getChannelId(AccountEntity account, Notification notification) {
switch (notification.getType()) {
default:
case MENTION:
return CHANNEL_MENTION + account.getIdentifier();
case FOLLOW:
@ -502,6 +517,10 @@ public class NotificationHelper {
return CHANNEL_BOOST + account.getIdentifier();
case FAVOURITE:
return CHANNEL_FAVOURITE + account.getIdentifier();
case POLL:
return CHANNEL_POLL + account.getIdentifier();
default:
return null;
}
}
@ -554,7 +573,7 @@ public class NotificationHelper {
}
@Nullable
private static String titleForType(Context context, Notification notification, BidiFormatter bidiFormatter) {
private static String titleForType(Context context, Notification notification, BidiFormatter bidiFormatter, AccountEntity account) {
String accountName = bidiFormatter.unicodeWrap(notification.getAccount().getName());
switch (notification.getType()) {
case MENTION:
@ -569,22 +588,43 @@ public class NotificationHelper {
case REBLOG:
return String.format(context.getString(R.string.notification_reblog_format),
accountName);
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);
}
}
return null;
}
private static String bodyForType(Notification notification) {
private static String bodyForType(Notification notification, Context context) {
switch (notification.getType()) {
case FOLLOW:
return "@" + notification.getAccount().getUsername();
case MENTION:
case FAVOURITE:
case REBLOG:
if (notification.getStatus().getSensitive()) {
if (!TextUtils.isEmpty(notification.getStatus().getSpoilerText())) {
return notification.getStatus().getSpoilerText();
} else {
return notification.getStatus().getContent().toString();
}
case POLL:
if (!TextUtils.isEmpty(notification.getStatus().getSpoilerText())) {
return notification.getStatus().getSpoilerText();
} else {
StringBuilder builder = new StringBuilder(notification.getStatus().getContent());
builder.append('\n');
Poll poll = notification.getStatus().getPoll();
for(PollOption option: poll.getOptions()) {
int percent = option.getPercent(poll.getVotesCount());
CharSequence optionText = HtmlUtils.fromHtml(context.getString(R.string.poll_option_format, percent, option.getTitle()));
builder.append(optionText);
builder.append('\n');
}
return builder.toString();
}
}
return null;
}