diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-08-02 07:25:44 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-08-02 07:25:44 +0000 |
commit | 8940d4da680d6c25f7a7a280f70d5e63971e624f (patch) | |
tree | fa9af2f02f2bdb64d18e72963a727d56e1fe0eb3 /app/src | |
parent | f86f06ffe513e250df51dbdc7045966b220a357f (diff) | |
download | infinity-for-reddit-8940d4da680d6c25f7a7a280f70d5e63971e624f.tar infinity-for-reddit-8940d4da680d6c25f7a7a280f70d5e63971e624f.tar.gz infinity-for-reddit-8940d4da680d6c25f7a7a280f70d5e63971e624f.tar.bz2 infinity-for-reddit-8940d4da680d6c25f7a7a280f70d5e63971e624f.tar.lz infinity-for-reddit-8940d4da680d6c25f7a7a280f70d5e63971e624f.tar.xz infinity-for-reddit-8940d4da680d6c25f7a7a280f70d5e63971e624f.tar.zst infinity-for-reddit-8940d4da680d6c25f7a7a280f70d5e63971e624f.zip |
Fixed deep link error handling.
Diffstat (limited to '')
3 files changed, 61 insertions, 32 deletions
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<ResolveInfo> 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<ResolveInfo> activities = pm.queryIntentActivities(intent, 0); + ArrayList<String> 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<ResolveInfo> 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<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0); + ArrayList<ResolveInfo> 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<String> 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); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 09bb103a..f3d2bbfb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -146,4 +146,7 @@ <string name="sort_relevance">Relevance</string> <string name="sort_comments">Comments</string> <string name="sort_activity">Activity</string> + + <string name="open_link_with">Open link with</string> + <string name="no_browser_found">No browser found</string> </resources> |