store avatars in cache folder

This commit is contained in:
Daniel Gultsch 2020-12-10 19:05:04 +01:00
parent 07e965f8f3
commit f584179f2f
6 changed files with 619 additions and 615 deletions

View File

@ -430,20 +430,18 @@ public class Contact implements ListItem, Blockable {
return getJid().getDomain().toEscapedString();
}
public boolean setAvatar(Avatar avatar) {
return setAvatar(avatar, false);
public void setAvatar(Avatar avatar) {
setAvatar(avatar, false);
}
public boolean setAvatar(Avatar avatar, boolean previouslyOmittedPepFetch) {
public void setAvatar(Avatar avatar, boolean previouslyOmittedPepFetch) {
if (this.avatar != null && this.avatar.equals(avatar)) {
return false;
} else {
return;
}
if (!previouslyOmittedPepFetch && this.avatar != null && this.avatar.origin == Avatar.Origin.PEP && avatar.origin == Avatar.Origin.VCARD) {
return false;
return;
}
this.avatar = avatar;
return true;
}
}
public String getAvatarFilename() {

View File

@ -214,14 +214,13 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi();
} else {
Contact contact = account.getRoster().getContact(from);
if (contact.setAvatar(avatar)) {
final Contact contact = account.getRoster().getContact(from);
contact.setAvatar(avatar);
mXmppConnectionService.syncRoster(account);
mXmppConnectionService.getAvatarService().clear(contact);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateRosterUi();
}
}
} else if (mXmppConnectionService.isDataSaverDisabled()) {
mXmppConnectionService.fetchAvatar(account, avatar);
}

View File

