mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge pull request #1304 from deltachat/fix-selfavatar
Ask the core for the selfavatar directory, fix "backup export/import loses self-avatar"
This commit is contained in:
@@ -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<Void, Void, byte[]>() {
|
||||
@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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user