Disable Contact Join Notification via Action.

master
Alex Hart 2020-08-06 15:47:34 -03:00 committed by Greyson Parrelli
parent 25c17082f2
commit 36d1e7c44a
9 changed files with 154 additions and 37 deletions

View File

@ -455,6 +455,9 @@
</intent-filter>
</activity>
<activity android:name=".contacts.TurnOffContactJoinedNotificationsActivity"
android:theme="@style/Theme.AppCompat.Dialog.Alert" />
<activity android:name=".messagerequests.MessageRequestMegaphoneActivity"
android:theme="@style/TextSecure.LightRegistrationTheme"
android:windowSoftInputMode="adjustResize"

View File

@ -0,0 +1,69 @@
package org.thoughtcrime.securesms.contacts;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MessagingDatabase;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.notifications.MarkReadReceiver;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.concurrent.SimpleTask;
import java.util.List;
/**
* Activity which displays a dialog to confirm whether to turn off "Contact Joined Signal" notifications.
*/
public class TurnOffContactJoinedNotificationsActivity extends AppCompatActivity {
private final static String EXTRA_THREAD_ID = "thread_id";
public static Intent newIntent(@NonNull Context context, long threadId) {
Intent intent = new Intent(context, TurnOffContactJoinedNotificationsActivity.class);
intent.putExtra(EXTRA_THREAD_ID, threadId);
return intent;
}
@Override
protected void onResume() {
super.onResume();
new AlertDialog.Builder(this)
.setMessage(R.string.TurnOffContactJoinedNotificationsActivity__turn_off_contact_joined_signal)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
handlePositiveAction(dialog);
})
.setNegativeButton(android.R.string.cancel, ((dialog, which) -> {
dialog.dismiss();
finish();
}))
.show();
}
private void handlePositiveAction(@NonNull DialogInterface dialog) {
SimpleTask.run(getLifecycle(), () -> {
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(this);
List<MessagingDatabase.MarkedMessageInfo> marked = threadDatabase.setRead(getIntent().getLongExtra(EXTRA_THREAD_ID, -1), false);
MarkReadReceiver.process(this, marked);
TextSecurePreferences.setNewContactsNotificationEnabled(this, false);
ApplicationDependencies.getMessageNotifier().updateNotification(this);
return null;
}, unused -> {
dialog.dismiss();
finish();
});
}
}

View File

@ -37,9 +37,9 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.permissions.Permissions;
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.registration.RegistrationUtil;
import org.thoughtcrime.securesms.storage.StorageSyncHelper;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.sms.IncomingJoinedMessage;
import org.thoughtcrime.securesms.util.FeatureFlags;

View File

@ -364,18 +364,25 @@ public class DefaultMessageNotifier implements MessageNotifier {
long timestamp = notifications.get(0).getTimestamp();
if (timestamp != 0) builder.setWhen(timestamp);
boolean isSingleNotificationContactJoined = notifications.size() == 1 && notifications.get(0).isJoin();
if (!KeyCachingService.isLocked(context) && RecipientUtil.isMessageRequestAccepted(context, recipient.resolve())) {
ReplyMethod replyMethod = ReplyMethod.forRecipient(context, recipient);
builder.addActions(notificationState.getMarkAsReadIntent(context, notificationId),
notificationState.getQuickReplyIntent(context, notifications.get(0).getRecipient()),
notificationState.getRemoteReplyIntent(context, notifications.get(0).getRecipient(), replyMethod),
replyMethod);
replyMethod,
!isSingleNotificationContactJoined);
builder.addAndroidAutoAction(notificationState.getAndroidAutoReplyIntent(context, notifications.get(0).getRecipient()),
notificationState.getAndroidAutoHeardIntent(context, notificationId), notifications.get(0).getTimestamp());
}
if (!KeyCachingService.isLocked(context) && isSingleNotificationContactJoined) {
builder.addTurnOffTheseNotificationsAction(notificationState.getTurnOffTheseNotificationsIntent(context));
}
ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size());
while(iterator.hasPrevious()) {
@ -538,7 +545,7 @@ public class DefaultMessageNotifier implements MessageNotifier {
}
if (threadRecipients == null || includeMessage) {
notificationState.addNotification(new NotificationItem(id, mms, recipient, conversationRecipient, threadRecipients, threadId, body, timestamp, receivedTimestamp, slideDeck, false));
notificationState.addNotification(new NotificationItem(id, mms, recipient, conversationRecipient, threadRecipients, threadId, body, timestamp, receivedTimestamp, slideDeck, false, record.isJoined()));
}
}
@ -572,7 +579,7 @@ public class DefaultMessageNotifier implements MessageNotifier {
}
if (threadRecipients == null || !threadRecipients.isMuted()) {
notificationState.addNotification(new NotificationItem(id, mms, reactionSender, conversationRecipient, threadRecipients, threadId, body, reaction.getDateReceived(), receivedTimestamp, null, true));
notificationState.addNotification(new NotificationItem(id, mms, reactionSender, conversationRecipient, threadRecipients, threadId, body, reaction.getDateReceived(), receivedTimestamp, null, true, record.isJoined()));
}
}
}