@ -118,13 +118,12 @@ public class PresenceParser extends AbstractParser implements
mXmppConnectionService.getAvatarService().clear(user);
}
if (user.getRealJid() != null) {
Contact c = conversation.getAccount().getRoster().getContact(user.getRealJid());
if (c.setAvatar(avatar)) {
final Contact c = conversation.getAccount().getRoster().getContact(user.getRealJid());
c.setAvatar(avatar);
mXmppConnectionService.syncRoster(conversation.getAccount());
mXmppConnectionService.getAvatarService().clear(c);
mXmppConnectionService.updateRosterUi();
}
}
} else if (mXmppConnectionService.isDataSaverDisabled()) {
mXmppConnectionService.fetchAvatar(mucOptions.getAccount(), avatar);
}
@ -268,7 +267,8 @@ public class PresenceParser extends AbstractParser implements
mXmppConnectionService.getAvatarService().clear(account);
mXmppConnectionService.updateConversationUi();
mXmppConnectionService.updateAccountUi();
} else if (contact.setAvatar(avatar)) {
} else {
contact.setAvatar(avatar);
mXmppConnectionService.syncRoster(account);
mXmppConnectionService.getAvatarService().clear(contact);
mXmppConnectionService.updateConversationUi();

View File

@ -1071,7 +1071,7 @@ public class FileBackend {
return null;
}
Avatar avatar = new Avatar();
File file = new File(getAvatarPath(hash));
final File file = getAvatarFile(hash);
FileInputStream is = null;
try {
avatar.size = file.length();
@ -1104,14 +1104,14 @@ public class FileBackend {
}
public boolean isAvatarCached(Avatar avatar) {
File file = new File(getAvatarPath(avatar.getFilename()));
final File file = getAvatarFile(avatar.getFilename());
return file.exists();
}
public boolean save(final Avatar avatar) {
File file;
if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename()));
file = getAvatarFile(avatar.getFilename());
avatar.size = file.length();
} else {
file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString());
@ -1133,12 +1133,12 @@ public class FileBackend {
mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest());
if (sha1sum.equals(avatar.sha1sum)) {
File outputFile = new File(getAvatarPath(avatar.getFilename()));
final File outputFile = getAvatarFile(avatar.getFilename());
if (outputFile.getParentFile().mkdirs()) {
Log.d(Config.LOGTAG, "created avatar directory");
}
String filename = getAvatarPath(avatar.getFilename());
if (!file.renameTo(new File(filename))) {
final File avatarFile = getAvatarFile(avatar.getFilename());
if (!file.renameTo(avatarFile)) {
Log.d(Config.LOGTAG, "unable to rename " + file.getAbsolutePath() + " to " + outputFile);
return false;
}
@ -1159,12 +1159,34 @@ public class FileBackend {
return true;
}
private String getAvatarPath(String avatar) {
return mXmppConnectionService.getFilesDir().getAbsolutePath() + "/avatars/" + avatar;
public void deleteHistoricAvatarPath() {
delete(getHistoricAvatarPath());
}
private void delete(final File file) {
if (file.isDirectory()) {
final File[] files = file.listFiles();
if (files != null) {
for (final File f : files) {
delete(f);
}
}
}
if (file.delete()) {
Log.d(Config.LOGTAG,"deleted "+file.getAbsolutePath());
}
}
private File getHistoricAvatarPath() {
return new File(mXmppConnectionService.getFilesDir(), "/avatars/");
}
private File getAvatarFile(String avatar) {
return new File(mXmppConnectionService.getCacheDir(), "/avatars/" + avatar);
}
public Uri getAvatarUri(String avatar) {
return Uri.parse("file:" + getAvatarPath(avatar));
return Uri.fromFile(getAvatarFile(avatar));
}
public Bitmap cropCenterSquare(Uri image, int size) {

View File

@ -59,7 +59,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private static final String CHANNEL_SYMBOL = "#";
final private ArrayList<Integer> sizes = new ArrayList<>();
final private Set<Integer> sizes = new HashSet<>();
final private HashMap<String, Set<String>> conversationDependentKeys = new HashMap<>();
protected XmppConnectionService mXmppConnectionService = null;
@ -224,9 +224,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
public void clear(Contact contact) {
synchronized (this.sizes) {
for (Integer size : sizes) {
this.mXmppConnectionService.getBitmapCache().remove(
key(contact, size));
for (final Integer size : sizes) {
this.mXmppConnectionService.getBitmapCache().remove(key(contact, size));
}
}
for (Conversation conversation : mXmppConnectionService.findAllConferencesWith(contact)) {
@ -240,10 +239,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private String key(Contact contact, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_CONTACT +
'\0' +
contact.getAccount().getJid().asBareJid() +
@ -255,10 +252,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private String key(MucOptions.User user, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_CONTACT +
'\0' +
user.getAccount().getJid().asBareJid() +
@ -416,12 +411,9 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private String key(final MucOptions options, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
+ "_" + String.valueOf(size);
return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid() + "_" + size;
}
private String key(List<MucOptions.User> users, int size) {
@ -524,10 +516,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private String key(Account account, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
+ String.valueOf(size);
}
@ -561,11 +551,9 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private String key(String name, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
return PREFIX_GENERIC + "_" + name + "_" + size;
}
private static boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) {

View File

@ -1127,6 +1127,7 @@ public class XmppConnectionService extends Service {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
startContactObserver();
}
mFileAddingExecutor.execute(fileBackend::deleteHistoricAvatarPath);
if (Compatibility.hasStoragePermission(this)) {
Log.d(Config.LOGTAG, "starting file observer");
mFileAddingExecutor.execute(this.fileObserver::startWatching);
@ -3696,19 +3697,17 @@ public class XmppConnectionService extends Service {
updateConversationUi();
updateAccountUi();
} else {
Contact contact = a.getRoster().getContact(avatar.owner);
if (contact.setAvatar(avatar)) {
final Contact contact = a.getRoster().getContact(avatar.owner);
contact.setAvatar(avatar);
syncRoster(account);
getAvatarService().clear(contact);
updateConversationUi();
updateRosterUi();
}
}
if (callback != null) {
callback.success(avatar);
}
Log.d(Config.LOGTAG, a.getJid().asBareJid()
+ ": successfully fetched pep avatar for " + avatar.owner);
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully fetched pep avatar for " + avatar.owner);
return;
}
} else {
@ -3758,13 +3757,12 @@ public class XmppConnectionService extends Service {
getAvatarService().clear(account);
updateAccountUi();
} else {
Contact contact = account.getRoster().getContact(avatar.owner);
if (contact.setAvatar(avatar, previouslyOmittedPepFetch)) {
final Contact contact = account.getRoster().getContact(avatar.owner);
contact.setAvatar(avatar, previouslyOmittedPepFetch);
syncRoster(account);
getAvatarService().clear(contact);
updateRosterUi();
}
}
updateConversationUi();
} else {
Conversation conversation = find(account, avatar.owner.asBareJid());
@ -3778,7 +3776,7 @@ public class XmppConnectionService extends Service {
}
if (user.getRealJid() != null) {
Contact contact = account.getRoster().getContact(user.getRealJid());
if (contact.setAvatar(avatar)) {
contact.setAvatar(avatar);
syncRoster(account);
getAvatarService().clear(contact);
updateRosterUi();
@ -3790,7 +3788,6 @@ public class XmppConnectionService extends Service {
}
}
}
}
});
}