Perfom a migration to notify users of new contacts.
This commit is contained in:
parent
f012a41345
commit
141cab1105
6 changed files with 165 additions and 8 deletions
|
@ -1196,6 +1196,21 @@ public class ThreadDatabase extends Database {
|
|||
return Objects.requireNonNull(getThreadRecord(getThreadIdFor(recipient)));
|
||||
}
|
||||
|
||||
public @NonNull Set<RecipientId> getAllThreadRecipients() {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
Set<RecipientId> ids = new HashSet<>();
|
||||
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, new String[] { RECIPIENT_ID }, null, null, null, null, null)) {
|
||||
while (cursor.moveToNext()) {
|
||||
ids.add(RecipientId.from(CursorUtil.requireString(cursor, RECIPIENT_ID)));
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
|
||||
@NonNull MergeResult merge(@NonNull RecipientId primaryRecipientId, @NonNull RecipientId secondaryRecipientId) {
|
||||
if (!databaseHelper.getWritableDatabase().inTransaction()) {
|
||||
throw new IllegalStateException("Must be in a transaction!");
|
||||
|
|
|
@ -46,6 +46,7 @@ import org.thoughtcrime.securesms.migrations.StickerLaunchMigrationJob;
|
|||
import org.thoughtcrime.securesms.migrations.StorageCapabilityMigrationJob;
|
||||
import org.thoughtcrime.securesms.migrations.StorageServiceMigrationJob;
|
||||
import org.thoughtcrime.securesms.migrations.TrimByLengthSettingsMigrationJob;
|
||||
import org.thoughtcrime.securesms.migrations.UserNotificationMigrationJob;
|
||||
import org.thoughtcrime.securesms.migrations.UuidMigrationJob;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -160,6 +161,7 @@ public final class JobManagerFactories {
|
|||
put(StorageCapabilityMigrationJob.KEY, new StorageCapabilityMigrationJob.Factory());
|
||||
put(StorageServiceMigrationJob.KEY, new StorageServiceMigrationJob.Factory());
|
||||
put(TrimByLengthSettingsMigrationJob.KEY, new TrimByLengthSettingsMigrationJob.Factory());
|
||||
put(UserNotificationMigrationJob.KEY, new UserNotificationMigrationJob.Factory());
|
||||
put(UuidMigrationJob.KEY, new UuidMigrationJob.Factory());
|
||||
|
||||
// Dead jobs
|
||||
|
|
|
@ -40,7 +40,7 @@ public class ApplicationMigrations {
|
|||
|
||||
private static final int LEGACY_CANONICAL_VERSION = 455;
|
||||
|
||||
public static final int CURRENT_VERSION = 24;
|
||||
public static final int CURRENT_VERSION = 25;
|
||||
|
||||
private static final class Version {
|
||||
static final int LEGACY = 1;
|
||||
|
@ -67,6 +67,7 @@ public class ApplicationMigrations {
|
|||
static final int CDS = 22;
|
||||
static final int BACKUP_NOTIFICATION = 23;
|
||||
static final int GV1_MIGRATION = 24;
|
||||
static final int USER_NOTIFICATION = 25;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -281,6 +282,10 @@ public class ApplicationMigrations {
|
|||
jobs.put(Version.GV1_MIGRATION, new AttributesMigrationJob());
|
||||
}
|
||||
|
||||
if (lastSeenVersion < Version.USER_NOTIFICATION) {
|
||||
jobs.put(Version.USER_NOTIFICATION, new UserNotificationMigrationJob());
|
||||
}
|
||||
|
||||
return jobs;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
package org.thoughtcrime.securesms.migrations;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
import androidx.core.app.TaskStackBuilder;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.MainActivity;
|
||||
import org.thoughtcrime.securesms.NewConversationActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
||||
import org.thoughtcrime.securesms.jobmanager.Data;
|
||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
||||
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
||||
import org.thoughtcrime.securesms.notifications.NotificationIds;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.thoughtcrime.securesms.util.SetUtil;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Show a user that contacts are newly available. Only for users that recently installed.
|
||||
*/
|
||||
public class UserNotificationMigrationJob extends MigrationJob {
|
||||
|
||||
private static final String TAG = Log.tag(UserNotificationMigrationJob.class);
|
||||
|
||||
public static final String KEY = "UserNotificationMigration";
|
||||
|
||||
UserNotificationMigrationJob() {
|
||||
this(new Parameters.Builder().build());
|
||||
}
|
||||
|
||||
private UserNotificationMigrationJob(Parameters parameters) {
|
||||
super(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getFactoryKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isUiBlocking() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
void performMigration() {
|
||||
if (!TextSecurePreferences.isPushRegistered(context) ||
|
||||
TextSecurePreferences.getLocalNumber(context) == null ||
|
||||
TextSecurePreferences.getLocalUuid(context) == null)
|
||||
{
|
||||
Log.w(TAG, "Not registered! Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TextSecurePreferences.isNewContactsNotificationEnabled(context)) {
|
||||
Log.w(TAG, "New contact notifications disabled! Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (TextSecurePreferences.getFirstInstallVersion(context) < 759) {
|
||||
Log.w(TAG, "Install is older than v5.0.8. Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
|
||||
|
||||
int threadCount = threadDatabase.getUnarchivedConversationListCount() +
|
||||
threadDatabase.getArchivedConversationListCount();
|
||||
|
||||
if (threadCount >= 3) {
|
||||
Log.w(TAG, "Already have 3 or more threads. Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
List<RecipientId> registered = DatabaseFactory.getRecipientDatabase(context).getRegistered();
|
||||
List<RecipientId> systemContacts = DatabaseFactory.getRecipientDatabase(context).getSystemContacts();
|
||||
Set<RecipientId> registeredSystemContacts = SetUtil.intersection(registered, systemContacts);
|
||||
Set<RecipientId> threadRecipients = threadDatabase.getAllThreadRecipients();
|
||||
|
||||
if (threadRecipients.containsAll(registeredSystemContacts)) {
|
||||
Log.w(TAG, "Threads already exist for all relevant contacts. Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
String message = context.getResources().getQuantityString(R.plurals.UserNotificationMigrationJob_d_contacts_are_on_signal,
|
||||
registeredSystemContacts.size(),
|
||||
registeredSystemContacts.size());
|
||||
|
||||
Intent mainActivityIntent = new Intent(context, MainActivity.class);
|
||||
Intent newConversationIntent = new Intent(context, NewConversationActivity.class);
|
||||
PendingIntent pendingIntent = TaskStackBuilder.create(context)
|
||||
.addNextIntent(mainActivityIntent)
|
||||
.addNextIntent(newConversationIntent)
|
||||
.getPendingIntent(0, 0);
|
||||
|
||||
Notification notification = new NotificationCompat.Builder(context, NotificationChannels.getMessagesChannel(context))
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentText(message)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build();
|
||||
|
||||
NotificationManagerCompat.from(context)
|
||||
.notify(NotificationIds.USER_NOTIFICATION_MIGRATION, notification);
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean shouldRetry(@NonNull Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static final class Factory implements Job.Factory<UserNotificationMigrationJob> {
|
||||
|
||||
@Override
|
||||
public @NonNull UserNotificationMigrationJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
||||
return new UserNotificationMigrationJob(parameters);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,13 +2,14 @@ package org.thoughtcrime.securesms.notifications;
|
|||
|
||||
public final class NotificationIds {
|
||||
|
||||
public static final int FCM_FAILURE = 12;
|
||||
public static final int PENDING_MESSAGES = 1111;
|
||||
public static final int MESSAGE_SUMMARY = 1338;
|
||||
public static final int APPLICATION_MIGRATION = 4242;
|
||||
public static final int SMS_IMPORT_COMPLETE = 31337;
|
||||
public static final int PRE_REGISTRATION_SMS = 5050;
|
||||
public static final int THREAD = 50000;
|
||||
public static final int FCM_FAILURE = 12;
|
||||
public static final int PENDING_MESSAGES = 1111;
|
||||
public static final int MESSAGE_SUMMARY = 1338;
|
||||
public static final int APPLICATION_MIGRATION = 4242;
|
||||
public static final int SMS_IMPORT_COMPLETE = 31337;
|
||||
public static final int PRE_REGISTRATION_SMS = 5050;
|
||||
public static final int THREAD = 50000;
|
||||
public static final int USER_NOTIFICATION_MIGRATION = 525600;
|
||||
|
||||
private NotificationIds() { }
|
||||
|
||||
|
|
|
@ -1687,6 +1687,11 @@
|
|||
<string name="UsernameEditFragment_usernames_must_be_between_a_and_b_characters">Usernames must be between %1$d and %2$d characters.</string>
|
||||
<string name="UsernameEditFragment_usernames_on_signal_are_optional">Usernames on Signal are optional. If you choose to create a username, other Signal users will be able to find you by this username and contact you without knowing your phone number.</string>
|
||||
|
||||
<plurals name="UserNotificationMigrationJob_d_contacts_are_on_signal">
|
||||
<item quantity="one">%d contact is on Signal!</item>
|
||||
<item quantity="other">%d contacts are on Signal!</item>
|
||||
</plurals>
|
||||
|
||||
<!-- VerifyIdentityActivity -->
|
||||
<string name="VerifyIdentityActivity_your_contact_is_running_an_old_version_of_signal">Your contact is running an old version of Signal. Please ask them to update before verifying your safety number.</string>
|
||||
<string name="VerifyIdentityActivity_your_contact_is_running_a_newer_version_of_Signal">Your contact is running a newer version of Signal with an incompatible QR code format. Please update to compare.</string>
|
||||
|
|
Loading…
Add table
Reference in a new issue