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..32f962a9bd36 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveLeoPreferences.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveLeoPreferences.java @@ -7,6 +7,11 @@ import android.os.Bundle; +import android.content.Context; +import android.content.DialogInterface; +import android.view.LayoutInflater; +import android.view.View; +import androidx.appcompat.app.AlertDialog; import androidx.annotation.NonNull; 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,44 @@ 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