Show frequently used intents on top
This commit is contained in:
		
							
								
								
									
										194
									
								
								mobile/src/com/cradle/iitc_mobile/share/IntentComparator.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										194
									
								
								mobile/src/com/cradle/iitc_mobile/share/IntentComparator.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,194 @@
 | 
				
			|||||||
 | 
					package com.cradle.iitc_mobile.share;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import android.app.Activity;
 | 
				
			||||||
 | 
					import android.content.pm.PackageManager;
 | 
				
			||||||
 | 
					import android.content.pm.ResolveInfo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.FileInputStream;
 | 
				
			||||||
 | 
					import java.io.FileNotFoundException;
 | 
				
			||||||
 | 
					import java.io.FileOutputStream;
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.io.ObjectInputStream;
 | 
				
			||||||
 | 
					import java.io.ObjectOutputStream;
 | 
				
			||||||
 | 
					import java.io.Serializable;
 | 
				
			||||||
 | 
					import java.util.Comparator;
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class IntentComparator implements Comparator<ResolveInfo> {
 | 
				
			||||||
 | 
					    public static class Component implements Serializable {
 | 
				
			||||||
 | 
					        private static final long serialVersionUID = -5043782754318376792L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public String name;
 | 
				
			||||||
 | 
					        public String packageName;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public Component() {
 | 
				
			||||||
 | 
					            name = null;
 | 
				
			||||||
 | 
					            packageName = null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public Component(ResolveInfo info) {
 | 
				
			||||||
 | 
					            name = info.activityInfo.name;
 | 
				
			||||||
 | 
					            packageName = info.activityInfo.applicationInfo.packageName;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @Override
 | 
				
			||||||
 | 
					        public boolean equals(Object o) {
 | 
				
			||||||
 | 
					            if (this == o)
 | 
				
			||||||
 | 
					                return true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (o == null)
 | 
				
			||||||
 | 
					                return false;
 | 
				
			||||||
 | 
					            if (o.getClass() != getClass())
 | 
				
			||||||
 | 
					                return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            Component c = (Component) o;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (name == null) {
 | 
				
			||||||
 | 
					                if (c.name != null)
 | 
				
			||||||
 | 
					                    return false;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                if (!name.equals(c.name))
 | 
				
			||||||
 | 
					                    return false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (name == null) {
 | 
				
			||||||
 | 
					                if (c.name != null)
 | 
				
			||||||
 | 
					                    return false;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                if (!name.equals(c.name))
 | 
				
			||||||
 | 
					                    return false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (packageName == null) {
 | 
				
			||||||
 | 
					                if (c.packageName != null)
 | 
				
			||||||
 | 
					                    return false;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                if (!packageName.equals(c.packageName))
 | 
				
			||||||
 | 
					                    return false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @Override
 | 
				
			||||||
 | 
					        public int hashCode() {
 | 
				
			||||||
 | 
					            int hc = 2;
 | 
				
			||||||
 | 
					            final int hashMultiplier = 7;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            hc += (name == null) ? 0 : name.hashCode();
 | 
				
			||||||
 | 
					            hc *= hashMultiplier;
 | 
				
			||||||
 | 
					            hc += (packageName == null) ? 0 : packageName.hashCode();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return hc;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @Override
 | 
				
			||||||
 | 
					        public String toString() {
 | 
				
			||||||
 | 
					            return packageName + "/" +
 | 
				
			||||||
 | 
					                    (name.startsWith(packageName + ".")
 | 
				
			||||||
 | 
					                            ? name.substring(packageName.length())
 | 
				
			||||||
 | 
					                            : name);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static final String INTENT_MAP_FILE = "share_intent_map";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private ShareActivity mActivity;
 | 
				
			||||||
 | 
					    private HashMap<Component, Integer> mIntentMap = new HashMap<Component, Integer>();
 | 
				
			||||||
 | 
					    private PackageManager mPackageManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    IntentComparator(ShareActivity activity) {
 | 
				
			||||||
 | 
					        mActivity = activity;
 | 
				
			||||||
 | 
					        mPackageManager = activity.getPackageManager();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        load();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @SuppressWarnings("unchecked")
 | 
				
			||||||
 | 
					    private void load() {
 | 
				
			||||||
 | 
					        ObjectInputStream objectIn = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            FileInputStream fileIn = mActivity.openFileInput(INTENT_MAP_FILE);
 | 
				
			||||||
 | 
					            objectIn = new ObjectInputStream(fileIn);
 | 
				
			||||||
 | 
					            mIntentMap = (HashMap<Component, Integer>) objectIn.readObject();
 | 
				
			||||||
 | 
					        } catch (FileNotFoundException e) {
 | 
				
			||||||
 | 
					            // Do nothing
 | 
				
			||||||
 | 
					        } catch (IOException e) {
 | 
				
			||||||
 | 
					            e.printStackTrace();
 | 
				
			||||||
 | 
					        } catch (ClassNotFoundException e) {
 | 
				
			||||||
 | 
					            e.printStackTrace();
 | 
				
			||||||
 | 
					        } finally {
 | 
				
			||||||
 | 
					            if (objectIn != null) {
 | 
				
			||||||
 | 
					                try {
 | 
				
			||||||
 | 
					                    objectIn.close();
 | 
				
			||||||
 | 
					                } catch (IOException e) {
 | 
				
			||||||
 | 
					                    e.printStackTrace();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public int compare(ResolveInfo lhs, ResolveInfo rhs) {
 | 
				
			||||||
 | 
					        int order;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Integer lCount = mIntentMap.get(new Component(lhs));
 | 
				
			||||||
 | 
					        Integer rCount = mIntentMap.get(new Component(rhs));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (lCount == null) lCount = 0;
 | 
				
			||||||
 | 
					        if (rCount == null) rCount = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (lCount > rCount) return -1;
 | 
				
			||||||
 | 
					        if (lCount < rCount) return 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        order = lhs.loadLabel(mPackageManager).toString()
 | 
				
			||||||
 | 
					                .compareTo(rhs.loadLabel(mPackageManager).toString());
 | 
				
			||||||
 | 
					        if (order != 0) return order;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (lhs.nonLocalizedLabel != null && rhs.nonLocalizedLabel != null) {
 | 
				
			||||||
 | 
					            order = lhs.nonLocalizedLabel.toString().compareTo(rhs.nonLocalizedLabel.toString());
 | 
				
			||||||
 | 
					            if (order != 0) return order;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        order = lhs.activityInfo.packageName.compareTo(rhs.activityInfo.packageName);
 | 
				
			||||||
 | 
					        if (order != 0) return order;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        order = lhs.activityInfo.name.compareTo(rhs.activityInfo.name);
 | 
				
			||||||
 | 
					        if (order != 0) return order;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void save() {
 | 
				
			||||||
 | 
					        ObjectOutputStream objectOut = null;
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            FileOutputStream fileOut = mActivity.openFileOutput(INTENT_MAP_FILE, Activity.MODE_PRIVATE);
 | 
				
			||||||
 | 
					            objectOut = new ObjectOutputStream(fileOut);
 | 
				
			||||||
 | 
					            objectOut.writeObject(mIntentMap);
 | 
				
			||||||
 | 
					            fileOut.getFD().sync();
 | 
				
			||||||
 | 
					        } catch (IOException e) {
 | 
				
			||||||
 | 
					            e.printStackTrace();
 | 
				
			||||||
 | 
					        } finally {
 | 
				
			||||||
 | 
					            if (objectOut != null) {
 | 
				
			||||||
 | 
					                try {
 | 
				
			||||||
 | 
					                    objectOut.close();
 | 
				
			||||||
 | 
					                } catch (IOException e) {
 | 
				
			||||||
 | 
					                    e.printStackTrace();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void trackIntentSelection(ResolveInfo info) {
 | 
				
			||||||
 | 
					        Component component = new Component(info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Integer counter = mIntentMap.get(component);
 | 
				
			||||||
 | 
					        if (counter == null)
 | 
				
			||||||
 | 
					            counter = 1;
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            counter++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        mIntentMap.put(component, counter);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -44,8 +44,12 @@ public class IntentFragment extends Fragment implements OnScrollListener, OnItem
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 | 
					    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 | 
				
			||||||
 | 
					        ((ShareActivity) getActivity()).getIntentComparator().trackIntentSelection(mListView.getItem(position));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Intent intent = mListView.getTargetIntent(position);
 | 
					        Intent intent = mListView.getTargetIntent(position);
 | 
				
			||||||
        startActivity(intent);
 | 
					        startActivity(intent);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        getActivity().finish();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,35 +21,11 @@ import com.cradle.iitc_mobile.R;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.Collections;
 | 
					import java.util.Collections;
 | 
				
			||||||
import java.util.Comparator;
 | 
					 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.HashSet;
 | 
					import java.util.HashSet;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class IntentListView extends ListView {
 | 
					public class IntentListView extends ListView {
 | 
				
			||||||
    public class IntentComparator implements Comparator<ResolveInfo> {
 | 
					 | 
				
			||||||
        @Override
 | 
					 | 
				
			||||||
        public int compare(ResolveInfo lhs, ResolveInfo rhs) {
 | 
					 | 
				
			||||||
            int order;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            order = lhs.loadLabel(mPackageManager).toString().compareTo(rhs.loadLabel(mPackageManager).toString());
 | 
					 | 
				
			||||||
            if (order != 0) return order;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (lhs.nonLocalizedLabel != null && rhs.nonLocalizedLabel != null) {
 | 
					 | 
				
			||||||
                order = lhs.nonLocalizedLabel.toString().compareTo(rhs.nonLocalizedLabel.toString());
 | 
					 | 
				
			||||||
                if (order != 0) return order;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            order = lhs.activityInfo.packageName.compareTo(rhs.activityInfo.packageName);
 | 
					 | 
				
			||||||
            if (order != 0) return order;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            order = lhs.activityInfo.name.compareTo(rhs.activityInfo.name);
 | 
					 | 
				
			||||||
            if (order != 0) return order;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            return 0;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private static class CopyHandler extends Pair<String, String> {
 | 
					    private static class CopyHandler extends Pair<String, String> {
 | 
				
			||||||
        public CopyHandler(ResolveInfo resolveInfo) {
 | 
					        public CopyHandler(ResolveInfo resolveInfo) {
 | 
				
			||||||
            super(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
 | 
					            super(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
 | 
				
			||||||
@@ -97,7 +73,8 @@ public class IntentListView extends ListView {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final HashMap<ComponentName, Intent> mActivities = new HashMap<ComponentName, Intent>();
 | 
					    private HashMap<ComponentName, Intent> mActivities = new HashMap<ComponentName, Intent>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private IntentAdapter mAdapter;
 | 
					    private IntentAdapter mAdapter;
 | 
				
			||||||
    private PackageManager mPackageManager;
 | 
					    private PackageManager mPackageManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -211,11 +188,10 @@ public class IntentListView extends ListView {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Collections.sort(allActivities, new IntentComparator());
 | 
					        Collections.sort(allActivities, ((ShareActivity) getContext()).getIntentComparator());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        mAdapter.addAll(allActivities);
 | 
					        mAdapter.addAll(allActivities);
 | 
				
			||||||
        mAdapter.setNotifyOnChange(true);
 | 
					        mAdapter.setNotifyOnChange(true);
 | 
				
			||||||
        mAdapter.notifyDataSetChanged();
 | 
					        mAdapter.notifyDataSetChanged();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,13 +19,14 @@ import java.net.URLEncoder;
 | 
				
			|||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ShareActivity extends FragmentActivity implements ActionBar.TabListener {
 | 
					public class ShareActivity extends FragmentActivity implements ActionBar.TabListener {
 | 
				
			||||||
 | 
					    private IntentComparator mComparator;
 | 
				
			||||||
 | 
					    private IntentFragmentAdapter mFragmentAdapter;
 | 
				
			||||||
    private boolean mIsPortal;
 | 
					    private boolean mIsPortal;
 | 
				
			||||||
    private String mLl;
 | 
					    private String mLl;
 | 
				
			||||||
    private SharedPreferences mSharedPrefs = null;
 | 
					    private SharedPreferences mSharedPrefs = null;
 | 
				
			||||||
    private String mTitle;
 | 
					    private String mTitle;
 | 
				
			||||||
 | 
					    private ViewPager mViewPager;
 | 
				
			||||||
    private int mZoom;
 | 
					    private int mZoom;
 | 
				
			||||||
    IntentFragmentAdapter mFragmentAdapter;
 | 
					 | 
				
			||||||
    ViewPager mViewPager;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void addTab(ArrayList<Intent> intents, int label, int icon) {
 | 
					    private void addTab(ArrayList<Intent> intents, int label, int icon) {
 | 
				
			||||||
        IntentFragment fragment = new IntentFragment();
 | 
					        IntentFragment fragment = new IntentFragment();
 | 
				
			||||||
@@ -53,9 +54,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private void setSelected(int position) {
 | 
					    private void setSelected(int position) {
 | 
				
			||||||
        // Activity not fully loaded yet (may occur during tab creation)
 | 
					        // Activity not fully loaded yet (may occur during tab creation)
 | 
				
			||||||
        if (mSharedPrefs == null) {
 | 
					        if (mSharedPrefs == null) { return; }
 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        mSharedPrefs
 | 
					        mSharedPrefs
 | 
				
			||||||
                .edit()
 | 
					                .edit()
 | 
				
			||||||
@@ -102,6 +101,8 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
 | 
				
			|||||||
        super.onCreate(savedInstanceState);
 | 
					        super.onCreate(savedInstanceState);
 | 
				
			||||||
        setContentView(R.layout.activity_share);
 | 
					        setContentView(R.layout.activity_share);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        mComparator = new IntentComparator(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        mFragmentAdapter = new IntentFragmentAdapter(getSupportFragmentManager());
 | 
					        mFragmentAdapter = new IntentFragmentAdapter(getSupportFragmentManager());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        final ActionBar actionBar = getActionBar();
 | 
					        final ActionBar actionBar = getActionBar();
 | 
				
			||||||
@@ -158,6 +159,16 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    protected void onDestroy() {
 | 
				
			||||||
 | 
					        super.onDestroy();
 | 
				
			||||||
 | 
					        mComparator.save();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public IntentComparator getIntentComparator() {
 | 
				
			||||||
 | 
					        return mComparator;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public boolean onOptionsItemSelected(MenuItem item) {
 | 
					    public boolean onOptionsItemSelected(MenuItem item) {
 | 
				
			||||||
        switch (item.getItemId()) {
 | 
					        switch (item.getItemId()) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user