From 9725d5e411e4c33d5d274749006811e17531d961 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 23 May 2024 23:05:28 -0400 Subject: Login using Chrome custom tab. Remove option to set setDomStorageEnabled() in LoginActivity. --- app/src/main/AndroidManifest.xml | 1 + .../activities/LoginActivity.java | 22 +----- .../activities/LoginChromeCustomTabActivity.java | 92 ++++++++++++++-------- app/src/main/res/drawable/ic_help_24dp.xml | 9 --- app/src/main/res/drawable/ic_login_24dp.xml | 9 +++ app/src/main/res/layout/activity_login.xml | 3 +- .../layout/activity_login_chrome_custom_tab.xml | 33 +++++++- app/src/main/res/values/strings.xml | 6 +- 8 files changed, 104 insertions(+), 71 deletions(-) delete mode 100644 app/src/main/res/drawable/ic_help_24dp.xml create mode 100644 app/src/main/res/drawable/ic_login_24dp.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6c533f12..b9867479 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -36,6 +36,7 @@ tools:replace="android:label"> { - /*new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) - .setTitle(R.string.have_trouble_login_title) - .setMessage(R.string.have_trouble_login_message) - .setPositiveButton(R.string.yes, (dialogInterface, i) -> { - enableDom = !enableDom; - ActivityCompat.recreate(this); - }) - .setNegativeButton(R.string.no, null) - .show();*/ - Intent intent = new Intent(this, LoginChromeCustomTabActivity.class); startActivity(intent); finish(); @@ -284,7 +265,6 @@ public class LoginActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(ENABLE_DOM_STATE, enableDom); outState.putBoolean(IS_AGREE_TO_USER_AGGREMENT_STATE, isAgreeToUserAgreement); } @@ -305,7 +285,7 @@ public class LoginActivity extends BaseActivity { @Override protected void applyCustomTheme() { - binding.coordinatorLayoutLoginActivity.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + binding.getRoot().setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(binding.appbarLayoutLoginActivity, null, binding.toolbarLoginActivity); binding.twoFaInfOTextViewLoginActivity.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); Drawable infoDrawable = Utils.getTintedDrawable(this, R.drawable.ic_info_preference_24dp, mCustomThemeWrapper.getPrimaryIconColor()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginChromeCustomTabActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginChromeCustomTabActivity.java index 84c06ea6..58db6f7b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginChromeCustomTabActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginChromeCustomTabActivity.java @@ -8,16 +8,15 @@ import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Bundle; import android.os.Handler; +import android.view.View; import android.widget.Toast; -import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; import androidx.browser.customtabs.CustomTabColorSchemeParams; import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsService; -import androidx.core.graphics.Insets; -import androidx.core.view.ViewCompat; -import androidx.core.view.WindowInsetsCompat; + +import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; import org.json.JSONException; @@ -39,6 +38,7 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.asynctasks.ParseAndInsertNewAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.databinding.ActivityLoginChromeCustomTabBinding; import ml.docilealligator.infinityforreddit.events.NewUserLoggedInEvent; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -67,49 +67,34 @@ public class LoginChromeCustomTabActivity extends BaseActivity { CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + private ActivityLoginChromeCustomTabBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { ((Infinity) getApplication()).getAppComponent().inject(this); + setImmersiveModeNotApplicable(); + super.onCreate(savedInstanceState); - EdgeToEdge.enable(this); - setContentView(R.layout.activity_login_chrome_custom_tab); - ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + //EdgeToEdge.enable(this); + binding = ActivityLoginChromeCustomTabBinding.inflate(getLayoutInflater()); + setContentView(binding.getRoot()); + /*ViewCompat.setOnApplyWindowInsetsListener(binding.getRoot(), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; - }); + });*/ applyCustomTheme(); - ArrayList resolveInfos = getCustomTabsPackages(getPackageManager()); - if (!resolveInfos.isEmpty()) { - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - // add share action to menu list - builder.setShareState(CustomTabsIntent.SHARE_STATE_ON); - builder.setDefaultColorSchemeParams( - new CustomTabColorSchemeParams.Builder() - .setToolbarColor(mCustomThemeWrapper.getColorPrimary()) - .build()); - CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.intent.setPackage(resolveInfos.get(0).activityInfo.packageName); - customTabsIntent.intent.putExtra("com.google.android.apps.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB", true); + setSupportActionBar(binding.toolbarLoginChromeCustomTabActivity); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); - try { - Uri.Builder uriBuilder = Uri.parse(APIUtils.OAUTH_URL).buildUpon(); - uriBuilder.appendQueryParameter(APIUtils.CLIENT_ID_KEY, APIUtils.CLIENT_ID); - uriBuilder.appendQueryParameter(APIUtils.RESPONSE_TYPE_KEY, APIUtils.RESPONSE_TYPE); - uriBuilder.appendQueryParameter(APIUtils.STATE_KEY, APIUtils.STATE); - uriBuilder.appendQueryParameter(APIUtils.REDIRECT_URI_KEY, APIUtils.REDIRECT_URI); - uriBuilder.appendQueryParameter(APIUtils.DURATION_KEY, APIUtils.DURATION); - uriBuilder.appendQueryParameter(APIUtils.SCOPE_KEY, APIUtils.SCOPE); + openLoginPage(); - customTabsIntent.launchUrl(this, uriBuilder.build()); - } catch (ActivityNotFoundException e) { - // TODO catch this - } - } + binding.openWebpageButtonLoginChromeCustomTabActivity.setOnClickListener(view -> { + openLoginPage(); + }); } @Override @@ -118,9 +103,12 @@ public class LoginChromeCustomTabActivity extends BaseActivity { Uri uri = intent.getData(); if (uri == null) { + binding.openWebpageButtonLoginChromeCustomTabActivity.setVisibility(View.VISIBLE); return; } + binding.openWebpageButtonLoginChromeCustomTabActivity.setVisibility(View.GONE); + String authCode = uri.getQueryParameter("code"); if (authCode != null) { String state = uri.getQueryParameter("state"); @@ -220,7 +208,45 @@ public class LoginChromeCustomTabActivity extends BaseActivity { @Override protected void applyCustomTheme() { + binding.getRoot().setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(binding.appbarLayoutLoginChromeCustomTabActivity, null, binding.toolbarLoginChromeCustomTabActivity); + binding.openWebpageButtonLoginChromeCustomTabActivity.setTextColor(mCustomThemeWrapper.getButtonTextColor()); + binding.openWebpageButtonLoginChromeCustomTabActivity.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme()); + if (typeface != null) { + binding.openWebpageButtonLoginChromeCustomTabActivity.setTypeface(typeface); + } + } + + private void openLoginPage() { + ArrayList resolveInfos = getCustomTabsPackages(getPackageManager()); + if (!resolveInfos.isEmpty()) { + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + // add share action to menu list + builder.setShareState(CustomTabsIntent.SHARE_STATE_ON); + builder.setDefaultColorSchemeParams( + new CustomTabColorSchemeParams.Builder() + .setToolbarColor(mCustomThemeWrapper.getColorPrimary()) + .build()); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.intent.setPackage(resolveInfos.get(0).activityInfo.packageName); + customTabsIntent.intent.putExtra("com.google.android.apps.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB", true); + + try { + Uri.Builder uriBuilder = Uri.parse(APIUtils.OAUTH_URL).buildUpon(); + uriBuilder.appendQueryParameter(APIUtils.CLIENT_ID_KEY, APIUtils.CLIENT_ID); + uriBuilder.appendQueryParameter(APIUtils.RESPONSE_TYPE_KEY, APIUtils.RESPONSE_TYPE); + uriBuilder.appendQueryParameter(APIUtils.STATE_KEY, APIUtils.STATE); + uriBuilder.appendQueryParameter(APIUtils.REDIRECT_URI_KEY, APIUtils.REDIRECT_URI); + uriBuilder.appendQueryParameter(APIUtils.DURATION_KEY, APIUtils.DURATION); + uriBuilder.appendQueryParameter(APIUtils.SCOPE_KEY, APIUtils.SCOPE); + customTabsIntent.launchUrl(this, uriBuilder.build()); + } catch (ActivityNotFoundException e) { + Snackbar.make(binding.getRoot(), R.string.custom_tab_not_available, Snackbar.LENGTH_LONG).show(); + } + } else { + Snackbar.make(binding.getRoot(), R.string.custom_tab_not_available, Snackbar.LENGTH_LONG).show(); + } } private ArrayList getCustomTabsPackages(PackageManager pm) { diff --git a/app/src/main/res/drawable/ic_help_24dp.xml b/app/src/main/res/drawable/ic_help_24dp.xml deleted file mode 100644 index 81e329b9..00000000 --- a/app/src/main/res/drawable/ic_help_24dp.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/drawable/ic_login_24dp.xml b/app/src/main/res/drawable/ic_login_24dp.xml new file mode 100644 index 00000000..2e5aa22b --- /dev/null +++ b/app/src/main/res/drawable/ic_login_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 4c4d3e9f..1f7ef7c6 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -4,7 +4,6 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:id="@+id/coordinator_layout_login_activity" tools:application="ml.docilealligator.infinityforreddit.activities.LoginActivity"> + android:src="@drawable/ic_login_24dp" /> diff --git a/app/src/main/res/layout/activity_login_chrome_custom_tab.xml b/app/src/main/res/layout/activity_login_chrome_custom_tab.xml index 534433ff..0bf28118 100644 --- a/app/src/main/res/layout/activity_login_chrome_custom_tab.xml +++ b/app/src/main/res/layout/activity_login_chrome_custom_tab.xml @@ -1,10 +1,37 @@ - - \ No newline at end of file + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8049801c..f15f2893 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1049,7 +1049,7 @@ %1$s pts - If you have 2-factor authentication enabled, kindly type your password like the following: <password>:<2FA code>.\nExample: yourpass:123456 + 2-factor authentication: <password>:<2FA code>.\nExample: yourpass:123456\nGoogle login or having trouble login: click the bottom right button. Block User Blocked @@ -1157,8 +1157,8 @@ Choose a user - Having Trouble Login - Do you want to try another way to login? + Open login page + Chrome custom tab not available. Is Chrome or Firefox installed? Vote -- cgit v1.2.3