import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.easychat.constant.Global;
import com.easychat.data.ChatInfo;
import com.easychat.data.UserInfoChat;
import com.easychat.mlisteners.ChatReceiver;
import com.easychat.parsedata.History;
import com.easychat.parsedata.HistoryallData;
import com.easychat.parsedata.HistoryallListData;
import com.easychat.parsedata.Userdata;
import com.easychat.quickactionbar.ActionItem;
import com.easychat.quickactionbar.QuickAction;
import com.easychat.rinterface.ResponseListener;
import com.easychat.task.HttpGetAsync;
import com.easychat.task.HttpPostAsync;
import com.easychat.utility.AroundLogger;
import com.easychat.utility.Utility;
import com.easychat.xmpp.XmppClient;
import com.google.gson.Gson;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;
public class ChatListAct extends Default implements OnClickListener, ChatReceiver {
private static final String TAG = "UserlistAct-";
//Widgets
EditText cedtchat;
Button cbtnsend;
ListView lvwUserlist;
TextView tvUsername;
ImageView ivStatus;
ImageButton imgbtnSmiley;
static HorizontalScrollView hScrollFooter;
ChatListAdapter mAdapter;
String username;
String ownId;
String userId;
String token;
String ownUsername;
public static String userProfileImage;
public static String ownProfileImage;
QuickAction quickAction;
HistoryallListData historyparselist;
List<HistoryallData> historylist;
ArrayList<ChatInfo> chatList;
Context mContext;
// XmppService mXmppService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.chatlist);
mContext = this;
bindComponents();
init();
addListeners();
}
private void bindComponents() {
AroundLogger.info(TAG + " bindComponents-");
lvwUserlist = (ListView) findViewById(R.id.clistchat);
hScrollFooter = (HorizontalScrollView) findViewById(R.id.hscrollview);
cedtchat = (EditText)findViewById(R.id.cedtchat);
cbtnsend = (Button)findViewById(R.id.cbtnsend);
tvUsername = (TextView) findViewById(R.id.tvUsername);
ivStatus = (ImageView) findViewById(R.id.ivStatus);
imgbtnSmiley = (ImageButton)findViewById(R.id.imgbtnSmiley);
}
private void init() {
AroundLogger.info(TAG + " init-");
chatList = new ArrayList<ChatInfo>();
mAdapter = new ChatListAdapter();
hScrollFooter.setVisibility(View.GONE);
lvwUserlist.setFocusable(false);
lvwUserlist.setFocusableInTouchMode(false);
lvwUserlist.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lvwUserlist.setStackFromBottom(true);
lvwUserlist.setAdapter(mAdapter);
ownUsername = Utility.getSharedKey("username", this);
username = getIntent().getStringExtra("username");
userId = getIntent().getStringExtra("userid");
ownId = Utility.getSharedKey("userid", this);
if(BuildConfig.DEBUG) {
AroundLogger.info(TAG + " init - userId=" + userId + " ownId=" + ownId);
}
GetUserInfo(userId, false);
GetUserInfo(ownId, true);
if(getIntent().getBooleanExtra("fromhistory", false)) {
AllchatHistory();
}
/*mAdapter = new ChatListAdapter(this, new ArrayList<ChatInfo>());
lvwUserlist.setAdapter(mAdapter);
lvwUserlist.setDivider(null);
lvwUserlist.setDividerHeight(0);*/
// userid = getIntent().getStringExtra("userid");
// status = getIntent().getStringExtra("status");
// sender_id = getIntent().getStringExtra("userid");
// Log.i(Global.TAG, "init - Status--->: "+status);
/*if(!TextUtils.isEmpty(status)) {
if (status.equalsIgnoreCase(getResources().getString(R.string.S_online))) {
ivStatus.setImageResource(R.drawable.green);
} else if (status.equalsIgnoreCase(getResources().getString(R.string.S_busy))) {
ivStatus.setImageResource(R.drawable.orange);
} else if (status.equalsIgnoreCase(getResources().getString(R.string.S_do_not_disturb))) {
ivStatus.setImageResource(R.drawable.grey);
} else if (status.equalsIgnoreCase(getResources().getString(R.string.S_Offline))) {
ivStatus.setImageResource(R.drawable.red);
}
}*/
String text = getIntent().getStringExtra("message");
if(!TextUtils.isEmpty(text)) {
ChatInfo mChatInfo = new ChatInfo();
mChatInfo.setComment(text);
mChatInfo.setReceiver(ownUsername);
mChatInfo.setSender(username);
mChatInfo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
chatList.add(mChatInfo);
notifyAdapter();
// mAdapter.add(mChatInfo);
/*Message message = new Message(ownUsername, Message.Type.chat);
message.setFrom(username);
message.setBody(text);
mAdapter.add(message);*/
}
tvUsername.setText(username);
setUpSmiles();
cedtchat.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
cedtchat.setText(Global.getSmiledText(ChatListAct.this, cedtchat.getText().toString()));
}
});
}
private void addListeners() {
AroundLogger.info(TAG + " addListeners-");
imgbtn_back.setOnClickListener(this);
cbtnsend.setOnClickListener(this);
imgbtnSmiley.setOnClickListener(this);
}
@Override
public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.imgbtn_back:
finish();
break;
case R.id.cbtnsend:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(cedtchat.getWindowToken(), 0);
String comment = cedtchat.getText().toString();
if(comment.trim().length() == 0) {
return;
}
cedtchat.setText("");
UserInfoChat mInfoChat = new UserInfoChat();
// mInfoChat.senderId = sender_id;
mInfoChat.userid = ownId;
Message message = new Message( Global.getReceiver(username), Message.Type.chat);
message.setFrom(Utility.getSharedKey("username", v.getContext()));
// message.setProperty("userid", userId);
message.setProperty("userinfo", mInfoChat);
message.setBody(comment);
XmppClient.sendMessage(this, message);
mAdapter.add(message);
notifyAdapter();
PostChat(comment);
break;
case R.id.imgbtnSmiley:
quickAction.show(v);
break;
default:
break;
}
}
@Override
public void receive(Message message) {
if(mAdapter != null) {
mAdapter.add(message);
notifyAdapter();
}
}
private void notifyAdapter() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
@Override
protected void onStart() {
super.onStart();
XmppClient.setCurrentChatListener(username, this);
// XmppClient.addChatReceiver(this);
}
@Override
protected void onStop() {
super.onStop();
XmppClient.removeCurrentChatListener();
// XmppClient.removeChatReceiver(this);
}
public void PostChat(String content) {
AroundLogger.info(TAG + "Post Chat is called---");
String token = Utility.getSharedKey("token", this);
List<Header> lstHeader = new ArrayList<Header>();
lstHeader.add(new BasicHeader("X-Apn-Authorization", token));
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("receiver_id", userId));
nameValuePairs.add(new BasicNameValuePair("content", content));
HttpPostAsync post = new HttpPostAsync(this);
post.isProgressBarEnabled(false);
post.setHeader(lstHeader);
post.setResponseListener(new ResponseListener() {
@Override
public void responseListener(String response) {
AroundLogger.info(TAG + "responseListener - response: " + response);
}
});
String StrUrl = Global.HOST + "chatHistory";
post.execute(StrUrl, nameValuePairs);
}
private void setUpSmiles() {
quickAction = new QuickAction(this, QuickAction.VERTICAL);
addActionItem(R.drawable.wink_, ";)");
addActionItem(R.drawable.smile, ":-)");
addActionItem(R.drawable.grin_, ":D");
addActionItem(R.drawable.grumpy, ">:c");
addActionItem(R.drawable.sunglasses, "8-)");
addActionItem(R.drawable.gasp, ":-o");
addActionItem(R.drawable.upset, ">.<");
addActionItem(R.drawable.cry, ":'c");
addActionItem(R.drawable.sad, ":(");
addActionItem(R.drawable.the_softy, ":'c");
addActionItem(R.drawable.the_sketchbag, "x-c");
//Set listener for action item clicked
quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction source, int pos, int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos);
cedtchat.setText(Global.getSmiledText(ChatListAct.this, cedtchat.getText() + actionItem.getSmileyName()));
cedtchat.setSelection(cedtchat.getText().length());
}
});
}
private void addActionItem(int drawable, String text) {
ActionItem actionItem = new ActionItem(drawable, null, getResources().getDrawable(drawable), text);
quickAction.addActionItem(actionItem);
}
public void AllchatHistory()
{
AroundLogger.info(TAG + "AllChatHistory is called-----");
token = Utility.getSharedKey("token",ChatListAct.this);
List<Header> lstHeader = new ArrayList<Header>();
lstHeader.add(new BasicHeader("X-Apn-Authorization",token));
HttpGetAsync get = new HttpGetAsync(mContext);
get.setHeader(lstHeader);
get.setResponseListener(new ResponseListener() {
@Override
public void responseListener(String response) {
AroundLogger.info(TAG + "responseListener - response: " + response);
if(!response.equalsIgnoreCase("")) {
Gson gson = new Gson();
historyparselist = gson.fromJson(response, HistoryallListData.class);
Map<Integer, History> mHashMap = historyparselist.historyList;
for (Integer key : mHashMap.keySet()) {
History mHistories = mHashMap.get(key);
ChatInfo chat= new ChatInfo();
chat.setSender(mHistories.sender);
chat.setReceiver(mHistories.receiver);
chat.setComment(mHistories.content);
chat.setTime(mHistories.time);
chatList.add(chat);
notifyAdapter();
}
/* historylist = historyparselist.historylist;
HistoryallData historydata;
for(int i=0;i<historylist.size();i++)
{
ChatInfo chat= new ChatInfo();
historydata=historylist.get(i);
chat.setSender(historydata.senderusername);
chat.setReceiver(ownUsername);
chat.setComment(historydata.content);
chat.setTime(historydata.createdate);
chatList.add(chat);
notifyAdapter();
}
*/
Collections.sort(chatList, mComparator);
notifyAdapter();
}
}
});
String StrUrl = Global.HOST+"chatHistory/" + userId;
get.execute(StrUrl);
}
Comparator<ChatInfo> mComparator = new Comparator<ChatInfo>() {
@Override
public int compare(ChatInfo lhs, ChatInfo rhs) {
return lhs.getTime().compareTo(rhs.getTime());
}
};
public void GetUserInfo(String uId, final boolean isOwnProfile) {
AroundLogger.info(TAG + "GetUser info is called---");
token = Utility.getSharedKey("token", this);
List<Header> lstHeader = new ArrayList<Header>();
lstHeader.add(new BasicHeader("X-Apn-Authorization", token));
AroundLogger.info(TAG + "User Info called---");
HttpGetAsync get = new HttpGetAsync(this);
get.setHeader(lstHeader);
get.setResponseListener(new ResponseListener() {
@Override
public void responseListener(String response) {
AroundLogger.info(TAG + "responseListener - response: " + response);
if(!TextUtils.isEmpty(response)) {
try {
Gson gson = new Gson();
Userdata userprofiledata = gson.fromJson(response,Userdata.class);
if(!isOwnProfile) {
String status = userprofiledata.status;
AroundLogger.info(TAG + "responseListener - Status--->: "+status);
if (status.equalsIgnoreCase(getResources().getString(R.string.S_online))) {
ivStatus.setImageResource(R.drawable.green);
} else if (status.equalsIgnoreCase(getResources().getString(R.string.S_busy))) {
ivStatus.setImageResource(R.drawable.orange);
} else if (status.equalsIgnoreCase(getResources().getString(R.string.dnd))) {
ivStatus.setImageResource(R.drawable.grey);
} else if (status.equalsIgnoreCase(getResources().getString(R.string.S_Offline))) {
ivStatus.setImageResource(R.drawable.red);
}
userProfileImage = userprofiledata.thumbnail_url;
tvUsername.setText(Global.getDisplayName(userprofiledata.nickname, userprofiledata.firstname, userprofiledata.lname, userprofiledata.username));
}else {
ownProfileImage = userprofiledata.thumbnail_url;
}
} catch (Exception e) {
// TODO: handle exception
AroundLogger.info(TAG + "catch clause....");
}
}
}
});
AroundLogger.info(TAG + "GetUserInfo - ");
String StrUrl = Global.HOST + "users/" + uId;
get.execute(StrUrl);
}
public class ChatListAdapter extends BaseAdapter{
String receiver;
SimpleDateFormat mDateFormat;
SimpleDateFormat mDateFormat2;
public ChatListAdapter() {
receiver = Utility.getSharedKey("username", ChatListAct.this);
chatList = new ArrayList<ChatInfo>();
mDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa");
mDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
public void add(ChatInfo mChatInfo) {
chatList.add(mChatInfo);
// notifyDataSetChanged();
}
public void add(Message message) {
ChatInfo mChatInfo = new ChatInfo();
if(message.getFrom().contains("@")) {
mChatInfo.setSender(StringUtils.parseName(message.getFrom()));
}else {
mChatInfo.setSender(message.getFrom());
}
mChatInfo.setReceiver(receiver);
mChatInfo.setComment(message.getBody());
mChatInfo.setTime(mDateFormat2.format(new Date()));
chatList.add(mChatInfo);
// notifyDataSetChanged();
}
@Override
public int getCount() {
return chatList.size();
}
@Override
public Object getItem(int position) {
return chatList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ChatInfo mChatInfo = chatList.get(position);
if(mChatInfo.getSender() == null || mChatInfo.getSender().equalsIgnoreCase(receiver)) {
convertView = getLayoutInflater().inflate(R.layout.chatlist_row_own, null);
}else {
convertView = getLayoutInflater().inflate(R.layout.chatlist_row_user, null);
}
TextView tvUsername = (TextView) convertView.findViewById(R.id.tvUsername);
ImageView userimage = (ImageView) convertView.findViewById(R.id.ivwuser);
if(mChatInfo.getSender() == null || mChatInfo.getSender().equalsIgnoreCase(receiver)) {
UrlImageViewHelper.setUrlDrawable(userimage, ChatListAct.ownProfileImage, R.drawable.no_image);
tvUsername.setText("Me");
}else {
UrlImageViewHelper.setUrlDrawable(userimage, ChatListAct.userProfileImage, R.drawable.no_image);
tvUsername.setText(mChatInfo.getSender());
}
TextView mView = (TextView)convertView.findViewById(R.id.tvComment);
mView.setText(Global.getSmiledText(mContext, mChatInfo.getComment()));
TextView tvChatTime = (TextView) convertView.findViewById(R.id.tvChatTime);
try {
tvChatTime.setText((mDateFormat.format(mDateFormat2.parse(mChatInfo.getTime()))));
} catch (ParseException e) {
e.printStackTrace();
}
return convertView;
}
}
}
create class name ChatListAdapter
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.easychat.app.R;
import com.easychat.constant.Global;
import com.easychat.data.ChatInfo;
import com.easychat.utility.Utility;
public class ChatListAdapter extends BaseAdapter{
ArrayList <ChatInfo> list;
Context mContext;
String receiver;
SimpleDateFormat mDateFormat;
public ChatListAdapter(Context context, ArrayList<ChatInfo> mList) {
this.mContext = context;
receiver = Utility.getSharedKey("username", context);
list = new ArrayList<ChatInfo>();
list = mList;
mDateFormat = new SimpleDateFormat("HH:mm aa");
}
public void add(ChatInfo mChatInfo) {
list.add(mChatInfo);
// notifyDataSetChanged();
}
public void add(Message message) {
ChatInfo mChatInfo = new ChatInfo();
if(message.getFrom().contains("@")) {
mChatInfo.setSender(StringUtils.parseName(message.getFrom()));
}else {
mChatInfo.setSender(message.getFrom());
}
mChatInfo.setReceiver(receiver);
mChatInfo.setComment(message.getBody());
mChatInfo.setTime(mDateFormat.format(new Date()));
list.add(mChatInfo);
// notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ChatInfo mChatInfo = list.get(position);
if(mChatInfo.getSender() == null || mChatInfo.getSender().equals(receiver)) {
convertView = ((Activity)mContext).getLayoutInflater().inflate(R.layout.chatlist_row_own, null);
}else {
convertView = ((Activity)mContext).getLayoutInflater().inflate(R.layout.chatlist_row_user, null);
}
if(mChatInfo.getSender() == null || mChatInfo.getSender().equals(receiver)) {
}else {
}
TextView mView = (TextView)convertView.findViewById(R.id.tvComment);
mView.setText(Global.getSmiledText(mContext, mChatInfo.getComment()));
TextView tvUsername = (TextView) convertView.findViewById(R.id.tvUsername);
tvUsername.setText(mChatInfo.getSender());
TextView tvChatTime = (TextView) convertView.findViewById(R.id.tvChatTime);
tvChatTime.setText((mChatInfo.getTime()));
return convertView;
}
}
create class name QuickAction
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.easychat.app.R;
/**
* QuickAction dialog, shows action list as icon and text like the one in Gallery3D app. Currently supports vertical
* and horizontal layout.
*
* @author Lorensius W. L. T <lorenz@londatiga.net>
*
* Contributors:
* - Kevin Peck <kevinwpeck@gmail.com>
*/
public class QuickAction extends PopupWindows implements OnDismissListener {
private View mRootView;
private ImageView mArrowUp;
private ImageView mArrowDown;
private LayoutInflater mInflater;
private ViewGroup mTrack;
private ScrollView mScroller;
private OnActionItemClickListener mItemClickListener;
private OnDismissListener mDismissListener;
private List<ActionItem> actionItems = new ArrayList<ActionItem>();
private boolean mDidAction;
private int mChildPos;
private int mInsertPos;
private int mAnimStyle;
private int mOrientation;
private int rootWidth=0;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
public static final int ANIM_GROW_FROM_LEFT = 1;
public static final int ANIM_GROW_FROM_RIGHT = 2;
public static final int ANIM_GROW_FROM_CENTER = 3;
public static final int ANIM_REFLECT = 4;
public static final int ANIM_AUTO = 5;
/**
* Constructor for default vertical layout
*
* @param context Context
*/
public QuickAction(Context context) {
this(context, VERTICAL);
}
/**
* Constructor allowing orientation override
*
* @param context Context
* @param orientation Layout orientation, can be vartical or horizontal
*/
public QuickAction(Context context, int orientation) {
super(context);
mOrientation = orientation;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (mOrientation == HORIZONTAL) {
setRootViewId(R.layout.popup_horizontal);
} else {
setRootViewId(R.layout.popup_vertical);
}
mAnimStyle = ANIM_AUTO;
mChildPos = 0;
}
/**
* Get action item at an index
*
* @param index Index of item (position from callback)
*
* @return Action Item at the position
*/
public ActionItem getActionItem(int index) {
return actionItems.get(index);
}
/**
* Set root view.
*
* @param id Layout resource id
*/
public void setRootViewId(int id) {
mRootView = (ViewGroup) mInflater.inflate(id, null);
mTrack = (ViewGroup) mRootView.findViewById(R.id.tracks);
mArrowDown = (ImageView) mRootView.findViewById(R.id.arrow_down);
mArrowUp = (ImageView) mRootView.findViewById(R.id.arrow_up);
mScroller = (ScrollView) mRootView.findViewById(R.id.scroller);
//This was previously defined on show() method, moved here to prevent force close that occured
//when tapping fastly on a view to show quickaction dialog.
//Thanx to zammbi (github.com/zammbi)
mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(mRootView);
}
/**
* Set animation style
*
* @param mAnimStyle animation style, default is set to ANIM_AUTO
*/
public void setAnimStyle(int mAnimStyle) {
this.mAnimStyle = mAnimStyle;
}
/**
* Set listener for action item clicked.
*
* @param listener Listener
*/
public void setOnActionItemClickListener(OnActionItemClickListener listener) {
mItemClickListener = listener;
}
/**
* Add action item
*
* @param action {@link ActionItem}
*/
public void addActionItem(ActionItem action) {
actionItems.add(action);
String title = action.getTitle();
Drawable icon = action.getIcon();
View container;
if (mOrientation == HORIZONTAL) {
container = mInflater.inflate(R.layout.action_item_horizontal, null);
} else {
container = mInflater.inflate(R.layout.action_item_vertical, null);
}
ImageView img = (ImageView) container.findViewById(R.id.iv_icon);
TextView text = (TextView) container.findViewById(R.id.tv_title);
if (icon != null) {
img.setImageDrawable(icon);
} else {
img.setVisibility(View.GONE);
}
if (title != null) {
text.setText(title);
} else {
text.setVisibility(View.GONE);
}
final int pos = mChildPos;
final int actionId = action.getActionId();
container.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(QuickAction.this, pos, actionId);
}
if (!getActionItem(pos).isSticky()) {
mDidAction = true;
dismiss();
}
}
});
container.setFocusable(true);
container.setClickable(true);
if (mOrientation == HORIZONTAL && mChildPos != 0) {
View separator = mInflater.inflate(R.layout.horiz_separator, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
separator.setLayoutParams(params);
separator.setPadding(5, 0, 5, 0);
mTrack.addView(separator, mInsertPos);
mInsertPos++;
}
mTrack.addView(container, mInsertPos);
mChildPos++;
mInsertPos++;
}
/**
* Show quickaction popup. Popup is automatically positioned, on top or bottom of anchor view.
*
*/
public void show (View anchor) {
preShow();
int xPos, yPos, arrowPos;
mDidAction = false;
int[] location = new int[2];
anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1]
+ anchor.getHeight());
//mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootHeight = mRootView.getMeasuredHeight();
if (rootWidth == 0) {
rootWidth = mRootView.getMeasuredWidth();
}
int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
int screenHeight = mWindowManager.getDefaultDisplay().getHeight();
//automatically get X coord of popup (top left)
if ((anchorRect.left + rootWidth) > screenWidth) {
xPos = anchorRect.left - (rootWidth-anchor.getWidth());
xPos = (xPos < 0) ? 0 : xPos;
arrowPos = anchorRect.centerX()-xPos;
} else {
if (anchor.getWidth() > rootWidth) {
xPos = anchorRect.centerX() - (rootWidth/2);
} else {
xPos = anchorRect.left;
}
arrowPos = anchorRect.centerX()-xPos;
}
int dyTop = anchorRect.top;
int dyBottom = screenHeight - anchorRect.bottom;
boolean onTop = (dyTop > dyBottom) ? true : false;
if (onTop) {
if (rootHeight > dyTop) {
yPos = 15;
LayoutParams l = mScroller.getLayoutParams();
l.height = dyTop - anchor.getHeight();
} else {
yPos = anchorRect.top - rootHeight;
}
} else {
yPos = anchorRect.bottom;
if (rootHeight > dyBottom) {
LayoutParams l = mScroller.getLayoutParams();
l.height = dyBottom;
}
}
showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), arrowPos);
setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);
mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
/**
* Set animation style
*
* @param screenWidth screen width
* @param requestedX distance from left edge
* @param onTop flag to indicate where the popup should be displayed. Set TRUE if displayed on top of anchor view
* and vice versa
*/
private void setAnimationStyle(int screenWidth, int requestedX, boolean onTop) {
int arrowPos = requestedX - mArrowUp.getMeasuredWidth()/2;
switch (mAnimStyle) {
case ANIM_GROW_FROM_LEFT:
mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);
break;
case ANIM_GROW_FROM_RIGHT:
mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right : R.style.Animations_PopDownMenu_Right);
break;
case ANIM_GROW_FROM_CENTER:
mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);
break;
case ANIM_REFLECT:
mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Reflect : R.style.Animations_PopDownMenu_Reflect);
break;
case ANIM_AUTO:
if (arrowPos <= screenWidth/4) {
mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);
} else if (arrowPos > screenWidth/4 && arrowPos < 3 * (screenWidth/4)) {
mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);
} else {
mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right : R.style.Animations_PopDownMenu_Right);
}
break;
}
}
/**
* Show arrow
*
* @param whichArrow arrow type resource id
* @param requestedX distance from left screen
*/
private void showArrow(int whichArrow, int requestedX) {
final View showArrow = (whichArrow == R.id.arrow_up) ? mArrowUp : mArrowDown;
final View hideArrow = (whichArrow == R.id.arrow_up) ? mArrowDown : mArrowUp;
final int arrowWidth = mArrowUp.getMeasuredWidth();
showArrow.setVisibility(View.VISIBLE);
ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams)showArrow.getLayoutParams();
param.leftMargin = requestedX - arrowWidth / 2;
hideArrow.setVisibility(View.INVISIBLE);
}
/**
* Set listener for window dismissed. This listener will only be fired if the quicakction dialog is dismissed
* by clicking outside the dialog or clicking on sticky item.
*/
public void setOnDismissListener(QuickAction.OnDismissListener listener) {
setOnDismissListener(this);
mDismissListener = listener;
}
@Override
public void onDismiss() {
if (!mDidAction && mDismissListener != null) {
mDismissListener.onDismiss();
}
}
/**
* Listener for item click
*
*/
public interface OnActionItemClickListener {
public abstract void onItemClick(QuickAction source, int pos, int actionId);
}
/**
* Listener for window dismiss
*
*/
public interface OnDismissListener {
public abstract void onDismiss();
}
}
create class name PopupWindows
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
/**
* Custom popup window.
*
* @author Lorensius W. L. T <lorenz@londatiga.net>
*
*/
public class PopupWindows {
protected Context mContext;
protected PopupWindow mWindow;
protected View mRootView;
protected Drawable mBackground = null;
protected WindowManager mWindowManager;
/**
* Constructor.
*
* @param context Context
*/
public PopupWindows(Context context) {
mContext = context;
mWindow = new PopupWindow(context);
mWindow.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
mWindow.dismiss();
return true;
}
return false;
}
});
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
/**
* On dismiss
*/
protected void onDismiss() {
}
/**
* On show
*/
protected void onShow() {
}
/**
* On pre show
*/
protected void preShow() {
if (mRootView == null)
throw new IllegalStateException("setContentView was not called with a view to display.");
onShow();
if (mBackground == null)
mWindow.setBackgroundDrawable(new BitmapDrawable());
else
mWindow.setBackgroundDrawable(mBackground);
mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setTouchable(true);
mWindow.setFocusable(true);
mWindow.setOutsideTouchable(true);
mWindow.setContentView(mRootView);
}
/**
* Set background drawable.
*
* @param background Background drawable
*/
public void setBackgroundDrawable(Drawable background) {
mBackground = background;
}
/**
* Set content view.
*
* @param root Root view
*/
public void setContentView(View root) {
mRootView = root;
mWindow.setContentView(root);
}
/**
* Set content view.
*
* @param layoutResID Resource id
*/
public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setContentView(inflator.inflate(layoutResID, null));
}
/**
* Set listener on window dismissed.
*
* @param listener
*/
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
mWindow.setOnDismissListener(listener);
}
/**
* Dismiss the popup window.
*/
public void dismiss() {
mWindow.dismiss();
}
}
create class name HistoryallListData
import java.io.Serializable;
import java.util.Map;
import com.google.gson.annotations.SerializedName;
public class HistoryallListData implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5319370330373259907L;
@SerializedName("chat_user_data")
public HistoryallData historyData;
@SerializedName("history")
public Map<Integer, History> historyList;
}
cretae class name ChatInfo
public class ChatInfo {
String sender;
String receiver;
String comment;
String image;
String time;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}