Major refactoring of ShareActivity. Google Maps now has labeled pins
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
package com.cradle.iitc_mobile.share;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ResolveInfo;
|
||||
|
||||
import com.cradle.iitc_mobile.Log;
|
||||
@ -16,7 +17,112 @@ import java.io.Serializable;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class IntentComparator implements Comparator<ResolveInfo> {
|
||||
public class IntentComparator implements Comparator<Intent> {
|
||||
private static final String INTENT_MAP_FILE = "share_intent_map";
|
||||
|
||||
private final ShareActivity mActivity;
|
||||
|
||||
private HashMap<Component, Integer> mIntentMap = new HashMap<Component, Integer>();
|
||||
|
||||
IntentComparator(final ShareActivity activity) {
|
||||
mActivity = activity;
|
||||
load();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void load() {
|
||||
ObjectInputStream objectIn = null;
|
||||
|
||||
try {
|
||||
final FileInputStream fileIn = mActivity.openFileInput(INTENT_MAP_FILE);
|
||||
objectIn = new ObjectInputStream(fileIn);
|
||||
mIntentMap = (HashMap<Component, Integer>) objectIn.readObject();
|
||||
} catch (final FileNotFoundException e) {
|
||||
// Do nothing
|
||||
} catch (final IOException e) {
|
||||
Log.w(e);
|
||||
} catch (final ClassNotFoundException e) {
|
||||
Log.w(e);
|
||||
} finally {
|
||||
if (objectIn != null) {
|
||||
try {
|
||||
objectIn.close();
|
||||
} catch (final IOException e) {
|
||||
Log.w(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(final Intent lhs, final Intent rhs) {
|
||||
int order;
|
||||
|
||||
// we might be merging multiple intents, so there could be more than one default
|
||||
if (IntentGenerator.isDefault(lhs) && !IntentGenerator.isDefault(rhs))
|
||||
return -1;
|
||||
if (IntentGenerator.isDefault(rhs) && !IntentGenerator.isDefault(lhs))
|
||||
return 1;
|
||||
|
||||
final ComponentName lComponent = lhs.getComponent();
|
||||
final ComponentName rComponent = rhs.getComponent();
|
||||
|
||||
// Show more frequently used items in top
|
||||
Integer lCount = mIntentMap.get(new Component(lComponent));
|
||||
Integer rCount = mIntentMap.get(new Component(rComponent));
|
||||
|
||||
if (lCount == null) lCount = 0;
|
||||
if (rCount == null) rCount = 0;
|
||||
|
||||
if (lCount > rCount) return -1;
|
||||
if (lCount < rCount) return 1;
|
||||
|
||||
// still no order. fall back to alphabetical order
|
||||
order = IntentGenerator.getTitle(lhs).compareTo(IntentGenerator.getTitle(rhs));
|
||||
if (order != 0) return order;
|
||||
|
||||
order = lComponent.getPackageName().compareTo(rComponent.getPackageName());
|
||||
if (order != 0) return order;
|
||||
|
||||
order = lComponent.getClassName().compareTo(rComponent.getClassName());
|
||||
if (order != 0) return order;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
ObjectOutputStream objectOut = null;
|
||||
try {
|
||||
final FileOutputStream fileOut = mActivity.openFileOutput(INTENT_MAP_FILE, Activity.MODE_PRIVATE);
|
||||
objectOut = new ObjectOutputStream(fileOut);
|
||||
objectOut.writeObject(mIntentMap);
|
||||
fileOut.getFD().sync();
|
||||
} catch (final IOException e) {
|
||||
Log.w(e);
|
||||
} finally {
|
||||
if (objectOut != null) {
|
||||
try {
|
||||
objectOut.close();
|
||||
} catch (final IOException e) {
|
||||
Log.w(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void trackIntentSelection(final Intent intent) {
|
||||
final Component component = new Component(intent.getComponent());
|
||||
|
||||
Integer counter = mIntentMap.get(component);
|
||||
if (counter == null) {
|
||||
counter = 1;
|
||||
} else {
|
||||
counter++;
|
||||
}
|
||||
|
||||
mIntentMap.put(component, counter);
|
||||
}
|
||||
|
||||
public static class Component implements Serializable {
|
||||
private static final long serialVersionUID = -5043782754318376792L;
|
||||
|
||||
@ -28,19 +134,24 @@ public class IntentComparator implements Comparator<ResolveInfo> {
|
||||
packageName = null;
|
||||
}
|
||||
|
||||
public Component(ResolveInfo info) {
|
||||
public Component(final ComponentName cn) {
|
||||
name = cn.getClassName();
|
||||
packageName = cn.getPackageName();
|
||||
}
|
||||
|
||||
public Component(final ResolveInfo info) {
|
||||
name = info.activityInfo.name;
|
||||
packageName = info.activityInfo.applicationInfo.packageName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
|
||||
if (o == null) return false;
|
||||
if (o.getClass() != this.getClass()) return false;
|
||||
|
||||
Component c = (Component) o;
|
||||
final Component c = (Component) o;
|
||||
|
||||
if (name == null) {
|
||||
if (c.name != null) return false;
|
||||
@ -83,120 +194,4 @@ public class IntentComparator implements Comparator<ResolveInfo> {
|
||||
: 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) {
|
||||
Log.w(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
Log.w(e);
|
||||
} finally {
|
||||
if (objectIn != null) {
|
||||
try {
|
||||
objectIn.close();
|
||||
} catch (IOException e) {
|
||||
Log.w(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(ResolveInfo lhs, ResolveInfo rhs) {
|
||||
int order;
|
||||
|
||||
// we might be merging multiple intents, so there could be more than one default
|
||||
if (lhs.isDefault && !rhs.isDefault)
|
||||
return -1;
|
||||
if (rhs.isDefault && !lhs.isDefault)
|
||||
return 1;
|
||||
|
||||
// Show more frequently used items in top
|
||||
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;
|
||||
|
||||
// don't known how these are set (or if they can be set at all), but it sounds promising...
|
||||
if (lhs.preferredOrder != rhs.preferredOrder)
|
||||
return rhs.preferredOrder - lhs.preferredOrder;
|
||||
if (lhs.priority != rhs.priority)
|
||||
return rhs.priority - lhs.priority;
|
||||
|
||||
// still no order. fall back to alphabetical 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;
|
||||
}
|
||||
|
||||
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) {
|
||||
Log.w(e);
|
||||
} finally {
|
||||
if (objectOut != null) {
|
||||
try {
|
||||
objectOut.close();
|
||||
} catch (IOException e) {
|
||||
Log.w(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user