Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RootListActivity.java:
- ---------------------------------------------
- package org.bicou.nr.ui.legacy;
- import org.bicou.nr.R;
- import org.bicou.nr.ui.MenuActionBar;
- import org.bicou.nr.ui.RootList;
- import org.bicou.nr.ui.MenuActionBar.MenuClickListener;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.Window;
- import android.widget.ListView;
- public class RootListActivity extends ListActivity {
- RootList mRootList = new RootList();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.rootlist);
- MenuClickListener updateClick = new MenuClickListener(R.id.update);
- findViewById(R.id.actionbar_btn_update).setOnClickListener(updateClick);
- mRootList.onCreate(savedInstanceState, this, this);
- }
- @Override
- public void onListItemClick(ListView l, View v, int position, long id) {
- openItem(position);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- return MenuActionBar.createOptionsMenu(this, menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (MenuActionBar.onOptionsItemSelected(this, item)) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private void openItem(int index) {
- Intent intent = new Intent(this, ItemsListActivity.class);
- intent.putExtra(ItemsListActivity.EXTRA_LABEL, mRootList.getItemId(index));
- startActivity(intent);
- }
- }
- RootList.java:
- -----------------------
- package org.bicou.nr.ui;
- import java.util.ArrayList;
- import java.util.List;
- import org.bicou.nr.R;
- import org.bicou.nr.data.Label;
- import org.bicou.nr.data.Subscription;
- import org.bicou.nr.data.providers.Labels;
- import org.bicou.nr.data.providers.Subscriptions;
- import org.bicou.nr.util.C;
- import org.bicou.nr.util.L;
- import android.app.Activity;
- import android.app.ListActivity;
- import android.app.ListFragment;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class RootList {
- LayoutInflater mInflater;
- private final List<ItemInRootList> mItemsInList = new ArrayList<ItemInRootList>();
- private Object mObject; // either the ListFragment or the ListActivity
- private Activity mActivity;
- private class ItemInRootList {
- public boolean isLabel;
- public String id;
- public String title;
- public String unreadCount;
- }
- /**
- *
- * @param savedInstanceState
- * @param act
- * @param lv
- * @param obj: either the ListFragment or the ListActivity
- */
- public void onCreate(Bundle savedInstanceState, Activity act, Object obj) {
- mActivity = act;
- mObject = obj;
- mInflater = mActivity.getLayoutInflater();
- refresh();
- }
- public void refresh() {
- createLabelsList();
- setListAdapter();
- }
- public void createLabelsList() {
- L.logCurrentFunction(this);
- String rootOrdering = getRootOrdering();
- if (rootOrdering == null || rootOrdering.length() < 8) {
- createDefaultLabelsList();
- } else {
- createOrderedLabelsList(rootOrdering);
- }
- L.d("create labels list: "+mItemsInList.size()+" items");
- }
- private void createDefaultLabelsList() {
- String[] projection = new String[] { Labels.KEY_LABEL, Labels.KEY_ID, Labels.KEY_UNREAD_COUNT };
- Cursor c = mActivity.managedQuery(Labels.CONTENT_URI, projection, null, null, null);
- if (c == null || c.getCount() == 0 || !c.moveToFirst()) {
- return;
- }
- mItemsInList.clear();
- ItemInRootList item;
- do {
- item = new ItemInRootList();
- item.isLabel = true;
- item.id = c.getString(c.getColumnIndex(Labels.KEY_ID));
- item.title = Label.label(item.id);
- //TODO: fix this label.label = c.getString(c.getColumnIndex(Labels.KEY_LABEL));
- item.unreadCount = c.getString(c.getColumnIndex(Labels.KEY_UNREAD_COUNT));
- mItemsInList.add(item);
- } while (c.moveToNext());
- }
- /**
- * get the subscription ordering for the item user/-/state/com.google/root
- * @return
- */
- private String getRootOrdering() {
- String[] projection = new String[] { Labels.KEY_SUBSCRIPTION_ORDERING };
- String where = Labels.KEY_ID + " LIKE '%/state/com.google/root'";
- String[] whereArgs = null;
- Cursor c = mActivity.getContentResolver().query(Labels.CONTENT_URI, projection, where, whereArgs, null);
- String order = null;
- if (c != null) {
- if (c.moveToFirst()) {
- order = c.getString(c.getColumnIndex(Labels.KEY_SUBSCRIPTION_ORDERING));
- }
- c.close();
- }
- return order;
- }
- private Label getLabel(String sortid) {
- String[] projection = new String[] { Labels.KEY_ID, Labels.KEY_UNREAD_COUNT };
- String where = Labels.KEY_SORTID + " = ?";
- String[] whereArgs = new String[] { sortid };
- Cursor c = mActivity.managedQuery(Labels.CONTENT_URI, projection, where, whereArgs, null);
- Label label = null;
- if (c != null && c.moveToFirst()) {
- try {
- label = new Label();
- label.id = c.getString(c.getColumnIndex(Labels.KEY_ID));
- label.unreadCount = c.getString(c.getColumnIndex(Labels.KEY_UNREAD_COUNT));
- } catch (NumberFormatException nfe) {
- L.logException(nfe);
- }
- }
- return label;
- }
- private Subscription getSubscription(String sortid) {
- String[] projection = new String[] {
- Subscriptions.KEY_ID,
- Subscriptions.KEY_TITLE,
- Subscriptions.KEY_UNREAD_COUNT
- };
- String where = Subscriptions.KEY_SORTID + " = ?";
- String[] whereArgs = new String[] { sortid };
- Cursor c = mActivity.managedQuery(Subscriptions.CONTENT_URI, projection, where, whereArgs, null);
- Subscription subs = null;
- if (c != null && c.moveToFirst()) {
- try {
- subs = new Subscription();
- subs.set(Subscription.TITLE, c.getString(c.getColumnIndex(Subscriptions.KEY_TITLE)));
- subs.set(Subscription.UNREAD_COUNT, c.getString(c.getColumnIndex(Subscriptions.KEY_UNREAD_COUNT)));
- } catch (NumberFormatException nfe) {
- L.logException(nfe);
- }
- }
- return subs;
- }
- private void createOrderedLabelsList(String order) {
- int i, nbItems = order.length() / 8;
- String sortid;
- Label label;
- Subscription subs;
- ItemInRootList item;
- L.d("nb items in root: "+nbItems);
- for (i = 0; i < nbItems; i++) {
- item = new ItemInRootList();
- sortid = order.substring(i*8, (i+1)*8);
- label = getLabel(sortid);
- L.d("item #"+i+": "+sortid+" / "+label);
- if (label == null) {
- subs = getSubscription(sortid);
- if (subs == null) {
- L.e("unable to show item "+sortid+" in ordered list!");
- continue;
- }
- item.isLabel = false;
- item.id = subs.get(Subscription.ID);
- item.title = subs.get(Subscription.TITLE);
- item.unreadCount = subs.get(Subscription.UNREAD_COUNT);
- } else {
- item.isLabel = true;
- item.id = label.id;
- item.title = label.label();
- item.unreadCount = label.unreadCount;
- }
- mItemsInList.add(item);
- }
- }
- public void setListAdapter() {
- if (C.IS_HONEYCOMB) {
- ((ListFragment) mObject).setListAdapter(new SubscriptionsAdapter());
- } else {
- SubscriptionsAdapter ad = new SubscriptionsAdapter();
- ((ListActivity) mObject).setListAdapter(ad);
- L.d("adapter view type count: "+ad.getViewTypeCount());
- }
- }
- static class SuscriptionsViewsHolder {
- TextView label;
- ImageView icon;
- }
- class SubscriptionsAdapter extends BaseAdapter {
- @Override
- public int getCount() {
- return mItemsInList.size();
- }
- @Override
- public Object getItem(int id) {
- return mItemsInList.get(id);
- }
- @Override
- public long getItemId(int arg0) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- SuscriptionsViewsHolder holder;
- ItemInRootList item = mItemsInList.get(position);
- if (convertView == null) {
- convertView = mInflater.inflate(R.layout.label, null);
- holder = new SuscriptionsViewsHolder();
- holder.label = (TextView) convertView.findViewById(R.id.label_label);
- holder.icon = (ImageView) convertView.findViewById(R.id.label_icon);
- convertView.setTag(holder);
- } else {
- holder = (SuscriptionsViewsHolder) convertView.getTag();
- }
- holder.label.setText(item.title+" ("+item.unreadCount+")");
- holder.icon.setImageResource(item.isLabel ? R.drawable.folder : R.drawable.file);
- return convertView;
- }
- }
- public String getItemId(int index) {
- if (index < mItemsInList.size()) {
- return mItemsInList.get(index).id;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement