From d7d5faaf6447690431cefbe8fb1f6d569ec922d4 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Wed, 22 Apr 2020 14:36:21 +0200 Subject: [PATCH 1/6] Ask the core for the selfavatar directory, fix #1381 backup export/import loses self-avatar --- src/org/thoughtcrime/securesms/profiles/AvatarHelper.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java index 19a9b81a2..a3b66afe8 100644 --- a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java +++ b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java @@ -72,9 +72,8 @@ public class AvatarHelper { } public static File getSelfAvatarFile(@NonNull Context context, @NonNull String address) { - File avatarDirectory = new File(context.getFilesDir(), AVATAR_DIRECTORY); - avatarDirectory.mkdirs(); - return new File(avatarDirectory, address+".jpg"); + String dirString = DcHelper.getContext(context).getConfig(DcHelper.CONFIG_SELF_AVATAR); + return new File(dirString); } public static void setSelfAvatar(@NonNull Context context, @NonNull String address, @Nullable byte[] data) throws IOException { From c8351d1dd233ce8efdd672cc218fa1eb916ba5ff Mon Sep 17 00:00:00 2001 From: Hocuri Date: Wed, 22 Apr 2020 15:05:38 +0200 Subject: [PATCH 2/6] Code style --- .../securesms/CreateProfileActivity.java | 4 ++-- .../securesms/profiles/AvatarHelper.java | 13 ++++--------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/org/thoughtcrime/securesms/CreateProfileActivity.java b/src/org/thoughtcrime/securesms/CreateProfileActivity.java index b45988527..f3b4bba3a 100644 --- a/src/org/thoughtcrime/securesms/CreateProfileActivity.java +++ b/src/org/thoughtcrime/securesms/CreateProfileActivity.java @@ -259,12 +259,12 @@ public class CreateProfileActivity extends BaseActionBarActivity { private void initializeProfileAvatar() { String address = DcHelper.get(this, DcHelper.CONFIG_ADDRESS); - if (AvatarHelper.getSelfAvatarFile(this, address).exists() && AvatarHelper.getSelfAvatarFile(this, address).length() > 0) { + if (AvatarHelper.getSelfAvatarFile(this).exists() && AvatarHelper.getSelfAvatarFile(this).length() > 0) { new AsyncTask() { @Override protected byte[] doInBackground(Void... params) { try { - return Util.readFully(AvatarHelper.getInputStreamFor(CreateProfileActivity.this, address)); + return Util.readFully(AvatarHelper.getInputStreamFor(CreateProfileActivity.this)); } catch (IOException e) { Log.w(TAG, e); return null; diff --git a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java index a3b66afe8..6246aef10 100644 --- a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java +++ b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java @@ -61,23 +61,18 @@ public class AvatarHelper { } } - public static InputStream getInputStreamFor(@NonNull Context context, @NonNull String address) + public static InputStream getInputStreamFor(@NonNull Context context) throws IOException { - return new FileInputStream(getSelfAvatarFile(context, address)); + return new FileInputStream(getSelfAvatarFile(context)); } - public static File getSelfAvatarFile(@NonNull Context context, @NonNull Address address) { - String name = new File(address.serialize()).getName(); - return getSelfAvatarFile(context, name); - } - - public static File getSelfAvatarFile(@NonNull Context context, @NonNull String address) { + public static File getSelfAvatarFile(@NonNull Context context) { String dirString = DcHelper.getContext(context).getConfig(DcHelper.CONFIG_SELF_AVATAR); return new File(dirString); } public static void setSelfAvatar(@NonNull Context context, @NonNull String address, @Nullable byte[] data) throws IOException { - File avatar = getSelfAvatarFile(context, address); + File avatar = getSelfAvatarFile(context); if (data == null) { avatar.delete(); DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, null); From bf6cfc552fe51b639c0e31b88c755b20cb185f55 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Wed, 22 Apr 2020 15:23:59 +0200 Subject: [PATCH 3/6] Write self avatar to temp file when changing it --- .../securesms/contacts/avatars/LocalFileContactPhoto.java | 2 +- .../securesms/contacts/avatars/MyProfileContactPhoto.java | 5 +++-- src/org/thoughtcrime/securesms/profiles/AvatarHelper.java | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/contacts/avatars/LocalFileContactPhoto.java b/src/org/thoughtcrime/securesms/contacts/avatars/LocalFileContactPhoto.java index 81b70240a..1fff9374b 100644 --- a/src/org/thoughtcrime/securesms/contacts/avatars/LocalFileContactPhoto.java +++ b/src/org/thoughtcrime/securesms/contacts/avatars/LocalFileContactPhoto.java @@ -42,7 +42,7 @@ public abstract class LocalFileContactPhoto implements ContactPhoto { @Override public @Nullable Uri getUri(@NonNull Context context) { - return isProfilePhoto() ? Uri.fromFile(AvatarHelper.getSelfAvatarFile(context, address)) : null; + return isProfilePhoto() ? Uri.fromFile(AvatarHelper.getSelfAvatarFile(context)) : null; } @Override diff --git a/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java b/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java index 0a093b32b..5f3311c6b 100644 --- a/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java +++ b/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java @@ -24,13 +24,14 @@ public class MyProfileContactPhoto implements ContactPhoto { @Override public InputStream openInputStream(Context context) throws IOException { - return AvatarHelper.getInputStreamFor(context, address); + return AvatarHelper.getInputStreamFor(context); } @Override public @Nullable Uri getUri(@NonNull Context context) { - return Uri.fromFile(AvatarHelper.getSelfAvatarFile(context, address)); + + return Uri.fromFile(AvatarHelper.getSelfAvatarFile(context)); } @Override diff --git a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java index 6246aef10..9f55a2544 100644 --- a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java +++ b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java @@ -72,11 +72,10 @@ public class AvatarHelper { } public static void setSelfAvatar(@NonNull Context context, @NonNull String address, @Nullable byte[] data) throws IOException { - File avatar = getSelfAvatarFile(context); if (data == null) { - avatar.delete(); DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, null); } else { + File avatar = File.createTempFile("prefix", "extension", context.getCacheDir()); FileOutputStream out = new FileOutputStream(avatar); if (data != null) { out.write(data); @@ -84,6 +83,7 @@ public class AvatarHelper { out.close(); String path = avatar.getPath(); DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, path); + avatar.delete(); } } } From d98a509779adc1a54d325da6a11f80664753a7a6 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Wed, 22 Apr 2020 15:45:46 +0200 Subject: [PATCH 4/6] Inline getInputStreamFor --- src/org/thoughtcrime/securesms/CreateProfileActivity.java | 3 ++- .../securesms/contacts/avatars/MyProfileContactPhoto.java | 4 ++-- src/org/thoughtcrime/securesms/profiles/AvatarHelper.java | 5 ----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/org/thoughtcrime/securesms/CreateProfileActivity.java b/src/org/thoughtcrime/securesms/CreateProfileActivity.java index f3b4bba3a..e77ad2d42 100644 --- a/src/org/thoughtcrime/securesms/CreateProfileActivity.java +++ b/src/org/thoughtcrime/securesms/CreateProfileActivity.java @@ -48,6 +48,7 @@ import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.ViewUtil; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.security.SecureRandom; import java.util.LinkedList; @@ -264,7 +265,7 @@ public class CreateProfileActivity extends BaseActionBarActivity { @Override protected byte[] doInBackground(Void... params) { try { - return Util.readFully(AvatarHelper.getInputStreamFor(CreateProfileActivity.this)); + return Util.readFully(new FileInputStream(AvatarHelper.getSelfAvatarFile(CreateProfileActivity.this))); } catch (IOException e) { Log.w(TAG, e); return null; diff --git a/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java b/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java index 5f3311c6b..8b3155f00 100644 --- a/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java +++ b/src/org/thoughtcrime/securesms/contacts/avatars/MyProfileContactPhoto.java @@ -8,6 +8,7 @@ import androidx.annotation.Nullable; import org.thoughtcrime.securesms.profiles.AvatarHelper; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; @@ -24,13 +25,12 @@ public class MyProfileContactPhoto implements ContactPhoto { @Override public InputStream openInputStream(Context context) throws IOException { - return AvatarHelper.getInputStreamFor(context); + return new FileInputStream(AvatarHelper.getSelfAvatarFile(context)); } @Override public @Nullable Uri getUri(@NonNull Context context) { - return Uri.fromFile(AvatarHelper.getSelfAvatarFile(context)); } diff --git a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java index 9f55a2544..2bc3bda94 100644 --- a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java +++ b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java @@ -61,11 +61,6 @@ public class AvatarHelper { } } - public static InputStream getInputStreamFor(@NonNull Context context) - throws IOException { - return new FileInputStream(getSelfAvatarFile(context)); - } - public static File getSelfAvatarFile(@NonNull Context context) { String dirString = DcHelper.getContext(context).getConfig(DcHelper.CONFIG_SELF_AVATAR); return new File(dirString); From 80133f82ee3beffa86759b4fcf6cb951abad59ea Mon Sep 17 00:00:00 2001 From: Hocuri Date: Thu, 23 Apr 2020 15:50:07 +0200 Subject: [PATCH 5/6] Also let the core care about group avatars --- .../securesms/profiles/AvatarHelper.java | 66 ++++++------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java index 2bc3bda94..acd0fd0dc 100644 --- a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java +++ b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java @@ -1,64 +1,40 @@ package org.thoughtcrime.securesms.profiles; -import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.b44t.messenger.DcContext; import org.thoughtcrime.securesms.connect.DcHelper; -import org.thoughtcrime.securesms.database.Address; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; public class AvatarHelper { - private static final String GROUP_TEMPLATE = "group_chat_avatar_%s_%o.jpg"; - - private static final String AVATAR_DIRECTORY = "avatars"; - - @SuppressLint("DefaultLocale") - private static String getFilePathForGroupAvatar(Context context, int chatId, long timestamp) { - return getAvatarDirectoryPath(context) + String.format(GROUP_TEMPLATE, chatId, timestamp); - } - - @NonNull - private static String getAvatarDirectoryPath(Context context) { - return context.getFilesDir().getAbsolutePath() + File.separator + AVATAR_DIRECTORY + File.separator; - } - public static void setGroupAvatar(Context context, int chatId, Bitmap bitmap) { - if (bitmap == null) { - return; - } - //noinspection ResultOfMethodCallIgnored - new File(getAvatarDirectoryPath(context)).mkdirs(); DcContext dcContext = DcHelper.getContext(context); - String avatarPath = dcContext.getChat(chatId).getProfileImage(); - if (avatarPath != null && !avatarPath.isEmpty()) { - File oldImage = new File(avatarPath); - if (oldImage.exists()) { + + if (bitmap == null) { + dcContext.setChatProfileImage(chatId, null); + } else { + try { + File avatar = File.createTempFile("groupavatar", "jpg", context.getCacheDir()); + FileOutputStream out = new FileOutputStream(avatar); + bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out); + out.close(); + dcContext.setChatProfileImage(chatId, avatar.getPath()); // The avatar is copied to the blobs directory here... //noinspection ResultOfMethodCallIgnored - oldImage.delete(); + avatar.delete(); // ..., now we can delete it. + } catch (IOException e) { + e.printStackTrace(); } } - avatarPath = getFilePathForGroupAvatar(context, chatId, System.currentTimeMillis()); - FileOutputStream outStream; - try { - outStream = new FileOutputStream(avatarPath); - bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outStream); - dcContext.setChatProfileImage(chatId, avatarPath); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } } public static File getSelfAvatarFile(@NonNull Context context) { @@ -66,19 +42,17 @@ public class AvatarHelper { return new File(dirString); } - public static void setSelfAvatar(@NonNull Context context, @NonNull String address, @Nullable byte[] data) throws IOException { + public static void setSelfAvatar(@NonNull Context context, @Nullable byte[] data) throws IOException { if (data == null) { DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, null); } else { - File avatar = File.createTempFile("prefix", "extension", context.getCacheDir()); + File avatar = File.createTempFile("selfavatar", "jpg", context.getCacheDir()); FileOutputStream out = new FileOutputStream(avatar); - if (data != null) { - out.write(data); - } + out.write(data); out.close(); - String path = avatar.getPath(); - DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, path); - avatar.delete(); + DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, avatar.getPath()); // The avatar is copied to the blobs directory here... + //noinspection ResultOfMethodCallIgnored + avatar.delete(); // ..., now we can delete it. } } } From 7f49746f5326f9af1c7cfd294e0abe4896680210 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Thu, 23 Apr 2020 19:37:10 +0200 Subject: [PATCH 6/6] fix compile error & name of temp file --- src/org/thoughtcrime/securesms/CreateProfileActivity.java | 2 +- src/org/thoughtcrime/securesms/profiles/AvatarHelper.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/org/thoughtcrime/securesms/CreateProfileActivity.java b/src/org/thoughtcrime/securesms/CreateProfileActivity.java index e77ad2d42..0d2e1be05 100644 --- a/src/org/thoughtcrime/securesms/CreateProfileActivity.java +++ b/src/org/thoughtcrime/securesms/CreateProfileActivity.java @@ -375,7 +375,7 @@ public class CreateProfileActivity extends BaseActionBarActivity { setStatusText(); try { - AvatarHelper.setSelfAvatar(CreateProfileActivity.this, DcHelper.get(context, DcHelper.CONFIG_ADDRESS), avatarBytes); + AvatarHelper.setSelfAvatar(CreateProfileActivity.this, avatarBytes); Prefs.setProfileAvatarId(CreateProfileActivity.this, new SecureRandom().nextInt()); } catch (IOException e) { Log.w(TAG, e); diff --git a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java index acd0fd0dc..911bc4e74 100644 --- a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java +++ b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java @@ -24,7 +24,7 @@ public class AvatarHelper { dcContext.setChatProfileImage(chatId, null); } else { try { - File avatar = File.createTempFile("groupavatar", "jpg", context.getCacheDir()); + File avatar = File.createTempFile("groupavatar", ".jpg", context.getCacheDir()); FileOutputStream out = new FileOutputStream(avatar); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out); out.close(); @@ -46,7 +46,7 @@ public class AvatarHelper { if (data == null) { DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, null); } else { - File avatar = File.createTempFile("selfavatar", "jpg", context.getCacheDir()); + File avatar = File.createTempFile("selfavatar", ".jpg", context.getCacheDir()); FileOutputStream out = new FileOutputStream(avatar); out.write(data); out.close();