Skip to content

Commit 075064a

Browse files
authored
Fix deeplinks for Android 11 (#455)
* Fix deeplink for Android 11 support * Remove redundant test
1 parent 603764c commit 075064a

File tree

3 files changed

+20
-36
lines changed

3 files changed

+20
-36
lines changed

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/actions/OpenUrlAction.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,16 @@ private static void openUriIntent(Context context, Intent uriIntent) {
5959
List<ResolveInfo> resolveInfoList =
6060
context.getPackageManager().queryIntentActivities(uriIntent, 0);
6161

62-
if (resolveInfoList.isEmpty())
63-
return;
64-
6562
// If url can be handled by current app - set package name to intent, so url
6663
// will be open by current app. Skip chooser dialog.
67-
for (ResolveInfo resolveInfo : resolveInfoList) {
68-
if (resolveInfo != null && resolveInfo.activityInfo != null &&
69-
resolveInfo.activityInfo.name != null) {
70-
if (resolveInfo.activityInfo.name.contains(
71-
context.getPackageName())) {
72-
uriIntent.setPackage(resolveInfo.activityInfo.packageName);
64+
if (!resolveInfoList.isEmpty()) {
65+
for (ResolveInfo resolveInfo : resolveInfoList) {
66+
if (resolveInfo != null && resolveInfo.activityInfo != null &&
67+
resolveInfo.activityInfo.name != null) {
68+
if (resolveInfo.activityInfo.name.contains(
69+
context.getPackageName())) {
70+
uriIntent.setPackage(resolveInfo.activityInfo.packageName);
71+
}
7372
}
7473
}
7574
}

AndroidSDKPush/src/main/java/com/leanplum/LeanplumPushService.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.app.Notification;
2727
import android.app.NotificationManager;
2828
import android.app.PendingIntent;
29+
import android.content.ActivityNotFoundException;
2930
import android.content.Context;
3031
import android.content.Intent;
3132
import android.content.pm.PackageManager;
@@ -579,13 +580,18 @@ private static boolean isActivityWithIntentStarted(Context context, Bundle notif
579580
String action = notification.getString(Keys.PUSH_MESSAGE_ACTION);
580581
if (action != null && action.contains(OPEN_URL)) {
581582
Intent deepLinkIntent = getDeepLinkIntent(notification);
582-
if (deepLinkIntent != null && activityHasIntent(context, deepLinkIntent)) {
583+
if (deepLinkIntent != null) {
584+
resolveIntentActivity(context, deepLinkIntent);
583585
String messageId = LeanplumPushService.getMessageId(notification);
584586
if (messageId != null) {
585587
ActionContext actionContext = new ActionContext(
586588
ActionManager.PUSH_NOTIFICATION_ACTION_NAME, null, messageId);
587589
actionContext.track(OPEN_ACTION, 0.0, null);
588-
context.startActivity(deepLinkIntent);
590+
try {
591+
context.startActivity(deepLinkIntent);
592+
} catch (ActivityNotFoundException e) {
593+
return false;
594+
}
589595
return true;
590596
}
591597
}
@@ -612,25 +618,22 @@ private static Intent getDeepLinkIntent(Bundle notification) {
612618
}
613619

614620
/**
615-
* Checks if there is some activity that can handle intent.
621+
* Checks if url can be handled by current app to skip chooser dialog.
616622
*/
617-
private static Boolean activityHasIntent(Context context, Intent deepLinkIntent) {
623+
private static void resolveIntentActivity(Context context, Intent deepLinkIntent) {
618624
List<ResolveInfo> resolveInfoList =
619625
context.getPackageManager().queryIntentActivities(deepLinkIntent, 0);
620626
if (resolveInfoList != null && !resolveInfoList.isEmpty()) {
621627
for (ResolveInfo resolveInfo : resolveInfoList) {
622628
if (resolveInfo != null && resolveInfo.activityInfo != null &&
623629
resolveInfo.activityInfo.name != null) {
624630
if (resolveInfo.activityInfo.name.contains(context.getPackageName())) {
625-
// If url can be handled by current app - set package name to intent, so url will be
626-
// open by current app. Skip chooser dialog.
627631
deepLinkIntent.setPackage(resolveInfo.activityInfo.packageName);
628632
}
629-
return true;
633+
return;
630634
}
631635
}
632636
}
633-
return false;
634637
}
635638

636639
private static Intent getActionIntent(Context context) {

AndroidSDKTests/src/test/java/com/leanplum/LeanplumPushServiceTest.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.net.Uri;
2929
import android.os.Build;
3030
import android.os.Bundle;
31+
import android.text.TextUtils;
3132
import androidx.annotation.Nullable;
3233
import androidx.core.app.NotificationCompat;
3334

@@ -248,25 +249,6 @@ public void getDeepLinkIntentTest() throws Exception {
248249
assertEquals(deepLinkIntent.toString().trim(), result.toString().trim());
249250
}
250251

251-
252-
/**
253-
* Test for {@link LeanplumPushService#activityHasIntent(Context, Intent)} that should return
254-
* false if there is no activity that can handle intent.
255-
*
256-
* @throws Exception
257-
*/
258-
@Test
259-
public void activityHasIntentTest() throws Exception {
260-
LeanplumPushService pushService = new LeanplumPushService();
261-
Method activityHasIntentMethod = LeanplumPushService.class.
262-
getDeclaredMethod("activityHasIntent", Context.class, Intent.class);
263-
activityHasIntentMethod.setAccessible(true);
264-
Intent deepLinkIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/"));
265-
deepLinkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
266-
Boolean result = (Boolean) activityHasIntentMethod.invoke(pushService, context, deepLinkIntent);
267-
assertTrue(!result);
268-
}
269-
270252
@Test
271253
public void testHandleNotification() {
272254
Bundle bundle = new Bundle();

0 commit comments

Comments
 (0)