Major refactoring of ShareActivity. Google Maps now has labeled pins

This commit is contained in:
fkloft 2014-02-18 17:46:02 +01:00
parent f9cf8d5518
commit 6fadfa9b39
5 changed files with 344 additions and 348 deletions

View File

@ -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);
}
}

View File

@ -27,8 +27,8 @@ public class IntentFragment extends Fragment implements OnScrollListener, OnItem
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle args = getArguments();
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final Bundle args = getArguments();
mIntents = args.getParcelableArrayList("intents");
mListView = new IntentListView(getActivity());
@ -43,23 +43,22 @@ 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));
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
final Intent intent = mListView.getItem(position);
((ShareActivity) getActivity()).getIntentComparator().trackIntentSelection(intent);
Intent intent = mListView.getTargetIntent(position);
startActivity(intent);
getActivity().finish();
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
public void onScroll(final AbsListView lv, final int firstItem, final int visibleItems, final int totalItems) {
mScrollIndex = mListView.getFirstVisiblePosition();
View v = mListView.getChildAt(0);
final View v = mListView.getChildAt(0);
mScrollTop = (v == null) ? 0 : v.getTop();
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
}
}

View File

@ -0,0 +1,143 @@
package com.cradle.iitc_mobile.share;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import com.cradle.iitc_mobile.Log;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class IntentGenerator {
private static final String EXTRA_FLAG_IS_DEFAULT = "IITCM_IS_DEFAULT";
private static final String EXTRA_FLAG_TITLE = "IITCM_TITLE";
private static final HashSet<ComponentName> KNOWN_COPY_HANDLERS = new HashSet<ComponentName>();
static {
if (KNOWN_COPY_HANDLERS.isEmpty()) {
KNOWN_COPY_HANDLERS.add(new ComponentName(
"com.google.android.apps.docs",
"com.google.android.apps.docs.app.SendTextToClipboardActivity"));
KNOWN_COPY_HANDLERS.add(new ComponentName(
"com.aokp.romcontrol",
"com.aokp.romcontrol.ShareToClipboard"));
}
}
public static String getTitle(final Intent intent) {
if (intent.hasExtra(EXTRA_FLAG_TITLE))
return intent.getStringExtra(EXTRA_FLAG_TITLE);
throw new IllegalArgumentException("Got an intent not generated by IntentGenerator");
}
public static boolean isDefault(final Intent intent) {
return intent.hasExtra(EXTRA_FLAG_IS_DEFAULT) && intent.getBooleanExtra(EXTRA_FLAG_IS_DEFAULT, false);
}
private final Context mContext;
private final PackageManager mPackageManager;
public IntentGenerator(final Context context) {
mContext = context;
mPackageManager = mContext.getPackageManager();
}
private boolean containsCopyIntent(final List<Intent> targets) {
for (final Intent intent : targets) {
for (final ComponentName handler : KNOWN_COPY_HANDLERS) {
if (handler.equals(intent.getComponent())) return true;
}
}
return false;
}
private ArrayList<Intent> resolveTargets(final Intent intent) {
final String packageName = mContext.getPackageName();
final List<ResolveInfo> activityList = mPackageManager.queryIntentActivities(intent, 0);
final ResolveInfo defaultTarget = mPackageManager.resolveActivity(intent, 0);
final ArrayList<Intent> list = new ArrayList<Intent>(activityList.size());
for (final ResolveInfo resolveInfo : activityList) {
final ActivityInfo activity = resolveInfo.activityInfo;
final ComponentName component = new ComponentName(activity.packageName, activity.name);
// remove IITCm from list
if (activity.packageName.equals(packageName)) continue;
final Intent targetIntent = new Intent(intent)
.setComponent(component)
.putExtra(EXTRA_FLAG_TITLE, activity.loadLabel(mPackageManager));
if (resolveInfo.activityInfo.name.equals(defaultTarget.activityInfo.name) &&
resolveInfo.activityInfo.packageName.equals(defaultTarget.activityInfo.packageName)) {
targetIntent.putExtra(EXTRA_FLAG_IS_DEFAULT, true);
}
list.add(targetIntent);
}
return list;
}
public ArrayList<Intent> getBrowserIntents(final String title, final String url) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
return resolveTargets(intent);
}
public ArrayList<Intent> getGeoIntents(final String title, final String mLl, final int mZoom) {
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(String.format("geo:%s&z=%d", mLl, mZoom)))
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
final ArrayList<Intent> targets = resolveTargets(intent);
// According to https://developer.android.com/guide/components/intents-common.html, markers can be labeled.
// Unfortunately, only Google Maps supports this, most other apps fail
for (final Intent target : targets) {
final ComponentName cn = target.getComponent();
if ("com.google.android.apps.maps".equals(cn.getPackageName())) {
try {
final String encodedTitle = URLEncoder.encode(title, "UTF-8");
target.setData(Uri.parse(String.format("geo:0,0?q=%s%%20(%s)&z=%d", mLl, encodedTitle, mZoom)));
} catch (final UnsupportedEncodingException e) {
Log.w(e);
}
break;
}
}
return targets;
}
public ArrayList<Intent> getShareIntents(final String title, final String text) {
final Intent intent = new Intent(Intent.ACTION_SEND)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, text)
.putExtra(Intent.EXTRA_SUBJECT, title);
final ArrayList<Intent> targets = resolveTargets(intent);
if (!containsCopyIntent(targets)) {
// add SendToClipboard intent in case Drive is not installed
targets.add(new Intent(intent).setComponent(new ComponentName(mContext, SendToClipboard.class)));
}
return targets;
}
}

