diff options
author | Chulwoo Lee <chulwoo@google.com> | 2014-05-07 16:27:30 +0900 |
---|---|---|
committer | Chulwoo Lee <chulwoo@google.com> | 2014-05-08 15:47:36 +0900 |
commit | 63f6ab4e8240eef8b078651f2e7b3c92ff5f196b (patch) | |
tree | 54ca19d6923d6d837ab1bf202659e2a4ea160508 /src/com/android/tv/search/TvProviderSearch.java | |
parent | c388ef387df025ae8a66eef460f69b11d24be9d5 (diff) | |
download | TV-63f6ab4e8240eef8b078651f2e7b3c92ff5f196b.tar.gz |
Implement local channel & program search
Search for channels and programs can be performed
from Quick Search Box and the search results are
shown in the 'TV' category.
Search is done for the four columns with LIKE query:
- Channels.DISPLAY_NAME
- Channels.DESCRIPTION
- Programs.TITLE
- Programs.DESCRIPTION
If the result item is clicked, the related channel
is aired on the TV app.
Change-Id: I1d567489210f69eb0b2bec7a023e6a0566147a57
Diffstat (limited to 'src/com/android/tv/search/TvProviderSearch.java')
-rw-r--r-- | src/com/android/tv/search/TvProviderSearch.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/com/android/tv/search/TvProviderSearch.java b/src/com/android/tv/search/TvProviderSearch.java new file mode 100644 index 00000000..3d6223db --- /dev/null +++ b/src/com/android/tv/search/TvProviderSearch.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2014 The Android Open Source 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 com.android.tv.search; + +import android.content.ContentUris; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.provider.TvContract.Channels; +import android.provider.TvContract.Programs; + +import java.util.ArrayList; +import java.util.List; + +public class TvProviderSearch { + public static List<SearchResult> search(Context context, String query) { + List<SearchResult> results = new ArrayList<SearchResult>(); + results.addAll(searchChannels(context, query, new String[] { + Channels.DISPLAY_NAME, + Channels.DESCRIPTION + })); + results.addAll(searchPrograms(context, query, new String[] { + Programs.TITLE, + Programs.DESCRIPTION + })); + return results; + } + + private static List<SearchResult> searchChannels(Context context, String query, + String[] columnNames) { + String[] projection = { + Channels._ID, + Channels.DISPLAY_NAME, + Channels.DESCRIPTION, + }; + return search(context, Channels.CONTENT_URI, projection, query, columnNames); + } + + // TODO: Consider the case when the searched programs are already ended or the user select a + // searched program which doesn't air right now. + private static List<SearchResult> searchPrograms(Context context, String query, + String[] columnNames) { + String[] projection = { + Programs.CHANNEL_ID, + Programs.TITLE, + Programs.DESCRIPTION, + }; + return search(context, Programs.CONTENT_URI, projection, query, columnNames); + } + + private static List<SearchResult> search(Context context, Uri uri, String[] projection, + String query, String[] columnNames) { + List<SearchResult> results = new ArrayList<SearchResult>(); + + StringBuilder sb = new StringBuilder("1=0"); + for (String columnName : columnNames) { + sb.append(" OR ").append(columnName).append(" like ?"); + } + String selection = sb.toString(); + String selectionArg = "%" + query + "%"; + String[] selectionArgs = new String[columnNames.length]; + for (int i=0; i<selectionArgs.length; ++i) { + selectionArgs[i] = selectionArg; + } + + Cursor cursor = null; + try { + cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, + null); + if (cursor != null) { + // TODO: Need to add image when available. + while (cursor.moveToNext()) { + int id = cursor.getInt(0); + String title = cursor.getString(1); + String description = cursor.getString(2); + + SearchResult result = SearchResult.builder() + .setTitle(title) + .setDescription(description) + .setIntentAction(Intent.ACTION_VIEW) + .setIntentData(ContentUris.withAppendedId(Channels.CONTENT_URI, id) + .toString()) + .build(); + results.add(result); + } + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + + return results; + } +} |