Extract IntentAdapter from IntentListView. Delete IntentListView
(only a small wrapper was left, remainders could be moved to IntentAdapter
This commit is contained in:
		
							
								
								
									
										68
									
								
								mobile/src/com/cradle/iitc_mobile/share/IntentAdapter.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								mobile/src/com/cradle/iitc_mobile/share/IntentAdapter.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
				
			|||||||
 | 
					package com.cradle.iitc_mobile.share;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import android.app.Activity;
 | 
				
			||||||
 | 
					import android.content.Context;
 | 
				
			||||||
 | 
					import android.content.Intent;
 | 
				
			||||||
 | 
					import android.content.pm.PackageManager;
 | 
				
			||||||
 | 
					import android.content.pm.PackageManager.NameNotFoundException;
 | 
				
			||||||
 | 
					import android.graphics.drawable.Drawable;
 | 
				
			||||||
 | 
					import android.util.DisplayMetrics;
 | 
				
			||||||
 | 
					import android.view.LayoutInflater;
 | 
				
			||||||
 | 
					import android.view.View;
 | 
				
			||||||
 | 
					import android.view.ViewGroup;
 | 
				
			||||||
 | 
					import android.widget.ArrayAdapter;
 | 
				
			||||||
 | 
					import android.widget.TextView;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.cradle.iitc_mobile.Log;
 | 
				
			||||||
 | 
					import com.cradle.iitc_mobile.R;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Collections;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class IntentAdapter extends ArrayAdapter<Intent> {
 | 
				
			||||||
 | 
					    private static final int MDPI_PX = 36;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final PackageManager mPackageManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public IntentAdapter(final Context context) {
 | 
				
			||||||
 | 
					        super(context, android.R.layout.simple_list_item_1);
 | 
				
			||||||
 | 
					        mPackageManager = getContext().getPackageManager();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public View getView(final int position, final View convertView, final ViewGroup parent) {
 | 
				
			||||||
 | 
					        final LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
 | 
				
			||||||
 | 
					        final TextView view = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        final Intent item = getItem(position);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        view.setText(IntentGenerator.getTitle(item));
 | 
				
			||||||
 | 
					        view.setCompoundDrawablePadding((int) getContext().getResources().getDimension(R.dimen.icon_margin));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // get icon and scale it manually to ensure that all have the same size
 | 
				
			||||||
 | 
					        final DisplayMetrics dm = new DisplayMetrics();
 | 
				
			||||||
 | 
					        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
 | 
				
			||||||
 | 
					        final float densityScale = dm.density;
 | 
				
			||||||
 | 
					        final float scaledWidth = MDPI_PX * densityScale;
 | 
				
			||||||
 | 
					        final float scaledHeight = MDPI_PX * densityScale;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            final Drawable icon = mPackageManager.getActivityIcon(item);
 | 
				
			||||||
 | 
					            icon.setBounds(0, 0, Math.round(scaledWidth), Math.round(scaledHeight));
 | 
				
			||||||
 | 
					            view.setCompoundDrawables(icon, null, null, null);
 | 
				
			||||||
 | 
					        } catch (final NameNotFoundException e) {
 | 
				
			||||||
 | 
					            Log.e(e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return view;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setIntents(final List<Intent> intents) {
 | 
				
			||||||
 | 
					        Collections.sort(intents, ((ShareActivity) getContext()).getIntentComparator());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        setNotifyOnChange(false);
 | 
				
			||||||
 | 
					        clear();
 | 
				
			||||||
 | 
					        addAll(intents);
 | 
				
			||||||
 | 
					        notifyDataSetChanged();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -10,13 +10,15 @@ import android.widget.AbsListView;
 | 
				
			|||||||
import android.widget.AbsListView.OnScrollListener;
 | 
					import android.widget.AbsListView.OnScrollListener;
 | 
				
			||||||
import android.widget.AdapterView;
 | 
					import android.widget.AdapterView;
 | 
				
			||||||
import android.widget.AdapterView.OnItemClickListener;
 | 
					import android.widget.AdapterView.OnItemClickListener;
 | 
				
			||||||
 | 
					import android.widget.ListView;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class IntentFragment extends Fragment implements OnScrollListener, OnItemClickListener {
 | 
					public class IntentFragment extends Fragment implements OnScrollListener, OnItemClickListener {
 | 
				
			||||||
    private ArrayList<Intent> mIntents;
 | 
					    private ArrayList<Intent> mIntents;
 | 
				
			||||||
    private IntentListView mListView;
 | 
					    private IntentAdapter mAdapter;
 | 
				
			||||||
    private int mScrollIndex, mScrollTop;
 | 
					    private int mScrollIndex, mScrollTop;
 | 
				
			||||||
 | 
					    private ListView mListView;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public int getIcon() {
 | 
					    public int getIcon() {
 | 
				
			||||||
        return getArguments().getInt("icon");
 | 
					        return getArguments().getInt("icon");
 | 
				
			||||||
@@ -31,8 +33,11 @@ public class IntentFragment extends Fragment implements OnScrollListener, OnItem
 | 
				
			|||||||
        final Bundle args = getArguments();
 | 
					        final Bundle args = getArguments();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        mIntents = args.getParcelableArrayList("intents");
 | 
					        mIntents = args.getParcelableArrayList("intents");
 | 
				
			||||||
        mListView = new IntentListView(getActivity());
 | 
					
 | 
				
			||||||
        mListView.setIntents(mIntents);
 | 
					        mAdapter = new IntentAdapter(getActivity());
 | 
				
			||||||
 | 
					        mAdapter.setIntents(mIntents);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        mListView = new ListView(getActivity());
 | 
				
			||||||
        if (mScrollIndex != -1 && mScrollTop != -1) {
 | 
					        if (mScrollIndex != -1 && mScrollTop != -1) {
 | 
				
			||||||
            mListView.setSelectionFromTop(mScrollIndex, mScrollTop);
 | 
					            mListView.setSelectionFromTop(mScrollIndex, mScrollTop);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -44,7 +49,7 @@ public class IntentFragment extends Fragment implements OnScrollListener, OnItem
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
 | 
					    public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
 | 
				
			||||||
        final Intent intent = mListView.getItem(position);
 | 
					        final Intent intent = mAdapter.getItem(position);
 | 
				
			||||||
        ((ShareActivity) getActivity()).getIntentComparator().trackIntentSelection(intent);
 | 
					        ((ShareActivity) getActivity()).getIntentComparator().trackIntentSelection(intent);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        startActivity(intent);
 | 
					        startActivity(intent);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,99 +0,0 @@
 | 
				
			|||||||
package com.cradle.iitc_mobile.share;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import android.app.Activity;
 | 
					 | 
				
			||||||
import android.content.Context;
 | 
					 | 
				
			||||||
import android.content.Intent;
 | 
					 | 
				
			||||||
import android.content.pm.PackageManager;
 | 
					 | 
				
			||||||
import android.content.pm.PackageManager.NameNotFoundException;
 | 
					 | 
				
			||||||
import android.graphics.drawable.Drawable;
 | 
					 | 
				
			||||||
import android.util.AttributeSet;
 | 
					 | 
				
			||||||
import android.util.DisplayMetrics;
 | 
					 | 
				
			||||||
import android.view.LayoutInflater;
 | 
					 | 
				
			||||||
import android.view.View;
 | 
					 | 
				
			||||||
import android.view.ViewGroup;
 | 
					 | 
				
			||||||
import android.widget.ArrayAdapter;
 | 
					 | 
				
			||||||
import android.widget.ListView;
 | 
					 | 
				
			||||||
import android.widget.TextView;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import com.cradle.iitc_mobile.Log;
 | 
					 | 
				
			||||||
import com.cradle.iitc_mobile.R;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.util.Collections;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
public class IntentListView extends ListView {
 | 
					 | 
				
			||||||
    private IntentAdapter mAdapter;
 | 
					 | 
				
			||||||
    private PackageManager mPackageManager;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public IntentListView(final Context context) {
 | 
					 | 
				
			||||||
        super(context);
 | 
					 | 
				
			||||||
        init();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public IntentListView(final Context context, final AttributeSet attrs) {
 | 
					 | 
				
			||||||
        super(context, attrs);
 | 
					 | 
				
			||||||
        init();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public IntentListView(final Context context, final AttributeSet attrs, final int defStyle) {
 | 
					 | 
				
			||||||
        super(context, attrs, defStyle);
 | 
					 | 
				
			||||||
        init();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private void init() {
 | 
					 | 
				
			||||||
        mPackageManager = getContext().getPackageManager();
 | 
					 | 
				
			||||||
        mAdapter = new IntentAdapter();
 | 
					 | 
				
			||||||
        setAdapter(mAdapter);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public Intent getItem(final int position) {
 | 
					 | 
				
			||||||
        return mAdapter.getItem(position);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void setIntents(final List<Intent> intents) {
 | 
					 | 
				
			||||||
        Collections.sort(intents, ((ShareActivity) getContext()).getIntentComparator());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        mAdapter.setNotifyOnChange(false);
 | 
					 | 
				
			||||||
        mAdapter.clear();
 | 
					 | 
				
			||||||
        mAdapter.addAll(intents);
 | 
					 | 
				
			||||||
        mAdapter.notifyDataSetChanged();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private class IntentAdapter extends ArrayAdapter<Intent> {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // actually the mdpi pixel size is 48, but this looks ugly...so scale icons down for listView
 | 
					 | 
				
			||||||
        private static final int MDPI_PX = 36;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        private IntentAdapter() {
 | 
					 | 
				
			||||||
            super(IntentListView.this.getContext(), android.R.layout.simple_list_item_1);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        @Override
 | 
					 | 
				
			||||||
        public View getView(final int position, final View convertView, final ViewGroup parent) {
 | 
					 | 
				
			||||||
            final LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
 | 
					 | 
				
			||||||
            final TextView view = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            final Intent item = getItem(position);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            view.setText(IntentGenerator.getTitle(item));
 | 
					 | 
				
			||||||
            view.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.icon_margin));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // get icon and scale it manually to ensure that all have the same size
 | 
					 | 
				
			||||||
            final DisplayMetrics dm = new DisplayMetrics();
 | 
					 | 
				
			||||||
            ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
 | 
					 | 
				
			||||||
            final float densityScale = dm.density;
 | 
					 | 
				
			||||||
            final float scaledWidth = MDPI_PX * densityScale;
 | 
					 | 
				
			||||||
            final float scaledHeight = MDPI_PX * densityScale;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
                final Drawable icon = mPackageManager.getActivityIcon(item);
 | 
					 | 
				
			||||||
                icon.setBounds(0, 0, Math.round(scaledWidth), Math.round(scaledHeight));
 | 
					 | 
				
			||||||
                view.setCompoundDrawables(icon, null, null, null);
 | 
					 | 
				
			||||||
            } catch (final NameNotFoundException e) {
 | 
					 | 
				
			||||||
                Log.e(e);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            return view;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user