From c6c9818751a142fca2284e758a9d956e9cbcd36c Mon Sep 17 00:00:00 2001 From: ekager Date: Wed, 3 Jul 2019 21:17:16 -0700 Subject: [PATCH] For #3240 - Create AuthCustomTabActivity to fix dead end on auth --- app/src/main/AndroidManifest.xml | 92 +++++++++++-------- .../mozilla/fenix/IntentReceiverActivity.kt | 8 +- .../features/FirefoxAccountsAuthFeature.kt | 2 +- .../fenix/customtabs/AuthCustomTabActivity.kt | 36 ++++++++ .../fenix/customtabs/CustomTabActivity.kt | 2 +- .../mozilla/fenix/settings/SupportUtils.kt | 13 ++- app/src/main/res/values/static_strings.xml | 3 + 7 files changed, 113 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/customtabs/AuthCustomTabActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 86377e1e5..74b281b0e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,13 @@ + xmlns:tools="http://schemas.android.com/tools" + package="org.mozilla.fenix"> - - - - + + + + @@ -14,53 +15,67 @@ - + android:name=".FenixApplication" + android:allowBackup="true" + android:icon="@mipmap/ic_launcher" + android:label="@string/app_name" + android:roundIcon="@mipmap/ic_launcher_round" + android:supportsRtl="true" + android:theme="@style/NormalTheme" + android:usesCleartextTraffic="true" + tools:ignore="UnusedAttribute"> + - + - + - + + + + + + + - - - + + + @@ -76,17 +91,18 @@ + android:resource="@mipmap/ic_launcher" /> - + + android:name=".customtabs.CustomTabsService" + android:exported="true" + tools:ignore="ExportedService"> diff --git a/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt b/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt index 05df1b0c7..83e041a98 100644 --- a/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt @@ -10,12 +10,14 @@ import android.os.Bundle import mozilla.components.browser.session.tab.CustomTabConfig import mozilla.components.support.utils.SafeIntent import org.mozilla.fenix.components.NotificationManager.Companion.RECEIVE_TABS_TAG +import org.mozilla.fenix.customtabs.AuthCustomTabActivity import org.mozilla.fenix.customtabs.CustomTabActivity import org.mozilla.fenix.ext.components import org.mozilla.fenix.utils.Settings class IntentReceiverActivity : Activity() { + @Suppress("ComplexMethod") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -34,7 +36,11 @@ class IntentReceiverActivity : Activity() { val openToBrowser = when { CustomTabConfig.isCustomTabIntent(SafeIntent(intent)) -> { - intent.setClassName(applicationContext, CustomTabActivity::class.java.name) + intent.setClassName( + applicationContext, + if (intent.hasExtra(getString(R.string.intent_extra_auth))) AuthCustomTabActivity::class.java.name + else CustomTabActivity::class.java.name + ) true } intent.action == Intent.ACTION_VIEW -> { diff --git a/app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt b/app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt index 0f3a932cb..7d873f614 100644 --- a/app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt +++ b/app/src/main/java/org/mozilla/fenix/components/features/FirefoxAccountsAuthFeature.kt @@ -46,7 +46,7 @@ class FirefoxAccountsAuthFeature( // UI to the user. // It's possible that the underlying problem will go away by the time the tab actually // loads, resulting in a confusing experience. - val intent = SupportUtils.createCustomTabIntent(context, authUrl) + val intent = SupportUtils.createAuthCustomTabIntent(context, authUrl) context.startActivity(intent) } } diff --git a/app/src/main/java/org/mozilla/fenix/customtabs/AuthCustomTabActivity.kt b/app/src/main/java/org/mozilla/fenix/customtabs/AuthCustomTabActivity.kt new file mode 100644 index 000000000..7cf34f1b7 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/customtabs/AuthCustomTabActivity.kt @@ -0,0 +1,36 @@ +package org.mozilla.fenix.customtabs + +/* This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import mozilla.components.concept.sync.AccountObserver +import mozilla.components.concept.sync.OAuthAccount +import mozilla.components.concept.sync.Profile +import mozilla.components.service.fxa.manager.FxaAccountManager +import org.mozilla.fenix.ext.components + +class AuthCustomTabActivity : CustomTabActivity() { + private lateinit var accountManager: FxaAccountManager + + // Navigate away from this activity when we have successful authentication + private val accountStateObserver = object : AccountObserver { + override fun onAuthenticated(account: OAuthAccount) { + this@AuthCustomTabActivity.finish() + } + + override fun onAuthenticationProblems() {} + + override fun onError(error: Exception) {} + + override fun onLoggedOut() {} + + override fun onProfileUpdated(profile: Profile) {} + } + + override fun onResume() { + super.onResume() + accountManager = this.components.backgroundServices.accountManager + accountManager.register(accountStateObserver, this, true) + } +} diff --git a/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabActivity.kt b/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabActivity.kt index 5132bf319..b817149dc 100644 --- a/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/customtabs/CustomTabActivity.kt @@ -6,6 +6,6 @@ package org.mozilla.fenix.customtabs import org.mozilla.fenix.HomeActivity -class CustomTabActivity : HomeActivity() { +open class CustomTabActivity : HomeActivity() { override val isCustomTab = true } diff --git a/app/src/main/java/org/mozilla/fenix/settings/SupportUtils.kt b/app/src/main/java/org/mozilla/fenix/settings/SupportUtils.kt index 7306cff80..c61c5db8e 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/SupportUtils.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/SupportUtils.kt @@ -47,8 +47,17 @@ object SupportUtils { } fun createCustomTabIntent(context: Context, url: String) = Intent(Intent.ACTION_VIEW).apply { - putExtra("android.support.customtabs.extra.TOOLBAR_COLOR", R.attr.foundation.getColorFromAttr(context)) - putExtra("android.support.customtabs.extra.SESSION", true) + putExtra(context.getString(R.string.intent_extra_toolbar_color), R.attr.foundation.getColorFromAttr(context)) + putExtra(context.getString(R.string.intent_extra_session), true) + setClassName(context.applicationContext, IntentReceiverActivity::class.java.name) + data = Uri.parse(url) + setPackage(context.packageName) + } + + fun createAuthCustomTabIntent(context: Context, url: String) = Intent(Intent.ACTION_VIEW).apply { + putExtra(context.getString(R.string.intent_extra_toolbar_color), R.attr.foundation.getColorFromAttr(context)) + putExtra(context.getString(R.string.intent_extra_session), true) + putExtra(context.getString(R.string.intent_extra_auth), true) setClassName(context.applicationContext, IntentReceiverActivity::class.java.name) data = Uri.parse(url) setPackage(context.packageName) diff --git a/app/src/main/res/values/static_strings.xml b/app/src/main/res/values/static_strings.xml index 597784f7d..dc1b21c4b 100644 --- a/app/src/main/res/values/static_strings.xml +++ b/app/src/main/res/values/static_strings.xml @@ -14,6 +14,9 @@ %1$s is produced by Mozilla. + android.support.customtabs.extra.TOOLBAR_COLOR + android.support.customtabs.extra.SESSION + support.customtabs.extra.AUTH