mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-26 09:08:43 +00:00
Move "directory" information into RecipientPreferencesDatabase
// FREEBIE
This commit is contained in:
@@ -101,7 +101,8 @@ public class DatabaseFactory {
|
||||
private static final int SANIFY_ATTACHMENT_DOWNLOAD = 36;
|
||||
private static final int NO_MORE_CANONICAL_ADDRESS_DATABASE = 37;
|
||||
private static final int NO_MORE_RECIPIENTS_PLURAL = 38;
|
||||
private static final int DATABASE_VERSION = 38;
|
||||
private static final int INTERNAL_DIRECTORY = 39;
|
||||
private static final int DATABASE_VERSION = 39;
|
||||
|
||||
private static final String DATABASE_NAME = "messages.db";
|
||||
private static final Object lock = new Object();
|
||||
@@ -1273,6 +1274,24 @@ public class DatabaseFactory {
|
||||
if (cursor != null) cursor.close();
|
||||
}
|
||||
|
||||
if (oldVersion < INTERNAL_DIRECTORY) {
|
||||
db.execSQL("ALTER TABLE recipient_preferences ADD COLUMN registered INTEGER DEFAULT 0");
|
||||
|
||||
DatabaseHelper directoryDatabaseHelper = new DatabaseHelper(context, "whisper_directory.db", null, 5);
|
||||
SQLiteDatabase directoryDatabase = directoryDatabaseHelper.getReadableDatabase();
|
||||
|
||||
Cursor cursor = directoryDatabase.query("directory", new String[] {"number", "registered"}, null, null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String address = new NumberMigrator(TextSecurePreferences.getLocalNumber(context)).migrate(cursor.getString(0));
|
||||
ContentValues contentValues = new ContentValues(1);
|
||||
|
||||
contentValues.put("recipient_ids", address);
|
||||
contentValues.put("registered", cursor.getInt(1) == 1);
|
||||
db.replace("recipient_preferences", null, contentValues);
|
||||
}
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
db.endTransaction();
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class RecipientPreferenceDatabase extends Database {
|
||||
|
||||
@@ -36,9 +39,10 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
private static final String SEEN_INVITE_REMINDER = "seen_invite_reminder";
|
||||
private static final String DEFAULT_SUBSCRIPTION_ID = "default_subscription_id";
|
||||
private static final String EXPIRE_MESSAGES = "expire_messages";
|
||||
private static final String REGISTERED = "registered";
|
||||
|
||||
private static final String[] RECIPIENT_PROJECTION = new String[] {
|
||||
BLOCK, NOTIFICATION, VIBRATE, MUTE_UNTIL, COLOR, SEEN_INVITE_REMINDER, DEFAULT_SUBSCRIPTION_ID, EXPIRE_MESSAGES
|
||||
BLOCK, NOTIFICATION, VIBRATE, MUTE_UNTIL, COLOR, SEEN_INVITE_REMINDER, DEFAULT_SUBSCRIPTION_ID, EXPIRE_MESSAGES, REGISTERED
|
||||
};
|
||||
|
||||
static final List<String> TYPED_RECIPIENT_PROJECTION = Stream.of(RECIPIENT_PROJECTION)
|
||||
@@ -74,7 +78,8 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
COLOR + " TEXT DEFAULT NULL, " +
|
||||
SEEN_INVITE_REMINDER + " INTEGER DEFAULT 0, " +
|
||||
DEFAULT_SUBSCRIPTION_ID + " INTEGER DEFAULT -1, " +
|
||||
EXPIRE_MESSAGES + " INTEGER DEFAULT 0);";
|
||||
EXPIRE_MESSAGES + " INTEGER DEFAULT 0, " +
|
||||
REGISTERED + " INTEGER DEFAULT 0);";
|
||||
|
||||
public RecipientPreferenceDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
||||
super(context, databaseHelper);
|
||||
@@ -122,6 +127,7 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
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));
|
||||
boolean registered = cursor.getInt(cursor.getColumnIndexOrThrow(REGISTERED)) == 1;
|
||||
|
||||
MaterialColor color;
|
||||
|
||||
@@ -135,7 +141,7 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
return new RecipientsPreferences(blocked, muteUntil,
|
||||
VibrateState.fromId(vibrateState),
|
||||
notificationUri, color, seenInviteReminder,
|
||||
defaultSubscriptionId, expireMessages);
|
||||
defaultSubscriptionId, expireMessages, registered);
|
||||
}
|
||||
|
||||
public void setColor(Recipient recipient, MaterialColor color) {
|
||||
@@ -190,6 +196,56 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
updateOrInsert(recipient, values);
|
||||
}
|
||||
|
||||
public Set<Address> getAllRecipients() {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
Set<Address> results = new HashSet<>();
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, new String[] {ADDRESS}, null, null, null, null, null)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
results.add(Address.fromExternal(context, cursor.getString(0)));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public void setRegistered(@NonNull List<Address> activeAddresses,
|
||||
@NonNull List<Address> inactiveAddresses)
|
||||
{
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
|
||||
for (Address activeAddress : activeAddresses) {
|
||||
ContentValues contentValues = new ContentValues(2);
|
||||
contentValues.put(ADDRESS, activeAddress.serialize());
|
||||
contentValues.put(REGISTERED, 1);
|
||||
|
||||
db.replace(TABLE_NAME, null, contentValues);
|
||||
}
|
||||
|
||||
for (Address inactiveAddress : inactiveAddresses) {
|
||||
ContentValues contentValues = new ContentValues(2);
|
||||
contentValues.put(ADDRESS, inactiveAddress.serialize());
|
||||
contentValues.put(REGISTERED, 0);
|
||||
|
||||
db.replace(TABLE_NAME, null, contentValues);
|
||||
}
|
||||
|
||||
context.getContentResolver().notifyChange(Uri.parse(RECIPIENT_PREFERENCES_URI), null);
|
||||
}
|
||||
|
||||
public List<Address> getRegistered() {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
List<Address> results = new LinkedList<>();
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, new String[] {ADDRESS}, REGISTERED + " = ?", new String[] {"1"}, null, null, null)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
results.add(Address.fromSerialized(cursor.getString(0)));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private void updateOrInsert(Recipient recipient, ContentValues contentValues) {
|
||||
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
||||
|
||||
@@ -218,6 +274,7 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
private final boolean seenInviteReminder;
|
||||
private final int defaultSubscriptionId;
|
||||
private final int expireMessages;
|
||||
private final boolean registered;
|
||||
|
||||
RecipientsPreferences(boolean blocked, long muteUntil,
|
||||
@NonNull VibrateState vibrateState,
|
||||
@@ -225,7 +282,8 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
@Nullable MaterialColor color,
|
||||
boolean seenInviteReminder,
|
||||
int defaultSubscriptionId,
|
||||
int expireMessages)
|
||||
int expireMessages,
|
||||
boolean registered)
|
||||
{
|
||||
this.blocked = blocked;
|
||||
this.muteUntil = muteUntil;
|
||||
@@ -235,6 +293,7 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
this.seenInviteReminder = seenInviteReminder;
|
||||
this.defaultSubscriptionId = defaultSubscriptionId;
|
||||
this.expireMessages = expireMessages;
|
||||
this.registered = registered;
|
||||
}
|
||||
|
||||
public @Nullable MaterialColor getColor() {
|
||||
@@ -268,6 +327,10 @@ public class RecipientPreferenceDatabase extends Database {
|
||||
public int getExpireMessages() {
|
||||
return expireMessages;
|
||||
}
|
||||
|
||||
public boolean isRegistered() {
|
||||
return registered;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BlockedReader {
|
||||
|
||||
@@ -1,295 +0,0 @@
|
||||
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.provider.ContactsContract.CommonDataKinds.Phone;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class TextSecureDirectory {
|
||||
|
||||
private static final int INTRODUCED_CHANGE_FROM_TOKEN_TO_E164_NUMBER = 2;
|
||||
private static final int INTRODUCED_VOICE_COLUMN = 4;
|
||||
private static final int INTRODUCED_VIDEO_COLUMN = 5;
|
||||
|
||||
private static final String DATABASE_NAME = "whisper_directory.db";
|
||||
private static final int DATABASE_VERSION = 5;
|
||||
|
||||
private static final String TABLE_NAME = "directory";
|
||||
private static final String ID = "_id";
|
||||
private static final String NUMBER = "number";
|
||||
private static final String REGISTERED = "registered";
|
||||
private static final String RELAY = "relay";
|
||||
private static final String TIMESTAMP = "timestamp";
|
||||
private static final String VOICE = "voice";
|
||||
private static final String VIDEO = "video";
|
||||
|
||||
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY, " +
|
||||
NUMBER + " TEXT UNIQUE, " +
|
||||
REGISTERED + " INTEGER, " +
|
||||
RELAY + " TEXT, " +
|
||||
TIMESTAMP + " INTEGER, " +
|
||||
VOICE + " INTEGER, " +
|
||||
VIDEO + " INTEGER);";
|
||||
|
||||
private static final Object instanceLock = new Object();
|
||||
private static volatile TextSecureDirectory instance;
|
||||
|
||||
public static TextSecureDirectory getInstance(Context context) {
|
||||
if (instance == null) {
|
||||
synchronized (instanceLock) {
|
||||
if (instance == null) {
|
||||
instance = new TextSecureDirectory(context.getApplicationContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private final DatabaseHelper databaseHelper;
|
||||
private final Context context;
|
||||
|
||||
private TextSecureDirectory(Context context) {
|
||||
this.context = context;
|
||||
this.databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
public boolean isSecureTextSupported(@NonNull Address address) throws NotInDirectoryException {
|
||||
if (address.isEmail()) return false;
|
||||
if (address.isGroup()) return true;
|
||||
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = db.query(TABLE_NAME,
|
||||
new String[]{REGISTERED}, NUMBER + " = ?",
|
||||
new String[] {address.serialize()}, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getInt(0) == 1;
|
||||
} else {
|
||||
throw new NotInDirectoryException();
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
// public boolean isSecureVoiceSupported(String e164number) throws NotInDirectoryException {
|
||||
// if (TextUtils.isEmpty(e164number)) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
// Cursor cursor = null;
|
||||
//
|
||||
// try {
|
||||
// cursor = db.query(TABLE_NAME,
|
||||
// new String[]{VOICE}, NUMBER + " = ?",
|
||||
// new String[] {e164number}, null, null, null);
|
||||
//
|
||||
// if (cursor != null && cursor.moveToFirst()) {
|
||||
// return cursor.getInt(0) == 1;
|
||||
// } else {
|
||||
// throw new NotInDirectoryException();
|
||||
// }
|
||||
//
|
||||
// } finally {
|
||||
// if (cursor != null)
|
||||
// cursor.close();
|
||||
// }
|
||||
// }
|
||||
|
||||
// public boolean isSecureVideoSupported(String e164number) throws NotInDirectoryException {
|
||||
// if (TextUtils.isEmpty(e164number)) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
// Cursor cursor = null;
|
||||
//
|
||||
// try {
|
||||
// cursor = db.query(TABLE_NAME,
|
||||
// new String[]{VIDEO}, NUMBER + " = ?",
|
||||
// new String[] {e164number}, null, null, null);
|
||||
//
|
||||
// if (cursor != null && cursor.moveToFirst()) {
|
||||
// return cursor.getInt(0) == 1;
|
||||
// } else {
|
||||
// throw new NotInDirectoryException();
|
||||
// }
|
||||
//
|
||||
// } finally {
|
||||
// if (cursor != null)
|
||||
// cursor.close();
|
||||
// }
|
||||
// }
|
||||
|
||||
public String getRelay(String e164number) {
|
||||
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = database.query(TABLE_NAME, null, NUMBER + " = ?", new String[]{e164number}, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow(RELAY));
|
||||
}
|
||||
|
||||
return null;
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void setNumber(ContactTokenDetails token, boolean active) {
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(NUMBER, token.getNumber());
|
||||
values.put(REGISTERED, active ? 1 : 0);
|
||||
values.put(TIMESTAMP, System.currentTimeMillis());
|
||||
values.put(RELAY, token.getRelay());
|
||||
values.put(VOICE, token.isVoice());
|
||||
values.put(VIDEO, token.isVideo());
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
public void setNumbers(List<ContactTokenDetails> activeTokens, Collection<Address> inactiveAddresses) {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
db.beginTransaction();
|
||||
|
||||
try {
|
||||
for (ContactTokenDetails token : activeTokens) {
|
||||
Log.w("Directory", "Adding active token: " + token.getNumber() + ", " + token.getToken() + ", video: " + token.isVideo());
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(NUMBER, token.getNumber());
|
||||
values.put(REGISTERED, 1);
|
||||
values.put(TIMESTAMP, timestamp);
|
||||
values.put(RELAY, token.getRelay());
|
||||
values.put(VOICE, token.isVoice());
|
||||
values.put(VIDEO, token.isVideo());
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
for (Address address : inactiveAddresses) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(NUMBER, address.serialize());
|
||||
values.put(REGISTERED, 0);
|
||||
values.put(TIMESTAMP, timestamp);
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Address> getPushEligibleContactNumbers() {
|
||||
final Uri uri = Phone.CONTENT_URI;
|
||||
final Set<Address> results = new HashSet<>();
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(uri, new String[] {Phone.NUMBER}, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
final String rawNumber = cursor.getString(0);
|
||||
if (!TextUtils.isEmpty(rawNumber)) {
|
||||
results.add(Address.fromExternal(context, rawNumber));
|
||||
}
|
||||
}
|
||||
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
|
||||
final SQLiteDatabase readableDb = databaseHelper.getReadableDatabase();
|
||||
if (readableDb != null) {
|
||||
cursor = readableDb.query(TABLE_NAME, new String[]{NUMBER},
|
||||
null, null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
results.add(Address.fromSerialized(cursor.getString(0)));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getActiveNumbers() {
|
||||
final List<String> results = new ArrayList<>();
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, new String[]{NUMBER},
|
||||
REGISTERED + " = 1", null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
results.add(cursor.getString(0));
|
||||
}
|
||||
return results;
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static class DatabaseHelper extends SQLiteOpenHelper {
|
||||
|
||||
public DatabaseHelper(Context context, String name,
|
||||
SQLiteDatabase.CursorFactory factory,
|
||||
int version)
|
||||
{
|
||||
super(context, name, factory, version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL(CREATE_TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
if (oldVersion < INTRODUCED_CHANGE_FROM_TOKEN_TO_E164_NUMBER) {
|
||||
db.execSQL("DROP TABLE directory;");
|
||||
db.execSQL("CREATE TABLE directory ( _id INTEGER PRIMARY KEY, " +
|
||||
"number TEXT UNIQUE, " +
|
||||
"registered INTEGER, " +
|
||||
"relay TEXT, " +
|
||||
"supports_sms INTEGER, " +
|
||||
"timestamp INTEGER);");
|
||||
}
|
||||
|
||||
if (oldVersion < INTRODUCED_VOICE_COLUMN) {
|
||||
db.execSQL("ALTER TABLE directory ADD COLUMN voice INTEGER;");
|
||||
}
|
||||
|
||||
if (oldVersion < INTRODUCED_VIDEO_COLUMN) {
|
||||
db.execSQL("ALTER TABLE directory ADD COLUMN video INTEGER;");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user