Skip to content

Commit

Permalink
Unicode insensitive comparison in All Apps search
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi666 committed Oct 8, 2017
1 parent de2bf40 commit 07049df
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
Expand Up @@ -21,6 +21,6 @@
public class DefaultAppSearchController extends AllAppsSearchBarController {

public DefaultAppSearchAlgorithm onInitializeSearch() {
return new DefaultAppSearchAlgorithm(mApps.getApps());
return new UnicodeStrippedAppSearchAlgorithm(mApps.getApps());
}
}
@@ -0,0 +1,29 @@
package ch.deletescape.lawnchair.allapps;

import java.util.List;

import ch.deletescape.lawnchair.AppInfo;
import ch.deletescape.lawnchair.util.UnicodeFilter;

/**
* A search algorithm that changes every non-ascii characters to theirs ascii equivalents and
* then performs comparison.
*/
public class UnicodeStrippedAppSearchAlgorithm extends DefaultAppSearchAlgorithm {
public UnicodeStrippedAppSearchAlgorithm(List<AppInfo> apps) {
super(apps);
}

@Override
protected boolean matches(AppInfo info, String query) {
String title = UnicodeFilter.filter(info.title.toString().toLowerCase());
String strippedQuery = UnicodeFilter.filter(query);
int queryLength = strippedQuery.length();

if (title.length() < queryLength || queryLength <= 0) {
return false;
}

return title.indexOf(strippedQuery) >= 0;
}
}
96 changes: 96 additions & 0 deletions app/src/main/java/ch/deletescape/lawnchair/util/UnicodeFilter.java
@@ -0,0 +1,96 @@
/*
* Copyright (C) 2012-2016 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ch.deletescape.lawnchair.util;

import java.text.Normalizer;
import java.util.regex.Pattern;

/**
* Attempts to substitute characters that cannot be encoded in the limited
* GSM 03.38 character set. In many cases this will prevent sending a message
* containing characters that would switch the message from 7-bit GSM
* encoding (160 char limit) to 16-bit Unicode encoding (70 char limit).
*/
public class UnicodeFilter {
private static final Pattern DIACRITICS_PATTERN =
Pattern.compile("\\p{InCombiningDiacriticalMarks}");

public static String filter(String source) {
StringBuilder output = new StringBuilder();
final int sourceLength = source.length();

for (int i = 0; i < sourceLength; i++) {
String s = String.valueOf(source.charAt(i));

// Try normalizing the character into Unicode NFKD form and
// stripping out diacritic mark characters.
s = Normalizer.normalize(s, Normalizer.Form.NFKD);
s = DIACRITICS_PATTERN.matcher(s).replaceAll("");

// Special case characters that don't get stripped by the
// above technique.
s = s.replace("Œ", "OE");
s = s.replace("œ", "oe");
s = s.replace("Ł", "L");
s = s.replace("ł", "l");
s = s.replace("Đ", "Dj");
s = s.replace("đ", "dj");
s = s.replace("Α", "A");
s = s.replace("Β", "B");
s = s.replace("Ε", "E");
s = s.replace("Ζ", "Z");
s = s.replace("Η", "H");
s = s.replace("Ι", "I");
s = s.replace("Κ", "K");
s = s.replace("Μ", "M");
s = s.replace("Ν", "N");
s = s.replace("Ο", "O");
s = s.replace("Ρ", "P");
s = s.replace("Τ", "T");
s = s.replace("Υ", "Y");
s = s.replace("Χ", "X");
s = s.replace("α", "A");
s = s.replace("β", "B");
s = s.replace("γ", "Γ");
s = s.replace("δ", "Δ");
s = s.replace("ε", "E");
s = s.replace("ζ", "Z");
s = s.replace("η", "H");
s = s.replace("θ", "Θ");
s = s.replace("ι", "I");
s = s.replace("κ", "K");
s = s.replace("λ", "Λ");
s = s.replace("μ", "M");
s = s.replace("ν", "N");
s = s.replace("ξ", "Ξ");
s = s.replace("ο", "O");
s = s.replace("π", "Π");
s = s.replace("ρ", "P");
s = s.replace("σ", "Σ");
s = s.replace("τ", "T");
s = s.replace("υ", "Y");
s = s.replace("φ", "Φ");
s = s.replace("χ", "X");
s = s.replace("ψ", "Ψ");
s = s.replace("ω", "Ω");
s = s.replace("ς", "Σ");
output.append(s);
}

return output.toString();
}
}

0 comments on commit 07049df

Please sign in to comment.