From 8485bfab9d491187dc4a5599d1f2657221d9004a Mon Sep 17 00:00:00 2001 From: Jay Harris Date: Thu, 12 Dec 2024 11:47:58 +1300 Subject: [PATCH] [AI Chat]: Add confirmation dialog for clearing history --- android/brave_java_resources.gni | 1 + .../browser/settings/BraveLeoPreferences.java | 51 ++++++++++++++++--- .../android/strings/android_brave_strings.grd | 6 +++ .../flags/android/chrome_feature_list.cc | 2 +- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/android/brave_java_resources.gni b/android/brave_java_resources.gni index 4743cd456bed..b7b022b1ebc8 100644 --- a/android/brave_java_resources.gni +++ b/android/brave_java_resources.gni @@ -794,6 +794,7 @@ brave_java_resources = [ "java/res/layout/brave_custom_tabs_toolbar.xml", "java/res/layout/brave_dialog_preference.xml", "java/res/layout/brave_exit_confirmation.xml", + "java/res/layout/brave_leo_clear_history_dialog.xml", "java/res/layout/brave_leo_reset_dialog.xml", "java/res/layout/brave_news_card_menu.xml", "java/res/layout/brave_news_load_new_content.xml", diff --git a/android/java/org/chromium/chrome/browser/settings/BraveLeoPreferences.java b/android/java/org/chromium/chrome/browser/settings/BraveLeoPreferences.java index d43dae153b17..d587dc9a9387 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveLeoPreferences.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveLeoPreferences.java @@ -5,9 +5,14 @@ package org.chromium.chrome.browser.settings; +import android.content.Context; +import android.content.DialogInterface; import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.preference.Preference; import androidx.preference.PreferenceCategory; @@ -43,6 +48,7 @@ public class BraveLeoPreferences extends BravePreferenceFragment private static final String PREF_DEFAULT_MODEL = "default_model"; private final ObservableSupplierImpl mPageTitle = new ObservableSupplierImpl<>(); + private ChromeSwitchPreference mHistory; @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { @@ -72,13 +78,13 @@ public void onCreate(Bundle savedInstanceState) { Preference history = findPreference(PREF_HISTORY); if (history != null) { - history.setOnPreferenceChangeListener(this); if (history instanceof ChromeSwitchPreference) { - ((ChromeSwitchPreference) history) - .setChecked(BraveLeoPrefUtils.getIsHistoryEnabled()); + mHistory = (ChromeSwitchPreference) history; + mHistory.setOnPreferenceChangeListener(this); + mHistory.setChecked(BraveLeoPrefUtils.getIsHistoryEnabled()); + mHistory.setVisible(ChromeFeatureList.isEnabled(BraveFeatureList.AI_CHAT_HISTORY)); } } - history.setVisible(ChromeFeatureList.isEnabled(BraveFeatureList.AI_CHAT_HISTORY)); BraveLeoUtils.verifySubscription( (subscriptionActive) -> { @@ -159,15 +165,48 @@ private void checkLinkPurchase() { }); } + private void showConfirmClearHistoryDialog() { + LayoutInflater inflater = + (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.brave_leo_clear_history_dialog, null); + + DialogInterface.OnClickListener onClickListener = + (dialog, button) -> { + if (button == AlertDialog.BUTTON_POSITIVE) { + BraveLeoPrefUtils.setIsHistoryEnabled(false); + mHistory.setChecked(false); + } else { + dialog.dismiss(); + } + }; + + AlertDialog.Builder alert = + new AlertDialog.Builder(getContext(), R.style.ThemeOverlay_BrowserUI_AlertDialog); + AlertDialog alertDialog = + alert.setTitle(R.string.leo_clear_history_title) + .setView(view) + .setPositiveButton(R.string.brave_leo_confirm_text, onClickListener) + .setNegativeButton(R.string.cancel, onClickListener) + .create(); + alertDialog.getDelegate().setHandleNativeActionModesEnabled(false); + alertDialog.show(); + } + @Override public boolean onPreferenceChange(@NonNull Preference preference, Object o) { String key = preference.getKey(); + boolean enabled = (boolean) o; if (PREF_AUTOCOMPLETE.equals(key)) { ChromeSharedPreferences.getInstance() - .writeBoolean(BravePreferenceKeys.BRAVE_LEO_AUTOCOMPLETE, (boolean) o); + .writeBoolean(BravePreferenceKeys.BRAVE_LEO_AUTOCOMPLETE, enabled); } if (PREF_HISTORY.equals(key)) { - BraveLeoPrefUtils.setIsHistoryEnabled((boolean) o); + if (enabled) { + BraveLeoPrefUtils.setIsHistoryEnabled(enabled); + } else { + showConfirmClearHistoryDialog(); + return false; + } } return true; diff --git a/browser/ui/android/strings/android_brave_strings.grd b/browser/ui/android/strings/android_brave_strings.grd index 140c758be766..fcb2cfb31390 100644 --- a/browser/ui/android/strings/android_brave_strings.grd +++ b/browser/ui/android/strings/android_brave_strings.grd @@ -3700,6 +3700,12 @@ If you don't accept this request, VPN will not reconnect and your internet conne Resetting the Leo assistant will require you to opt-in to use Leo in the future and will also clear your chat history. Clearing your chat history will delete all your previous conversations with Leo. This action cannot be undone. + + Clear chat history + + + Disabling chat history will delete all your previous conversations with Leo. This action cannot be undone. + Confirm diff --git a/chromium_src/chrome/browser/flags/android/chrome_feature_list.cc b/chromium_src/chrome/browser/flags/android/chrome_feature_list.cc index 14b830286b9f..876559c07d98 100644 --- a/chromium_src/chrome/browser/flags/android/chrome_feature_list.cc +++ b/chromium_src/chrome/browser/flags/android/chrome_feature_list.cc @@ -52,7 +52,7 @@ #include "src/chrome/browser/flags/android/chrome_feature_list.cc" #undef kForceWebContentsDarkMode -#undef BRAVE_AI_CHAT_FLAG +#undef BRAVE_AI_CHAT_FLAGS namespace chrome { namespace android {