From 5aaed766740d683bd1b5b0667454b63d6dd83b16 Mon Sep 17 00:00:00 2001 From: brave-builds Date: Wed, 8 Jan 2025 04:35:39 +0000 Subject: [PATCH] Uplift of #27147 (squashed) to beta --- .../utils/QuickSearchEnginesUtil.java | 319 +++++++++++++----- 1 file changed, 236 insertions(+), 83 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/quick_search_engines/utils/QuickSearchEnginesUtil.java b/android/java/org/chromium/chrome/browser/quick_search_engines/utils/QuickSearchEnginesUtil.java index b6f200680350..d19ebd27830e 100644 --- a/android/java/org/chromium/chrome/browser/quick_search_engines/utils/QuickSearchEnginesUtil.java +++ b/android/java/org/chromium/chrome/browser/quick_search_engines/utils/QuickSearchEnginesUtil.java @@ -26,192 +26,345 @@ import java.util.List; import java.util.Map; +/** + * Utility class for managing quick search engines functionality. Provides methods for saving and + * retrieving search engine preferences, enabling/disabling the quick search engines feature, and + * managing default search engine settings. + */ public class QuickSearchEnginesUtil { + // Search engine URL templates private static final String YOUTUBE_SEARCH_ENGINE_URL = "https://www.youtube.com/results?search_query={searchTerms}"; public static final String GOOGLE_SEARCH_ENGINE_URL = "https://www.google.com/search?q={searchTerms}"; + /** + * Saves the provided search engines map to SharedPreferences + * + * @param searchEnginesMap Map of search engine keywords to their QuickSearchEnginesModel + */ public static void saveSearchEnginesIntoPref( Map searchEnginesMap) { new SharedPreferencesHelper() .saveMap(BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES, searchEnginesMap); } + /** + * Retrieves the saved search engines map from SharedPreferences + * + * @return Map of search engine keywords to their QuickSearchEnginesModel, or null if not found + */ public static Map getQuickSearchEnginesFromPref() { SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(); - Map searchEnginesMap = - sharedPreferencesHelper.getMap( - BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES, - String.class, - QuickSearchEnginesModel.class); - return searchEnginesMap; + return sharedPreferencesHelper.getMap( + BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES, + String.class, + QuickSearchEnginesModel.class); } - public static void setQuickSearchEnginesFeature(boolean showShow) { + /** + * Sets whether the quick search engines feature should be shown + * + * @param shouldShow True to show the feature, false to hide it + */ + public static void setQuickSearchEnginesFeature(boolean shouldShow) { ChromeSharedPreferences.getInstance() - .writeBoolean(BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES_FEATURE, showShow); + .writeBoolean(BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES_FEATURE, shouldShow); } + /** + * Gets whether the quick search engines feature is enabled + * + * @return True if the feature is enabled, false otherwise. Defaults to true. + */ public static boolean getQuickSearchEnginesFeature() { return ChromeSharedPreferences.getInstance() .readBoolean(BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES_FEATURE, true); } + /** + * Saves the keyword of the previous default search engine + * + * @param dseKeyword Keyword identifying the previous default search engine + */ public static void setPreviousDSE(String dseKeyword) { ChromeSharedPreferences.getInstance() .writeString( BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES_PREVIOUS_DSE, dseKeyword); } + /** + * Gets the keyword of the previously saved default search engine + * + * @return Keyword of the previous default search engine, or empty string if not found + */ public static String getPreviousDSE() { return ChromeSharedPreferences.getInstance() .readString(BravePreferenceKeys.BRAVE_QUICK_SEARCH_ENGINES_PREVIOUS_DSE, ""); } + /** + * Gets the list of quick search engines for a given profile + * + * @param profile The user profile to get search engines for + * @return Map of search engine keywords to their QuickSearchEnginesModel + */ private static Map getQuickSearchEngines(Profile profile) { + // Get template URL service and default search engine TemplateUrlService templateUrlService = TemplateUrlServiceFactory.getForProfile(profile); - List templateUrls = templateUrlService.getTemplateUrls(); - TemplateUrl defaultSearchEngineTemplateUrl = - templateUrlService.getDefaultSearchEngineTemplateUrl(); + TemplateUrl defaultSearchEngine = templateUrlService.getDefaultSearchEngineTemplateUrl(); + + // Get sorted and filtered list of template URLs + List templateUrls = + getSortedTemplateUrls(templateUrlService, defaultSearchEngine); + + // Get existing or create new search engines map + Map searchEnginesMap = getOrCreateSearchEnginesMap(); + + // Initialize previous default search engine if not already set + initializePreviousDSEIfNeeded(defaultSearchEngine); + + // Handle search engine updates based on whether default has changed + if (hasDefaultSearchEngineChanged(defaultSearchEngine)) { + addPreviousDSE(profile, searchEnginesMap, defaultSearchEngine); + } else { + updateSearchEngines(searchEnginesMap, templateUrls, defaultSearchEngine); + } + + // Handle YouTube search engine and save preferences + handleYtSearchEngine(searchEnginesMap); + saveSearchEnginesIntoPref(searchEnginesMap); + return searchEnginesMap; + } + + /** Gets sorted and filtered list of template URLs */ + private static List getSortedTemplateUrls( + TemplateUrlService service, TemplateUrl defaultSearchEngine) { + List templateUrls = service.getTemplateUrls(); + // Sort and filter unnecessary template URLs based on region SearchEngineAdapter.sortAndFilterUnnecessaryTemplateUrl( - templateUrls, - defaultSearchEngineTemplateUrl, - templateUrlService.isEeaChoiceCountry()); - - Map searchEnginesMap = - getQuickSearchEnginesFromPref() != null - ? getQuickSearchEnginesFromPref() - : new LinkedHashMap(); + templateUrls, defaultSearchEngine, service.isEeaChoiceCountry()); + return templateUrls; + } + + /** Gets existing search engines map from preferences or creates new one if none exists */ + private static Map getOrCreateSearchEnginesMap() { + Map existing = getQuickSearchEnginesFromPref(); + return existing != null ? existing : new LinkedHashMap(); + } + + /** Sets the previous default search engine if not already initialized */ + private static void initializePreviousDSEIfNeeded(TemplateUrl defaultSearchEngine) { if (getPreviousDSE().isEmpty()) { - setPreviousDSE(defaultSearchEngineTemplateUrl.getShortName()); + setPreviousDSE(defaultSearchEngine.getShortName()); + } + } + + /** Checks if the default search engine has changed from the previous one */ + private static boolean hasDefaultSearchEngineChanged(TemplateUrl defaultSearchEngine) { + return !defaultSearchEngine.getShortName().equals(getPreviousDSE()); + } + + /** Updates search engines map with new engines and removes default search engine */ + private static void updateSearchEngines( + Map searchEnginesMap, + List templateUrls, + TemplateUrl defaultSearchEngine) { + // Add any new search engines not already in preferences + for (TemplateUrl templateUrl : templateUrls) { + if (!searchEnginesMap.containsKey(templateUrl.getKeyword())) { + addSearchEngines(searchEnginesMap, templateUrl); + } } + // Remove default search engine from quick search options + removeDefaultSearchEngine(searchEnginesMap, defaultSearchEngine); + } - if (!defaultSearchEngineTemplateUrl.getShortName().equals(getPreviousDSE())) { - addPreviousDSE(profile, searchEnginesMap, defaultSearchEngineTemplateUrl); + /** + * Handles adding YouTube as a quick search engine option when Google is present + * + * @param searchEnginesMap Map of search engine keywords to their QuickSearchEnginesModel + */ + private static void handleYtSearchEngine( + Map searchEnginesMap) { + // If Google search is not present, just add YouTube search + if (!searchEnginesMap.containsKey(BraveActivity.GOOGLE_SEARCH_ENGINE_KEYWORD)) { + addYtQuickSearchEnginesModel(searchEnginesMap); } else { - for (TemplateUrl templateUrl : templateUrls) { - if (!searchEnginesMap.containsKey(templateUrl.getKeyword())) { - addSearchEngines(searchEnginesMap, templateUrl); - if (BraveActivity.GOOGLE_SEARCH_ENGINE_KEYWORD.equals(templateUrl.getKeyword()) - && !searchEnginesMap.containsKey( - BraveActivity.YOUTUBE_SEARCH_ENGINE_KEYWORD)) { - addYtQuickSearchEnginesModel(searchEnginesMap); - } + // Create new map to preserve ordering while adding YouTube after Google + Map ytSearchEnginesMap = + new LinkedHashMap(); + + // Iterate through existing engines + for (Map.Entry entry : searchEnginesMap.entrySet()) { + QuickSearchEnginesModel quickSearchEnginesModel = entry.getValue(); + ytSearchEnginesMap.put( + quickSearchEnginesModel.getKeyword(), quickSearchEnginesModel); + + // Add YouTube search right after Google if it's not already present + if (BraveActivity.GOOGLE_SEARCH_ENGINE_KEYWORD.equals( + quickSearchEnginesModel.getKeyword()) + && !searchEnginesMap.containsKey( + BraveActivity.YOUTUBE_SEARCH_ENGINE_KEYWORD)) { + addYtQuickSearchEnginesModel(ytSearchEnginesMap); } } - if (!searchEnginesMap.containsKey(BraveActivity.GOOGLE_SEARCH_ENGINE_KEYWORD)) { - addYtQuickSearchEnginesModel(searchEnginesMap); - } - removeDefaultSearchEngine(searchEnginesMap, defaultSearchEngineTemplateUrl); + + // Replace original map with new ordered map containing YouTube + searchEnginesMap.clear(); + searchEnginesMap.putAll(ytSearchEnginesMap); } - saveSearchEnginesIntoPref(searchEnginesMap); - return searchEnginesMap; } + /** + * Adds the previous default search engine (DSE) to the quick search engines list. This method: + * 1. Retrieves the previous DSE from preferences 2. Removes both previous and current DSE from + * the existing map 3. Creates a new ordered map with previous DSE as first entry 4. Updates the + * stored previous DSE preference + * + * @param profile The user profile to get search engine data from + * @param searchEnginesMap Map of current quick search engines + * @param defaultSearchEngineTemplateUrl The current default search engine + */ private static void addPreviousDSE( Profile profile, Map searchEnginesMap, TemplateUrl defaultSearchEngineTemplateUrl) { - // Create new map to store search engines with previous DSE first - Map orderedSearchEnginesMap = - new LinkedHashMap(); - - // Get previous default search engine template URL + // Get previous default search engine TemplateUrl previousDSETemplateUrl = BraveSearchEngineUtils.getTemplateUrlByShortName(profile, getPreviousDSE()); - // Remove previous DSE from current map if it exists + // Create new ordered map and remove existing entries + Map orderedMap = new LinkedHashMap<>(); searchEnginesMap.remove(previousDSETemplateUrl.getKeyword()); - - // Remove current default search engine removeDefaultSearchEngine(searchEnginesMap, defaultSearchEngineTemplateUrl); - // Add previous DSE as first entry in ordered map - addSearchEngines(orderedSearchEnginesMap, previousDSETemplateUrl); - - // Add remaining search engines after previous DSE - orderedSearchEnginesMap.putAll(searchEnginesMap); + // Rebuild map with previous DSE first + addSearchEngines(orderedMap, previousDSETemplateUrl); + orderedMap.putAll(searchEnginesMap); - // Update reference to ordered map + // Update the search engines map reference searchEnginesMap.clear(); - searchEnginesMap.putAll(orderedSearchEnginesMap); + searchEnginesMap.putAll(orderedMap); - // Update previous DSE to current default + // Store current DSE as previous for next time setPreviousDSE(defaultSearchEngineTemplateUrl.getShortName()); } + /** + * Removes the default search engine from the quick search engines map if present. + * + * @param searchEnginesMap Map containing quick search engines + * @param defaultSearchEngineTemplateUrl The default search engine template URL to remove + */ private static void removeDefaultSearchEngine( Map searchEnginesMap, TemplateUrl defaultSearchEngineTemplateUrl) { - if (searchEnginesMap.containsKey(defaultSearchEngineTemplateUrl.getKeyword())) { - searchEnginesMap.remove(defaultSearchEngineTemplateUrl.getKeyword()); - } + String keyword = defaultSearchEngineTemplateUrl.getKeyword(); + searchEnginesMap.remove(keyword); // Map.remove() returns null if key not present } + /** + * Adds a search engine to the provided map. Creates a new QuickSearchEnginesModel from the + * template URL and adds it to the map using the keyword as the key. + * + * @param searchEnginesMap Map to add the search engine to + * @param searchEngineTemplateUrl Template URL containing search engine details + */ private static void addSearchEngines( Map searchEnginesMap, TemplateUrl searchEngineTemplateUrl) { + String keyword = searchEngineTemplateUrl.getKeyword(); QuickSearchEnginesModel quickSearchEnginesModel = new QuickSearchEnginesModel( searchEngineTemplateUrl.getShortName(), - searchEngineTemplateUrl.getKeyword(), + keyword, searchEngineTemplateUrl.getURL(), true); - searchEnginesMap.put(searchEngineTemplateUrl.getKeyword(), quickSearchEnginesModel); + searchEnginesMap.put(keyword, quickSearchEnginesModel); } + /** + * Adds YouTube as a quick search engine option to the provided map. Creates a new + * QuickSearchEnginesModel for YouTube and adds it using YouTube's keyword as the key. + * + * @param searchEnginesMap Map to add the YouTube search engine to + */ private static void addYtQuickSearchEnginesModel( Map searchEnginesMap) { Context context = ContextUtils.getApplicationContext(); - QuickSearchEnginesModel ytQuickSearchEnginesModel = + String youtubeDisplayName = context.getResources().getString(R.string.youtube); + + QuickSearchEnginesModel youtubeSearchEngine = new QuickSearchEnginesModel( - context.getResources().getString(R.string.youtube), + youtubeDisplayName, BraveActivity.YOUTUBE_SEARCH_ENGINE_KEYWORD, YOUTUBE_SEARCH_ENGINE_URL, true); - searchEnginesMap.put(ytQuickSearchEnginesModel.getKeyword(), ytQuickSearchEnginesModel); + + searchEnginesMap.put(youtubeSearchEngine.getKeyword(), youtubeSearchEngine); } + /** + * Gets the list of quick search engines for the settings page, excluding the default search + * engine + * + * @param profile The user profile to get search engines for + * @return List of quick search engines models, excluding the default search engine + */ public static List getQuickSearchEnginesForSettings(Profile profile) { Map searchEnginesMap = getQuickSearchEngines(profile); - TemplateUrlService templateUrlService = TemplateUrlServiceFactory.getForProfile(profile); - TemplateUrl defaultSearchEngineTemplateUrl = - templateUrlService.getDefaultSearchEngineTemplateUrl(); + String defaultEngineKeyword = getDefaultSearchEngineKeyword(profile); + List quickSearchEngines = new ArrayList<>(); - for (Map.Entry entry : searchEnginesMap.entrySet()) { - QuickSearchEnginesModel quickSearchEnginesModel = entry.getValue(); - if (!quickSearchEnginesModel - .getKeyword() - .equals(defaultSearchEngineTemplateUrl.getKeyword())) { - quickSearchEngines.add(quickSearchEnginesModel); + for (QuickSearchEnginesModel engine : searchEnginesMap.values()) { + if (!engine.getKeyword().equals(defaultEngineKeyword)) { + quickSearchEngines.add(engine); } } return quickSearchEngines; } + /** + * Gets the list of enabled quick search engines for display in the UI + * + * @param profile The user profile to get search engines for + * @return List of enabled quick search engines models + */ public static List getQuickSearchEnginesForView(Profile profile) { Map searchEnginesMap = getQuickSearchEngines(profile); + List quickSearchEngines = new ArrayList<>(); - for (Map.Entry entry : searchEnginesMap.entrySet()) { - QuickSearchEnginesModel quickSearchEnginesModel = entry.getValue(); - if (quickSearchEnginesModel.isEnabled()) { - quickSearchEngines.add(quickSearchEnginesModel); + for (QuickSearchEnginesModel engine : searchEnginesMap.values()) { + if (engine.isEnabled()) { + quickSearchEngines.add(engine); } } return quickSearchEngines; } + /** + * Gets the default search engine as a QuickSearchEnginesModel + * + * @param profile The user profile to get the default search engine for + * @return QuickSearchEnginesModel representing the default search engine + */ public static QuickSearchEnginesModel getDefaultSearchEngine(Profile profile) { - TemplateUrlService templateUrlService = TemplateUrlServiceFactory.getForProfile(profile); - TemplateUrl defaultSearchEngineTemplateUrl = - templateUrlService.getDefaultSearchEngineTemplateUrl(); - QuickSearchEnginesModel defaultSearchEnginesModel = - new QuickSearchEnginesModel( - defaultSearchEngineTemplateUrl.getShortName(), - defaultSearchEngineTemplateUrl.getKeyword(), - defaultSearchEngineTemplateUrl.getURL(), - true); - return defaultSearchEnginesModel; + TemplateUrl defaultEngine = + TemplateUrlServiceFactory.getForProfile(profile) + .getDefaultSearchEngineTemplateUrl(); + + return new QuickSearchEnginesModel( + defaultEngine.getShortName(), + defaultEngine.getKeyword(), + defaultEngine.getURL(), + true); + } + + /** Helper method to get the keyword of the default search engine */ + private static String getDefaultSearchEngineKeyword(Profile profile) { + return TemplateUrlServiceFactory.getForProfile(profile) + .getDefaultSearchEngineTemplateUrl() + .getKeyword(); } }