From cb9f7d49ba13890872796c71b16b6f7e9f14625f Mon Sep 17 00:00:00 2001 From: François-Xavier Thomas Date: Sun, 31 Jan 2016 17:41:56 +0100 Subject: Make suggestions work in offline search --- .../daneren2005/dsub/domain/SearchCritera.java | 39 +++++++++++++++++++++- .../dsub/service/OfflineMusicService.java | 29 ++++++++-------- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java b/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java index 20d46aa0..12c641f2 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java @@ -18,6 +18,8 @@ */ package github.daneren2005.dsub.domain; +import java.util.regex.Pattern; + /** * The criteria for a music search. * @@ -29,6 +31,7 @@ public class SearchCritera { private final int artistCount; private final int albumCount; private final int songCount; + private Pattern pattern; public SearchCritera(String query, int artistCount, int albumCount, int songCount) { this.query = query; @@ -52,4 +55,38 @@ public class SearchCritera { public int getSongCount() { return songCount; } -} \ No newline at end of file + + /** + * Returns and caches a pattern instance that can be used to check if a + * string matches the query. + */ + public Pattern getPattern() { + + // If the pattern wasn't already cached, create a new regular expression + // from the search string : + // * Surround the search string with ".*" (match anything) + // * Replace spaces and wildcard '*' characters with ".*" + // * All other characters are properly quoted + if (this.pattern == null) { + String regex = ".*"; + String currentPart = ""; + for (int i = 0; i < query.length(); i++) { + char c = query.charAt(i); + if (c == '*' || c == ' ') { + regex += Pattern.quote(currentPart); + regex += ".*"; + currentPart = ""; + } else { + currentPart += c; + } + } + if (currentPart.length() > 0) { + regex += Pattern.quote(currentPart); + } + regex += ".*"; + this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + } + + return this.pattern; + } +} diff --git a/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java index b8633624..a1657f39 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java @@ -307,7 +307,15 @@ public class OfflineMusicService implements MusicService { } } }); - + + // Respect counts in search criteria + int artistCount = Math.min(artists.size(), criteria.getArtistCount()); + int albumCount = Math.min(albums.size(), criteria.getAlbumCount()); + int songCount = Math.min(songs.size(), criteria.getSongCount()); + artists = artists.subList(0, artistCount); + albums = albums.subList(0, albumCount); + songs = songs.subList(0, songCount); + return new SearchResult(artists, albums, songs); } @@ -359,20 +367,13 @@ public class OfflineMusicService implements MusicService { } } private int matchCriteria(SearchCritera criteria, String name) { - String query = criteria.getQuery().toLowerCase(); - String[] queryParts = query.split(" "); - String[] nameParts = name.toLowerCase().split(" "); - - int closeness = 0; - for(String queryPart : queryParts) { - for(String namePart : nameParts) { - if(namePart.equals(queryPart)) { - closeness++; - } - } + if (criteria.getPattern().matcher(name).matches()) { + return Util.getStringDistance( + criteria.getQuery().toLowerCase(), + name.toLowerCase()); + } else { + return 0; } - - return closeness; } @Override -- cgit v1.2.3 From b0a9bed5112dd3cd5e1cbb6b0ec9132d59fe7ef2 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 1 Feb 2016 11:42:59 -0800 Subject: Return empty result on blank query --- .../java/github/daneren2005/dsub/provider/DSubSearchProvider.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/main/java/github/daneren2005/dsub/provider/DSubSearchProvider.java b/app/src/main/java/github/daneren2005/dsub/provider/DSubSearchProvider.java index f91c364e..ba8c80c1 100644 --- a/app/src/main/java/github/daneren2005/dsub/provider/DSubSearchProvider.java +++ b/app/src/main/java/github/daneren2005/dsub/provider/DSubSearchProvider.java @@ -58,6 +58,10 @@ public class DSubSearchProvider extends ContentProvider { @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + if(selectionArgs[0].isEmpty()) { + return null; + } + String query = selectionArgs[0] + "*"; SearchResult searchResult = search(query); return createCursor(selectionArgs[0], searchResult); -- cgit v1.2.3 From d93a8939cf7d327ce6f82b7c093a18f3bd5377ad Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 1 Feb 2016 11:43:12 -0800 Subject: Fix indentions to use tabs --- .../daneren2005/dsub/domain/SearchCritera.java | 125 +++++++++++---------- 1 file changed, 63 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java b/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java index 12c641f2..ed2400ef 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java @@ -27,66 +27,67 @@ import java.util.regex.Pattern; */ public class SearchCritera { - private final String query; - private final int artistCount; - private final int albumCount; - private final int songCount; - private Pattern pattern; - - public SearchCritera(String query, int artistCount, int albumCount, int songCount) { - this.query = query; - this.artistCount = artistCount; - this.albumCount = albumCount; - this.songCount = songCount; - } - - public String getQuery() { - return query; - } - - public int getArtistCount() { - return artistCount; - } - - public int getAlbumCount() { - return albumCount; - } - - public int getSongCount() { - return songCount; - } - - /** - * Returns and caches a pattern instance that can be used to check if a - * string matches the query. - */ - public Pattern getPattern() { - - // If the pattern wasn't already cached, create a new regular expression - // from the search string : - // * Surround the search string with ".*" (match anything) - // * Replace spaces and wildcard '*' characters with ".*" - // * All other characters are properly quoted - if (this.pattern == null) { - String regex = ".*"; - String currentPart = ""; - for (int i = 0; i < query.length(); i++) { - char c = query.charAt(i); - if (c == '*' || c == ' ') { - regex += Pattern.quote(currentPart); - regex += ".*"; - currentPart = ""; - } else { - currentPart += c; - } - } - if (currentPart.length() > 0) { - regex += Pattern.quote(currentPart); - } - regex += ".*"; - this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); - } - - return this.pattern; - } + private final String query; + private final int artistCount; + private final int albumCount; + private final int songCount; + private Pattern pattern; + + public SearchCritera(String query, int artistCount, int albumCount, int songCount) { + this.query = query; + this.artistCount = artistCount; + this.albumCount = albumCount; + this.songCount = songCount; + } + + public String getQuery() { + return query; + } + + public int getArtistCount() { + return artistCount; + } + + public int getAlbumCount() { + return albumCount; + } + + public int getSongCount() { + return songCount; + } + + /** + * Returns and caches a pattern instance that can be used to check if a + * string matches the query. + */ + public Pattern getPattern() { + + // If the pattern wasn't already cached, create a new regular expression + // from the search string : + // * Surround the search string with ".*" (match anything) + // * Replace spaces and wildcard '*' characters with ".*" + // * All other characters are properly quoted + if (this.pattern == null) { + String regex = ".*"; + String currentPart = ""; + for (int i = 0; i < query.length(); i++) { + char c = query.charAt(i); + if (c == '*' || c == ' ') { + regex += Pattern.quote(currentPart); + regex += ".*"; + currentPart = ""; + } else { + currentPart += c; + } + } + if (currentPart.length() > 0) { + regex += Pattern.quote(currentPart); + } + + regex += ".*"; + this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + } + + return this.pattern; + } } -- cgit v1.2.3