diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-09-10 01:02:59 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-09-10 01:02:59 +0000 |
commit | c50da35b4f713c14b6b266466a0a1feaa1ad4c20 (patch) | |
tree | a28e1db0c78411f7bf142456c90be43c00e13cc0 | |
parent | fe18c9d52082d57b458ab54b687081890a9c4ff1 (diff) | |
download | infinity-for-reddit-c50da35b4f713c14b6b266466a0a1feaa1ad4c20.tar infinity-for-reddit-c50da35b4f713c14b6b266466a0a1feaa1ad4c20.tar.gz infinity-for-reddit-c50da35b4f713c14b6b266466a0a1feaa1ad4c20.tar.bz2 infinity-for-reddit-c50da35b4f713c14b6b266466a0a1feaa1ad4c20.tar.lz infinity-for-reddit-c50da35b4f713c14b6b266466a0a1feaa1ad4c20.tar.xz infinity-for-reddit-c50da35b4f713c14b6b266466a0a1feaa1ad4c20.tar.zst infinity-for-reddit-c50da35b4f713c14b6b266466a0a1feaa1ad4c20.zip |
Handle ActivityNotFoundException when opening Custom Tabs.
Diffstat (limited to '')
-rw-r--r-- | .idea/caches/build_file_checksums.ser | bin | 533 -> 533 bytes | |||
-rw-r--r-- | .idea/caches/gradle_models.ser | bin | 309044 -> 489944 bytes | |||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java | 51 |
3 files changed, 30 insertions, 21 deletions
diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser Binary files differindex cc776a95..a3d72774 100644 --- a/.idea/caches/build_file_checksums.ser +++ b/.idea/caches/build_file_checksums.ser diff --git a/.idea/caches/gradle_models.ser b/.idea/caches/gradle_models.ser Binary files differindex b68d70df..d535e37f 100644 --- a/.idea/caches/gradle_models.ser +++ b/.idea/caches/gradle_models.ser diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java index 76ef9ffd..8c5cde11 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -152,28 +153,13 @@ public class LinkResolverActivity extends AppCompatActivity { if(uri.getScheme() == null) { uri = Uri.parse("http://" + uri.toString()); } - 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(); + try { + customTabsIntent.launchUrl(this, uri); + } catch (ActivityNotFoundException e) { + openInBrowser(uri, pm); } + } else { + openInBrowser(uri, pm); } } @@ -196,6 +182,29 @@ public class LinkResolverActivity extends AppCompatActivity { return packagesSupportingCustomTabs; } + private void openInBrowser(Uri uri, PackageManager pm) { + 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(); + } + } + public static Uri getRedditUriByPath(String path) { return Uri.parse("https://www.reddit.com" + path); } |