try to store avatar url locally

This commit is contained in:
ryanzhao 2019-11-19 17:04:42 +11:00 committed by Mikunj
parent 439d39e380
commit 5a4b619c9d
4 changed files with 52 additions and 0 deletions

View File

@ -409,8 +409,23 @@ public class CreateProfileActivity extends BaseActionBarActivity implements Inje
// ======== // ========
// accountManager.setProfileAvatar(profileKey, avatar); // accountManager.setProfileAvatar(profileKey, avatar);
// ======== // ========
//TODO: there is no need to upload the avatar again if there is no change
AvatarHelper.setAvatar(CreateProfileActivity.this, Address.fromSerialized(TextSecurePreferences.getLocalNumber(context)), avatarBytes); AvatarHelper.setAvatar(CreateProfileActivity.this, Address.fromSerialized(TextSecurePreferences.getLocalNumber(context)), avatarBytes);
TextSecurePreferences.setProfileAvatarId(CreateProfileActivity.this, new SecureRandom().nextInt()); TextSecurePreferences.setProfileAvatarId(CreateProfileActivity.this, new SecureRandom().nextInt());
//Loki - Upload the profile photo here
if (avatar != null) {
Log.d("Loki", "Start uploading profile photo");
LokiStorageAPI storageAPI = LokiStorageAPI.shared;
Triple<Long, String, byte[]> result = storageAPI.uploadProfilePhoto(storageAPI.getServer(), avatarBytes);
String url = result.component2();
Log.d("Loki", "Upload profile photo success, the url is " + url);
TextSecurePreferences.setProfileAvatarUrl(CreateProfileActivity.this, url);
}
else {
TextSecurePreferences.setProfileAvatarUrl(CreateProfileActivity.this, null);
}
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, e); Log.w(TAG, e);
return false; return false;

View File

@ -136,6 +136,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
db.execSQL(LokiThreadDatabase.getCreatePublicChatTableCommand()); db.execSQL(LokiThreadDatabase.getCreatePublicChatTableCommand());
db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand()); db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand());
db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand()); db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand());
db.execSQL(LokiUserDatabase.getCreateProfileAvatarUrlTableCommand());
executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, SmsDatabase.CREATE_INDEXS);
executeStatements(db, MmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS);

View File

@ -16,6 +16,7 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database
companion object { companion object {
// Shared // Shared
private val displayName = "display_name" private val displayName = "display_name"
private val profileAvatarUrl = "profile_avatar_url"
// Display name cache // Display name cache
private val displayNameTable = "loki_user_display_name_database" private val displayNameTable = "loki_user_display_name_database"
private val hexEncodedPublicKey = "hex_encoded_public_key" private val hexEncodedPublicKey = "hex_encoded_public_key"
@ -24,6 +25,9 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database
private val serverDisplayNameTable = "loki_user_server_display_name_database" private val serverDisplayNameTable = "loki_user_server_display_name_database"
private val serverID = "server_id" private val serverID = "server_id"
@JvmStatic val createServerDisplayNameTableCommand = "CREATE TABLE $serverDisplayNameTable ($hexEncodedPublicKey TEXT, $serverID TEXT, $displayName TEXT, PRIMARY KEY ($hexEncodedPublicKey, $serverID));" @JvmStatic val createServerDisplayNameTableCommand = "CREATE TABLE $serverDisplayNameTable ($hexEncodedPublicKey TEXT, $serverID TEXT, $displayName TEXT, PRIMARY KEY ($hexEncodedPublicKey, $serverID));"
// Profile Avatar URL cache
private val profileAvatarUrlTable = "loki_user_profile_avatar_url_database"
@JvmStatic val createProfileAvatarUrlTableCommand = "CREATE TABLE $profileAvatarUrlTable ($hexEncodedPublicKey TEXT PRIMARY KEY, $profileAvatarUrl TEXT);"
} }
override fun getDisplayName(hexEncodedPublicKey: String): String? { override fun getDisplayName(hexEncodedPublicKey: String): String? {
@ -66,4 +70,27 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database
Log.d("Loki", "Couldn't save server display name due to exception: $e.") Log.d("Loki", "Couldn't save server display name due to exception: $e.")
} }
} }
override fun getProfileAvatarUrl(hexEncodedPublicKey: String): String? {
if (hexEncodedPublicKey == TextSecurePreferences.getLocalNumber(context)) {
return TextSecurePreferences.getProfileAvatarUrl(context)
} else {
val database = databaseHelper.readableDatabase
return database.get(profileAvatarUrlTable, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { cursor ->
cursor.getString(cursor.getColumnIndexOrThrow(profileAvatarUrl))
}
}
}
//TODO figure out what to do with Recipient
/*
fun setProfileAvatarUrl(hexEncodedPublicKey: String, profileAvatarUrl: String) {
val database = databaseHelper.writableDatabase
val row = ContentValues(2)
row.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
row.put(Companion.profileAvatarUrl, profileAvatarUrl)
database.insertOrUpdate(profileAvatarUrlTable, row, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey ))
Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).notifyListeners()
}
*/
} }

View File

@ -121,6 +121,7 @@ public class TextSecurePreferences {
private static final String PROFILE_KEY_PREF = "pref_profile_key"; private static final String PROFILE_KEY_PREF = "pref_profile_key";
private static final String PROFILE_NAME_PREF = "pref_profile_name"; private static final String PROFILE_NAME_PREF = "pref_profile_name";
private static final String PROFILE_AVATAR_ID_PREF = "pref_profile_avatar_id"; private static final String PROFILE_AVATAR_ID_PREF = "pref_profile_avatar_id";
private static final String PROFILE_AVATAR_URL_PREF = "pref_profile_avatar_url";
public static final String READ_RECEIPTS_PREF = "pref_read_receipts"; public static final String READ_RECEIPTS_PREF = "pref_read_receipts";
public static final String INCOGNITO_KEYBORAD_PREF = "pref_incognito_keyboard"; public static final String INCOGNITO_KEYBORAD_PREF = "pref_incognito_keyboard";
private static final String UNAUTHORIZED_RECEIVED = "pref_unauthorized_received"; private static final String UNAUTHORIZED_RECEIVED = "pref_unauthorized_received";
@ -401,6 +402,14 @@ public class TextSecurePreferences {
return getIntegerPreference(context, PROFILE_AVATAR_ID_PREF, 0); return getIntegerPreference(context, PROFILE_AVATAR_ID_PREF, 0);
} }
public static void setProfileAvatarUrl(Context context, String url) {
setStringPreference(context, PROFILE_AVATAR_URL_PREF, url);
}
public static String getProfileAvatarUrl(Context context) {
return getStringPreference(context, PROFILE_AVATAR_URL_PREF, null);
}
public static int getNotificationPriority(Context context) { public static int getNotificationPriority(Context context) {
return Integer.valueOf(getStringPreference(context, NOTIFICATION_PRIORITY_PREF, String.valueOf(NotificationCompat.PRIORITY_HIGH))); return Integer.valueOf(getStringPreference(context, NOTIFICATION_PRIORITY_PREF, String.valueOf(NotificationCompat.PRIORITY_HIGH)));
} }