Advertisement
Guest User

Untitled

a guest
May 17th, 2011
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.77 KB | None | 0 0
  1. RootListActivity.java:
  2. ---------------------------------------------
  3.  
  4.  
  5. package org.bicou.nr.ui.legacy;
  6.  
  7. import org.bicou.nr.R;
  8. import org.bicou.nr.ui.MenuActionBar;
  9. import org.bicou.nr.ui.RootList;
  10. import org.bicou.nr.ui.MenuActionBar.MenuClickListener;
  11.  
  12. import android.app.ListActivity;
  13. import android.content.Intent;
  14. import android.os.Bundle;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.view.Window;
  19. import android.widget.ListView;
  20.  
  21. public class RootListActivity extends ListActivity {
  22.     RootList mRootList = new RootList();
  23.  
  24.     @Override
  25.     public void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  28.         setContentView(R.layout.rootlist);
  29.         MenuClickListener updateClick = new MenuClickListener(R.id.update);
  30.         findViewById(R.id.actionbar_btn_update).setOnClickListener(updateClick);
  31.         mRootList.onCreate(savedInstanceState, this, this);
  32.     }
  33.  
  34.     @Override
  35.     public void onListItemClick(ListView l, View v, int position, long id) {
  36.         openItem(position);
  37.     }
  38.  
  39.     @Override
  40.     public boolean onCreateOptionsMenu(Menu menu) {
  41.         return MenuActionBar.createOptionsMenu(this, menu);
  42.     }
  43.  
  44.     @Override
  45.     public boolean onOptionsItemSelected(MenuItem item) {
  46.         if (MenuActionBar.onOptionsItemSelected(this, item)) {
  47.             return true;
  48.         }
  49.         return super.onOptionsItemSelected(item);
  50.     }
  51.  
  52.     private void openItem(int index) {
  53.         Intent intent = new Intent(this, ItemsListActivity.class);
  54.         intent.putExtra(ItemsListActivity.EXTRA_LABEL, mRootList.getItemId(index));
  55.         startActivity(intent);
  56.     }
  57. }
  58.  
  59.  
  60.  
  61.  
  62. RootList.java:
  63. -----------------------
  64.  
  65.  
  66. package org.bicou.nr.ui;
  67.  
  68. import java.util.ArrayList;
  69. import java.util.List;
  70.  
  71. import org.bicou.nr.R;
  72. import org.bicou.nr.data.Label;
  73. import org.bicou.nr.data.Subscription;
  74. import org.bicou.nr.data.providers.Labels;
  75. import org.bicou.nr.data.providers.Subscriptions;
  76. import org.bicou.nr.util.C;
  77. import org.bicou.nr.util.L;
  78.  
  79. import android.app.Activity;
  80. import android.app.ListActivity;
  81. import android.app.ListFragment;
  82. import android.database.Cursor;
  83. import android.os.Bundle;
  84. import android.view.LayoutInflater;
  85. import android.view.View;
  86. import android.view.ViewGroup;
  87. import android.widget.BaseAdapter;
  88. import android.widget.ImageView;
  89. import android.widget.TextView;
  90.  
  91. public class RootList {
  92.     LayoutInflater mInflater;
  93.     private final List<ItemInRootList> mItemsInList = new ArrayList<ItemInRootList>();
  94.    
  95.     private Object mObject; // either the ListFragment or the ListActivity
  96.     private Activity mActivity;
  97.  
  98.     private class ItemInRootList {
  99.         public boolean isLabel;
  100.         public String id;
  101.         public String title;
  102.         public String unreadCount;
  103.     }
  104.  
  105.     /**
  106.      *
  107.      * @param savedInstanceState
  108.      * @param act
  109.      * @param lv
  110.      * @param obj: either the ListFragment or the ListActivity
  111.      */
  112.     public void onCreate(Bundle savedInstanceState, Activity act, Object obj) {
  113.         mActivity = act;
  114.         mObject = obj;
  115.         mInflater = mActivity.getLayoutInflater();
  116.  
  117.         refresh();
  118.     }
  119.    
  120.     public void refresh() {
  121.         createLabelsList();
  122.         setListAdapter();
  123.     }
  124.  
  125.     public void createLabelsList() {
  126.         L.logCurrentFunction(this);
  127.        
  128.         String rootOrdering = getRootOrdering();
  129.        
  130.         if (rootOrdering == null || rootOrdering.length() < 8) {
  131.             createDefaultLabelsList();
  132.         } else {
  133.             createOrderedLabelsList(rootOrdering);
  134.         }
  135.        
  136.         L.d("create labels list: "+mItemsInList.size()+" items");
  137.     }
  138.    
  139.     private void createDefaultLabelsList() {
  140.         String[] projection = new String[] { Labels.KEY_LABEL, Labels.KEY_ID, Labels.KEY_UNREAD_COUNT };
  141.         Cursor c = mActivity.managedQuery(Labels.CONTENT_URI, projection, null, null, null);
  142.        
  143.         if (c == null || c.getCount() == 0 || !c.moveToFirst()) {
  144.             return;
  145.         }
  146.  
  147.         mItemsInList.clear();
  148.         ItemInRootList item;
  149.        
  150.         do {
  151.             item = new ItemInRootList();
  152.             item.isLabel = true;
  153.             item.id = c.getString(c.getColumnIndex(Labels.KEY_ID));
  154.             item.title = Label.label(item.id);
  155.             //TODO: fix this label.label = c.getString(c.getColumnIndex(Labels.KEY_LABEL));
  156.             item.unreadCount = c.getString(c.getColumnIndex(Labels.KEY_UNREAD_COUNT));
  157.             mItemsInList.add(item);
  158.         } while (c.moveToNext());
  159.     }
  160.    
  161.     /**
  162.      * get the subscription ordering for the item user/-/state/com.google/root
  163.      * @return
  164.      */
  165.     private String getRootOrdering() {
  166.         String[] projection = new String[] { Labels.KEY_SUBSCRIPTION_ORDERING };
  167.         String where = Labels.KEY_ID + " LIKE '%/state/com.google/root'";
  168.         String[] whereArgs = null;
  169.         Cursor c = mActivity.getContentResolver().query(Labels.CONTENT_URI, projection, where, whereArgs, null);
  170.        
  171.         String order = null;
  172.         if (c != null) {
  173.             if (c.moveToFirst()) {
  174.                 order = c.getString(c.getColumnIndex(Labels.KEY_SUBSCRIPTION_ORDERING));
  175.             }
  176.             c.close();
  177.         }
  178.         return order;
  179.     }
  180.    
  181.     private Label getLabel(String sortid) {
  182.         String[] projection = new String[] { Labels.KEY_ID, Labels.KEY_UNREAD_COUNT };
  183.         String where = Labels.KEY_SORTID + " = ?";
  184.         String[] whereArgs = new String[] { sortid };
  185.  
  186.         Cursor c = mActivity.managedQuery(Labels.CONTENT_URI, projection, where, whereArgs, null);
  187.         Label label = null;
  188.        
  189.         if (c != null && c.moveToFirst()) {
  190.             try {
  191.                 label = new Label();
  192.                 label.id = c.getString(c.getColumnIndex(Labels.KEY_ID));
  193.                 label.unreadCount = c.getString(c.getColumnIndex(Labels.KEY_UNREAD_COUNT));
  194.             } catch (NumberFormatException nfe) {
  195.                 L.logException(nfe);
  196.             }
  197.         }
  198.        
  199.         return label;
  200.     }
  201.    
  202.     private Subscription getSubscription(String sortid) {
  203.         String[] projection = new String[] {
  204.                 Subscriptions.KEY_ID,
  205.                 Subscriptions.KEY_TITLE,
  206.                 Subscriptions.KEY_UNREAD_COUNT
  207.         };
  208.         String where = Subscriptions.KEY_SORTID + " = ?";
  209.         String[] whereArgs = new String[] { sortid };
  210.  
  211.         Cursor c = mActivity.managedQuery(Subscriptions.CONTENT_URI, projection, where, whereArgs, null);
  212.         Subscription subs = null;
  213.        
  214.         if (c != null && c.moveToFirst()) {
  215.             try {
  216.                 subs = new Subscription();
  217.                 subs.set(Subscription.TITLE, c.getString(c.getColumnIndex(Subscriptions.KEY_TITLE)));
  218.                 subs.set(Subscription.UNREAD_COUNT, c.getString(c.getColumnIndex(Subscriptions.KEY_UNREAD_COUNT)));
  219.             } catch (NumberFormatException nfe) {
  220.                 L.logException(nfe);
  221.             }
  222.         }
  223.        
  224.         return subs;
  225.     }
  226.    
  227.     private void createOrderedLabelsList(String order) {
  228.         int i, nbItems = order.length() / 8;
  229.         String sortid;
  230.         Label label;
  231.         Subscription subs;
  232.         ItemInRootList item;
  233.        
  234.         L.d("nb items in root: "+nbItems);
  235.        
  236.         for (i = 0; i < nbItems; i++) {
  237.             item = new ItemInRootList();
  238.             sortid = order.substring(i*8, (i+1)*8);
  239.             label = getLabel(sortid);
  240.             L.d("item #"+i+": "+sortid+" / "+label);
  241.            
  242.             if (label == null) {
  243.                 subs = getSubscription(sortid);
  244.                
  245.                 if (subs == null) {
  246.                     L.e("unable to show item "+sortid+" in ordered list!");
  247.                     continue;
  248.                 }
  249.                
  250.                 item.isLabel = false;
  251.                 item.id = subs.get(Subscription.ID);
  252.                 item.title = subs.get(Subscription.TITLE);
  253.                 item.unreadCount = subs.get(Subscription.UNREAD_COUNT);
  254.             } else {
  255.                 item.isLabel = true;
  256.                 item.id = label.id;
  257.                 item.title = label.label();
  258.                 item.unreadCount = label.unreadCount;
  259.             }
  260.            
  261.             mItemsInList.add(item);
  262.         }
  263.     }
  264.    
  265.     public void setListAdapter() {
  266.         if (C.IS_HONEYCOMB) {
  267.             ((ListFragment) mObject).setListAdapter(new SubscriptionsAdapter());
  268.         } else {
  269.             SubscriptionsAdapter ad = new SubscriptionsAdapter();
  270.             ((ListActivity) mObject).setListAdapter(ad);
  271.             L.d("adapter view type count: "+ad.getViewTypeCount());
  272.         }
  273.     }
  274.    
  275.     static class SuscriptionsViewsHolder {
  276.         TextView label;
  277.         ImageView icon;
  278.     }
  279.    
  280.     class SubscriptionsAdapter extends BaseAdapter {
  281.         @Override
  282.         public int getCount() {
  283.             return mItemsInList.size();
  284.         }
  285.  
  286.         @Override
  287.         public Object getItem(int id) {
  288.             return mItemsInList.get(id);
  289.         }
  290.  
  291.         @Override
  292.         public long getItemId(int arg0) {
  293.             return 0;
  294.         }
  295.  
  296.         @Override
  297.         public View getView(int position, View convertView, ViewGroup parent) {
  298.             SuscriptionsViewsHolder holder;
  299.             ItemInRootList item = mItemsInList.get(position);
  300.  
  301.             if (convertView == null) {
  302.                 convertView = mInflater.inflate(R.layout.label, null);
  303.  
  304.                 holder = new SuscriptionsViewsHolder();
  305.                 holder.label = (TextView) convertView.findViewById(R.id.label_label);
  306.                 holder.icon = (ImageView) convertView.findViewById(R.id.label_icon);
  307.  
  308.                 convertView.setTag(holder);
  309.             } else {
  310.                 holder = (SuscriptionsViewsHolder) convertView.getTag();
  311.             }
  312.  
  313.             holder.label.setText(item.title+" ("+item.unreadCount+")");
  314.             holder.icon.setImageResource(item.isLabel ? R.drawable.folder : R.drawable.file);
  315.  
  316.             return convertView;
  317.         }
  318.     }
  319.  
  320.     public String getItemId(int index) {
  321.         if (index < mItemsInList.size()) {
  322.             return mItemsInList.get(index).id;
  323.         }
  324.         return null;
  325.     }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement