From 816fb7d94d4e2626b7d71139de882a26687e7fae Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 22 Jan 2021 15:32:13 +0100 Subject: [PATCH] complete customTabs for links https://developers.google.com/web/android/custom-tabs --- .../conversations/ui/EditAccountActivity.java | 14 +++----- .../conversations/ui/util/CustomTab.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/util/CustomTab.java diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index a37d802a5..c7c57cffe 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -64,9 +64,9 @@ import eu.siacs.conversations.services.XmppConnectionService.OnCaptchaRequested; import eu.siacs.conversations.ui.adapter.KnownHostsAdapter; import eu.siacs.conversations.ui.adapter.PresenceTemplateAdapter; import eu.siacs.conversations.ui.util.AvatarWorkerTask; +import eu.siacs.conversations.ui.util.CustomTab; import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.ui.util.SoftKeyboardUtils; -import eu.siacs.conversations.utils.CharSequenceUtils; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.EasyOnboardingInvite; import eu.siacs.conversations.utils.MenuDoubleTabUtil; @@ -169,8 +169,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat URL url = connection != null && redirectionWorthyStatus ? connection.getRedirectionUrl() : null; if (url != null && !wasDisabled) { try { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url.toString()))); - overridePendingTransition(R.animator.fade_in, R.animator.fade_out); + CustomTab.openTab(EditAccountActivity.this, Uri.parse(url.toString()), isDarkTheme()); return; } catch (ActivityNotFoundException e) { ToastCompat.makeText(EditAccountActivity.this, R.string.application_found_to_open_website, Toast.LENGTH_SHORT).show(); @@ -667,13 +666,11 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat } this.binding.showPrivacyPolicy.setOnClickListener(view -> { final Uri uri = Uri.parse("https://jabber.pix-art.de/privacy/"); - Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(browserIntent); + CustomTab.openTab(EditAccountActivity.this, uri, isDarkTheme()); }); this.binding.showTermsOfUse.setOnClickListener(view -> { final Uri uri = Uri.parse("https://jabber.pix-art.de/termsofuse/"); - Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(browserIntent); + CustomTab.openTab(EditAccountActivity.this, uri, isDarkTheme()); }); } @@ -960,8 +957,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat builder.setPositiveButton(R.string.confirm, (dialog, which) -> { try { Uri uri = Uri.parse(getSupportSite(mAccount.getJid().getDomain().toEscapedString())); - final Intent intent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(intent); + CustomTab.openTab(EditAccountActivity.this, uri, isDarkTheme()); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/eu/siacs/conversations/ui/util/CustomTab.java b/src/main/java/eu/siacs/conversations/ui/util/CustomTab.java new file mode 100644 index 000000000..c67cfd5ef --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/util/CustomTab.java @@ -0,0 +1,36 @@ +package eu.siacs.conversations.ui.util; + +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.graphics.Color; +import android.net.Uri; +import android.provider.Browser; + +import androidx.browser.customtabs.CustomTabColorSchemeParams; +import androidx.browser.customtabs.CustomTabsIntent; + +import eu.siacs.conversations.R; + +import static eu.siacs.conversations.utils.Compatibility.runsTwentyOne; + +public class CustomTab { + public static void openTab(Context context, Uri uri, boolean dark) throws ActivityNotFoundException { + CustomTabsIntent.Builder tabBuilder = new CustomTabsIntent.Builder(); + tabBuilder.setShowTitle(true); + tabBuilder.setUrlBarHidingEnabled(false); + tabBuilder.setDefaultColorSchemeParams(new CustomTabColorSchemeParams.Builder() + .setToolbarColor(StyledAttributes.getColor(context, R.attr.colorPrimary)) + .setSecondaryToolbarColor(StyledAttributes.getColor(context, R.attr.colorPrimaryDark)) + .setNavigationBarColor(Color.BLUE) + .build()); + tabBuilder.setColorScheme(dark ? CustomTabsIntent.COLOR_SCHEME_DARK : CustomTabsIntent.COLOR_SCHEME_LIGHT); + tabBuilder.setShareState(CustomTabsIntent.SHARE_STATE_ON); + CustomTabsIntent customTabsIntent = tabBuilder.build(); + if (runsTwentyOne()) { + customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); + } + customTabsIntent.intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); + customTabsIntent.launchUrl(context, uri); + } +}