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;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
private static final String TABLE_NAME = "recipient_preferences";
|
|
|
|
private static final String ID = "_id";
|
|
|
|
private static final String RECIPIENT_IDS = "recipient_ids";
|
|
|
|
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";
|
2015-06-23 22:10:50 +00:00
|
|
|
private static final String COLOR = "color";
|
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, " +
|
|
|
|
RECIPIENT_IDS + " TEXT UNIQUE, " +
|
|
|
|
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, " +
|
|
|
|
COLOR + " INTEGER DEFAULT -1);";
|
2015-06-09 14:37:20 +00:00
|
|
|
|
|
|
|
public RecipientPreferenceDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Cursor getBlocked() {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
|
2015-06-11 20:00:50 +00:00
|
|
|
Cursor cursor = database.query(TABLE_NAME, new String[] {ID, RECIPIENT_IDS}, BLOCK + " = 1",
|
|
|
|
null, null, null, null, null);
|
|
|
|
cursor.setNotificationUri(context.getContentResolver(), Uri.parse(RECIPIENT_PREFERENCES_URI));
|
|
|
|
|
|
|
|
return cursor;
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<RecipientsPreferences> getRecipientsPreferences(@NonNull long[] recipients) {
|
|
|
|
Arrays.sort(recipients);
|
|
|
|
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = database.query(TABLE_NAME, null, RECIPIENT_IDS + " = ?",
|
|
|
|
new String[] {Util.join(recipients, " ")},
|
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToNext()) {
|
|
|
|
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));
|
2015-06-23 22:10:50 +00:00
|
|
|
int color = cursor.getInt(cursor.getColumnIndexOrThrow(COLOR));
|
2015-06-09 14:37:20 +00:00
|
|
|
Uri notificationUri = notification == null ? null : Uri.parse(notification);
|
|
|
|
|
|
|
|
Log.w(TAG, "Muted until: " + muteUntil);
|
|
|
|
|
|
|
|
return Optional.of(new RecipientsPreferences(blocked, muteUntil,
|
|
|
|
VibrateState.fromId(vibrateState),
|
2015-06-23 22:10:50 +00:00
|
|
|
notificationUri,
|
|
|
|
color == -1 ? Optional.<Integer>absent() :
|
|
|
|
Optional.of(color)));
|
2015-06-09 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Optional.absent();
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 22:10:50 +00:00
|
|
|
public void setColor(Recipients recipients, int color) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(COLOR, color);
|
|
|
|
updateOrInsert(recipients, values);
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public void setBlocked(Recipients recipients, boolean blocked) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(BLOCK, blocked ? 1 : 0);
|
|
|
|
updateOrInsert(recipients, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRingtone(Recipients recipients, @Nullable Uri notification) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(NOTIFICATION, notification == null ? null : notification.toString());
|
|
|
|
updateOrInsert(recipients, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setVibrate(Recipients recipients, @NonNull VibrateState enabled) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(VIBRATE, enabled.getId());
|
|
|
|
updateOrInsert(recipients, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMuted(Recipients recipients, long until) {
|
|
|
|
Log.w(TAG, "Setting muted until: " + until);
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(MUTE_UNTIL, until);
|
|
|
|
updateOrInsert(recipients, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateOrInsert(Recipients recipients, ContentValues contentValues) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
database.beginTransaction();
|
|
|
|
|
|
|
|
int updated = database.update(TABLE_NAME, contentValues, RECIPIENT_IDS + " = ?",
|
|
|
|
new String[] {String.valueOf(recipients.getSortedIdsString())});
|
|
|
|
|
|
|
|
if (updated < 1) {
|
|
|
|
contentValues.put(RECIPIENT_IDS, recipients.getSortedIdsString());
|
|
|
|
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-23 22:10:50 +00:00
|
|
|
private final boolean blocked;
|
|
|
|
private final long muteUntil;
|
|
|
|
private final VibrateState vibrateState;
|
|
|
|
private final Uri notification;
|
|
|
|
private final Optional<Integer> color;
|
|
|
|
|
|
|
|
public RecipientsPreferences(boolean blocked, long muteUntil, VibrateState vibrateState,
|
|
|
|
Uri notification, Optional<Integer> color)
|
|
|
|
{
|
2015-06-09 14:37:20 +00:00
|
|
|
this.blocked = blocked;
|
|
|
|
this.muteUntil = muteUntil;
|
|
|
|
this.vibrateState = vibrateState;
|
|
|
|
this.notification = notification;
|
2015-06-23 22:10:50 +00:00
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<Integer> getColor() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|