Skip to content

Commit

Permalink
Merge pull request #478 from gdg-x/477/external-intent
Browse files Browse the repository at this point in the history
Fix external Intent problem.
  • Loading branch information
friedger committed Sep 3, 2015
2 parents 136007f + 2dd6992 commit 9e564c2
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 23 deletions.
Expand Up @@ -27,7 +27,6 @@ protected void onCreate(Bundle savedInstanceState) {
}

finish();

}

/**
Expand Down
@@ -1,7 +1,6 @@
package org.gdg.frisbee.android.arrow;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
Expand All @@ -13,6 +12,7 @@

import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.app.App;
import org.gdg.frisbee.android.utils.Utils;
import org.gdg.frisbee.android.view.BitmapBorderTransformation;

import butterknife.Bind;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
context.startActivity(Utils.createExternalIntent(context,
Uri.parse("https://plus.google.com/" + organizer.getPlusId() + "/posts")));
}
});
Expand Down
Expand Up @@ -16,7 +16,6 @@

package org.gdg.frisbee.android.chapter;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -226,7 +225,7 @@ private void addOrganizerToUI(final Person organizer) {
public void onClick(View view) {
String url = organizer.getUrl();
if (!TextUtils.isEmpty(url)) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
startActivity(Utils.createExternalIntent(getActivity(), Uri.parse(url)));
}
}
});
Expand Down
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
Expand All @@ -18,9 +17,10 @@
import org.gdg.frisbee.android.Const;
import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.api.model.LeadMessage;
import org.gdg.frisbee.android.utils.Utils;

import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.ButterKnife;

public class LeadFragment extends ListFragment {
private LeadAnnouncementsAdapter mAdapter;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
final LeadMessage item = mAdapter.getItem(position);
if (item.getType() == LeadMessage.Type.resource) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getLinkUrl())));
startActivity(Utils.createExternalIntent(getActivity(), Uri.parse(item.getLinkUrl())));
}
}

Expand Down
Expand Up @@ -328,9 +328,7 @@ private void populateArticle(ViewHolder mViewHolder, ViewGroup container, final
attachmentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(attachment.getUrl()));
mContext.startActivity(i);
mContext.startActivity(Utils.createExternalIntent(mContext, Uri.parse(attachment.getUrl())));
}
});
}
Expand Down Expand Up @@ -370,7 +368,7 @@ public void onClick(View view) {
playVideoIntent.putExtra(YoutubeActivity.EXTRA_VIDEO_ID, videoId);
mContext.startActivity(playVideoIntent);
} else {
Intent viewUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(attachment.getUrl()));
Intent viewUrlIntent = Utils.createExternalIntent(mContext, Uri.parse(attachment.getUrl()));
if (Utils.canLaunch(mContext, viewUrlIntent)) {
mContext.startActivity(viewUrlIntent);
}
Expand Down Expand Up @@ -456,9 +454,7 @@ public void onClick(View view) {
}

private void openEventInGPlus(String uri) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uri));
mContext.startActivity(i);
mContext.startActivity(Utils.createExternalIntent(mContext, Uri.parse(uri)));

}

Expand Down
@@ -1,6 +1,5 @@
package org.gdg.frisbee.android.common;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
Expand All @@ -10,6 +9,7 @@

import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.api.model.GdgPerson;
import org.gdg.frisbee.android.utils.Utils;

import butterknife.ButterKnife;

Expand All @@ -36,6 +36,6 @@ public void onActivityCreated(Bundle savedInstanceState) {
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
GdgPerson person = mAdapter.getItem(position);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(person.getUrl())));
startActivity(Utils.createExternalIntent(getActivity(), Uri.parse(person.getUrl())));
}
}
Expand Up @@ -17,7 +17,6 @@
package org.gdg.frisbee.android.eventseries;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
Expand All @@ -35,6 +34,7 @@
import org.gdg.frisbee.android.api.model.Event;
import org.gdg.frisbee.android.api.model.SimpleEvent;
import org.gdg.frisbee.android.app.App;
import org.gdg.frisbee.android.utils.Utils;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
Expand All @@ -44,8 +44,8 @@
import java.util.Collections;
import java.util.Comparator;

import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.ButterKnife;

class EventAdapter extends BaseAdapter {

Expand Down Expand Up @@ -164,9 +164,7 @@ private void openEventLink(SimpleEvent event) {
}

private void openEventInExternalApp(String uri) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uri));
mContext.startActivity(i);
mContext.startActivity(Utils.createExternalIntent(mContext, Uri.parse(uri)));
}

public void sort(Comparator<Item> eventComparator) {
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/org/gdg/frisbee/android/utils/Utils.java
Expand Up @@ -16,14 +16,17 @@

package org.gdg.frisbee.android.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ShareCompat;
import android.util.DisplayMetrics;
import android.util.Patterns;
import android.util.TypedValue;
Expand Down Expand Up @@ -209,8 +212,18 @@ public static boolean isEmailAddress(String possibleEmail) {
return Patterns.EMAIL_ADDRESS.matcher(possibleEmail).matches();
}


public static boolean canLaunch(Context context, final Intent viewUrlIntent) {
return context.getPackageManager().resolveActivity(viewUrlIntent, PackageManager.MATCH_DEFAULT_ONLY) != null;
}

@SuppressWarnings("deprecation")
public static Intent createExternalIntent(Context context, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra(ShareCompat.EXTRA_CALLING_PACKAGE, context.getPackageName());
if (context instanceof Activity) {
intent.putExtra(ShareCompat.EXTRA_CALLING_ACTIVITY, ((Activity) context).getComponentName());
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
return intent;
}
}

0 comments on commit 9e564c2

Please sign in to comment.