2015-06-09 14:37:20 +00:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2017-08-07 04:43:11 +00:00
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2017-02-18 04:43:24 +00:00
|
|
|
import org.greenrobot.eventbus.EventBus;
|
2015-06-30 16:16:05 +00:00
|
|
|
import org.thoughtcrime.securesms.color.MaterialColor;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2016-08-26 23:53:23 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2015-06-09 14:37:20 +00:00
|
|
|
|
2017-08-07 04:43:11 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public class RecipientPreferenceDatabase extends Database {
|
|
|
|
|
|
|
|
private static final String TAG = RecipientPreferenceDatabase.class.getSimpleName();
|
2015-06-11 20:00:50 +00:00
|
|
|
private static final String RECIPIENT_PREFERENCES_URI = "content://textsecure/recipients/";
|
2015-06-09 14:37:20 +00:00
|
|
|
|
2017-08-07 04:43:11 +00:00
|
|
|
static final String TABLE_NAME = "recipient_preferences";
|
2016-02-06 00:10:33 +00:00
|
|
|
private static final String ID = "_id";
|
2017-08-01 15:56:00 +00:00
|
|
|
private static final String ADDRESS = "recipient_ids";
|
2016-02-06 00:10:33 +00:00
|
|
|
private static final String BLOCK = "block";
|
|
|
|
private static final String NOTIFICATION = "notification";
|
|
|
|
private static final String VIBRATE = "vibrate";
|
|
|
|
private static final String MUTE_UNTIL = "mute_until";
|
|
|
|
private static final String COLOR = "color";
|
|
|
|
private static final String SEEN_INVITE_REMINDER = "seen_invite_reminder";
|
|
|
|
private static final String DEFAULT_SUBSCRIPTION_ID = "default_subscription_id";
|
2016-08-16 03:23:56 +00:00
|
|
|
private static final String EXPIRE_MESSAGES = "expire_messages";
|
2015-06-09 14:37:20 +00:00
|
|
|
|
2017-08-07 04:43:11 +00:00
|
|
|
private static final String[] RECIPIENT_PROJECTION = new String[] {
|
|
|
|
BLOCK, NOTIFICATION, VIBRATE, MUTE_UNTIL, COLOR, SEEN_INVITE_REMINDER, DEFAULT_SUBSCRIPTION_ID, EXPIRE_MESSAGES
|
|
|
|
};
|
|
|
|
|
|
|
|
static final List<String> TYPED_RECIPIENT_PROJECTION = Stream.of(RECIPIENT_PROJECTION)
|
|
|
|
.map(columnName -> TABLE_NAME + "." + columnName)
|
|
|
|
.toList();
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public enum VibrateState {
|
|
|
|
DEFAULT(0), ENABLED(1), DISABLED(2);
|
|
|
|
|
|
|
|
private final int id;
|
|
|
|
|
|
|
|
VibrateState(int id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static VibrateState fromId(int id) {
|
|
|
|
return values()[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final String CREATE_TABLE =
|
|
|
|
"CREATE TABLE " + TABLE_NAME +
|
|
|
|
" (" + ID + " INTEGER PRIMARY KEY, " +
|
2017-08-01 15:56:00 +00:00
|
|
|
ADDRESS + " TEXT UNIQUE, " +
|
2015-06-09 14:37:20 +00:00
|
|
|
BLOCK + " INTEGER DEFAULT 0," +
|
|
|
|
NOTIFICATION + " TEXT DEFAULT NULL, " +
|
|
|
|
VIBRATE + " INTEGER DEFAULT " + VibrateState.DEFAULT.getId() + ", " +
|
2015-06-23 22:10:50 +00:00
|
|
|
MUTE_UNTIL + " INTEGER DEFAULT 0, " +
|
2015-10-14 04:44:01 +00:00
|
|
|
COLOR + " TEXT DEFAULT NULL, " +
|
2016-02-06 00:10:33 +00:00
|
|
|
SEEN_INVITE_REMINDER + " INTEGER DEFAULT 0, " +
|
2016-08-16 03:23:56 +00:00
|
|
|
DEFAULT_SUBSCRIPTION_ID + " INTEGER DEFAULT -1, " +
|
|
|
|
EXPIRE_MESSAGES + " INTEGER DEFAULT 0);";
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
public RecipientPreferenceDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Cursor getBlocked() {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
Cursor cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS}, BLOCK + " = 1",
|
2015-06-11 20:00:50 +00:00
|
|
|
null, null, null, null, null);
|
|
|
|
cursor.setNotificationUri(context.getContentResolver(), Uri.parse(RECIPIENT_PREFERENCES_URI));
|
|
|
|
|
|
|
|
return cursor;
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 23:53:23 +00:00
|
|
|
public BlockedReader readerForBlocked(Cursor cursor) {
|
|
|
|
return new BlockedReader(context, cursor);
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
|
|
|
|
public Optional<RecipientsPreferences> getRecipientsPreferences(@NonNull Address address) {
|
2015-06-09 14:37:20 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
2017-08-01 15:56:00 +00:00
|
|
|
cursor = database.query(TABLE_NAME, null, ADDRESS + " = ?", new String[] {address.serialize()}, null, null, null);
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToNext()) {
|
2017-08-07 04:43:11 +00:00
|
|
|
return Optional.of(getRecipientPreferences(cursor));
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Optional.absent();
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 04:43:11 +00:00
|
|
|
RecipientsPreferences getRecipientPreferences(@NonNull Cursor cursor) {
|
|
|
|
boolean blocked = cursor.getInt(cursor.getColumnIndexOrThrow(BLOCK)) == 1;
|
|
|
|
String notification = cursor.getString(cursor.getColumnIndexOrThrow(NOTIFICATION));
|
|
|
|
int vibrateState = cursor.getInt(cursor.getColumnIndexOrThrow(VIBRATE));
|
|
|
|
long muteUntil = cursor.getLong(cursor.getColumnIndexOrThrow(MUTE_UNTIL));
|
|
|
|
String serializedColor = cursor.getString(cursor.getColumnIndexOrThrow(COLOR));
|
|
|
|
Uri notificationUri = notification == null ? null : Uri.parse(notification);
|
|
|
|
boolean seenInviteReminder = cursor.getInt(cursor.getColumnIndexOrThrow(SEEN_INVITE_REMINDER)) == 1;
|
|
|
|
int defaultSubscriptionId = cursor.getInt(cursor.getColumnIndexOrThrow(DEFAULT_SUBSCRIPTION_ID));
|
|
|
|
int expireMessages = cursor.getInt(cursor.getColumnIndexOrThrow(EXPIRE_MESSAGES));
|
|
|
|
|
|
|
|
MaterialColor color;
|
|
|
|
|
|
|
|
try {
|
|
|
|
color = serializedColor == null ? null : MaterialColor.fromSerialized(serializedColor);
|
|
|
|
} catch (MaterialColor.UnknownColorException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
color = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RecipientsPreferences(blocked, muteUntil,
|
|
|
|
VibrateState.fromId(vibrateState),
|
|
|
|
notificationUri, color, seenInviteReminder,
|
|
|
|
defaultSubscriptionId, expireMessages);
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setColor(Recipient recipient, MaterialColor color) {
|
2015-06-23 22:10:50 +00:00
|
|
|
ContentValues values = new ContentValues();
|
2015-06-30 16:16:05 +00:00
|
|
|
values.put(COLOR, color.serialize());
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
2015-06-23 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setDefaultSubscriptionId(@NonNull Recipient recipient, int defaultSubscriptionId) {
|
2016-02-06 00:10:33 +00:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(DEFAULT_SUBSCRIPTION_ID, defaultSubscriptionId);
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
|
|
|
EventBus.getDefault().post(new RecipientPreferenceEvent(recipient));
|
2016-02-06 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setBlocked(Recipient recipient, boolean blocked) {
|
2015-06-09 14:37:20 +00:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(BLOCK, blocked ? 1 : 0);
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setRingtone(Recipient recipient, @Nullable Uri notification) {
|
2015-06-09 14:37:20 +00:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(NOTIFICATION, notification == null ? null : notification.toString());
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setVibrate(Recipient recipient, @NonNull VibrateState enabled) {
|
2015-06-09 14:37:20 +00:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(VIBRATE, enabled.getId());
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setMuted(Recipient recipient, long until) {
|
2015-06-09 14:37:20 +00:00
|
|
|
Log.w(TAG, "Setting muted until: " + until);
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(MUTE_UNTIL, until);
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setSeenInviteReminder(Recipient recipient, boolean seen) {
|
2015-10-14 04:44:01 +00:00
|
|
|
ContentValues values = new ContentValues(1);
|
|
|
|
values.put(SEEN_INVITE_REMINDER, seen ? 1 : 0);
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
2015-10-14 04:44:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public void setExpireMessages(Recipient recipient, int expiration) {
|
|
|
|
recipient.setExpireMessages(expiration);
|
2016-08-16 03:23:56 +00:00
|
|
|
|
|
|
|
ContentValues values = new ContentValues(1);
|
|
|
|
values.put(EXPIRE_MESSAGES, expiration);
|
2017-08-01 15:56:00 +00:00
|
|
|
updateOrInsert(recipient, values);
|
2016-08-16 03:23:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
private void updateOrInsert(Recipient recipient, ContentValues contentValues) {
|
2015-06-09 14:37:20 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
database.beginTransaction();
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
int updated = database.update(TABLE_NAME, contentValues, ADDRESS + " = ?",
|
|
|
|
new String[] {recipient.getAddress().serialize()});
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
if (updated < 1) {
|
2017-08-01 15:56:00 +00:00
|
|
|
contentValues.put(ADDRESS, recipient.getAddress().serialize());
|
2015-06-09 14:37:20 +00:00
|
|
|
database.insert(TABLE_NAME, null, contentValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
database.setTransactionSuccessful();
|
|
|
|
database.endTransaction();
|
2015-06-11 20:00:50 +00:00
|
|
|
|
|
|
|
context.getContentResolver().notifyChange(Uri.parse(RECIPIENT_PREFERENCES_URI), null);
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class RecipientsPreferences {
|
2015-06-30 16:16:05 +00:00
|
|
|
private final boolean blocked;
|
|
|
|
private final long muteUntil;
|
|
|
|
private final VibrateState vibrateState;
|
|
|
|
private final Uri notification;
|
|
|
|
private final MaterialColor color;
|
2015-10-14 04:44:01 +00:00
|
|
|
private final boolean seenInviteReminder;
|
2016-02-06 00:10:33 +00:00
|
|
|
private final int defaultSubscriptionId;
|
2016-08-16 03:23:56 +00:00
|
|
|
private final int expireMessages;
|
2015-06-30 16:16:05 +00:00
|
|
|
|
2017-08-02 18:04:10 +00:00
|
|
|
RecipientsPreferences(boolean blocked, long muteUntil,
|
|
|
|
@NonNull VibrateState vibrateState,
|
|
|
|
@Nullable Uri notification,
|
|
|
|
@Nullable MaterialColor color,
|
|
|
|
boolean seenInviteReminder,
|
|
|
|
int defaultSubscriptionId,
|
|
|
|
int expireMessages)
|
2015-06-23 22:10:50 +00:00
|
|
|
{
|
2016-02-06 00:10:33 +00:00
|
|
|
this.blocked = blocked;
|
|
|
|
this.muteUntil = muteUntil;
|
|
|
|
this.vibrateState = vibrateState;
|
|
|
|
this.notification = notification;
|
|
|
|
this.color = color;
|
|
|
|
this.seenInviteReminder = seenInviteReminder;
|
|
|
|
this.defaultSubscriptionId = defaultSubscriptionId;
|
2016-08-16 03:23:56 +00:00
|
|
|
this.expireMessages = expireMessages;
|
2015-06-23 22:10:50 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 16:16:05 +00:00
|
|
|
public @Nullable MaterialColor getColor() {
|
2015-06-23 22:10:50 +00:00
|
|
|
return color;
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBlocked() {
|
|
|
|
return blocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getMuteUntil() {
|
|
|
|
return muteUntil;
|
|
|
|
}
|
|
|
|
|
|
|
|
public @NonNull VibrateState getVibrateState() {
|
|
|
|
return vibrateState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public @Nullable Uri getRingtone() {
|
|
|
|
return notification;
|
|
|
|
}
|
2015-10-14 04:44:01 +00:00
|
|
|
|
|
|
|
public boolean hasSeenInviteReminder() {
|
|
|
|
return seenInviteReminder;
|
|
|
|
}
|
2016-02-06 00:10:33 +00:00
|
|
|
|
|
|
|
public Optional<Integer> getDefaultSubscriptionId() {
|
|
|
|
return defaultSubscriptionId != -1 ? Optional.of(defaultSubscriptionId) : Optional.<Integer>absent();
|
|
|
|
}
|
2016-08-16 03:23:56 +00:00
|
|
|
|
|
|
|
public int getExpireMessages() {
|
|
|
|
return expireMessages;
|
|
|
|
}
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
2016-08-26 23:53:23 +00:00
|
|
|
|
|
|
|
public static class BlockedReader {
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private final Cursor cursor;
|
|
|
|
|
2017-08-02 18:04:10 +00:00
|
|
|
BlockedReader(Context context, Cursor cursor) {
|
2016-08-26 23:53:23 +00:00
|
|
|
this.context = context;
|
|
|
|
this.cursor = cursor;
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public @NonNull Recipient getCurrent() {
|
|
|
|
String serialized = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS));
|
|
|
|
return RecipientFactory.getRecipientFor(context, Address.fromSerialized(serialized), false);
|
2016-08-26 23:53:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public @Nullable Recipient getNext() {
|
2016-08-26 23:53:23 +00:00
|
|
|
if (!cursor.moveToNext()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getCurrent();
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 04:43:24 +00:00
|
|
|
|
|
|
|
public static class RecipientPreferenceEvent {
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
private final Recipient recipient;
|
2017-02-18 04:43:24 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public RecipientPreferenceEvent(Recipient recipients) {
|
|
|
|
this.recipient = recipients;
|
2017-02-18 04:43:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public Recipient getRecipient() {
|
|
|
|
return recipient;
|
2017-02-18 04:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|