Update avatars on renamings.

This commit is contained in:
B. Petersen
2017-01-11 00:01:56 +01:00
parent a921523014
commit 6d3c170e96
6 changed files with 96 additions and 63 deletions
@@ -176,6 +176,7 @@ public class ContactsController {
ContactsContract.CommonDataKinds.Email.ADDRESS
//ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
};
private static final Object s_sync = new Object();
private static HashMap<View, String> s_viewBindings = new HashMap<>();
private static HashMap<String, AvtCacheEntry> s_avtCache = new HashMap<>();
@@ -183,8 +184,7 @@ public class ContactsController {
// to detect changes of the avatar images eg. in the Contacts App,
// this function should be called whenever the App goes to background or back to foreground.
// instead of emptying the cache, we force a reloading (looks smarter - most times, the avatars do not change)
synchronized (s_viewBindings /*we use s_viewBindings for locking!*/ ) {
Log.i(TAG, "Avatar cache discarded.");
synchronized (s_sync) {
for (AvtCacheEntry cacheEntry : s_avtCache.values()) {
cacheEntry.m_needsReload = true;
}
@@ -215,6 +215,15 @@ public class ContactsController {
}
}
setupAvatarByStrings(avtView, avtImageReceiver, avtDrawable, tempEmail, tempName);
}
public static void setupAvatarByStrings(final View avtView,
final ImageReceiver avtImageReceiver,
final AvatarDrawable avtDrawable,
String tempEmail,
String tempName)
{
if( tempEmail == null ) {
tempEmail = "fallback:" + tempName;
}
@@ -222,12 +231,12 @@ public class ContactsController {
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)
// bind e-mail+name 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);
synchronized (s_sync) {
s_viewBindings.put(avtView, email+fallbackName);
cacheEntry = s_avtCache.get(email+fallbackName);
}
if( cacheEntry != null )
@@ -254,8 +263,8 @@ public class ContactsController {
@Override
public void run() {
// is the avatar still desired?
synchronized (s_viewBindings) {
if (!s_viewBindings.get(avtView).equals(email)) {
synchronized (s_sync) {
if (!s_viewBindings.get(avtView).equals(email+fallbackName)) {
return;
}
}
@@ -297,8 +306,8 @@ public class ContactsController {
@Override
public void run() {
// is the avatar still desired?
synchronized (s_viewBindings) {
if (!s_viewBindings.get(avtView).equals(email)) {
synchronized (s_sync) {
if (!s_viewBindings.get(avtView).equals(email+fallbackName)) {
return;
}
}
@@ -311,8 +320,8 @@ public class ContactsController {
}
avtView.invalidate();
synchronized (s_viewBindings /*we use s_viewBindings for locking!*/ ) {
s_avtCache.put(email, new AvtCacheEntry(photoBitmap, fallbackName));
synchronized (s_sync) {
s_avtCache.put(email+fallbackName, new AvtCacheEntry(photoBitmap, fallbackName));
}
}
});
@@ -42,6 +42,7 @@ import android.widget.LinearLayout;
import android.widget.Toast;
import com.b44t.messenger.AndroidUtilities;
import com.b44t.messenger.ContactsController;
import com.b44t.messenger.LocaleController;
import com.b44t.messenger.MrContact;
import com.b44t.messenger.MrMailbox;
@@ -182,7 +183,6 @@ public class ContactAddActivity extends BaseFragment implements NotificationCent
avatarImage = new BackupImageView(context);
avatarImage.setRoundRadius(AndroidUtilities.dp(32));
avatarDrawable.setInfoByName(nameToSet!=null? nameToSet : "?");
avatarImage.setImageDrawable(avatarDrawable);
frameLayout.addView(avatarImage);
FrameLayout.LayoutParams layoutParams1 = (FrameLayout.LayoutParams) avatarImage.getLayoutParams();
@@ -195,7 +195,8 @@ public class ContactAddActivity extends BaseFragment implements NotificationCent
layoutParams1.gravity = Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
avatarImage.setLayoutParams(layoutParams1);
{
avatarDrawable.setDrawPhoto(true);
//avatarDrawable.setDrawPhoto(true);
/* TODO: let the user select a photo for groups (contact photos come from the system's address book)
avatarImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@@ -229,6 +230,7 @@ public class ContactAddActivity extends BaseFragment implements NotificationCent
showDialog(builder.create());
}
});
*/
}
nameTextView = new EditText(context);
@@ -256,25 +258,22 @@ public class ContactAddActivity extends BaseFragment implements NotificationCent
layoutParams1.rightMargin = LocaleController.isRTL ? AndroidUtilities.dp(96) : AndroidUtilities.dp(16);
layoutParams1.gravity = Gravity.CENTER_VERTICAL;
nameTextView.setLayoutParams(layoutParams1);
{
nameTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
nameTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
@Override
public void afterTextChanged(Editable s) {
avatarDrawable.setInfoByName(nameTextView.length() > 0 ? nameTextView.getText().toString() : "?");
avatarImage.invalidate();
}
});
}
@Override
public void afterTextChanged(Editable s) {
updateAvatar();
}
});
if( do_what==CREATE_CONTACT ) {
emailTextView = new EditText(context);
@@ -287,6 +286,22 @@ public class ContactAddActivity extends BaseFragment implements NotificationCent
emailTextView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
emailTextView.setPadding(0, 0, 0, AndroidUtilities.dp(8));
emailTextView.setHint(LocaleController.getString("EmailAddress", R.string.EmailAddress));
emailTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
updateAvatar();
}
});
AndroidUtilities.clearCursorDrawable(emailTextView);
linearLayout.addView(emailTextView);
LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) emailTextView.getLayoutParams();
@@ -299,10 +314,27 @@ public class ContactAddActivity extends BaseFragment implements NotificationCent
emailTextView.setLayoutParams(layoutParams2);
}
updateAvatar();
nameToSet = null;
return fragmentView;
}
private void updateAvatar()
{
String email = null;
if( emailTextView != null ) {
email = emailTextView.length() > 0? emailTextView.getText().toString() : null;
}
else if( user_id != 0 ) {
MrContact mrContact = MrMailbox.getContact(user_id);
email = mrContact.getAddr();
}
ContactsController.setupAvatarByStrings(avatarImage, avatarImage.imageReceiver, avatarDrawable,
email,
nameTextView.length() > 0 ? nameTextView.getText().toString() : "?");
}
@Override
public void didUploadedPhoto(final TLRPC.InputFile file, final TLRPC.PhotoSize small, final TLRPC.PhotoSize big) {
Toast.makeText(getParentActivity(), LocaleController.getString("NotYetImplemented", R.string.NotYetImplemented), Toast.LENGTH_LONG).show();
@@ -238,9 +238,7 @@ public class ContactsActivity extends BaseFragment implements NotificationCenter
}
else if( id == id_done_button )
{
if (selectedContacts.isEmpty()) {
return;
}
/*it's okay if selectedContacts is empty - a group only with SELF with created then*/
ArrayList<Integer> result = new ArrayList<>();
result.addAll(selectedContacts.keySet());
Bundle args = new Bundle();
@@ -44,6 +44,7 @@ import android.widget.ListView;
import android.widget.Toast;
import com.b44t.messenger.AndroidUtilities;
import com.b44t.messenger.ContactsController;
import com.b44t.messenger.LocaleController;
import com.b44t.messenger.MrContact;
import com.b44t.messenger.MrMailbox;
@@ -91,26 +92,9 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati
avatarUpdater.parentFragment = this;
avatarUpdater.delegate = this;
avatarUpdater.returnOnly = true;
selectedContacts = getArguments().getIntegerArrayList("result");
final ArrayList<Integer> usersToLoad = new ArrayList<>();
for (Integer uid : selectedContacts) {
if (MessagesController.getInstance().getUser(uid) == null) {
usersToLoad.add(uid);
}
}
if (!usersToLoad.isEmpty()) {
final ArrayList<TLRPC.User> users = new ArrayList<>();
if (usersToLoad.size() != users.size()) {
return false;
}
if (!users.isEmpty()) {
//for (TLRPC.User user : users) {
// MessagesController.getInstance().putUser(user, true);
//}
} else {
return false;
}
}
selectedContacts = getArguments().getIntegerArrayList("result"); /* may be empty - in this case a group only with SELF is created */
if( selectedContacts == null ) { selectedContacts = new ArrayList<>(); }
selectedContacts.add(MrContact.MR_CONTACT_ID_SELF);
return super.onFragmentCreate();
}
@@ -187,7 +171,7 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati
avatarImage = new BackupImageView(context);
avatarImage.setRoundRadius(AndroidUtilities.dp(32));
avatarDrawable.setInfoByName("?");
//avatarDrawable.setInfoByName("?");
avatarImage.setImageDrawable(avatarDrawable);
frameLayout.addView(avatarImage);
FrameLayout.LayoutParams layoutParams1 = (FrameLayout.LayoutParams) avatarImage.getLayoutParams();
@@ -200,7 +184,8 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati
layoutParams1.gravity = Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
avatarImage.setLayoutParams(layoutParams1);
{
avatarDrawable.setDrawPhoto(true);
//avatarDrawable.setDrawPhoto(true);
/* TODO: let the user select a photo for the group
avatarImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@@ -234,6 +219,7 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati
showDialog(builder.create());
}
});
*/
}
nameTextView = new EditText(context);
@@ -276,16 +262,13 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati
@Override
public void afterTextChanged(Editable s) {
avatarDrawable.setInfoByName(nameTextView.length() > 0 ? nameTextView.getText().toString() : "?");
avatarImage.invalidate();
updateAvatar();
}
});
}
GreySectionCell sectionCell = new GreySectionCell(context);
sectionCell.setText(
LocaleController.getString("MeAnd", R.string.MeAnd) + " " +
LocaleController.formatPluralString("Members", selectedContacts.size()));
sectionCell.setText(LocaleController.formatPluralString("Members", selectedContacts.size()));
linearLayout.addView(sectionCell);
listView = new ListView(context);
@@ -299,9 +282,17 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati
layoutParams.height = LayoutHelper.MATCH_PARENT;
listView.setLayoutParams(layoutParams);
updateAvatar();
return fragmentView;
}
private void updateAvatar()
{
ContactsController.setupAvatarByStrings(avatarImage, avatarImage.imageReceiver, avatarDrawable, null,
nameTextView.length() > 0 ? nameTextView.getText().toString() : "?");
}
@Override
public void didUploadedPhoto(final TLRPC.InputFile file, final TLRPC.PhotoSize small, final TLRPC.PhotoSize big) {
Toast.makeText(getParentActivity(), LocaleController.getString("NotYetImplemented", R.string.NotYetImplemented), Toast.LENGTH_LONG).show();
@@ -584,6 +584,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
}
if ( chat_id != 0 && chat_id!= MrChat.MR_CHAT_ID_DEADDROP ) {
/* TODO: let the user select a photo for the group
writeButton = new ImageView(context);
try {
writeButton.setBackgroundResource(R.drawable.floating_user_states);
@@ -616,7 +617,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
CharSequence[] items;
TLRPC.Chat chat = MrChat.chatId2chat(chat_id);
if (chat.photo == null || chat.photo.photo_big == null /*|| chat.photo instanceof TLRPC.TL_chatPhotoEmpty*/) {
if (chat.photo == null || chat.photo.photo_big == null ) {
items = new CharSequence[]{LocaleController.getString("FromCamera", R.string.FromCamera), LocaleController.getString("FromGalley", R.string.FromGalley)};
} else {
items = new CharSequence[]{LocaleController.getString("FromCamera", R.string.FromCamera), LocaleController.getString("FromGalley", R.string.FromGalley), LocaleController.getString("DeletePhoto", R.string.DeletePhoto)};
@@ -637,6 +638,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
showDialog(builder.create());
}
});
*/
}
needLayout();
@@ -1407,7 +1409,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
} else if (i == startChatRow) {
textCell.setText(LocaleController.getString("NewChat", R.string.NewChat));
} else if (i == settingsNotificationsRow) {
textCell.setTextAndIcon(LocaleController.getString("NotificationsAndSounds", R.string.NotificationsAndSounds), R.drawable.menu_settings);
textCell.setTextAndIcon(LocaleController.getString("Settings", R.string.Settings), R.drawable.menu_settings);
} else if (i == addMemberRow) {
textCell.setText(LocaleController.getString("AddMember", R.string.AddMember));
}
@@ -128,7 +128,8 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi
public View createView(Context context) {
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setAllowOverlayTitle(true);
actionBar.setTitle(LocaleController.getString("NotificationsAndSounds", R.string.NotificationsAndSounds));
actionBar.setTitle(m_mrChat.getName());
actionBar.setSubtitle(LocaleController.getString("Settings", R.string.Settings));
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
@@ -546,7 +547,7 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi
if (view == null) {
view = new HeaderCell(mContext);
}
((HeaderCell) view).setText(m_mrChat.getName());
((HeaderCell) view).setText(LocaleController.getString("NotificationsAndSounds", R.string.NotificationsAndSounds));
}
else if (type == TYPE_TEXTSETTINGS) {
if (view == null) {