aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/provider/DSubSearchProvider.java
blob: b47cbd28ce2eda5c978f55e524a8e3f9c6366a09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2010 (C) Sindre Mehus
 */
package github.daneren2005.dsub.provider;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import github.daneren2005.dsub.R;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.domain.SearchCritera;
import github.daneren2005.dsub.domain.SearchResult;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
import github.daneren2005.dsub.util.Util;

/**
 * Provides search suggestions based on recent searches.
 *
 * @author Sindre Mehus
 */
public class DSubSearchProvider extends ContentProvider {
	private static final String TAG = DSubSearchProvider.class.getSimpleName();

	private static final String RESOURCE_PREFIX = "android.resource://github.daneren2005.dsub/";
	private static final String[] COLUMNS = {"_id",
			SearchManager.SUGGEST_COLUMN_TEXT_1,
			SearchManager.SUGGEST_COLUMN_TEXT_2,
			SearchManager.SUGGEST_COLUMN_INTENT_DATA,
			SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA,
			SearchManager.SUGGEST_COLUMN_ICON_1};

	@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);
	}

	private SearchResult search(String query) {
		MusicService musicService = MusicServiceFactory.getMusicService(getContext());
		if (musicService == null) {
			return null;
		}

		try {
			return musicService.search(new SearchCritera(query, 5, 10, 10), getContext(), null);
		} catch (Exception e) {
			return null;
		}
	}

	private Cursor createCursor(String query, SearchResult searchResult) {
		MatrixCursor cursor = new MatrixCursor(COLUMNS);
		if (searchResult == null) {
			return cursor;
		}
		
		// Add all results into one pot
		List<Object> results = new ArrayList<Object>();
		results.addAll(searchResult.getArtists());
		results.addAll(searchResult.getAlbums());
		results.addAll(searchResult.getSongs());
		
		// For each, calculate its string distance to the query
		for(Object obj: results) {
			if(obj instanceof Artist) {
				Artist artist = (Artist) obj;
				artist.setCloseness(Util.getStringDistance(query, artist.getName()));
			} else {
				MusicDirectory.Entry entry = (MusicDirectory.Entry) obj;
				entry.setCloseness(Util.getStringDistance(query, entry.getTitle()));
			}
		}
		
		// Sort based on the closeness paramater
		Collections.sort(results, new Comparator<Object>() {
			@Override
			public int compare(Object lhs, Object rhs) {
				// Get the closeness of the two objects
				int left, right;
				boolean leftArtist = lhs instanceof Artist;
				boolean rightArtist = rhs instanceof Artist;
				if (leftArtist) {
					left = ((Artist) lhs).getCloseness();
				} else {
					left = ((MusicDirectory.Entry) lhs).getCloseness();
				}
				if (rightArtist) {
					right = ((Artist) rhs).getCloseness();
				} else {
					right = ((MusicDirectory.Entry) rhs).getCloseness();
				}

				if (left == right) {
					if(leftArtist && rightArtist) {
						return 0;
					} else if(leftArtist) {
						return -1;
					} else if(rightArtist) {
						return 1;
					} else {
						return 0;
					}
				} else if (left > right) {
					return 1;
				} else {
					return -1;
				}
			}
		});
		
		// Done sorting, add results to cursor
		for(Object obj: results) {
			if(obj instanceof Artist) {
				Artist artist = (Artist) obj;
				String icon = RESOURCE_PREFIX + R.drawable.ic_action_artist;
				cursor.addRow(new Object[]{artist.getId().hashCode(), artist.getName(), null, "dsub-ar-" + artist.getId(), artist.getName(), icon});
			} else {
				MusicDirectory.Entry entry = (MusicDirectory.Entry) obj;
				
				if(entry.isDirectory()) {
					String icon = RESOURCE_PREFIX + R.drawable.ic_action_album;
					cursor.addRow(new Object[]{entry.getId().hashCode(), entry.getTitle(), entry.getArtist(), entry.getId(), entry.getTitle(), icon});
				} else {
					String icon = RESOURCE_PREFIX + R.drawable.ic_action_song;
					String id;
					if(Util.isTagBrowsing(getContext())) {
						id = entry.getAlbumId();
					} else {
						id = entry.getParent();
					}

					String artistDisplay;
					if(entry.getArtist() == null) {
						if(entry.getAlbum() != null) {
							artistDisplay = entry.getAlbumDisplay();
						} else {
							artistDisplay = "";
						}
					} else if(entry.getAlbum() != null) {
						artistDisplay = entry.getArtist() + " - " + entry.getAlbumDisplay();
					} else {
						artistDisplay = entry.getArtist();
					}

					cursor.addRow(new Object[]{entry.getId().hashCode(), entry.getTitle(), artistDisplay, "dsub-so-" + id, entry.getTitle(), icon});
				}
			}
		}
		return cursor;
	}

	@Override
	public boolean onCreate() {
		return false;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues contentValues) {
		return null;
	}

	@Override
	public int delete(Uri uri, String s, String[] strings) {
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
		return 0;
	}

}