From 8940d4da680d6c25f7a7a280f70d5e63971e624f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 2 Aug 2019 15:25:44 +0800 Subject: Fixed deep link error handling. --- .../infinityforreddit/LinkResolverActivity.java | 64 +++++++++++++++++++--- .../infinityforreddit/ViewUserDetailActivity.java | 26 +-------- 2 files changed, 58 insertions(+), 32 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java index 93ddc7dd..8df02968 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java @@ -1,14 +1,20 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Bundle; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.browser.customtabs.CustomTabsIntent; +import java.util.ArrayList; import java.util.List; +import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION; + public class LinkResolverActivity extends AppCompatActivity { private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/*[\\w+]*/*"; @@ -58,12 +64,56 @@ public class LinkResolverActivity extends AppCompatActivity { } private void deepLinkError(Uri uri) { - //Deep link error handling - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - // add share action to menu list - builder.addDefaultShareMenuItem(); - builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); - CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(this, uri); + PackageManager pm = getPackageManager(); + ArrayList resolveInfos = getCustomTabsPackages(pm); + if(!resolveInfos.isEmpty()) { + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + // add share action to menu list + builder.addDefaultShareMenuItem(); + builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.intent.setPackage(resolveInfos.get(0).activityInfo.packageName); + customTabsIntent.launchUrl(this, uri); + } else { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(uri); + + List activities = pm.queryIntentActivities(intent, 0); + ArrayList packageNames = new ArrayList<>(); + + String currentPackageName = getApplicationContext().getPackageName(); + + for(ResolveInfo info : activities) { + if(!info.activityInfo.packageName.equals(currentPackageName)) { + packageNames.add(info.activityInfo.packageName); + } + } + + if(!packageNames.isEmpty()) { + intent.setPackage(packageNames.get(0)); + startActivity(intent); + } else { + Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show(); + } + } + } + + private ArrayList getCustomTabsPackages(PackageManager pm) { + // Get default VIEW intent handler. + Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com")); + + // Get all apps that can handle VIEW intents. + List resolvedActivityList = pm.queryIntentActivities(activityIntent, 0); + ArrayList packagesSupportingCustomTabs = new ArrayList<>(); + for (ResolveInfo info : resolvedActivityList) { + Intent serviceIntent = new Intent(); + serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION); + serviceIntent.setPackage(info.activityInfo.packageName); + // Check if this package also resolves the Custom Tabs service. + if (pm.resolveService(serviceIntent, 0) != null) { + packagesSupportingCustomTabs.add(info); + } + } + return packagesSupportingCustomTabs; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 36fecc05..cc1edbad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; import android.content.SharedPreferences; -import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; @@ -14,7 +13,6 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; -import androidx.browser.customtabs.CustomTabsIntent; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -31,8 +29,6 @@ import com.google.android.material.chip.Chip; import com.google.android.material.snackbar.Snackbar; import com.google.android.material.tabs.TabLayout; -import java.util.List; - import javax.inject.Inject; import javax.inject.Named; @@ -75,7 +71,6 @@ public class ViewUserDetailActivity extends AppCompatActivity { private AppBarLayout.LayoutParams params; private String userName; - private Uri userUri; private boolean subscriptionReady = false; private boolean isInLazyMode = false; private int colorPrimary; @@ -111,26 +106,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - if(getIntent().getData() != null) { - userUri = getIntent().getData(); - List segments = userUri.getPathSegments(); - int userIndex = segments.indexOf("user"); - if(userIndex >= 0 && userIndex < segments.size() - 1) { - userName = segments.get(userIndex + 1); - } else { - //Deep link error handling - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - builder.addDefaultShareMenuItem(); - builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); - CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(this, userUri); - finish(); - return; - } - } else { - userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); - } - + userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); String title = "u/" + userName; userNameTextView.setText(title); -- cgit v1.2.3