View File

@ -27,6 +27,7 @@ public class NotificationItem {
private final long messageReceivedTimestamp;
@Nullable private final SlideDeck slideDeck;
private final boolean jumpToMessage;
private final boolean isJoin;
public NotificationItem(long id,
boolean mms,
@ -38,7 +39,8 @@ public class NotificationItem {
long notificationTimestamp,
long messageReceivedTimestamp,
@Nullable SlideDeck slideDeck,
boolean jumpToMessage)
boolean jumpToMessage,
boolean isJoin)
{
this.id = id;
this.mms = mms;
@ -51,6 +53,7 @@ public class NotificationItem {
this.messageReceivedTimestamp = messageReceivedTimestamp;
this.slideDeck = slideDeck;
this.jumpToMessage = jumpToMessage;
this.isJoin = isJoin;
}
public @NonNull Recipient getRecipient() {
@ -98,6 +101,10 @@ public class NotificationItem {
return mms;
}
public boolean isJoin() {
return isJoin;
}
private static int getStartingPosition(@NonNull Context context, long threadId, long receivedTimestampMs) {
return DatabaseFactory.getMmsSmsDatabase(context).getMessagePositionInConversation(threadId, receivedTimestampMs);
}

View File

@ -8,6 +8,7 @@ import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.thoughtcrime.securesms.contacts.TurnOffContactJoinedNotificationsActivity;
import org.thoughtcrime.securesms.conversation.ConversationActivity;
import org.thoughtcrime.securesms.conversation.ConversationPopupActivity;
import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
@ -101,6 +102,15 @@ public class NotificationState {
return list;
}
public PendingIntent getTurnOffTheseNotificationsIntent(Context context) {
long threadId = threads.iterator().next();
return PendingIntent.getActivity(context,
0,
TurnOffContactJoinedNotificationsActivity.newIntent(context, threadId),
PendingIntent.FLAG_UPDATE_CURRENT);
}
public PendingIntent getMarkAsReadIntent(Context context, int notificationId) {
long[] threadArray = new long[threads.size()];
int index = 0;

View File

@ -7,6 +7,8 @@ import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.text.SpannableStringBuilder;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
@ -17,8 +19,6 @@ import androidx.core.app.Person;
import androidx.core.app.RemoteInput;
import androidx.core.graphics.drawable.IconCompat;
import android.text.SpannableStringBuilder;
import com.annimon.stream.Stream;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
@ -156,43 +156,57 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil
extend(new NotificationCompat.CarExtender().setUnreadConversation(unreadConversationBuilder.build()));
}
public void addTurnOffTheseNotificationsAction(@NonNull PendingIntent turnOffTheseNotificationsIntent) {
Action turnOffTheseNotifications = new Action(R.drawable.check,
context.getString(R.string.MessageNotifier_turn_off_these_notifications),
turnOffTheseNotificationsIntent);
addAction(turnOffTheseNotifications);
}
public void addActions(@NonNull PendingIntent markReadIntent,
@NonNull PendingIntent quickReplyIntent,
@NonNull PendingIntent wearableReplyIntent,
@NonNull ReplyMethod replyMethod)
@NonNull ReplyMethod replyMethod,
boolean replyEnabled)
{
Action markAsReadAction = new Action(R.drawable.check,
context.getString(R.string.MessageNotifier_mark_read),
markReadIntent);
String actionName = context.getString(R.string.MessageNotifier_reply);
String label = context.getString(replyMethodLongDescription(replyMethod));
Action replyAction = new Action(R.drawable.ic_reply_white_36dp,
actionName,
quickReplyIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
replyAction = new Action.Builder(R.drawable.ic_reply_white_36dp,
actionName,
wearableReplyIntent)
.addRemoteInput(new RemoteInput.Builder(DefaultMessageNotifier.EXTRA_REMOTE_REPLY)
.setLabel(label).build())
.build();
}
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
actionName,
wearableReplyIntent)
.addRemoteInput(new RemoteInput.Builder(DefaultMessageNotifier.EXTRA_REMOTE_REPLY)
.setLabel(label).build())
.build();
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
Action markAsReadAction = new Action(R.drawable.check,
context.getString(R.string.MessageNotifier_mark_read),
markReadIntent);
addAction(markAsReadAction);
addAction(replyAction);
extender.addAction(markAsReadAction);
extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction)
.addAction(wearableReplyAction));
if (replyEnabled) {
String actionName = context.getString(R.string.MessageNotifier_reply);
String label = context.getString(replyMethodLongDescription(replyMethod));
Action replyAction = new Action(R.drawable.ic_reply_white_36dp,
actionName,
quickReplyIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
replyAction = new Action.Builder(R.drawable.ic_reply_white_36dp,
actionName,
wearableReplyIntent)
.addRemoteInput(new RemoteInput.Builder(DefaultMessageNotifier.EXTRA_REMOTE_REPLY)
.setLabel(label)
.build())
.build();
}
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
actionName,
wearableReplyIntent)
.addRemoteInput(new RemoteInput.Builder(DefaultMessageNotifier.EXTRA_REMOTE_REPLY)
.setLabel(label)
.build())
.build();
addAction(replyAction);
extend(extender.addAction(wearableReplyAction));
}
}
@StringRes

