Skip to content

Commit

Permalink
For mozilla-mobile#3240 - Create AuthCustomTabActivity to fix dead en…
Browse files Browse the repository at this point in the history
…d on auth
  • Loading branch information
ekager committed Jul 8, 2019
1 parent 15d887e commit 2a6b271
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 41 deletions.
94 changes: 56 additions & 38 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,66 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="org.mozilla.fenix">
xmlns:tools="http://schemas.android.com/tools"
package="org.mozilla.fenix">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<!-- Needed to prompt the user to give permission to install a downloaded apk -->
<uses-permission-sdk-23 android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".FenixApplication"
android:theme="@style/NormalTheme"
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute">
<activity android:name=".HomeActivity"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize"
android:configChanges="keyboard|keyboardHidden|mcc|mnc|orientation|screenSize|locale|layoutDirection|smallestScreenSize|screenLayout">
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">
<activity
android:name=".HomeActivity"
android:configChanges="keyboard|keyboardHidden|mcc|mnc|orientation|screenSize|locale|layoutDirection|smallestScreenSize|screenLayout"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".customtabs.CustomTabActivity"
android:configChanges="keyboard|keyboardHidden|mcc|mnc|orientation|screenSize|locale|layoutDirection|smallestScreenSize|screenLayout"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
android:exported="false"
android:taskAffinity=""
android:persistableMode="persistNever"
android:autoRemoveFromRecents="false"
android:label="@string/app_name" />
<activity
android:name=".customtabs.CustomTabActivity"
android:autoRemoveFromRecents="false"
android:configChanges="keyboard|keyboardHidden|mcc|mnc|orientation|screenSize|locale|layoutDirection|smallestScreenSize|screenLayout"
android:exported="false"
android:label="@string/app_name"
android:persistableMode="persistNever"
android:taskAffinity=""
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />

<activity
android:name=".customtabs.AuthCustomTabActivity"
android:autoRemoveFromRecents="false"
android:configChanges="keyboard|keyboardHidden|mcc|mnc|orientation|screenSize|locale|layoutDirection|smallestScreenSize|screenLayout"
android:exported="false"
android:label="@string/app_name"
android:persistableMode="persistNever"
android:taskAffinity=""
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />

<activity android:name=".IntentReceiverActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />

<data android:scheme="http" />
<data android:scheme="https" />
<data android:mimeType="text/html"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="application/xhtml+xml"/>
<data android:mimeType="text/html" />
<data android:mimeType="text/plain" />
<data android:mimeType="application/xhtml+xml" />
</intent-filter>

<intent-filter>
Expand All @@ -76,17 +93,18 @@

<meta-data
android:name="com.android.systemui.action_assist_icon"
android:resource="@mipmap/ic_launcher"/>
android:resource="@mipmap/ic_launcher" />
</activity>

<activity android:name=".browser.BrowserPerformanceTestActivity"
android:exported="${isRaptorEnabled}"
android:enabled="${isRaptorEnabled}"/>
<activity
android:name=".browser.BrowserPerformanceTestActivity"
android:enabled="${isRaptorEnabled}"
android:exported="${isRaptorEnabled}" />

<service
android:name=".customtabs.CustomTabsService"
android:exported="true"
tools:ignore="ExportedService">
android:name=".customtabs.CustomTabsService"
android:exported="true"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent-filter>
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/org/mozilla/fenix/IntentReceiverActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(EXTRA_AUTH)) AuthCustomTabActivity::class.java.name
else CustomTabActivity::class.java.name
)
true
}
intent.action == Intent.ACTION_VIEW -> {
Expand All @@ -56,4 +62,8 @@ class IntentReceiverActivity : Activity() {

finish()
}

companion object {
const val EXTRA_AUTH = "support.customtabs.extra.AUTH"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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)
this.components.backgroundServices.accountManager.register(accountStateObserver, owner = this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions app/src/main/java/org/mozilla/fenix/settings/SupportUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ object SupportUtils {
setPackage(context.packageName)
}

fun createAuthCustomTabIntent(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("support.customtabs.extra.AUTH", true)
setClassName(context.applicationContext, IntentReceiverActivity::class.java.name)
data = Uri.parse(url)
setPackage(context.packageName)
}

private fun getEncodedTopicUTF8(topic: String): String {
try {
return URLEncoder.encode(topic, "UTF-8")
Expand Down

0 comments on commit 2a6b271

Please sign in to comment.