Guest User

Untitled

a guest
Nov 15th, 2010
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.75 KB | None | 0 0
  1. package org.kman.AndroMail;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5.  
  6. import org.kman.AndroMail.data.MailConstants;
  7.  
  8. import android.app.Activity;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.database.Cursor;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.provider.BaseColumns;
  15. import android.util.Log;
  16. import android.view.LayoutInflater;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.view.View.OnClickListener;
  20. import android.widget.AdapterView;
  21. import android.widget.CursorAdapter;
  22. import android.widget.ListAdapter;
  23. import android.widget.ListView;
  24. import android.widget.TextView;
  25. import android.widget.AdapterView.OnItemClickListener;
  26.  
  27. public class MessageListActivity extends Activity {
  28.  
  29.     private static final String TAG = "MessageListActivity";
  30.  
  31.     public static final String MESSAGE_LIST_URI_EXTRA = "MESSAGE_LIST_URI";
  32.  
  33.     @Override
  34.     public void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.messagelist);
  37.  
  38.         Intent intent = getIntent();
  39.         mMessageListUri = intent.getParcelableExtra(MESSAGE_LIST_URI_EXTRA);
  40.         mMessageListView = (ListView) findViewById(R.id.message_list);
  41.  
  42.         /*
  43.          * Set list view click handler
  44.          */
  45.  
  46.         mMessageListView.setOnItemClickListener(new OnItemClickListener() {
  47.             @Override
  48.             public void onItemClick(AdapterView<?> parnet, View view, int position, long id) {
  49.                 onMessageClick(position, id);
  50.             }
  51.         });
  52.  
  53.         /*
  54.          * Set list view adapter
  55.          */
  56.         mListCursor = managedQuery(mMessageListUri, MessageAdapter.gProjectionMap, null, null, null);
  57.         mAdapter = new MessageAdapter(this, mListCursor);
  58.         mMessageListView.setAdapter(mAdapter);
  59.  
  60.         int count = mListCursor.getCount();
  61.         Log.i(TAG, "Cursor message count = " + String.valueOf(count));
  62.     }
  63.  
  64.     private void onMessageClick(int position, long id) {
  65.         Log.i(TAG, "onMessageClick: pos = " + String.valueOf(position) + ", id = " + id);
  66.  
  67.         Uri messageUri = Uri.withAppendedPath(mMessageListUri, String.valueOf(id));
  68.         Intent intent = new Intent(this, MessageDisplayActivity.class);
  69.         intent.putExtra(MessageDisplayActivity.MESSAGE_URI_EXTRA, messageUri);
  70.         startActivity(intent);
  71.     }
  72.  
  73.     private static final class MessageAdapter extends CursorAdapter {
  74.  
  75.         public static final String gProjectionMap[] = new String[] { BaseColumns._ID,
  76.                 MailConstants.MESSAGE_SUBJECT, MailConstants.MESSAGE_FROM,
  77.                 MailConstants.MESSAGE_WHEN, MailConstants.MESSAGE_MSG_ID };
  78.  
  79.         private static final int VIEW_TYPE_GROUP_START = 0;
  80.         private static final int VIEW_TYPE_GROUP_CONT = 1;
  81.         private static final int VIEW_TYPE_COUNT = 2;
  82.  
  83.         MessageAdapter(Context context, Cursor cursor) {
  84.             super(context, cursor);
  85.  
  86.             mInflater = LayoutInflater.from(context);
  87.  
  88.             mColSubject = cursor.getColumnIndex(MailConstants.MESSAGE_SUBJECT);
  89.             mColFrom = cursor.getColumnIndex(MailConstants.MESSAGE_FROM);
  90.             mColWhen = cursor.getColumnIndex(MailConstants.MESSAGE_WHEN);
  91.             mColMsgID = cursor.getColumnIndex(MailConstants.MESSAGE_MSG_ID);
  92.         }
  93.  
  94.         @Override
  95.         public int getViewTypeCount() {
  96.             return VIEW_TYPE_COUNT;
  97.         }
  98.  
  99.         @Override
  100.         public int getItemViewType(int position) {
  101.             /*
  102.              * There is always a group header before the first data item
  103.              */
  104.             if (position == 0) {
  105.                 return VIEW_TYPE_GROUP_START;
  106.             }
  107.  
  108.             /*
  109.              * Get date for current and previous data items
  110.              */
  111.  
  112.             Cursor cursor = getCursor();
  113.  
  114.             int oldPosition = cursor.getPosition();
  115.  
  116.             cursor.moveToPosition(position);
  117.             Date dThis = new Date(cursor.getLong(mColWhen));
  118.  
  119.             cursor.moveToPosition(position - 1);
  120.             Date dPrev = new Date(cursor.getLong(mColWhen));
  121.  
  122.             /*
  123.              * Restore cursor position
  124.              */
  125.  
  126.             cursor.moveToPosition(oldPosition);
  127.  
  128.             /*
  129.              * Do grouping check
  130.              *
  131.              * This is bogus (compares day of month values), and should be
  132.              * replaced to use day of year
  133.              */
  134.  
  135.             if (dThis.getDay() != dPrev.getDay()) {
  136.                 return VIEW_TYPE_GROUP_START;
  137.             }
  138.  
  139.             return VIEW_TYPE_GROUP_CONT;
  140.         }
  141.  
  142.         @Override
  143.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  144.  
  145.             int nViewType;
  146.  
  147.             int position = cursor.getPosition();
  148.             if (position == 0) {
  149.                 /*
  150.                  * Group header for position 0
  151.                  */
  152.                 nViewType = VIEW_TYPE_GROUP_START;
  153.             } else {
  154.                 /*
  155.                  * Compare date fields for this and previous items
  156.                  */
  157.                 Date dThis = new Date(cursor.getLong(mColWhen));
  158.  
  159.                 cursor.moveToPosition(position - 1);
  160.                 Date dPrev = new Date(cursor.getLong(mColWhen));
  161.  
  162.                 /*
  163.                  * This check is bogus and should be replaced
  164.                  */
  165.                 if (dThis.getDay() != dPrev.getDay()) {
  166.                     nViewType = VIEW_TYPE_GROUP_START;
  167.                 } else {
  168.                     nViewType = VIEW_TYPE_GROUP_CONT;
  169.                 }
  170.  
  171.                 /*
  172.                  * Restore cursor position
  173.                  */
  174.                 cursor.moveToPosition(position);
  175.             }
  176.  
  177.             View v;
  178.  
  179.             if (nViewType == VIEW_TYPE_GROUP_START) {
  180.                 /*
  181.                  * Inflate a layout for group start
  182.                  */
  183.                 v = mInflater.inflate(R.layout.message_list_item_with_header, parent, false);
  184.  
  185.                 /*
  186.                  * Prevent clicking behaviour on the list header
  187.                  */
  188.                 View vHeader = v.findViewById(R.id.message_item_when_header);
  189.                 vHeader.setOnClickListener(new OnClickListener() {
  190.                     @Override
  191.                     public void onClick(View v) {
  192.                     }
  193.                 });
  194.             } else {
  195.                 /*
  196.                  * Inflate a layout for intra-group item
  197.                  */
  198.                 v = mInflater.inflate(R.layout.message_list_item, parent, false);
  199.             }
  200.             return v;
  201.         }
  202.  
  203.         @Override
  204.         public void bindView(View view, Context context, Cursor cursor) {
  205.             TextView tv;
  206.  
  207.             tv = (TextView) view.findViewById(R.id.message_item_subject);
  208.             tv.setText(cursor.getString(mColSubject));
  209.  
  210.             tv = (TextView) view.findViewById(R.id.message_item_from);
  211.             tv.setText(cursor.getString(mColFrom));
  212.  
  213.             tv = (TextView) view.findViewById(R.id.message_item_when);
  214.             Date d = new Date(cursor.getLong(mColWhen));
  215.             tv.setText(gDateFormatData.format(d));
  216.  
  217.             tv = (TextView) view.findViewById(R.id.message_item_id);
  218.             tv.setText(cursor.getString(mColMsgID));
  219.  
  220.             /*
  221.              * If there is a group header, set its value to just the date
  222.              * (without the time)
  223.              */
  224.             tv = (TextView) view.findViewById(R.id.message_item_when_header);
  225.             if (tv != null) {
  226.                 tv.setText(gDateFormatGroup.format(d));
  227.             }
  228.         }
  229.  
  230.         LayoutInflater mInflater;
  231.  
  232.         private int mColSubject;
  233.         private int mColFrom;
  234.         private int mColWhen;
  235.         private int mColMsgID;
  236.  
  237.         private static SimpleDateFormat gDateFormatData = new SimpleDateFormat(
  238.                 "E, dd MMM yyyy HH:mm:ss");
  239.         private static SimpleDateFormat gDateFormatGroup = new SimpleDateFormat("E, dd MMM yyyy");
  240.     }
  241.  
  242.     private Uri mMessageListUri;
  243.     private ListView mMessageListView;
  244.     private Cursor mListCursor;
  245.     private ListAdapter mAdapter;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment