Skip to content

Commit

Permalink
Preload the login page
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmassel committed Nov 27, 2024
1 parent 9dd5d90 commit e8ffbf9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
3 changes: 3 additions & 0 deletions WordPress/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,9 @@
<intent>
<action android:name="android.intent.action.MAIN" />
</intent>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
<!-- required for Android 11 (API level 30) or higher -->
<package android:name="com.jetpack.android" />
</queries>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
// Attempt Login if this activity was created in response to a user confirming login
mLoginHelper.tryLoginWithDataString(getIntent().getDataString());

// Start preloading the WordPress.com login page if needed – this avoids visual hitches
// when displaying that screen
mLoginHelper.bindCustomTabsService(this);

if (mLoginHelper.isLoggedIn()) {
this.loggedInAndFinish(new ArrayList<Integer>(), true);
return;
Expand Down Expand Up @@ -477,7 +481,7 @@ public void showWPcomLoginScreen(@NonNull Context context) {
.setShowTitle(false)
.build();

intent.launchUrl(this, mLoginHelper.loginUri());
intent.launchUrl(this, mLoginHelper.getWpcomLoginUri());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.wordpress.android.ui.accounts.login

import android.content.ComponentName
import android.content.Context
import android.net.Uri
import android.util.Log
import androidx.browser.customtabs.CustomTabsCallback
import androidx.browser.customtabs.CustomTabsClient
import androidx.browser.customtabs.CustomTabsServiceConnection
import androidx.browser.customtabs.CustomTabsSession
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.runBlocking
Expand All @@ -14,13 +20,12 @@ import kotlin.coroutines.CoroutineContext
class WPcomLoginHelper @Inject constructor(
private val loginClient: WPcomLoginClient,
private val accountStore: AccountStore,
private val appSecrets: AppSecrets
appSecrets: AppSecrets
) {
private val context: CoroutineContext = Dispatchers.IO

fun loginUri(): Uri {
return loginClient.loginUri(appSecrets.redirectUri)
}
val wpcomLoginUri = loginClient.loginUri(appSecrets.redirectUri)
private val customTabsServiceConnection = ServiceConnection(wpcomLoginUri)

fun tryLoginWithDataString(data: String?) {
if (data == null) {
Expand All @@ -44,7 +49,47 @@ class WPcomLoginHelper @Inject constructor(
context.cancel()
}

fun bindCustomTabsService(context: Context) {
customTabsServiceConnection.bind(context)
}

private fun codeFromAuthorizationUri(string: String): String? {
return Uri.parse(string).getQueryParameter("code")
}
}

class ServiceConnection(
var uri: Uri
): CustomTabsServiceConnection() {
private var client: CustomTabsClient? = null
private var session: CustomTabsSession? = null

override fun onCustomTabsServiceConnected(name: ComponentName, client: CustomTabsClient) {
client.warmup(0)
this.client = client

val session = client.newSession(CustomTabsCallback())
session?.mayLaunchUrl(uri, null, null)
session?.mayLaunchUrl(Uri.parse("https://wordpress.com/log-in/"), null, null)

this.session = session
}

override fun onServiceDisconnected(name: ComponentName?) {
this.client = null
this.session = null
}

fun bind(context: Context) {
// Do nothing if there is an existing service connection
if (this.client != null) {
return
}

// Get the default browser package name, this will be null if
// the default browser does not provide a CustomTabsService
val packageName = CustomTabsClient.getPackageName(context, null) ?: return

CustomTabsClient.bindCustomTabsService(context, packageName, this)
}
}

0 comments on commit e8ffbf9

Please sign in to comment.