mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Load avatars in working thread, cache avatars, reload avatars after pause (they may have been changed in another app then).
This commit is contained in:
@@ -1,10 +1,29 @@
|
||||
/*
|
||||
* This is the source code of Telegram for Android v. 1.3.x.
|
||||
* It is licensed under GNU GPL v. 2 or later.
|
||||
* You should have received a copy of the license in this archive (see LICENSE).
|
||||
/*******************************************************************************
|
||||
*
|
||||
* Copyright Nikolai Kudashov, 2013-2016.
|
||||
*/
|
||||
* Messenger Android Frontend
|
||||
* Copyright (C) 2016 Björn Petersen Software Design and Development
|
||||
* Contact: r10s@b44t.com, http://b44t.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see http://www.gnu.org/licenses/ .
|
||||
*
|
||||
*******************************************************************************
|
||||
*
|
||||
* File: ContactsController.java
|
||||
* Authors: Björn Petersen
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
package com.b44t.messenger;
|
||||
|
||||
@@ -24,46 +43,29 @@ import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.ContactsContract;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.b44t.ui.Components.AvatarDrawable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ContactsController {
|
||||
|
||||
static ContentResolver s_cr;
|
||||
private static final String TAG = "ContactsController";
|
||||
|
||||
public static class Contact {
|
||||
public String name;
|
||||
public String email;
|
||||
}
|
||||
|
||||
private String[] projectionNames = {
|
||||
private final static String[] projectionNames = {
|
||||
ContactsContract.Data.DISPLAY_NAME,
|
||||
ContactsContract.CommonDataKinds.Email.ADDRESS
|
||||
};
|
||||
|
||||
private static volatile ContactsController Instance = null;
|
||||
|
||||
public static ContactsController getInstance() {
|
||||
ContactsController localInstance = Instance;
|
||||
if (localInstance == null) {
|
||||
synchronized (ContactsController.class) {
|
||||
localInstance = Instance;
|
||||
if (localInstance == null) {
|
||||
Instance = localInstance = new ContactsController();
|
||||
}
|
||||
}
|
||||
}
|
||||
return localInstance;
|
||||
}
|
||||
|
||||
public ContactsController() {
|
||||
}
|
||||
|
||||
public String readContactsFromPhoneBook() {
|
||||
public static String readContactsFromPhoneBook() {
|
||||
HashMap<String, String> contactsMap = new HashMap<String, String>();
|
||||
String allContacts = "";
|
||||
try {
|
||||
@@ -95,7 +97,7 @@ public class ContactsController {
|
||||
return allContacts;
|
||||
}
|
||||
|
||||
private boolean hasContactsPermission() {
|
||||
private static boolean hasContactsPermission() {
|
||||
if (Build.VERSION.SDK_INT >= 23) {
|
||||
return ApplicationLoader.applicationContext.checkSelfPermission(android.Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
@@ -107,14 +109,14 @@ public class ContactsController {
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
FileLog.e("messenger", e);
|
||||
;
|
||||
} finally {
|
||||
try {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
FileLog.e("messenger", e);
|
||||
;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -157,74 +159,156 @@ public class ContactsController {
|
||||
/* Handle contact images
|
||||
**********************************************************************************************/
|
||||
|
||||
public static void setupAvatar(ImageReceiver avtImageReceiver,
|
||||
AvatarDrawable avtDrawable,
|
||||
MrContact mrContact, MrChat mrChat)
|
||||
{
|
||||
// get email to search avatar image for
|
||||
String fallbackName = "";
|
||||
String email = null;
|
||||
if( mrContact!=null ) {
|
||||
email = mrContact.getAddr();
|
||||
fallbackName = mrContact.getDisplayName();
|
||||
static class AvtCacheEntry {
|
||||
public Bitmap m_avatarBitmap;
|
||||
public String m_fallbackName;
|
||||
AvtCacheEntry(Bitmap avatarBitmap, String fallbackName) {
|
||||
m_avatarBitmap = avatarBitmap;
|
||||
m_fallbackName = fallbackName;
|
||||
}
|
||||
else if( mrChat != null ) {
|
||||
fallbackName = mrChat.getName();
|
||||
if(mrChat.getType()==MrChat.MR_CHAT_NORMAL) {
|
||||
}
|
||||
|
||||
private final static String[] s_projectionAvatars = new String[]{
|
||||
ContactsContract.Contacts._ID,
|
||||
ContactsContract.Contacts.PHOTO_ID,
|
||||
ContactsContract.CommonDataKinds.Email.ADDRESS
|
||||
//ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
|
||||
};
|
||||
private static HashMap<View, String> s_viewBindings = new HashMap<>();
|
||||
private static HashMap<String, AvtCacheEntry> s_avtCache = new HashMap<>();
|
||||
|
||||
public static void cleanupAvatarCache() {
|
||||
// to detect changes of the avatar images eg. in the Contacts App,
|
||||
// this function should be called whenever the App goes to background.
|
||||
synchronized (s_viewBindings /*we use s_viewBindings for locking!*/ ) {
|
||||
Log.i(TAG, "Avatar cache discarded.");
|
||||
s_avtCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setupAvatar(final View avtView,
|
||||
final ImageReceiver avtImageReceiver,
|
||||
final AvatarDrawable avtDrawable,
|
||||
MrContact mrContact,
|
||||
MrChat mrChat)
|
||||
{
|
||||
// get email/name to search avatar image for
|
||||
String tempEmail = null;
|
||||
String tempName = "";
|
||||
if (mrContact != null) {
|
||||
tempEmail = mrContact.getAddr();
|
||||
tempName = mrContact.getDisplayName();
|
||||
} else if (mrChat != null) {
|
||||
tempName = mrChat.getName();
|
||||
if (mrChat.getType() == MrChat.MR_CHAT_NORMAL) {
|
||||
int[] contact_ids = MrMailbox.getChatContacts(mrChat.getId());
|
||||
if( contact_ids.length==1 ) {
|
||||
if (contact_ids.length == 1) {
|
||||
MrContact mrc = MrMailbox.getContact(contact_ids[0]);
|
||||
email = mrc.getAddr();
|
||||
fallbackName = mrc.getDisplayName();
|
||||
tempEmail = mrc.getAddr();
|
||||
tempName = mrc.getDisplayName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to get avatar image from the address book
|
||||
Bitmap photoBitmap = null;
|
||||
if( email!=null ) {
|
||||
try {
|
||||
if (s_cr == null) {
|
||||
s_cr = ApplicationLoader.applicationContext.getContentResolver();
|
||||
}
|
||||
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, Uri.encode(email));
|
||||
Cursor pCur = s_cr.query(uri,
|
||||
new String[] {
|
||||
ContactsContract.Contacts._ID,
|
||||
ContactsContract.Contacts.PHOTO_ID,
|
||||
ContactsContract.CommonDataKinds.Email.ADDRESS
|
||||
//ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
|
||||
},
|
||||
null, null, null);
|
||||
if (pCur != null ) {
|
||||
if( pCur.getCount() > 0 ) {
|
||||
while (pCur.moveToNext()) {
|
||||
int contact_id = pCur.getInt(0);
|
||||
int photo_id = pCur.getInt(1);
|
||||
String addr = pCur.getString(2);
|
||||
if( addr.equalsIgnoreCase(email) && contact_id > 0 && photo_id > 0) {
|
||||
Bitmap tempBitmap = loadContactPhoto(s_cr, contact_id, photo_id);
|
||||
if( tempBitmap != null ) {
|
||||
photoBitmap = createRoundBitmap(tempBitmap);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if( tempEmail == null ) {
|
||||
tempEmail = "fallback:" + tempName;
|
||||
}
|
||||
|
||||
final String email = tempEmail;
|
||||
final String fallbackName = tempName;
|
||||
|
||||
// bind e-email address to view object to detect overwrites and discard loading old images (may happen on fast scrolling)
|
||||
// moreover, check if the avatar is in cache
|
||||
AvtCacheEntry cacheEntry;
|
||||
synchronized (s_viewBindings) {
|
||||
s_viewBindings.put(avtView, email);
|
||||
cacheEntry = s_avtCache.get(email);
|
||||
}
|
||||
|
||||
if( cacheEntry != null )
|
||||
{
|
||||
// can use avatar from cache, very fine
|
||||
if (cacheEntry.m_avatarBitmap != null) {
|
||||
avtImageReceiver.setImageBitmap(cacheEntry.m_avatarBitmap);
|
||||
} else {
|
||||
avtDrawable.setInfoByName(cacheEntry.m_fallbackName);
|
||||
avtImageReceiver.setImage(null, "50_50", avtDrawable, null, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// avatar is not in cache, empty the image (may be the bitmap of another use as we re-use the objects)
|
||||
avtImageReceiver.setImage(null, "50_50", null, null, false);
|
||||
|
||||
// load avatar in a working thread (when loaded, we'll add it to cache and invalidate back in the GUI thread)
|
||||
Utilities.searchQueue.postRunnable(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// is the avatar still desired?
|
||||
synchronized (s_viewBindings) {
|
||||
if (!s_viewBindings.get(avtView).equals(email)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
pCur.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
if( photoBitmap != null ) {
|
||||
avtImageReceiver.setImageBitmap(photoBitmap);
|
||||
}
|
||||
else {
|
||||
avtDrawable.setInfoByName(fallbackName);
|
||||
avtImageReceiver.setImage(null, "50_50", avtDrawable, null, false);
|
||||
// try to get avatar image from the address book
|
||||
Bitmap tempBitmap = null;
|
||||
if (!email.startsWith("fallback:")) {
|
||||
try {
|
||||
if (s_cr == null) {
|
||||
s_cr = ApplicationLoader.applicationContext.getContentResolver();
|
||||
}
|
||||
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, Uri.encode(email));
|
||||
Cursor pCur = s_cr.query(uri, s_projectionAvatars, null, null, null);
|
||||
if (pCur != null) {
|
||||
if (pCur.getCount() > 0) {
|
||||
while (pCur.moveToNext()) {
|
||||
int contact_id = pCur.getInt(0);
|
||||
int photo_id = pCur.getInt(1);
|
||||
String addr = pCur.getString(2);
|
||||
if (addr.equalsIgnoreCase(email) && contact_id > 0 && photo_id > 0) {
|
||||
Bitmap tempBitmap2 = loadContactPhoto(s_cr, contact_id, photo_id);
|
||||
if (tempBitmap2 != null) {
|
||||
tempBitmap = createRoundBitmap(tempBitmap2);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pCur.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
final Bitmap photoBitmap = tempBitmap;
|
||||
|
||||
AndroidUtilities.runOnUIThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// is the avatar still desired?
|
||||
synchronized (s_viewBindings) {
|
||||
if (!s_viewBindings.get(avtView).equals(email)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (photoBitmap != null) {
|
||||
avtImageReceiver.setImageBitmap(photoBitmap);
|
||||
} else {
|
||||
avtDrawable.setInfoByName(fallbackName);
|
||||
avtImageReceiver.setImage(null, "50_50", avtDrawable, null, false);
|
||||
}
|
||||
avtView.invalidate();
|
||||
|
||||
synchronized (s_viewBindings /*we use s_viewBindings for locking!*/ ) {
|
||||
s_avtCache.put(email, new AvtCacheEntry(photoBitmap, fallbackName));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3610,7 +3610,7 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate
|
||||
|
||||
if (isChat && !messageObject.isOutOwner() && messageObject.isFromUser()) {
|
||||
isAvatarVisible = true;
|
||||
ContactsController.setupAvatar(avatarImage, avatarDrawable, mrContact, null);
|
||||
ContactsController.setupAvatar(this, avatarImage, avatarDrawable, mrContact, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -644,7 +644,7 @@ public class DialogCell extends BaseCell {
|
||||
}
|
||||
}
|
||||
|
||||
ContactsController.setupAvatar(avatarImage, avatarDrawable, null, m_mrChat);
|
||||
ContactsController.setupAvatar(this, avatarImage, avatarDrawable, null, m_mrChat);
|
||||
|
||||
if (getMeasuredWidth() != 0 || getMeasuredHeight() != 0) {
|
||||
buildLayout();
|
||||
|
||||
@@ -173,7 +173,7 @@ public class UserCell extends FrameLayout {
|
||||
imageView.setImageResource(currentResId);
|
||||
}
|
||||
|
||||
ContactsController.setupAvatar(avatarImageView.imageReceiver, avatarDrawable, m_mrContact, null);
|
||||
ContactsController.setupAvatar(avatarImageView, avatarImageView.imageReceiver, avatarDrawable, m_mrContact, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -114,7 +114,7 @@ public class ChatAvatarContainer extends FrameLayout {
|
||||
|
||||
public void checkAndUpdateAvatar() {
|
||||
if (avatarImageView != null) {
|
||||
ContactsController.setupAvatar(avatarImageView.imageReceiver, avatarDrawable, null, parentFragment.m_mrChat);
|
||||
ContactsController.setupAvatar(avatarImageView, avatarImageView.imageReceiver, avatarDrawable, null, parentFragment.m_mrChat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ public class ContactsActivity extends BaseFragment implements NotificationCenter
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
String pbcontacts = ContactsController.getInstance().readContactsFromPhoneBook();
|
||||
String pbcontacts = ContactsController.readContactsFromPhoneBook();
|
||||
if (!pbcontacts.isEmpty()) {
|
||||
if (MrMailbox.addAddressBook(pbcontacts) > 0) {
|
||||
AndroidUtilities.runOnUIThread(new Runnable() {
|
||||
|
||||
@@ -42,6 +42,7 @@ import android.widget.RelativeLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.b44t.messenger.AndroidUtilities;
|
||||
import com.b44t.messenger.ContactsController;
|
||||
import com.b44t.messenger.ImageLoader;
|
||||
import com.b44t.messenger.MessageObject;
|
||||
import com.b44t.messenger.MessagesController;
|
||||
@@ -1378,6 +1379,7 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa
|
||||
if (PhotoViewer.getInstance().isVisible()) {
|
||||
PhotoViewer.getInstance().onPause();
|
||||
}
|
||||
ContactsController.cleanupAvatarCache();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1436,6 +1438,7 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa
|
||||
passcodeView.onResume();
|
||||
}
|
||||
ConnectionsManager.getInstance().setAppPaused(false, false);
|
||||
ContactsController.cleanupAvatarCache();
|
||||
updateCurrentConnectionState();
|
||||
if (PhotoViewer.getInstance().isVisible()) {
|
||||
PhotoViewer.getInstance().onResume();
|
||||
|
||||
@@ -1232,7 +1232,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
|
||||
nameTextView[a].setRightDrawable(rightIcon);
|
||||
}
|
||||
|
||||
ContactsController.setupAvatar(avatarImage.imageReceiver, avatarDrawable, mrContact, mrChat);
|
||||
ContactsController.setupAvatar(avatarImage, avatarImage.imageReceiver, avatarDrawable, mrContact, mrChat);
|
||||
}
|
||||
|
||||
private void createActionBarMenu() {
|
||||
|
||||
Reference in New Issue
Block a user