package com.mydomain.app.activity;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.mydomain.app.utils.*;
public class ActivityInbox extends ListActivity implements OnClickListener, StateModelListener {
private ArrayList<MessageSummary> m_aryMessages = null;
private InboxAdapter m_Adapter;
private final Runnable m_runUpdateDisplay = new Runnable() {
public void run() {
updateDisplay();
}
};
private Runnable viewOrders;
private static final String INBOX_STATE_MODEL = "InboxStateModel";
// handler for thread posts
private final Handler m_hdlHandler = new Handler();
// model to manage thread state
private StateModel m_smInbox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_inbox);
// if the first time in, create state model for activity
if (m_smInbox == null) {
m_smInbox = new StateModel();
}
// TODO set pointers to ui controls
// TODO put cache logic here
if(savedInstanceState == null) {
viewOrders = new Runnable() {
public void run() {
launchInboxThread();
}
};
// TODO update this to use State Model
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
}
}
@Override
protected void onPause() {
super.onPause();
// detach from the model
m_smInbox.setLoginModelListener(null);
}
@Override
protected void onResume() {
super.onResume();
// attach to the model
m_smInbox.setLoginModelListener(this);
// synchronize the display, in case the thread completed
// while this activity was not visible. For example, if
// a phone call occurred while the thread was running.
updateDisplay();
}
public void stateModelChanged(StateModel lm) {
// this may be called from a background thread, so post to the handler
m_hdlHandler.post(m_runUpdateDisplay);
}
public void onClick(View v) {
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save screen state
outState.putSerializable(INBOX_STATE_MODEL, m_smInbox);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// restore screen state
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(INBOX_STATE_MODEL)) {
m_smInbox = (StateModel)savedInstanceState.getSerializable(INBOX_STATE_MODEL);
}
}
}
private final Runnable returnRes = new Runnable() {
public void run() {
if(m_aryMessages != null && m_aryMessages.size() > 0) {
m_Adapter.notifyDataSetChanged();
for(int i=0;i<m_aryMessages.size();i++)
m_Adapter.add(m_aryMessages.get(i));
m_Adapter.notifyDataSetChanged();
}
}
};
private void launchInboxThread() {
// TODO launch inbox thread
m_aryMessages = new ArrayList<MessageSummary>();
MessageSummary msgSum = new MessageSummary();
msgSum.setMessage("SF services");
msgSum.setSummary("Pending");
MessageSummary msgSum2 = new MessageSummary();
msgSum2.setMessage("SF advertisement");
msgSum2.setSummary("Completed");
m_aryMessages.add(msgSum);
m_aryMessages.add(msgSum2);
runOnUiThread(returnRes);
}
private void updateDisplay() {
// TODO update display
}
}
final class InboxAdapter extends ArrayAdapter<MessageSummary> {
private ArrayList<MessageSummary> items;
public InboxAdapter(Context context, int textViewResourceId, ArrayList<MessageSummary> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.layout_inbox_item, null);
}
MessageSummary msgSum = items.get(position);
if (msgSum != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText("Name: "+msgSum.getMessage());
}
if(bt != null) {
bt.setText("Status: "+ msgSum.getSummary());
}
}
return v;
}
}