diff --git a/src/org/thoughtcrime/securesms/CreateProfileActivity.java b/src/org/thoughtcrime/securesms/CreateProfileActivity.java index d30324ae2a..a597f44a89 100644 --- a/src/org/thoughtcrime/securesms/CreateProfileActivity.java +++ b/src/org/thoughtcrime/securesms/CreateProfileActivity.java @@ -409,8 +409,23 @@ public class CreateProfileActivity extends BaseActionBarActivity implements Inje // ======== // 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); 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 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) { Log.w(TAG, e); return false; diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 6715feb5ee..9d63491677 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -136,6 +136,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiThreadDatabase.getCreatePublicChatTableCommand()); db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand()); db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand()); + db.execSQL(LokiUserDatabase.getCreateProfileAvatarUrlTableCommand()); executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS); diff --git a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt index a46307e48b..859d1d93a4 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt @@ -16,6 +16,7 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database companion object { // Shared private val displayName = "display_name" + private val profileAvatarUrl = "profile_avatar_url" // Display name cache private val displayNameTable = "loki_user_display_name_database" 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 serverID = "server_id" @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? { @@ -66,4 +70,27 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database 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() + } + */ } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index c6025a3ec7..00f334bc1a 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -121,6 +121,7 @@ public class TextSecurePreferences { 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_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 INCOGNITO_KEYBORAD_PREF = "pref_incognito_keyboard"; private static final String UNAUTHORIZED_RECEIVED = "pref_unauthorized_received"; @@ -401,6 +402,14 @@ public class TextSecurePreferences { 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) { return Integer.valueOf(getStringPreference(context, NOTIFICATION_PRIORITY_PREF, String.valueOf(NotificationCompat.PRIORITY_HIGH))); }