2015-06-09 16:37:20 +02:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2021-05-18 01:12:33 +02:00
|
|
|
|
2020-08-19 02:06:26 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2015-06-09 16:37:20 +02:00
|
|
|
|
2017-08-07 06:43:11 +02:00
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2018-01-25 04:17:44 +01:00
|
|
|
import net.sqlcipher.database.SQLiteDatabase;
|
2021-01-13 07:11:30 +01:00
|
|
|
|
2021-05-18 08:11:38 +02:00
|
|
|
import org.session.libsession.utilities.Address;
|
2021-07-20 08:06:59 +02:00
|
|
|
import org.session.libsession.utilities.MaterialColor;
|
|
|
|
import org.session.libsession.utilities.Util;
|
2021-05-18 08:03:47 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient;
|
2021-07-20 08:06:59 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient.RecipientSettings;
|
|
|
|
import org.session.libsession.utilities.recipients.Recipient.RegisteredState;
|
|
|
|
import org.session.libsession.utilities.recipients.Recipient.UnidentifiedAccessMode;
|
2021-02-01 01:35:53 +01:00
|
|
|
import org.session.libsignal.utilities.Base64;
|
2021-05-18 01:12:33 +02:00
|
|
|
import org.session.libsignal.utilities.Log;
|
2021-07-20 08:06:59 +02:00
|
|
|
import org.session.libsignal.utilities.guava.Optional;
|
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
2015-06-09 16:37:20 +02:00
|
|
|
|
2018-08-16 18:47:43 +02:00
|
|
|
import java.io.Closeable;
|
2017-08-15 03:11:13 +02:00
|
|
|
import java.io.IOException;
|
2017-08-07 06:43:11 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-08-22 03:37:39 +02:00
|
|
|
public class RecipientDatabase extends Database {
|
2015-06-09 16:37:20 +02:00
|
|
|
|
2017-08-22 03:37:39 +02:00
|
|
|
private static final String TAG = RecipientDatabase.class.getSimpleName();
|
2015-06-09 16:37:20 +02:00
|
|
|
|
2020-07-16 04:49:37 +02:00
|
|
|
static final String TABLE_NAME = "recipient_preferences";
|
2018-05-22 11:13:10 +02:00
|
|
|
private static final String ID = "_id";
|
2020-07-16 04:49:37 +02:00
|
|
|
public static final String ADDRESS = "recipient_ids";
|
2018-05-22 11:13:10 +02: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";
|
|
|
|
private static final String EXPIRE_MESSAGES = "expire_messages";
|
|
|
|
private static final String REGISTERED = "registered";
|
|
|
|
private static final String PROFILE_KEY = "profile_key";
|
|
|
|
private static final String SYSTEM_DISPLAY_NAME = "system_display_name";
|
|
|
|
private static final String SYSTEM_PHOTO_URI = "system_contact_photo";
|
|
|
|
private static final String SYSTEM_PHONE_LABEL = "system_phone_label";
|
|
|
|
private static final String SYSTEM_CONTACT_URI = "system_contact_uri";
|
|
|
|
private static final String SIGNAL_PROFILE_NAME = "signal_profile_name";
|
|
|
|
private static final String SIGNAL_PROFILE_AVATAR = "signal_profile_avatar";
|
|
|
|
private static final String PROFILE_SHARING = "profile_sharing_approval";
|
|
|
|
private static final String CALL_RINGTONE = "call_ringtone";
|
|
|
|
private static final String CALL_VIBRATE = "call_vibrate";
|
|
|
|
private static final String NOTIFICATION_CHANNEL = "notification_channel";
|
|
|
|
private static final String UNIDENTIFIED_ACCESS_MODE = "unidentified_access_mode";
|
2019-04-12 21:22:38 +02:00
|
|
|
private static final String FORCE_SMS_SELECTION = "force_sms_selection";
|
2021-07-20 08:06:59 +02:00
|
|
|
private static final String NOTIFY_TYPE = "notify_type"; // all, mentions only, none
|
2015-06-09 16:37:20 +02:00
|
|
|
|
2017-08-07 06:43:11 +02:00
|
|
|
private static final String[] RECIPIENT_PROJECTION = new String[] {
|
2018-02-16 20:10:35 +01:00
|
|
|
BLOCK, NOTIFICATION, CALL_RINGTONE, VIBRATE, CALL_VIBRATE, MUTE_UNTIL, COLOR, SEEN_INVITE_REMINDER, DEFAULT_SUBSCRIPTION_ID, EXPIRE_MESSAGES, REGISTERED,
|
2017-11-26 19:45:39 +01:00
|
|
|
PROFILE_KEY, SYSTEM_DISPLAY_NAME, SYSTEM_PHOTO_URI, SYSTEM_PHONE_LABEL, SYSTEM_CONTACT_URI,
|
2018-05-22 11:13:10 +02:00
|
|
|
SIGNAL_PROFILE_NAME, SIGNAL_PROFILE_AVATAR, PROFILE_SHARING, NOTIFICATION_CHANNEL,
|
2019-04-12 21:22:38 +02:00
|
|
|
UNIDENTIFIED_ACCESS_MODE,
|
2021-07-20 08:06:59 +02:00
|
|
|
FORCE_SMS_SELECTION, NOTIFY_TYPE,
|
2017-08-07 06:43:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static final List<String> TYPED_RECIPIENT_PROJECTION = Stream.of(RECIPIENT_PROJECTION)
|
|
|
|
.map(columnName -> TABLE_NAME + "." + columnName)
|
|
|
|
.toList();
|
|
|
|
|
2015-06-09 16:37:20 +02:00
|
|
|
public static final String CREATE_TABLE =
|
|
|
|
"CREATE TABLE " + TABLE_NAME +
|
|
|
|
" (" + ID + " INTEGER PRIMARY KEY, " +
|
2017-08-01 17:56:00 +02:00
|
|
|
ADDRESS + " TEXT UNIQUE, " +
|
2015-06-09 16:37:20 +02:00
|
|
|
BLOCK + " INTEGER DEFAULT 0," +
|
|
|
|
NOTIFICATION + " TEXT DEFAULT NULL, " +
|
2021-01-13 07:11:30 +01:00
|
|
|
VIBRATE + " INTEGER DEFAULT " + Recipient.VibrateState.DEFAULT.getId() + ", " +
|
2015-06-24 00:10:50 +02:00
|
|
|
MUTE_UNTIL + " INTEGER DEFAULT 0, " +
|
2015-10-14 06:44:01 +02:00
|
|
|
COLOR + " TEXT DEFAULT NULL, " +
|
2016-02-06 01:10:33 +01:00
|
|
|
SEEN_INVITE_REMINDER + " INTEGER DEFAULT 0, " +
|
2016-08-16 05:23:56 +02:00
|
|
|
DEFAULT_SUBSCRIPTION_ID + " INTEGER DEFAULT -1, " +
|
2017-08-07 23:24:53 +02:00
|
|
|
EXPIRE_MESSAGES + " INTEGER DEFAULT 0, " +
|
2017-08-08 00:31:12 +02:00
|
|
|
REGISTERED + " INTEGER DEFAULT 0, " +
|
2017-08-15 03:11:13 +02:00
|
|
|
SYSTEM_DISPLAY_NAME + " TEXT DEFAULT NULL, " +
|
2017-11-26 19:45:39 +01:00
|
|
|
SYSTEM_PHOTO_URI + " TEXT DEFAULT NULL, " +
|
|
|
|
SYSTEM_PHONE_LABEL + " TEXT DEFAULT NULL, " +
|
|
|
|
SYSTEM_CONTACT_URI + " TEXT DEFAULT NULL, " +
|
2017-08-15 03:11:13 +02:00
|
|
|
PROFILE_KEY + " TEXT DEFAULT NULL, " +
|
|
|
|
SIGNAL_PROFILE_NAME + " TEXT DEFAULT NULL, " +
|
2017-08-17 06:49:41 +02:00
|
|
|
SIGNAL_PROFILE_AVATAR + " TEXT DEFAULT NULL, " +
|
2018-02-16 20:10:35 +01:00
|
|
|
PROFILE_SHARING + " INTEGER DEFAULT 0, " +
|
|
|
|
CALL_RINGTONE + " TEXT DEFAULT NULL, " +
|
2021-01-13 07:11:30 +01:00
|
|
|
CALL_VIBRATE + " INTEGER DEFAULT " + Recipient.VibrateState.DEFAULT.getId() + ", " +
|
2018-05-22 11:13:10 +02:00
|
|
|
NOTIFICATION_CHANNEL + " TEXT DEFAULT NULL, " +
|
2019-04-12 21:22:38 +02:00
|
|
|
UNIDENTIFIED_ACCESS_MODE + " INTEGER DEFAULT 0, " +
|
|
|
|
FORCE_SMS_SELECTION + " INTEGER DEFAULT 0);";
|
2015-06-09 16:37:20 +02:00
|
|
|
|
2021-07-20 08:06:59 +02:00
|
|
|
public static String getCreateNotificationTypeCommand() {
|
|
|
|
return "ALTER TABLE "+ TABLE_NAME + " " +
|
|
|
|
"ADD COLUMN " + NOTIFY_TYPE + " INTEGER DEFAULT 0;";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final int NOTIFY_TYPE_ALL = 0;
|
|
|
|
public static final int NOTIFY_TYPE_MENTIONS = 1;
|
|
|
|
public static final int NOTIFY_TYPE_NONE = 2;
|
|
|
|
|
2018-01-25 04:17:44 +01:00
|
|
|
public RecipientDatabase(Context context, SQLCipherOpenHelper databaseHelper) {
|
2015-06-09 16:37:20 +02:00
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
|
|
|
|
2018-08-16 18:47:43 +02:00
|
|
|
public RecipientReader getRecipientsWithNotificationChannels() {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS}, NOTIFICATION_CHANNEL + " NOT NULL",
|
|
|
|
null, null, null, null, null);
|
|
|
|
|
|
|
|
return new RecipientReader(context, cursor);
|
2016-08-27 01:53:23 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 03:47:37 +02:00
|
|
|
public Optional<RecipientSettings> getRecipientSettings(@NonNull Address address) {
|
2015-06-09 16:37:20 +02:00
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
2017-08-01 17:56:00 +02:00
|
|
|
cursor = database.query(TABLE_NAME, null, ADDRESS + " = ?", new String[] {address.serialize()}, null, null, null);
|
2015-06-09 16:37:20 +02:00
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToNext()) {
|
2017-08-22 19:44:04 +02:00
|
|
|
return getRecipientSettings(cursor);
|
2015-06-09 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Optional.absent();
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
Optional<RecipientSettings> getRecipientSettings(@NonNull Cursor cursor) {
|
2018-05-22 11:13:10 +02:00
|
|
|
boolean blocked = cursor.getInt(cursor.getColumnIndexOrThrow(BLOCK)) == 1;
|
|
|
|
String messageRingtone = cursor.getString(cursor.getColumnIndexOrThrow(NOTIFICATION));
|
|
|
|
String callRingtone = cursor.getString(cursor.getColumnIndexOrThrow(CALL_RINGTONE));
|
|
|
|
int messageVibrateState = cursor.getInt(cursor.getColumnIndexOrThrow(VIBRATE));
|
|
|
|
int callVibrateState = cursor.getInt(cursor.getColumnIndexOrThrow(CALL_VIBRATE));
|
|
|
|
long muteUntil = cursor.getLong(cursor.getColumnIndexOrThrow(MUTE_UNTIL));
|
2021-07-20 08:06:59 +02:00
|
|
|
int notifyType = cursor.getInt(cursor.getColumnIndexOrThrow(NOTIFY_TYPE));
|
2018-05-22 11:13:10 +02:00
|
|
|
String serializedColor = cursor.getString(cursor.getColumnIndexOrThrow(COLOR));
|
|
|
|
int defaultSubscriptionId = cursor.getInt(cursor.getColumnIndexOrThrow(DEFAULT_SUBSCRIPTION_ID));
|
|
|
|
int expireMessages = cursor.getInt(cursor.getColumnIndexOrThrow(EXPIRE_MESSAGES));
|
|
|
|
int registeredState = cursor.getInt(cursor.getColumnIndexOrThrow(REGISTERED));
|
|
|
|
String profileKeyString = cursor.getString(cursor.getColumnIndexOrThrow(PROFILE_KEY));
|
|
|
|
String systemDisplayName = cursor.getString(cursor.getColumnIndexOrThrow(SYSTEM_DISPLAY_NAME));
|
|
|
|
String systemContactPhoto = cursor.getString(cursor.getColumnIndexOrThrow(SYSTEM_PHOTO_URI));
|
|
|
|
String systemPhoneLabel = cursor.getString(cursor.getColumnIndexOrThrow(SYSTEM_PHONE_LABEL));
|
|
|
|
String systemContactUri = cursor.getString(cursor.getColumnIndexOrThrow(SYSTEM_CONTACT_URI));
|
|
|
|
String signalProfileName = cursor.getString(cursor.getColumnIndexOrThrow(SIGNAL_PROFILE_NAME));
|
|
|
|
String signalProfileAvatar = cursor.getString(cursor.getColumnIndexOrThrow(SIGNAL_PROFILE_AVATAR));
|
|
|
|
boolean profileSharing = cursor.getInt(cursor.getColumnIndexOrThrow(PROFILE_SHARING)) == 1;
|
|
|
|
String notificationChannel = cursor.getString(cursor.getColumnIndexOrThrow(NOTIFICATION_CHANNEL));
|
|
|
|
int unidentifiedAccessMode = cursor.getInt(cursor.getColumnIndexOrThrow(UNIDENTIFIED_ACCESS_MODE));
|
2019-04-12 21:22:38 +02:00
|
|
|
boolean forceSmsSelection = cursor.getInt(cursor.getColumnIndexOrThrow(FORCE_SMS_SELECTION)) == 1;
|
2017-08-07 06:43:11 +02:00
|
|
|
|
|
|
|
MaterialColor color;
|
2018-05-22 11:13:10 +02:00
|
|
|
byte[] profileKey = null;
|
2017-08-07 06:43:11 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
color = serializedColor == null ? null : MaterialColor.fromSerialized(serializedColor);
|
|
|
|
} catch (MaterialColor.UnknownColorException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
color = null;
|
|
|
|
}
|
|
|
|
|
2017-08-15 03:11:13 +02:00
|
|
|
if (profileKeyString != null) {
|
|
|
|
try {
|
|
|
|
profileKey = Base64.decode(profileKeyString);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
profileKey = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 03:47:37 +02:00
|
|
|
return Optional.of(new RecipientSettings(blocked, muteUntil,
|
2021-07-20 08:06:59 +02:00
|
|
|
notifyType,
|
2021-01-13 07:11:30 +01:00
|
|
|
Recipient.VibrateState.fromId(messageVibrateState),
|
|
|
|
Recipient.VibrateState.fromId(callVibrateState),
|
2018-02-16 20:10:35 +01:00
|
|
|
Util.uri(messageRingtone), Util.uri(callRingtone),
|
2021-01-13 07:11:30 +01:00
|
|
|
color, defaultSubscriptionId, expireMessages,
|
|
|
|
Recipient.RegisteredState.fromId(registeredState),
|
2017-11-26 19:45:39 +01:00
|
|
|
profileKey, systemDisplayName, systemContactPhoto,
|
|
|
|
systemPhoneLabel, systemContactUri,
|
2018-08-16 18:47:43 +02:00
|
|
|
signalProfileName, signalProfileAvatar, profileSharing,
|
2021-01-13 07:11:30 +01:00
|
|
|
notificationChannel, Recipient.UnidentifiedAccessMode.fromMode(unidentifiedAccessMode),
|
2019-04-12 21:22:38 +02:00
|
|
|
forceSmsSelection));
|
2017-08-07 06:43:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
public void setColor(@NonNull Recipient recipient, @NonNull MaterialColor color) {
|
2015-06-24 00:10:50 +02:00
|
|
|
ContentValues values = new ContentValues();
|
2015-06-30 18:16:05 +02:00
|
|
|
values.put(COLOR, color.serialize());
|
2017-08-08 00:31:12 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
2017-08-22 19:44:04 +02:00
|
|
|
recipient.resolve().setColor(color);
|
2015-06-24 00:10:50 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 17:56:00 +02:00
|
|
|
public void setDefaultSubscriptionId(@NonNull Recipient recipient, int defaultSubscriptionId) {
|
2016-02-06 01:10:33 +01:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(DEFAULT_SUBSCRIPTION_ID, defaultSubscriptionId);
|
2017-08-08 00:31:12 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
2017-08-22 19:44:04 +02:00
|
|
|
recipient.resolve().setDefaultSubscriptionId(Optional.of(defaultSubscriptionId));
|
2016-02-06 01:10:33 +01:00
|
|
|
}
|
|
|
|
|
2019-04-12 21:22:38 +02:00
|
|
|
public void setForceSmsSelection(@NonNull Recipient recipient, boolean forceSmsSelection) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(FORCE_SMS_SELECTION, forceSmsSelection ? 1 : 0);
|
|
|
|
updateOrInsert(recipient.getAddress(), contentValues);
|
|
|
|
recipient.resolve().setForceSmsSelection(forceSmsSelection);
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
public void setBlocked(@NonNull Recipient recipient, boolean blocked) {
|
2015-06-09 16:37:20 +02:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(BLOCK, blocked ? 1 : 0);
|
2017-08-08 00:31:12 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
2017-08-22 19:44:04 +02:00
|
|
|
recipient.resolve().setBlocked(blocked);
|
2015-06-09 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
public void setMuted(@NonNull Recipient recipient, long until) {
|
2015-06-09 16:37:20 +02:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(MUTE_UNTIL, until);
|
2017-08-08 00:31:12 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
2017-08-22 19:44:04 +02:00
|
|
|
recipient.resolve().setMuted(until);
|
2015-06-09 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 08:06:59 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param recipient to modify notifications for
|
|
|
|
* @param notifyType the new notification type {@link #NOTIFY_TYPE_ALL}, {@link #NOTIFY_TYPE_MENTIONS} or {@link #NOTIFY_TYPE_NONE}
|
|
|
|
*/
|
|
|
|
public void setNotifyType(@NonNull Recipient recipient, int notifyType) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(NOTIFY_TYPE, notifyType);
|
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
|
|
|
recipient.resolve().setNotifyType(notifyType);
|
2021-07-26 06:50:43 +02:00
|
|
|
notifyConversationListListeners();
|
2021-07-20 08:06:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
public void setExpireMessages(@NonNull Recipient recipient, int expiration) {
|
2017-08-01 17:56:00 +02:00
|
|
|
recipient.setExpireMessages(expiration);
|
2016-08-16 05:23:56 +02:00
|
|
|
|
|
|
|
ContentValues values = new ContentValues(1);
|
|
|
|
values.put(EXPIRE_MESSAGES, expiration);
|
2017-08-08 00:31:12 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
2017-08-22 19:44:04 +02:00
|
|
|
recipient.resolve().setExpireMessages(expiration);
|
2017-08-08 00:31:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 11:13:10 +02:00
|
|
|
public void setUnidentifiedAccessMode(@NonNull Recipient recipient, @NonNull UnidentifiedAccessMode unidentifiedAccessMode) {
|
|
|
|
ContentValues values = new ContentValues(1);
|
|
|
|
values.put(UNIDENTIFIED_ACCESS_MODE, unidentifiedAccessMode.getMode());
|
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
|
|
|
recipient.resolve().setUnidentifiedAccessMode(unidentifiedAccessMode);
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
public void setProfileKey(@NonNull Recipient recipient, @Nullable byte[] profileKey) {
|
2017-08-15 03:11:13 +02:00
|
|
|
ContentValues values = new ContentValues(1);
|
|
|
|
values.put(PROFILE_KEY, profileKey == null ? null : Base64.encodeBytes(profileKey));
|
2017-08-22 19:44:04 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), values);
|
|
|
|
recipient.resolve().setProfileKey(profileKey);
|
2017-08-15 03:11:13 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
public void setProfileAvatar(@NonNull Recipient recipient, @Nullable String profileAvatar) {
|
2017-08-15 03:11:13 +02:00
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(SIGNAL_PROFILE_AVATAR, profileAvatar);
|
2017-08-22 19:44:04 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), contentValues);
|
|
|
|
recipient.resolve().setProfileAvatar(profileAvatar);
|
2017-08-15 03:11:13 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 07:16:15 +01:00
|
|
|
public void setProfileName(@NonNull Recipient recipient, @Nullable String profileName) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(SYSTEM_DISPLAY_NAME, profileName);
|
|
|
|
updateOrInsert(recipient.getAddress(), contentValues);
|
2021-04-30 08:00:33 +02:00
|
|
|
recipient.resolve().setName(profileName);
|
2021-03-01 07:16:15 +01:00
|
|
|
recipient.resolve().setProfileName(profileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProfileSharing(@NonNull Recipient recipient, boolean enabled) {
|
2017-08-17 06:49:41 +02:00
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(PROFILE_SHARING, enabled ? 1 : 0);
|
2017-08-22 19:44:04 +02:00
|
|
|
updateOrInsert(recipient.getAddress(), contentValues);
|
|
|
|
recipient.setProfileSharing(enabled);
|
2017-08-17 06:49:41 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 18:47:43 +02:00
|
|
|
public void setNotificationChannel(@NonNull Recipient recipient, @Nullable String notificationChannel) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(NOTIFICATION_CHANNEL, notificationChannel);
|
|
|
|
updateOrInsert(recipient.getAddress(), contentValues);
|
|
|
|
recipient.setNotificationChannel(notificationChannel);
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:44:04 +02:00
|
|
|
public void setRegistered(@NonNull Recipient recipient, RegisteredState registeredState) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(REGISTERED, registeredState.getId());
|
|
|
|
updateOrInsert(recipient.getAddress(), contentValues);
|
|
|
|
recipient.setRegistered(registeredState);
|
|
|
|
}
|
2017-08-07 23:24:53 +02:00
|
|
|
|
2017-08-08 00:31:12 +02:00
|
|
|
private void updateOrInsert(Address address, ContentValues contentValues) {
|
2015-06-09 16:37:20 +02:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
database.beginTransaction();
|
|
|
|
|
2017-08-01 17:56:00 +02:00
|
|
|
int updated = database.update(TABLE_NAME, contentValues, ADDRESS + " = ?",
|
2017-08-08 00:31:12 +02:00
|
|
|
new String[] {address.serialize()});
|
2015-06-09 16:37:20 +02:00
|
|
|
|
|
|
|
if (updated < 1) {
|
2017-08-08 00:31:12 +02:00
|
|
|
contentValues.put(ADDRESS, address.serialize());
|
2015-06-09 16:37:20 +02:00
|
|
|
database.insert(TABLE_NAME, null, contentValues);
|
|
|
|
}
|
2017-11-26 19:45:39 +01:00
|
|
|
|
|
|
|
database.setTransactionSuccessful();
|
|
|
|
database.endTransaction();
|
2017-08-16 04:23:42 +02:00
|
|
|
}
|
2015-06-09 16:37:20 +02:00
|
|
|
|
2018-08-16 18:47:43 +02:00
|
|
|
public static class RecipientReader implements Closeable {
|
2016-08-27 01:53:23 +02:00
|
|
|
|
|
|
|
private final Context context;
|
2018-08-16 18:47:43 +02:00
|
|
|
private final Cursor cursor;
|
2016-08-27 01:53:23 +02:00
|
|
|
|
2018-08-16 18:47:43 +02:00
|
|
|
RecipientReader(Context context, Cursor cursor) {
|
2016-08-27 01:53:23 +02:00
|
|
|
this.context = context;
|
|
|
|
this.cursor = cursor;
|
|
|
|
}
|
|
|
|
|
2017-08-01 17:56:00 +02:00
|
|
|
public @NonNull Recipient getCurrent() {
|
|
|
|
String serialized = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS));
|
2021-02-17 06:09:36 +01:00
|
|
|
return Recipient.from(context, Address.fromSerialized(serialized), false);
|
2016-08-27 01:53:23 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 17:56:00 +02:00
|
|
|
public @Nullable Recipient getNext() {
|
2018-09-26 00:41:42 +02:00
|
|
|
if (cursor != null && !cursor.moveToNext()) {
|
2016-08-27 01:53:23 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getCurrent();
|
|
|
|
}
|
2018-08-16 18:47:43 +02:00
|
|
|
|
|
|
|
public void close() {
|
|
|
|
cursor.close();
|
|
|
|
}
|
2016-08-27 01:53:23 +02:00
|
|
|
}
|
2015-06-09 16:37:20 +02:00
|
|
|
}
|