View File

@ -492,6 +492,10 @@ public class TextSecurePreferences {
return new NotificationPrivacyPreference(getStringPreference(context, NOTIFICATION_PRIVACY_PREF, "all"));
}
public static void setNewContactsNotificationEnabled(Context context, boolean isEnabled) {
setBooleanPreference(context, NEW_CONTACTS_NOTIFICATIONS, isEnabled);
}
public static boolean isNewContactsNotificationEnabled(Context context) {
return getBooleanPreference(context, NEW_CONTACTS_NOTIFICATIONS, true);
}

View File

@ -1340,6 +1340,7 @@
<string name="MessageNotifier_error_delivering_message">Error delivering message.</string>
<string name="MessageNotifier_mark_all_as_read">Mark all as read</string>
<string name="MessageNotifier_mark_read">Mark read</string>
<string name="MessageNotifier_turn_off_these_notifications">Turn off these notifications</string>
<string name="MessageNotifier_media_message">Media message</string>
<string name="MessageNotifier_sticker">Sticker</string>
<string name="MessageNotifier_view_once_photo">View-once photo</string>
@ -1361,6 +1362,8 @@
<string name="MessageNotifier_reacted_s_to_your_sticker">Reacted %1$s to your sticker.</string>
<string name="MessageNotifier_this_message_was_deleted">This message was deleted.</string>
<string name="TurnOffContactJoinedNotificationsActivity__turn_off_contact_joined_signal">Turn off contact joined Signal notifications? You can enable them again in Signal > Settings > Notifications.</string>
<!-- Notification Channels -->
<string name="NotificationChannel_messages">Default</string>
<string name="NotificationChannel_calls">Calls</string>