Android Open Source - DashNotifier Package Adapter






From Project

Back to project page DashNotifier.

License

The source code is released under:

MIT License

If you think the Android project DashNotifier listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.umang.dashnotifier;
//from  w ww  .j av  a 2s  . c o m
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class PackageAdapter extends ArrayAdapter<PackageItem> implements
    SectionIndexer, Filterable {

  private Context context;
  private String[] mSections;
  private int[] mPositions;
  private ArrayList<PackageItem> values;
  private ArrayList<PackageItem> filtered;
  final private int bound;
  private Filter filter;

  public PackageAdapter(Context context, ArrayList<PackageItem> values) {
    super(context, R.layout.app_item, values);
    this.context = context;
    this.values = new ArrayList<PackageItem>();
    this.values.addAll(values);
    this.filtered = new ArrayList<PackageItem>();
    this.filtered.addAll(values);
    bound = context.getResources().getDimensionPixelSize(
        R.dimen.app_picker_padding);
  }

  @Override
  public int getCount() {
    return filtered.size();
  }

  public void setSection(List<String> sections, List<Integer> positions) {
    mSections = sections.toArray(new String[sections.size()]);
    mPositions = new int[positions.size()];
    for (int i = 0; i < positions.size(); i++) {
      mPositions[i] = positions.get(i);
    }

  }

  public PackageItem getByPosition(int position) {
    return values.get(position);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    if (convertView == null) {
      convertView = View.inflate(context, R.layout.app_item, null);
      viewHolder = new ViewHolder();
      viewHolder.appRow = (TextView) convertView
          .findViewById(R.id.appitem);
      convertView.setTag(viewHolder);
    } else {
      viewHolder = (ViewHolder) convertView.getTag();
    }

    PackageItem item = filtered.get(position);
    
    if (item != null) {
      Drawable icon = item.getIcon();
      icon.setBounds(0, 0, bound, bound);
      viewHolder.appRow.setText(item.getName());
      viewHolder.appRow.setCompoundDrawables(icon, null, null, null);
    }

    return convertView;
  }

  // Using a ViewHolder results in >2x performance improvement per row
  static class ViewHolder {
    TextView appRow;
  }

  @Override
  public int getPositionForSection(int section) {
    if (section < 0 || section >= mSections.length) {
      return -1;
    }

    return mPositions[section];
  }

  @Override
  public int getSectionForPosition(int position) {
    if (position < 0 || position >= getCount()) {
      return -1;
    }

    int index = Arrays.binarySearch(mPositions, position);

    return index >= 0 ? index : -index - 2;
  }

  @Override
  public Object[] getSections() {

    return mSections;
  }

  @Override
  public Filter getFilter() {
    if (filter == null)
      filter = new ApplicationNameFilter();
    return filter;
  }

  private class ApplicationNameFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

      constraint = constraint.toString().toLowerCase();
      FilterResults result = new FilterResults();
      if (constraint != null && constraint.toString().length() > 0) {
        ArrayList<PackageItem> filteredItems = new ArrayList<PackageItem>();

        for (int i = 0, l = values.size(); i < l; i++) {
          PackageItem item = values.get(i);
          if (item.getName().toLowerCase().contains(constraint))
            filteredItems.add(item);
        }
        result.count = filteredItems.size();
        result.values = filteredItems;
      } else {
        synchronized (this) {
          result.values = values;
          result.count = values.size();
        }
      }
      return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
        FilterResults results) {

      filtered = (ArrayList<PackageItem>) results.values;
      notifyDataSetChanged();
      clear();
      for (int i = 0, l = filtered.size(); i < l; i++)
        add(filtered.get(i));
      notifyDataSetInvalidated();
    }
  }

}




Java Source Code List

com.google.android.apps.dashclock.configuration.AppChooserPreference.java
com.google.android.apps.dashclock.ui.SimplePagedTabsHelper.java
com.umang.dashnotifier.AbstractSettings.java
com.umang.dashnotifier.AppListFragment.java
com.umang.dashnotifier.AppSelectActivity.java
com.umang.dashnotifier.ClickIntentActivity.java
com.umang.dashnotifier.Commons.java
com.umang.dashnotifier.DashNotificationListenerAcc.java
com.umang.dashnotifier.DashNotificationListener.java
com.umang.dashnotifier.DashNotifierExtension10.java
com.umang.dashnotifier.DashNotifierExtension11.java
com.umang.dashnotifier.DashNotifierExtension2.java
com.umang.dashnotifier.DashNotifierExtension3.java
com.umang.dashnotifier.DashNotifierExtension4.java
com.umang.dashnotifier.DashNotifierExtension5.java
com.umang.dashnotifier.DashNotifierExtension6.java
com.umang.dashnotifier.DashNotifierExtension7.java
com.umang.dashnotifier.DashNotifierExtension8.java
com.umang.dashnotifier.DashNotifierExtension9.java
com.umang.dashnotifier.DashNotifierExtension.java
com.umang.dashnotifier.DashNotifierSettingsActivity10.java
com.umang.dashnotifier.DashNotifierSettingsActivity11.java
com.umang.dashnotifier.DashNotifierSettingsActivity2.java
com.umang.dashnotifier.DashNotifierSettingsActivity3.java
com.umang.dashnotifier.DashNotifierSettingsActivity4.java
com.umang.dashnotifier.DashNotifierSettingsActivity5.java
com.umang.dashnotifier.DashNotifierSettingsActivity6.java
com.umang.dashnotifier.DashNotifierSettingsActivity7.java
com.umang.dashnotifier.DashNotifierSettingsActivity8.java
com.umang.dashnotifier.DashNotifierSettingsActivity9.java
com.umang.dashnotifier.DashNotifierSettingsActivity.java
com.umang.dashnotifier.HelpUtils.java
com.umang.dashnotifier.IconPicker.java
com.umang.dashnotifier.PackageAdapter.java
com.umang.dashnotifier.PackageItem.java
com.umang.dashnotifier.PrefsFragment.java
com.umang.dashnotifier.UnlockReceiver.java
com.umang.dashnotifier.provider.NotifSQLiteHelper.java
com.umang.dashnotifier.provider.NotificationProvider.java
com.umang.dashnotifier.provider.NotificationStore.java