diff --git a/mobile/src/com/cradle/iitc_mobile/share/IntentComparator.java b/mobile/src/com/cradle/iitc_mobile/share/IntentComparator.java new file mode 100644 index 00000000..4a86d994 --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/share/IntentComparator.java @@ -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 { + 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 mIntentMap = new HashMap(); + 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) 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); + } +} \ No newline at end of file diff --git a/mobile/src/com/cradle/iitc_mobile/share/IntentFragment.java b/mobile/src/com/cradle/iitc_mobile/share/IntentFragment.java index 5a181099..ef3aaaeb 100644 --- a/mobile/src/com/cradle/iitc_mobile/share/IntentFragment.java +++ b/mobile/src/com/cradle/iitc_mobile/share/IntentFragment.java @@ -44,8 +44,12 @@ public class IntentFragment extends Fragment implements OnScrollListener, OnItem @Override public void onItemClick(AdapterView parent, View view, int position, long id) { + ((ShareActivity) getActivity()).getIntentComparator().trackIntentSelection(mListView.getItem(position)); + Intent intent = mListView.getTargetIntent(position); startActivity(intent); + + getActivity().finish(); } @Override diff --git a/mobile/src/com/cradle/iitc_mobile/share/IntentListView.java b/mobile/src/com/cradle/iitc_mobile/share/IntentListView.java index c62b92fe..fdec0b86 100644 --- a/mobile/src/com/cradle/iitc_mobile/share/IntentListView.java +++ b/mobile/src/com/cradle/iitc_mobile/share/IntentListView.java @@ -21,35 +21,11 @@ import com.cradle.iitc_mobile.R; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; public class IntentListView extends ListView { - public class IntentComparator implements Comparator { - @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 { public CopyHandler(ResolveInfo resolveInfo) { super(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name); @@ -97,7 +73,8 @@ public class IntentListView extends ListView { } } - private final HashMap mActivities = new HashMap(); + private HashMap mActivities = new HashMap(); + private IntentAdapter mAdapter; 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.setNotifyOnChange(true); mAdapter.notifyDataSetChanged(); } - } diff --git a/mobile/src/com/cradle/iitc_mobile/share/ShareActivity.java b/mobile/src/com/cradle/iitc_mobile/share/ShareActivity.java index fdb1d49f..873daa83 100644 --- a/mobile/src/com/cradle/iitc_mobile/share/ShareActivity.java +++ b/mobile/src/com/cradle/iitc_mobile/share/ShareActivity.java @@ -19,13 +19,14 @@ import java.net.URLEncoder; import java.util.ArrayList; public class ShareActivity extends FragmentActivity implements ActionBar.TabListener { + private IntentComparator mComparator; + private IntentFragmentAdapter mFragmentAdapter; private boolean mIsPortal; private String mLl; private SharedPreferences mSharedPrefs = null; private String mTitle; + private ViewPager mViewPager; private int mZoom; - IntentFragmentAdapter mFragmentAdapter; - ViewPager mViewPager; private void addTab(ArrayList intents, int label, int icon) { IntentFragment fragment = new IntentFragment(); @@ -53,9 +54,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList private void setSelected(int position) { // Activity not fully loaded yet (may occur during tab creation) - if (mSharedPrefs == null) { - return; - } + if (mSharedPrefs == null) { return; } mSharedPrefs .edit() @@ -102,6 +101,8 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList super.onCreate(savedInstanceState); setContentView(R.layout.activity_share); + mComparator = new IntentComparator(this); + mFragmentAdapter = new IntentFragmentAdapter(getSupportFragmentManager()); 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 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {