diff --git a/src/org/thoughtcrime/securesms/CreateProfileActivity.java b/src/org/thoughtcrime/securesms/CreateProfileActivity.java index b45988527..0d2e1be05 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; @@ -259,12 +260,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(new FileInputStream(AvatarHelper.getSelfAvatarFile(CreateProfileActivity.this))); } catch (IOException e) { Log.w(TAG, e); return null; @@ -374,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/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..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,13 @@ public class MyProfileContactPhoto implements ContactPhoto { @Override public InputStream openInputStream(Context context) throws IOException { - return AvatarHelper.getInputStreamFor(context, address); + return new FileInputStream(AvatarHelper.getSelfAvatarFile(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 19a9b81a2..911bc4e74 100644 --- a/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java +++ b/src/org/thoughtcrime/securesms/profiles/AvatarHelper.java @@ -1,95 +1,58 @@ 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 InputStream getInputStreamFor(@NonNull Context context, @NonNull String address) - throws IOException { - return new FileInputStream(getSelfAvatarFile(context, address)); + public static File getSelfAvatarFile(@NonNull Context context) { + String dirString = DcHelper.getContext(context).getConfig(DcHelper.CONFIG_SELF_AVATAR); + return new File(dirString); } - 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) { - File avatarDirectory = new File(context.getFilesDir(), AVATAR_DIRECTORY); - avatarDirectory.mkdirs(); - return new File(avatarDirectory, address+".jpg"); - } - - public static void setSelfAvatar(@NonNull Context context, @NonNull String address, @Nullable byte[] data) throws IOException { - File avatar = getSelfAvatarFile(context, address); + public static void setSelfAvatar(@NonNull Context context, @Nullable byte[] data) throws IOException { if (data == null) { - avatar.delete(); DcHelper.set(context, DcHelper.CONFIG_SELF_AVATAR, null); } else { + 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); + 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. } } }