Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 61957c33 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

ProfileManager: Allow overriding User-Agent in WebView

parent af28974d
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ import android.webkit.WebSettings
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
import org.microg.gms.firebase.auth.core.R
import org.microg.gms.profile.Build
import org.microg.gms.profile.ProfileManager
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
@@ -47,6 +49,8 @@ class ReCaptchaActivity : AppCompatActivity() {
        settings.setSupportZoom(false)
        settings.displayZoomControls = false
        settings.cacheMode = WebSettings.LOAD_NO_CACHE
        ProfileManager.ensureInitialized(this)
        settings.userAgentString = Build.generateWebViewUserAgentString(settings.userAgentString)
        view.addJavascriptInterface(object : Any() {
            @JavascriptInterface
            fun onReCaptchaToken(token: String) {
+6 −3
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@ package org.microg.gms.firebase.auth

import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
@@ -17,6 +16,8 @@ import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.FrameLayout
import org.microg.gms.firebase.auth.core.R
import org.microg.gms.profile.Build
import org.microg.gms.profile.ProfileManager
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
@@ -32,7 +33,7 @@ class ReCaptchaOverlay(val context: Context, val apiKey: String, val hostname: S
    var container: View? = null

    private fun show() {
        val layoutParamsType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val layoutParamsType = if (android.os.Build.VERSION.SDK_INT >= 26) {
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else {
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
@@ -80,6 +81,8 @@ class ReCaptchaOverlay(val context: Context, val apiKey: String, val hostname: S
            settings.setSupportZoom(false)
            settings.displayZoomControls = false
            settings.cacheMode = WebSettings.LOAD_NO_CACHE
            ProfileManager.ensureInitialized(context)
            settings.userAgentString = Build.generateWebViewUserAgentString(settings.userAgentString)
            view.addJavascriptInterface(object : Any() {
                @JavascriptInterface
                fun onReCaptchaToken(token: String) {
@@ -110,7 +113,7 @@ class ReCaptchaOverlay(val context: Context, val apiKey: String, val hostname: S
    }

    companion object {
        fun isSupported(context: Context): Boolean = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(context)
        fun isSupported(context: Context): Boolean = android.os.Build.VERSION.SDK_INT < 23 || Settings.canDrawOverlays(context)

        suspend fun awaitToken(context: Context, apiKey: String, hostname: String? = null) = suspendCoroutine<String> { continuation ->
            ReCaptchaOverlay(context, apiKey, hostname ?: "localhost:5000", continuation).show()
+8 −4
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2021, microG Project Team
 * SPDX-FileCopyrightText: 2021 microG Project Team
 * SPDX-License-Identifier: Apache-2.0
 */

package org.microg.gms.profile

import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import kotlin.random.Random

object Build {
    @JvmField
@@ -94,4 +91,11 @@ object Build {
        @JvmField
        var SECURITY_PATCH: String? = null
    }

    fun generateWebViewUserAgentString(original: String): String {
        if (!original.startsWith("Mozilla/5.0 (")) return original
        val closeParen: Int = original.indexOf(')')

        return "Mozilla/5.0 (Linux; Android ${VERSION.RELEASE}; $MODEL Build/$ID; wv)${original.substring(closeParen + 1)}"
    }
}
+10 −8
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
@@ -37,11 +36,11 @@ import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.StringRes;
import androidx.webkit.WebViewClientCompat;

import com.google.android.gms.R;

@@ -55,6 +54,8 @@ import org.microg.gms.checkin.LastCheckinInfo;
import org.microg.gms.common.HttpFormClient;
import org.microg.gms.common.Utils;
import org.microg.gms.people.PeopleManager;
import org.microg.gms.profile.Build;
import org.microg.gms.profile.ProfileManager;

import java.io.IOException;
import java.util.Locale;
@@ -106,7 +107,7 @@ public class LoginActivity extends AssistantActivity {
        webView.addJavascriptInterface(new JsBridge(), "mm");
        authContent = (ViewGroup) findViewById(R.id.auth_content);
        ((ViewGroup) findViewById(R.id.auth_root)).addView(webView);
        webView.setWebViewClient(new WebViewClient() {
        webView.setWebViewClient(new WebViewClientCompat() {
            @Override
            public void onPageFinished(WebView view, String url) {
                Log.d(TAG, "pageFinished: " + view.getUrl());
@@ -135,14 +136,14 @@ public class LoginActivity extends AssistantActivity {
                AccountManager accountManager = AccountManager.get(this);
                Account account = new Account(getIntent().getStringExtra(EXTRA_EMAIL), accountType);
                accountManager.addAccountExplicitly(account, getIntent().getStringExtra(EXTRA_TOKEN), null);
                if (isAuthVisible(this) && SDK_INT >= Build.VERSION_CODES.O) {
                if (isAuthVisible(this) && SDK_INT >= 26) {
                    accountManager.setAccountVisibility(account, PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, VISIBILITY_USER_MANAGED_VISIBLE);
                }
                retrieveGmsToken(account);
            } else {
                retrieveRtToken(getIntent().getStringExtra(EXTRA_TOKEN));
            }
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        } else if (android.os.Build.VERSION.SDK_INT < 21) {
            init();
        } else {
            setMessage(R.string.auth_before_connect);
@@ -200,13 +201,14 @@ public class LoginActivity extends AssistantActivity {
        webView.setLayoutParams(new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        webView.setBackgroundColor(Color.TRANSPARENT);
        prepareWebViewSettings(webView.getSettings());
        prepareWebViewSettings(context, webView.getSettings());
        return webView;
    }

    @SuppressLint("SetJavaScriptEnabled")
    private static void prepareWebViewSettings(WebSettings settings) {
        settings.setUserAgentString(settings.getUserAgentString() + MAGIC_USER_AGENT);
    private static void prepareWebViewSettings(Context context, WebSettings settings) {
        ProfileManager.ensureInitialized(context);
        settings.setUserAgentString(Build.INSTANCE.generateWebViewUserAgentString(settings.getUserAgentString()) + MAGIC_USER_AGENT);
        settings.setJavaScriptEnabled(true);
        settings.setSupportMultipleWindows(false);
        settings.setSaveFormData(false);
+4 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import androidx.lifecycle.lifecycleScope
import androidx.webkit.WebViewClientCompat
import com.google.android.gms.safetynet.SafetyNetStatusCodes.*
import org.microg.gms.droidguard.core.DroidGuardResultCreator
import org.microg.gms.profile.Build
import org.microg.gms.profile.ProfileManager
import org.microg.gms.safetynet.core.ui.R
import java.io.ByteArrayInputStream
import java.net.URLEncoder
@@ -92,6 +94,8 @@ class ReCaptchaActivity : AppCompatActivity() {
                displayZoomControls = false
                setSupportZoom(false)
                cacheMode = WebSettings.LOAD_NO_CACHE
                ProfileManager.ensureInitialized(this@ReCaptchaActivity)
                userAgentString = Build.generateWebViewUserAgentString(userAgentString)
            }
            addJavascriptInterface(object {
                @JavascriptInterface