diff --git a/app/src/main/java/de/markusfisch/android/pielauncher/content/AppMenu.java b/app/src/main/java/de/markusfisch/android/pielauncher/content/AppMenu.java index c3b55ae9..398b3702 100644 --- a/app/src/main/java/de/markusfisch/android/pielauncher/content/AppMenu.java +++ b/app/src/main/java/de/markusfisch/android/pielauncher/content/AppMenu.java @@ -157,51 +157,45 @@ public List filterAppsBy(Context context, String query) { ? "" : query.trim().toLowerCase(defaultLocale); + Preferences prefs = PieLauncherApp.getPrefs(context); + int strategy = prefs.getSearchStrictness(); ArrayList list = new ArrayList<>(); - ArrayList contain = new ArrayList<>(); ArrayList hamming = new ArrayList<>(); if (query.isEmpty()) { list.addAll(apps.values()); } else { - int searchParameterPref = PieLauncherApp - .getPrefs(context).getSearchParameter(); - for (Map.Entry entry : - apps.entrySet()) { - String searchParameter; + int item = prefs.getSearchParameter(); + for (Map.Entry entry : apps.entrySet()) { AppIcon appIcon = entry.getValue(); - if (searchParameterPref == - Preferences.SEARCH_PARAMETER_PACKAGE_NAME) { - searchParameter = appIcon.componentName - .getPackageName().toLowerCase(defaultLocale); - } else { - searchParameter = appIcon.label.toLowerCase(defaultLocale); + String subject = getSubject(item, appIcon, defaultLocale); + boolean add = false; + switch (strategy) { + case Preferences.SEARCH_STRICTNESS_STARTS_WITH: + add = subject.startsWith(query); + break; + case Preferences.SEARCH_STRICTNESS_HAMMING: + if (hammingDistance(subject, query) < 2) { + hamming.add(appIcon); + } + // Fall through because HAMMING includes CONTAINS + // for historical reasons. + case Preferences.SEARCH_STRICTNESS_CONTAINS: + add = subject.contains(query); + break; } - if (searchParameter.startsWith(query)) { + if (add) { list.add(appIcon); - } else if (searchParameter.contains(query)) { - contain.add(appIcon); - } else if (hammingDistance(searchParameter, query) < 2) { - hamming.add(appIcon); } } } - Collections.sort(list, appLabelComparator); - - int searchStrictness = - PieLauncherApp.getPrefs(context).getSearchStrictness(); - if (searchStrictness == Preferences.SEARCH_STRICTNESS_STARTS_WITH) { - return list; - } - Collections.sort(contain, appLabelComparator); - list.addAll(contain); - if (searchStrictness == Preferences.SEARCH_STRICTNESS_CONTAINS) { - return list; + Collections.sort(list, appLabelComparator); + if (strategy == Preferences.SEARCH_STRICTNESS_HAMMING) { + // Only append hamming matches as they're less likely + // as good as exact matches. + Collections.sort(hamming, appLabelComparator); + list.addAll(hamming); } - - Collections.sort(hamming, appLabelComparator); - list.addAll(hamming); - return list; } @@ -522,6 +516,15 @@ private LauncherApps getLauncherApps(Context context) { return launcherApps; } + private static String getSubject(int item, AppIcon appIcon, + Locale defaultLocale) { + if (item == Preferences.SEARCH_PARAMETER_PACKAGE_NAME) { + return appIcon.componentName.getPackageName() + .toLowerCase(defaultLocale); + } + return appIcon.label.toLowerCase(defaultLocale); + } + private static int hammingDistance(String a, String b) { int count = 0; for (int i = 0, l = Math.min(a.length(), b.length()); i < l; ++i) {