View File

@ -1,16 +1,13 @@
package com.cradle.iitc_mobile.share;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -18,90 +15,27 @@ 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.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
public class IntentListView extends ListView {
private static class CopyHandler extends Pair<String, String> {
public CopyHandler(ResolveInfo resolveInfo) {
super(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
}
public CopyHandler(String packageName, String name) {
super(packageName, name);
}
}
private class IntentAdapter extends ArrayAdapter<ResolveInfo> {
// 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(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
TextView view = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
ActivityInfo info = getItem(position).activityInfo;
CharSequence label = info.loadLabel(mPackageManager);
// get icon and scale it manually to ensure that all have the same size
Drawable icon = info.loadIcon(mPackageManager);
DisplayMetrics dm = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
float densityScale = dm.density;
float scaledWidth = MDPI_PX * densityScale;
float scaledHeight = MDPI_PX * densityScale;
icon.setBounds(0,0,Math.round(scaledWidth),Math.round(scaledHeight));
view.setText(label);
view.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.icon_margin));
view.setCompoundDrawables(icon, null, null, null);
return view;
}
}
private static final HashSet<CopyHandler> KNOWN_COPY_HANDLERS = new HashSet<CopyHandler>();
static {
if (KNOWN_COPY_HANDLERS.isEmpty()) {
KNOWN_COPY_HANDLERS.add(new CopyHandler(
"com.google.android.apps.docs",
"com.google.android.apps.docs.app.SendTextToClipboardActivity"));
KNOWN_COPY_HANDLERS.add(new CopyHandler(
"com.aokp.romcontrol",
"com.aokp.romcontrol.ShareToClipboard"));
}
}
private HashMap<ComponentName, Intent> mActivities = new HashMap<ComponentName, Intent>();
private IntentAdapter mAdapter;
private PackageManager mPackageManager;
public IntentListView(Context context) {
public IntentListView(final Context context) {
super(context);
init();
}
public IntentListView(Context context, AttributeSet attrs) {
public IntentListView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
public IntentListView(Context context, AttributeSet attrs, int defStyle) {
public IntentListView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init();
}
@ -112,98 +46,54 @@ public class IntentListView extends ListView {
setAdapter(mAdapter);
}
public ResolveInfo getItem(int position) {
public Intent getItem(final int position) {
return mAdapter.getItem(position);
}
public Intent getTargetIntent(int position) {
ActivityInfo activity = mAdapter.getItem(position).activityInfo;
public void setIntents(final List<Intent> intents) {
Collections.sort(intents, ((ShareActivity) getContext()).getIntentComparator());
ComponentName activityId = new ComponentName(activity.packageName, activity.name);
Intent intentType = mActivities.get(activityId);
Intent intent = new Intent(intentType)
.setComponent(activityId)
.setPackage(activity.packageName);
return intent;
}
// wrapper method for single intents
public void setIntent(Intent intent) {
ArrayList<Intent> intentList = new ArrayList<Intent>(1);
intentList.add(intent);
setIntents(intentList);
}
public void setIntents(ArrayList<Intent> intents) {
mAdapter.setNotifyOnChange(false);
mAdapter.clear();
String packageName = getContext().getPackageName();
ArrayList<ResolveInfo> allActivities = new ArrayList<ResolveInfo>();
for (Intent intent : intents) {
List<ResolveInfo> activityList = mPackageManager.queryIntentActivities(intent, 0);
ResolveInfo defaultTarget = mPackageManager.resolveActivity(intent, 0);
boolean hasCopyIntent = false;
for (ResolveInfo resolveInfo : activityList) { // search for "Copy to clipboard" handler
CopyHandler handler = new CopyHandler(resolveInfo);
if (KNOWN_COPY_HANDLERS.contains(handler)) {
hasCopyIntent = true;
}
}
// use traditional loop since list may change during iteration
for (int i = 0; i < activityList.size(); i++) {
ResolveInfo info = activityList.get(i);
ActivityInfo activity = info.activityInfo;
// fix bug in PackageManager - a replaced package name might cause non-exported intents to appear
if (!activity.exported && !activity.packageName.equals(packageName)) {
activityList.remove(i);
i--;
continue;
}
// remove all IITCm intents, except for SendToClipboard in case Drive is not installed
if (activity.packageName.equals(packageName)) {
if (hasCopyIntent || !activity.name.equals(SendToClipboard.class.getCanonicalName())) {
activityList.remove(i);
i--;
continue;
}
}
}
// add to activity hash map if they doesn't exist
for (ResolveInfo resolveInfo : activityList) {
ActivityInfo activity = resolveInfo.activityInfo;
ComponentName activityId = new ComponentName(activity.packageName, activity.name);
// ResolveInfo.isDefault usually means "The target would like to be considered a default action that the
// user can perform on this data." It is set by the package manager, but we overwrite it to store
// whether this app is the default for the given intent
resolveInfo.isDefault = resolveInfo.activityInfo.name.equals(defaultTarget.activityInfo.name)
&& resolveInfo.activityInfo.packageName.equals(defaultTarget.activityInfo.packageName);
if (!mActivities.containsKey(activityId)) {
mActivities.put(activityId, intent);
allActivities.add(resolveInfo);
}
}
}
Collections.sort(allActivities, ((ShareActivity) getContext()).getIntentComparator());
mAdapter.addAll(allActivities);
mAdapter.setNotifyOnChange(true);
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;
}
}
}

View File

@ -4,7 +4,6 @@ import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
@ -12,16 +11,14 @@ import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import com.cradle.iitc_mobile.Log;
import com.cradle.iitc_mobile.R;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
public class ShareActivity extends FragmentActivity implements ActionBar.TabListener {
private IntentComparator mComparator;
private IntentFragmentAdapter mFragmentAdapter;
private IntentGenerator mGenerator;
private boolean mIsPortal;
private String mLl;
private SharedPreferences mSharedPrefs = null;
@ -29,9 +26,9 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
private ViewPager mViewPager;
private int mZoom;
private void addTab(ArrayList<Intent> intents, int label, int icon) {
IntentFragment fragment = new IntentFragment();
Bundle args = new Bundle();
private void addTab(final ArrayList<Intent> intents, final int label, final int icon) {
final IntentFragment fragment = new IntentFragment();
final Bundle args = new Bundle();
args.putParcelableArrayList("intents", intents);
args.putString("title", getString(label));
args.putInt("icon", icon);
@ -39,13 +36,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
mFragmentAdapter.add(fragment);
}
private void addTab(Intent intent, int label, int icon) {
ArrayList<Intent> intents = new ArrayList<Intent>(1);
intents.add(intent);
addTab(intents, label, icon);
}
private String getUrl() {
private String getIntelUrl() {
String url = "http://www.ingress.com/intel?ll=" + mLl + "&z=" + mZoom;
if (mIsPortal) {
url += "&pll=" + mLl;
@ -53,7 +44,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
return url;
}
private void setSelected(int position) {
private void setSelected(final int position) {
// Activity not fully loaded yet (may occur during tab creation)
if (mSharedPrefs == null) return;
@ -63,53 +54,20 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
.apply();
}
private void setupIntents() {
setupShareIntent(getUrl());
// we merge gmaps intents with geo intents since it is not possible
// anymore to set a labeled marker on geo intents
ArrayList<Intent> intents = new ArrayList<Intent>();
String gMapsUri;
try {
gMapsUri = "http://maps.google.com/?q=loc:" + mLl
+ "%20(" + URLEncoder.encode(mTitle, "UTF-8") + ")&z=" + mZoom;
} catch (UnsupportedEncodingException e) {
gMapsUri = "http://maps.google.com/?ll=" + mLl + "&z=" + mZoom;
Log.w(e);
}
Intent gMapsIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(gMapsUri));
intents.add(gMapsIntent);
String geoUri = "geo:" + mLl;
Intent geoIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(geoUri));
intents.add(geoIntent);
addTab(intents, R.string.tab_map, R.drawable.ic_action_place);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getUrl()));
addTab(intent, R.string.tab_browser, R.drawable.ic_action_web_site);
}
private void setupShareIntent(String str) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, str);
intent.putExtra(Intent.EXTRA_SUBJECT, mTitle);
addTab(intent, R.string.tab_share, R.drawable.ic_action_share);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
mComparator = new IntentComparator(this);
mGenerator = new IntentGenerator(this);
mFragmentAdapter = new IntentFragmentAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
final Intent intent = getIntent();
// from portallinks/permalinks we build 3 intents (share / geo / vanilla-intel-link)
if (!intent.getBooleanExtra("onlyShare", false)) {
mTitle = intent.getStringExtra("title");
@ -118,10 +76,21 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
mIsPortal = intent.getBooleanExtra("isPortal", false);
actionBar.setTitle(mTitle);
setupIntents();
addTab(mGenerator.getShareIntents(mTitle, getIntelUrl()),
R.string.tab_share,
R.drawable.ic_action_share);
addTab(mGenerator.getGeoIntents(mTitle, mLl, mZoom),
R.string.tab_map,
R.drawable.ic_action_place);
addTab(mGenerator.getBrowserIntents(mTitle, getIntelUrl()),
R.string.tab_browser,
R.drawable.ic_action_web_site);
} else {
mTitle = getString(R.string.app_name);
setupShareIntent(intent.getStringExtra("shareString"));
final String shareString = intent.getStringExtra("shareString");
addTab(mGenerator.getShareIntents(mTitle, shareString), R.string.tab_share, R.drawable.ic_action_share);
}
mViewPager = (ViewPager) findViewById(R.id.pager);
@ -129,7 +98,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
public void onPageSelected(final int position) {
if (actionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_STANDARD) {
actionBar.setSelectedNavigationItem(position);
}
@ -138,7 +107,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
});
for (int i = 0; i < mFragmentAdapter.getCount(); i++) {
IntentFragment fragment = (IntentFragment) mFragmentAdapter.getItem(i);
final IntentFragment fragment = (IntentFragment) mFragmentAdapter.getItem(i);
actionBar.addTab(actionBar
.newTab()
@ -152,7 +121,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
}
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int selected = mSharedPrefs.getInt("pref_share_selected_tab", 0);
final int selected = mSharedPrefs.getInt("pref_share_selected_tab", 0);
if (selected < mFragmentAdapter.getCount()) {
mViewPager.setCurrentItem(selected);
if (actionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_STANDARD) {
@ -172,7 +141,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
@ -182,17 +151,17 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
public void onTabReselected(final ActionBar.Tab tab, final FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
int position = tab.getPosition();
public void onTabSelected(final ActionBar.Tab tab, final FragmentTransaction fragmentTransaction) {
final int position = tab.getPosition();
mViewPager.setCurrentItem(position);
setSelected(position);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
public void onTabUnselected(final ActionBar.Tab tab, final FragmentTransaction fragmentTransaction) {
}
}