Make account settings (name and image) usable #14

This commit is contained in:
daniel.boehrs
2018-09-28 10:27:21 +02:00
parent a64ebdee04
commit 75971f7320
6 changed files with 67 additions and 52 deletions
@@ -36,9 +36,9 @@ import com.soundcloud.android.crop.Crop;
import org.thoughtcrime.securesms.components.InputAwareLayout;
import org.thoughtcrime.securesms.components.emoji.EmojiDrawer;
import org.thoughtcrime.securesms.components.emoji.EmojiToggle;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto;
import org.thoughtcrime.securesms.crypto.ProfileKeyUtil;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.dependencies.InjectableType;
import org.thoughtcrime.securesms.jobs.MultiDeviceProfileKeyUpdateJob;
import org.thoughtcrime.securesms.mms.GlideApp;
@@ -287,14 +287,14 @@ public class CreateProfileActivity extends BaseActionBarActivity implements Inje
}
private void initializeProfileAvatar(boolean excludeSystem) {
Address ourAddress = Address.fromSerialized(TextSecurePreferences.getLocalNumber(this));
String address = DcHelper.get(this, DcHelper.CONFIG_ADDRESS);
if (AvatarHelper.getAvatarFile(this, ourAddress).exists() && AvatarHelper.getAvatarFile(this, ourAddress).length() > 0) {
if (AvatarHelper.getAvatarFile(this, address).exists() && AvatarHelper.getAvatarFile(this, address).length() > 0) {
new AsyncTask<Void, Void, byte[]>() {
@Override
protected byte[] doInBackground(Void... params) {
try {
return Util.readFully(AvatarHelper.getInputStreamFor(CreateProfileActivity.this, ourAddress));
return Util.readFully(AvatarHelper.getInputStreamFor(CreateProfileActivity.this, address));
} catch (IOException e) {
Log.w(TAG, e);
return null;
@@ -429,18 +429,11 @@ public class CreateProfileActivity extends BaseActionBarActivity implements Inje
protected Boolean doInBackground(Void... params) {
Context context = CreateProfileActivity.this;
byte[] profileKey = ProfileKeyUtil.getProfileKey(CreateProfileActivity.this);
DcHelper.set(context, DcHelper.CONFIG_DISPLAY_NAME, name);
TextSecurePreferences.setProfileName(context, name);
try {
accountManager.setProfileName(profileKey, name);
TextSecurePreferences.setProfileName(context, name);
} catch (IOException e) {
Log.w(TAG, e);
return false;
}
try {
accountManager.setProfileAvatar(profileKey, avatar);
AvatarHelper.setAvatar(CreateProfileActivity.this, Address.fromSerialized(TextSecurePreferences.getLocalNumber(context)), avatarBytes);
AvatarHelper.setAvatar(CreateProfileActivity.this, DcHelper.get(context, DcHelper.CONFIG_ADDRESS), avatarBytes);
TextSecurePreferences.setProfileAvatarId(CreateProfileActivity.this, new SecureRandom().nextInt());
} catch (IOException e) {
Log.w(TAG, e);
@@ -8,19 +8,19 @@ import org.thoughtcrime.securesms.ApplicationContext;
public class DcHelper {
private static final String CONFIG_ADDRESS = "addr";
private static final String CONFIG_MAIL_SERVER = "mail_server";
private static final String CONFIG_MAIL_USER = "mail_user";
private static final String CONFIG_MAIL_PASSWORD = "mail_pw";
private static final String CONFIG_MAIL_PORT = "mail_port";
private static final String CONFIG_SEND_SERVER = "send_server";
private static final String CONFIG_SEND_USER = "send_user";
private static final String CONFIG_SEND_PASSWORD = "send_pw";
private static final String CONFIG_SEND_PORT = "send_port";
private static final String CONFIG_SERVER_FLAGS = "server_flags";
private static final String CONFIG_DISPLAY_NAME = "displayname";
private static final String CONFIG_SELF_STATUS = "selfstatus";
private static final String CONFIG_E2EE_ENABLED = "e2ee_enabled";
public static final String CONFIG_ADDRESS = "addr";
public static final String CONFIG_MAIL_SERVER = "mail_server";
public static final String CONFIG_MAIL_USER = "mail_user";
public static final String CONFIG_MAIL_PASSWORD = "mail_pw";
public static final String CONFIG_MAIL_PORT = "mail_port";
public static final String CONFIG_SEND_SERVER = "send_server";
public static final String CONFIG_SEND_USER = "send_user";
public static final String CONFIG_SEND_PASSWORD = "send_pw";
public static final String CONFIG_SEND_PORT = "send_port";
public static final String CONFIG_SERVER_FLAGS = "server_flags";
public static final String CONFIG_DISPLAY_NAME = "displayname";
public static final String CONFIG_SELF_STATUS = "selfstatus";
public static final String CONFIG_E2EE_ENABLED = "e2ee_enabled";
public static ApplicationDcContext getContext(Context context) {
return ApplicationContext.getInstance(context).dcContext;
@@ -31,19 +31,14 @@ public class DcHelper {
return dcContext.isConfigured() == 1;
}
public static String getAccountAddress(Context context) {
public static String get(Context context, String key) {
DcContext dcContext = getContext(context);
return dcContext.getConfig(CONFIG_ADDRESS, "");
return dcContext.getConfig(key, "");
}
public static String getAccountName(Context context) {
public static void set(Context context, String key, String value) {
DcContext dcContext = getContext(context);
return dcContext.getConfig(CONFIG_DISPLAY_NAME, "");
}
public static String getAccountStatus(Context context) {
DcContext dcContext = getContext(context);
return dcContext.getConfig(CONFIG_SELF_STATUS, "");
dcContext.setConfig(key, value);
}
}
@@ -6,9 +6,7 @@ import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.profiles.AvatarHelper;
import org.thoughtcrime.securesms.util.Conversions;
import java.io.IOException;
import java.io.InputStream;
@@ -16,10 +14,10 @@ import java.security.MessageDigest;
public class ProfileContactPhoto implements ContactPhoto {
private final @NonNull Address address;
private final @NonNull String address;
private final @NonNull String avatarObject;
public ProfileContactPhoto(@NonNull Address address, @NonNull String avatarObject) {
public ProfileContactPhoto(@NonNull String address, @NonNull String avatarObject) {
this.address = address;
this.avatarObject = avatarObject;
}
@@ -41,7 +39,7 @@ public class ProfileContactPhoto implements ContactPhoto {
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
messageDigest.update(address.serialize().getBytes());
messageDigest.update(address.getBytes());
messageDigest.update(avatarObject.getBytes());
}
@@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.preferences.widgets;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.preference.Preference;
@@ -16,8 +15,10 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.contacts.avatars.ProfileContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto;
import org.thoughtcrime.securesms.mms.GlideApp;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
public class ProfilePreference extends Preference {
@@ -63,9 +64,9 @@ public class ProfilePreference extends Preference {
public void refresh() {
if (profileAddressView == null) return;
final String address = DcHelper.getAccountAddress(getContext());
final String profileName = DcHelper.getAccountName(getContext());
final Bitmap profileImage = null;
final String address = DcHelper.get(getContext(), DcHelper.CONFIG_ADDRESS);
final String profileName = DcHelper.get(getContext(), DcHelper.CONFIG_DISPLAY_NAME);
final ProfileContactPhoto profileImage = new ProfileContactPhoto(address, String.valueOf(TextSecurePreferences.getProfileAvatarId(getContext())));
GlideApp.with(getContext().getApplicationContext())
.load(profileImage)
@@ -27,6 +27,12 @@ public class AvatarHelper {
return new FileInputStream(getAvatarFile(context, address));
}
public static InputStream getInputStreamFor(@NonNull Context context, @NonNull String address)
throws IOException
{
return new FileInputStream(getAvatarFile(context, address));
}
public static List<File> getAvatarFiles(@NonNull Context context) {
File avatarDirectory = new File(context.getFilesDir(), AVATAR_DIRECTORY);
File[] results = avatarDirectory.listFiles();
@@ -36,14 +42,22 @@ public class AvatarHelper {
}
public static void delete(@NonNull Context context, @NonNull Address address) {
getAvatarFile(context, address).delete();
delete(getAvatarFile(context, address));
}
public static void delete(@NonNull File avatar) {
avatar.delete();
}
public static @NonNull File getAvatarFile(@NonNull Context context, @NonNull Address address) {
String name = new File(address.serialize()).getName();
return getAvatarFile(context, name);
}
public static @NonNull File getAvatarFile(@NonNull Context context, @NonNull String address) {
File avatarDirectory = new File(context.getFilesDir(), AVATAR_DIRECTORY);
avatarDirectory.mkdirs();
return new File(avatarDirectory, new File(address.serialize()).getName());
return new File(avatarDirectory, address);
}
public static void setAvatar(@NonNull Context context, @NonNull Address address, @Nullable byte[] data)
@@ -52,10 +66,24 @@ public class AvatarHelper {
if (data == null) {
delete(context, address);
} else {
FileOutputStream out = new FileOutputStream(getAvatarFile(context, address));
out.write(data);
out.close();
File avatar = getAvatarFile(context, address);
writeAvatarFile(avatar, data);
}
}
public static void setAvatar(@NonNull Context context, @NonNull String address, @Nullable byte[] data) throws IOException {
File avatar = getAvatarFile(context, address);
if (data == null) {
delete(avatar);
} else {
writeAvatarFile(avatar, data);
}
}
private static void writeAvatarFile(@NonNull File avatar, @Nullable byte[] data) throws IOException {
FileOutputStream out = new FileOutputStream(avatar);
out.write(data);
out.close();
}
}
@@ -440,7 +440,7 @@ public class Recipient implements RecipientModifiedListener {
public synchronized @Nullable ContactPhoto getContactPhoto() {
if (isGroupRecipient() && groupAvatarId != null) return new GroupRecordContactPhoto(address, groupAvatarId);
else if (systemContactPhoto != null) return new SystemContactPhoto(address, systemContactPhoto, 0);
else if (profileAvatar != null) return new ProfileContactPhoto(address, profileAvatar);
else if (profileAvatar != null) return new ProfileContactPhoto(address.toEmailString(), profileAvatar);
else return null;
}