Commit 4ace1632 authored by Grigory Fedorov's avatar Grigory Fedorov

ChatViewer now also uses ContactTitleActionBarInflater....

ChatViewer now also uses ContactTitleActionBarInflater. ContactTitleActionBarInflater better encapsulates inner views.
parent 673647ed
......@@ -16,26 +16,17 @@ package com.xabber.android.ui;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.xabber.android.data.ActivityManager;
import com.xabber.android.data.Application;
import com.xabber.android.data.NetworkException;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.archive.MessageArchiveManager;
......@@ -56,7 +47,7 @@ import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.adapter.ChatViewerAdapter;
import com.xabber.android.ui.dialog.ChatExportDialogFragment;
import com.xabber.android.ui.helper.ContactTitleInflater;
import com.xabber.android.ui.helper.ContactTitleActionBarInflater;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.android.ui.preferences.ChatEditor;
import com.xabber.androiddev.R;
......@@ -92,19 +83,14 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
ChatViewerAdapter chatViewerAdapter;
ViewPager viewPager;
private View actionBarView;
Collection<ChatViewerFragment> registeredChats = new HashSet<>();
Collection<RecentChatFragment> recentChatFragments = new HashSet<>();
private String actionWithAccount = null;
private String actionWithUser = null;
private int[] accountActionBarColors;
private int[] accountStatusBarColors;
private Window window;
private int defaultStatusBarColor;
private Animation shakeAnimation;
private ContactTitleActionBarInflater contactTitleActionBarInflater;
private boolean isChatSelected;
private Menu menu = null;
......@@ -133,38 +119,16 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
exitOnSend = savedInstanceState.getBoolean(SAVED_EXIT_ON_SEND);
}
shakeAnimation = AnimationUtils.loadAnimation(this, R.anim.shake);
contactTitleActionBarInflater = new ContactTitleActionBarInflater(this);
contactTitleActionBarInflater.setUpActionBarView();
window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
defaultStatusBarColor = window.getStatusBarColor();
}
accountActionBarColors = getResources().getIntArray(R.array.account_action_bar);
accountStatusBarColors = getResources().getIntArray(R.array.account_status_bar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBarView = LayoutInflater.from(this).inflate(R.layout.contact_title, null);
actionBarView.setOnClickListener(new View.OnClickListener() {
contactTitleActionBarInflater.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollChat();
}
});
actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
setContentView(R.layout.activity_chat_viewer);
if (account != null && user != null) {
......@@ -584,11 +548,9 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
&& actionWithUser != null && actionWithUser.equals(user)) {
updateActionBar(account, user);
if (incoming) {
actionBarView.findViewById(R.id.name_holder).startAnimation(shakeAnimation);
contactTitleActionBarInflater.playIncomingAnimation();
}
}
}
@Override
......@@ -649,13 +611,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
createOptionsMenu();
if (!isChatSelected) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(defaultStatusBarColor);
}
getSupportActionBar().setBackgroundDrawable(null);
getSupportActionBar().setDisplayShowTitleEnabled(true);
actionBarView.setVisibility(View.GONE);
setTitle(getString(R.string.chat_list));
contactTitleActionBarInflater.restoreDefaultTitleView(getString(R.string.chat_list));
MessageManager.getInstance().removeVisibleChat();
return;
}
......@@ -672,20 +628,14 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
}
private void updateActionBar(String account, String user) {
actionBarView.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayShowTitleEnabled(false);
final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
ContactTitleInflater.updateTitle(actionBarView, this, abstractContact);
int colorLevel = AccountManager.getInstance().getColorLevel(abstractContact.getAccount());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(accountStatusBarColors[colorLevel]);
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(accountActionBarColors[colorLevel]));
contactTitleActionBarInflater.update(abstractContact);
SecurityLevel securityLevel = OTRManager.getInstance().getSecurityLevel(account, user);
SettingsManager.SecurityOtrMode securityOtrMode = SettingsManager.securityOtrMode();
ImageView securityView = (ImageView) actionBarView.findViewById(R.id.security);
ImageView securityView = contactTitleActionBarInflater.getSecurityView();
if (securityLevel == SecurityLevel.plain
&& (securityOtrMode == SettingsManager.SecurityOtrMode.disabled
......
......@@ -17,10 +17,8 @@ package com.xabber.android.ui;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import com.xabber.android.data.Application;
import com.xabber.android.data.NetworkException;
......@@ -51,7 +49,7 @@ public class ContactEditor extends GroupListActivity implements
super.onCreate(savedInstanceState);
contactTitleActionBarInflater = new ContactTitleActionBarInflater(this);
contactTitleActionBarInflater.setActionBarView();
contactTitleActionBarInflater.setUpActionBarView();
}
@Override
......@@ -103,12 +101,8 @@ public class ContactEditor extends GroupListActivity implements
private void update() {
AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
contactTitleActionBarInflater.update(abstractContact);
View actionBarView = contactTitleActionBarInflater.getActionBarView();
((TextView) actionBarView.findViewById(R.id.name))
.setText(getString(R.string.contact_editor_title, abstractContact.getName()));
((TextView) actionBarView.findViewById(R.id.status_text)).setText(user);
contactTitleActionBarInflater.setName(getString(R.string.contact_editor_title, abstractContact.getName()));
contactTitleActionBarInflater.setStatusText(user);
}
@Override
......
......@@ -119,7 +119,7 @@ public class FingerprintViewer extends ManagedActivity implements
isUpdating = false;
contactTitleActionBarInflater = new ContactTitleActionBarInflater(this);
contactTitleActionBarInflater.setActionBarView();
contactTitleActionBarInflater.setUpActionBarView();
}
@Override
......
......@@ -102,7 +102,7 @@ public class QuestionViewer extends ManagedActivity implements
findViewById(R.id.send).setOnClickListener(this);
contactTitleActionBarInflater = new ContactTitleActionBarInflater(this);
contactTitleActionBarInflater.setActionBarView();
contactTitleActionBarInflater.setUpActionBarView();
}
@Override
......
......@@ -89,13 +89,13 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
hint = null;
appearanceStyle = SettingsManager.chatsAppearanceStyle();
ChatsDivide chatsDivide = SettingsManager.chatsDivide();
if (chatsDivide == ChatsDivide.always
|| (chatsDivide == ChatsDivide.portial && !activity
.getResources().getBoolean(R.bool.landscape)))
if (chatsDivide == ChatsDivide.always || (chatsDivide == ChatsDivide.portial
&& !activity.getResources().getBoolean(R.bool.landscape))) {
divider = "\n";
else
} else {
divider = " ";
}
}
@Override
public int getCount() {
......@@ -104,11 +104,12 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
@Override
public Object getItem(int position) {
if (position < messages.size())
if (position < messages.size()) {
return messages.get(position);
else
} else {
return null;
}
}
@Override
public long getItemId(int position) {
......@@ -122,18 +123,17 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
@Override
public int getItemViewType(int position) {
if (position < messages.size())
if (position < messages.size()) {
return TYPE_MESSAGE;
else
} else {
return hint == null ? TYPE_EMPTY : TYPE_HINT;
}
}
private void append(SpannableStringBuilder builder, CharSequence text,
CharacterStyle span) {
private void append(SpannableStringBuilder builder, CharSequence text, CharacterStyle span) {
int start = builder.length();
builder.append(text);
builder.setSpan(span, start, start + text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(span, start, start + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
@Override
......@@ -142,24 +142,32 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
final View view;
if (convertView == null) {
final int resource;
if (type == TYPE_MESSAGE)
switch (type) {
case TYPE_MESSAGE:
resource = R.layout.chat_viewer_message;
else if (type == TYPE_HINT)
break;
case TYPE_HINT:
resource = R.layout.chat_viewer_info;
else if (type == TYPE_EMPTY)
break;
case TYPE_EMPTY:
resource = R.layout.chat_viewer_empty;
else
break;
default:
throw new IllegalStateException();
view = activity.getLayoutInflater()
.inflate(resource, parent, false);
if (type == TYPE_MESSAGE)
((TextView) view.findViewById(R.id.text)).setTextAppearance(
activity, appearanceStyle);
} else
}
view = activity.getLayoutInflater().inflate(resource, parent, false);
if (type == TYPE_MESSAGE) {
((TextView) view.findViewById(R.id.text)).setTextAppearance(activity, appearanceStyle);
}
} else {
view = convertView;
}
if (type == TYPE_EMPTY)
if (type == TYPE_EMPTY) {
return view;
}
if (type == TYPE_HINT) {
TextView textView = ((TextView) view.findViewById(R.id.info));
......@@ -177,111 +185,94 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
if (isMUC) {
name = resource;
} else {
if (incoming)
if (incoming) {
name = RosterManager.getInstance().getName(account, user);
else
} else {
name = AccountManager.getInstance().getNickName(account);
}
}
if (incoming) {
if (view.getBackground() == null)
if (view.getBackground() == null) {
view.setBackgroundResource(R.drawable.chat_bg);
view.getBackground().setLevel(
AccountManager.getInstance().getColorLevel(account));
}
view.getBackground().setLevel(AccountManager.getInstance().getColorLevel(account));
} else {
view.setBackgroundDrawable(null);
}
Spannable text = messageItem.getSpannable();
TextView textView = (TextView) view.findViewById(R.id.text);
ImageView avatarView = (ImageView) view.findViewById(R.id.avatar);
ChatAction action = messageItem.getAction();
String time = StringUtils.getSmartTimeText(messageItem.getTimestamp());
SpannableStringBuilder builder = new SpannableStringBuilder();
if (action == null) {
int messageResource = R.drawable.ic_message_delivered;
if (!incoming) {
if (messageItem.isError())
if (messageItem.isError()) {
messageResource = R.drawable.ic_message_has_error;
else if (!messageItem.isSent())
} else if (!messageItem.isSent()) {
messageResource = R.drawable.ic_message_not_sent;
else if (!messageItem.isDelivered())
} else if (!messageItem.isDelivered()) {
messageResource = R.drawable.ic_message_not_delivered;
}
}
append(builder, " ", new ImageSpan(activity, messageResource));
append(builder, " ", new TextAppearanceSpan(activity,
R.style.ChatHeader));
append(builder, time, new TextAppearanceSpan(activity,
R.style.ChatHeader_Time));
append(builder, " ", new TextAppearanceSpan(activity,
R.style.ChatHeader));
append(builder, name, new TextAppearanceSpan(activity,
R.style.ChatHeader_Name));
append(builder, divider, new TextAppearanceSpan(activity,
R.style.ChatHeader));
append(builder, " ", new TextAppearanceSpan(activity, R.style.ChatHeader));
append(builder, time, new TextAppearanceSpan(activity, R.style.ChatHeader_Time));
append(builder, " ", new TextAppearanceSpan(activity, R.style.ChatHeader));
append(builder, name, new TextAppearanceSpan(activity, R.style.ChatHeader_Name));
append(builder, divider, new TextAppearanceSpan(activity, R.style.ChatHeader));
Date timeStamp = messageItem.getDelayTimestamp();
if (timeStamp != null) {
String delay = activity.getString(
incoming ? R.string.chat_delay : R.string.chat_typed,
String delay = activity.getString(incoming ? R.string.chat_delay : R.string.chat_typed,
StringUtils.getSmartTimeText(timeStamp));
append(builder, delay, new TextAppearanceSpan(activity,
R.style.ChatHeader_Delay));
append(builder, divider, new TextAppearanceSpan(activity,
R.style.ChatHeader));
append(builder, delay, new TextAppearanceSpan(activity, R.style.ChatHeader_Delay));
append(builder, divider, new TextAppearanceSpan(activity, R.style.ChatHeader));
}
if (messageItem.isUnencypted()) {
append(builder,
activity.getString(R.string.otr_unencrypted_message),
new TextAppearanceSpan(activity,
R.style.ChatHeader_Delay));
append(builder, divider, new TextAppearanceSpan(activity,
R.style.ChatHeader));
append(builder, activity.getString(R.string.otr_unencrypted_message),
new TextAppearanceSpan(activity, R.style.ChatHeader_Delay));
append(builder, divider, new TextAppearanceSpan(activity, R.style.ChatHeader));
}
Emoticons.getSmiledText(activity.getApplication(), text);
if (messageItem.getTag() == null)
if (messageItem.getTag() == null) {
builder.append(text);
else
append(builder, text, new TextAppearanceSpan(activity,
R.style.ChatRead));
} else {
append(builder, time, new TextAppearanceSpan(activity,
R.style.ChatHeader_Time));
append(builder, " ", new TextAppearanceSpan(activity,
R.style.ChatHeader));
text = Emoticons.newSpannable(action.getText(activity, name,
text.toString()));
append(builder, text, new TextAppearanceSpan(activity, R.style.ChatRead));
}
} else {
append(builder, time, new TextAppearanceSpan(activity, R.style.ChatHeader_Time));
append(builder, " ", new TextAppearanceSpan(activity, R.style.ChatHeader));
text = Emoticons.newSpannable(action.getText(activity, name, text.toString()));
Emoticons.getSmiledText(activity.getApplication(), text);
append(builder, text, new TextAppearanceSpan(activity,
R.style.ChatHeader_Delay));
append(builder, text, new TextAppearanceSpan(activity, R.style.ChatHeader_Delay));
}
textView.setText(builder);
textView.setMovementMethod(LinkMovementMethod.getInstance());
if (SettingsManager.chatsShowAvatars()) {
avatarView.setVisibility(View.VISIBLE);
if (!incoming
|| (isMUC && MUCManager.getInstance()
.getNickname(account, user)
.equalsIgnoreCase(resource))) {
avatarView.setImageDrawable(AvatarManager.getInstance()
.getAccountAvatar(account));
if (!incoming || (isMUC && MUCManager.getInstance().getNickname(account, user).equalsIgnoreCase(resource))) {
avatarView.setImageDrawable(AvatarManager.getInstance().getAccountAvatar(account));
} else {
if (isMUC) {
if ("".equals(resource)) {
avatarView.setImageDrawable(AvatarManager.getInstance()
.getRoomAvatar(user));
avatarView.setImageDrawable(AvatarManager.getInstance().getRoomAvatar(user));
} else {
avatarView.setImageDrawable(AvatarManager.getInstance()
.getOccupantAvatar(user + "/" + resource));
avatarView.setImageDrawable(AvatarManager.getInstance().getOccupantAvatar(user + "/" + resource));
}
} else {
avatarView.setImageDrawable(AvatarManager.getInstance()
.getUserAvatar(user));
avatarView.setImageDrawable(AvatarManager.getInstance().getUserAvatar(user));
}
}
((RelativeLayout.LayoutParams) textView.getLayoutParams()).addRule(
RelativeLayout.RIGHT_OF, R.id.avatar);
((RelativeLayout.LayoutParams) textView.getLayoutParams()).addRule(RelativeLayout.RIGHT_OF, R.id.avatar);
} else {
avatarView.setVisibility(View.GONE);
((RelativeLayout.LayoutParams) textView.getLayoutParams()).addRule(
RelativeLayout.RIGHT_OF, 0);
((RelativeLayout.LayoutParams) textView.getLayoutParams()).addRule(RelativeLayout.RIGHT_OF, 0);
}
return view;
}
......@@ -318,26 +309,21 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
* @return New hint.
*/
private String getHint() {
AccountItem accountItem = AccountManager.getInstance().getAccount(
account);
boolean online;
if (accountItem == null)
online = false;
else
online = accountItem.getState().isConnected();
final AbstractContact abstractContact = RosterManager.getInstance()
.getBestContact(account, user);
AccountItem accountItem = AccountManager.getInstance().getAccount(account);
boolean online = accountItem != null && accountItem.getState().isConnected();
final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
if (!online) {
if (abstractContact instanceof RoomContact)
if (abstractContact instanceof RoomContact) {
return activity.getString(R.string.muc_is_unavailable);
else
} else {
return activity.getString(R.string.account_is_offline);
}
} else if (!abstractContact.getStatusMode().isOnline()) {
if (abstractContact instanceof RoomContact)
if (abstractContact instanceof RoomContact) {
return activity.getString(R.string.muc_is_unavailable);
else
return activity.getString(R.string.contact_is_offline,
abstractContact.getName());
} else {
return activity.getString(R.string.contact_is_offline, abstractContact.getName());
}
}
return null;
}
......@@ -348,8 +334,9 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
*/
public void updateInfo() {
String info = getHint();
if (this.hint == info || (this.hint != null && this.hint.equals(info)))
if (this.hint.equals(info) || (this.hint != null && this.hint.equals(info))) {
return;
}
this.hint = info;
notifyDataSetChanged();
}
......
......@@ -8,6 +8,10 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.roster.AbstractContact;
......@@ -20,17 +24,17 @@ public class ContactTitleActionBarInflater {
private int[] accountActionBarColors;
private int[] accountStatusBarColors;
private Window window;
private int defaultStatusBarColor;
private Animation shakeAnimation = null;
public ContactTitleActionBarInflater(ActionBarActivity activity) {
this.activity = activity;
}
public void setActionBarView() {
window = this.activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
public void setUpActionBarView() {
accountActionBarColors = activity.getResources().getIntArray(R.array.account_action_bar);
accountStatusBarColors = activity.getResources().getIntArray(R.array.account_status_bar);
......@@ -44,9 +48,19 @@ public class ContactTitleActionBarInflater {
actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window = this.activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
defaultStatusBarColor = window.getStatusBarColor();
}
}
public void update(AbstractContact abstractContact) {
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
actionBarView.setVisibility(View.VISIBLE);
ContactTitleInflater.updateTitle(actionBarView, activity, abstractContact);
int colorLevel = AccountManager.getInstance().getColorLevel(abstractContact.getAccount());
......@@ -56,7 +70,37 @@ public class ContactTitleActionBarInflater {
activity.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(accountActionBarColors[colorLevel]));
}
public View getActionBarView() {
return actionBarView;
public void restoreDefaultTitleView(String title) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(defaultStatusBarColor);
}
activity.getSupportActionBar().setBackgroundDrawable(null);
activity.getSupportActionBar().setDisplayShowTitleEnabled(true);
actionBarView.setVisibility(View.GONE);
activity.setTitle(title);
}
public ImageView getSecurityView() {
return (ImageView)actionBarView.findViewById(R.id.security);
}
public void playIncomingAnimation() {
if (shakeAnimation == null) {
shakeAnimation = AnimationUtils.loadAnimation(activity, R.anim.shake);
}
actionBarView.findViewById(R.id.name_holder).startAnimation(shakeAnimation);
}
public void setOnClickListener(View.OnClickListener onClickListener) {
actionBarView.setOnClickListener(onClickListener);
}
public void setName(String name) {
((TextView) actionBarView.findViewById(R.id.name)).setText(name);
}
public void setStatusText(String user) {
((TextView) actionBarView.findViewById(R.id.status_text)).setText(user);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment