From 7ce644e9220403553c1a9bdf38f13557d8fe8e06 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 9 Feb 2023 17:23:51 +0100 Subject: Add back custom tabs --- .../fedilab/android/mastodon/helper/Helper.java | 35 +++++++++++++----- .../android/ui/fragment/FragmentLoginMain.java | 43 ++++++++++++++++++---- .../main/res/menus/mastodon/menu/main_login.xml | 23 +++--------- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_interface.xml | 7 ++++ .../metadata/android/en/changelogs/478.txt | 5 ++- 6 files changed, 78 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java index 57336df24..f75461e98 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java @@ -76,6 +76,8 @@ import androidx.annotation.IdRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.browser.customtabs.CustomTabColorSchemeParams; +import androidx.browser.customtabs.CustomTabsIntent; import androidx.core.app.ActivityOptionsCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -646,16 +648,29 @@ public class Helper { if (url == null) { return; } - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://") && !url.toLowerCase().startsWith("gemini://")) { - url = "http://" + url; - } - intent.setData(Uri.parse(url)); - try { - context.startActivity(intent); - } catch (Exception e) { - Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show(); + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); + boolean customTab = sharedpreferences.getBoolean(context.getString(R.string.SET_CUSTOM_TABS), true); + if (customTab) { + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + int colorInt = ThemeHelper.getAttColor(context, R.attr.statusBar); + CustomTabColorSchemeParams defaultColors = new CustomTabColorSchemeParams.Builder() + .setToolbarColor(colorInt) + .build(); + builder.setDefaultColorSchemeParams(defaultColors); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.launchUrl(context, Uri.parse(url)); + } else { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://") && !url.toLowerCase().startsWith("gemini://")) { + url = "http://" + url; + } + intent.setData(Uri.parse(url)); + try { + context.startActivity(intent); + } catch (Exception e) { + Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show(); + } } } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginMain.java b/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginMain.java index 8ef6a8f34..87ab4bac2 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginMain.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginMain.java @@ -25,6 +25,7 @@ import static app.fedilab.android.activities.LoginActivity.softwareLogin; import android.Manifest; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; @@ -43,9 +44,12 @@ import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.widget.PopupMenu; +import androidx.browser.customtabs.CustomTabColorSchemeParams; +import androidx.browser.customtabs.CustomTabsIntent; import androidx.core.app.ActivityCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import androidx.preference.PreferenceManager; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; @@ -60,6 +64,7 @@ import app.fedilab.android.mastodon.client.entities.app.Account; import app.fedilab.android.mastodon.client.entities.app.InstanceSocial; import app.fedilab.android.mastodon.helper.Helper; import app.fedilab.android.mastodon.helper.MastodonHelper; +import app.fedilab.android.mastodon.helper.ThemeHelper; import app.fedilab.android.mastodon.helper.ZipHelper; import app.fedilab.android.mastodon.viewmodel.mastodon.AppsVM; import app.fedilab.android.mastodon.viewmodel.mastodon.InstanceSocialVM; @@ -214,6 +219,10 @@ public class FragmentLoginMain extends Fragment { menuInflater.inflate(R.menu.main_login, popupMenu.getMenu()); MenuItem adminTabItem = popupMenu.getMenu().findItem(R.id.action_request_admin); adminTabItem.setChecked(requestedAdmin); + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); + boolean customTab = sharedpreferences.getBoolean(getString(R.string.SET_CUSTOM_TABS), true); + popupMenu.getMenu().findItem(R.id.action_custom_tabs).setChecked(customTab); + popupMenu.setOnMenuItemClickListener(item -> { int itemId = item.getItemId(); if (itemId == R.id.action_proxy) { @@ -242,6 +251,13 @@ public class FragmentLoginMain extends Fragment { } else { proceed(); } + } else if (itemId == R.id.action_custom_tabs) { + boolean newValue = !item.isChecked(); + item.setChecked(newValue); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(getString(R.string.SET_CUSTOM_TABS), newValue); + editor.apply(); + return false; } return false; }); @@ -291,13 +307,26 @@ public class FragmentLoginMain extends Fragment { client_idLogin = app.client_id; client_secretLogin = app.client_secret; String redirectUrl = MastodonHelper.authorizeURL(currentInstanceLogin, client_idLogin, requestedAdmin); - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.setData(Uri.parse(redirectUrl)); - try { - startActivity(intent); - } catch (Exception e) { - Toasty.error(requireActivity(), getString(R.string.toast_error), Toast.LENGTH_LONG).show(); + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); + boolean customTab = sharedpreferences.getBoolean(getString(R.string.SET_CUSTOM_TABS), true); + if (customTab) { + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + int colorInt = ThemeHelper.getAttColor(requireActivity(), R.attr.statusBar); + CustomTabColorSchemeParams defaultColors = new CustomTabColorSchemeParams.Builder() + .setToolbarColor(colorInt) + .build(); + builder.setDefaultColorSchemeParams(defaultColors); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.launchUrl(requireActivity(), Uri.parse(redirectUrl)); + } else { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setData(Uri.parse(redirectUrl)); + try { + startActivity(intent); + } catch (Exception e) { + Toasty.error(requireActivity(), getString(R.string.toast_error), Toast.LENGTH_LONG).show(); + } } } else { Toasty.error(requireActivity(), getString(R.string.client_error), Toasty.LENGTH_SHORT).show(); diff --git a/app/src/main/res/menus/mastodon/menu/main_login.xml b/app/src/main/res/menus/mastodon/menu/main_login.xml index cbfb46fe7..30b14748e 100644 --- a/app/src/main/res/menus/mastodon/menu/main_login.xml +++ b/app/src/main/res/menus/mastodon/menu/main_login.xml @@ -1,17 +1,7 @@ - - + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 939c34516..75382f5dd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1509,6 +1509,7 @@ SET_NITTER_HOST nitter.net SET_USE_CACHE + SET_CUSTOM_TABS SET_BIBLIOGRAM SET_BIBLIOGRAM_HOST bibliogram.art diff --git a/app/src/main/res/xml/pref_interface.xml b/app/src/main/res/xml/pref_interface.xml index 69a98c525..440723cb9 100644 --- a/app/src/main/res/xml/pref_interface.xml +++ b/app/src/main/res/xml/pref_interface.xml @@ -69,6 +69,13 @@ app:summary="@string/set_use_cache_indication" app:title="@string/set_use_cache" /> + +