From 5693bc384d2a6b8267b098e828fae2ef875a68a7 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 5 Feb 2020 13:41:20 +0800 Subject: Fix bugs related to changing default post layout. Rewrite opening links to handle error. Version 2.2.0. --- .../Activity/LinkResolverActivity.java | 41 ++++++++++++++++------ .../Adapter/CommentAndPostRecyclerViewAdapter.java | 2 +- .../Adapter/PostRecyclerViewAdapter.java | 4 +-- .../Event/ChangeDefaultPostLayoutEvent.java | 9 +++++ .../infinityforreddit/Fragment/PostFragment.java | 38 +++++++++++++++++++- .../Settings/InterfacePreferenceFragment.java | 4 +-- 6 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDefaultPostLayoutEvent.java (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java index b066cb99..a049e9e0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java @@ -155,17 +155,21 @@ public class LinkResolverActivity extends AppCompatActivity { private void deepLinkError(Uri uri) { PackageManager pm = getPackageManager(); - if (mSharedPreferences.getBoolean(SharedPreferencesUtils.OPEN_LINK_IN_APP, false)) { - openInCustomTabs(uri, pm); - return; - } String authority = uri.getAuthority(); if(authority != null && (authority.contains("reddit.com") || authority.contains("redd.it") || authority.contains("reddit.app.link"))) { - openInCustomTabs(uri, pm); + openInCustomTabs(uri, pm, false); return; } + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.OPEN_LINK_IN_APP, false)) { + openInCustomTabs(uri, pm, true); + } else { + openInBrowser(uri, pm, true); + } + } + + private void openInBrowser(Uri uri, PackageManager pm, boolean handleError) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); @@ -184,13 +188,22 @@ public class LinkResolverActivity extends AppCompatActivity { try { startActivity(intent); } catch (ActivityNotFoundException e) { - openInCustomTabs(uri, pm); + if (handleError) { + openInCustomTabs(uri, pm, false); + } else { + Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show(); + } } } else { - openInCustomTabs(uri, pm); + if (handleError) { + openInCustomTabs(uri, pm, false); + } 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")); @@ -210,7 +223,7 @@ public class LinkResolverActivity extends AppCompatActivity { return packagesSupportingCustomTabs; } - private void openInCustomTabs(Uri uri, PackageManager pm) { + private void openInCustomTabs(Uri uri, PackageManager pm, boolean handleError) { ArrayList resolveInfos = getCustomTabsPackages(pm); if (!resolveInfos.isEmpty()) { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); @@ -225,10 +238,18 @@ public class LinkResolverActivity extends AppCompatActivity { try { customTabsIntent.launchUrl(this, uri); } catch (ActivityNotFoundException e) { - Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show(); + if (handleError) { + openInBrowser(uri, pm, false); + } else { + Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show(); + } } } else { - Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show(); + if (handleError) { + openInBrowser(uri, pm, false); + } else { + Toast.makeText(this, R.string.no_browser_found, Toast.LENGTH_SHORT).show(); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index a506ceba..52372b76 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1305,7 +1305,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - EventBus.getDefault().post(new ChangePostLayoutEvent(Integer.parseInt((String) newValue))); + EventBus.getDefault().post(new ChangeDefaultPostLayoutEvent(Integer.parseInt((String) newValue))); return true; }); } -- cgit v1.2.3