TvProviderSearch.java revision 10e4bd79f61c624b451a4f01fc1a9ea7c16168db
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tv.search;
18
19import android.content.ContentUris;
20import android.content.Context;
21import android.content.Intent;
22import android.database.Cursor;
23import android.net.Uri;
24import android.provider.TvContract.Channels;
25import android.provider.TvContract.Programs;
26
27import java.util.ArrayList;
28import java.util.List;
29
30public class TvProviderSearch {
31    public static List<SearchResult> search(Context context, String query) {
32        List<SearchResult> results = new ArrayList<SearchResult>();
33        results.addAll(searchChannels(context, query, new String[] {
34                Channels.COLUMN_DISPLAY_NAME,
35                Channels.COLUMN_DESCRIPTION
36        }));
37        results.addAll(searchPrograms(context, query, new String[] {
38                Programs.COLUMN_TITLE,
39                Programs.COLUMN_DESCRIPTION
40        }));
41        return results;
42    }
43
44    private static List<SearchResult> searchChannels(Context context, String query,
45            String[] columnNames) {
46        String[] projection = {
47                Channels._ID,
48                Channels.COLUMN_DISPLAY_NAME,
49                Channels.COLUMN_DESCRIPTION,
50        };
51        return search(context, Channels.CONTENT_URI, projection, query, columnNames);
52    }
53
54    // TODO: Consider the case when the searched programs are already ended or the user select a
55    //       searched program which doesn't air right now.
56    private static List<SearchResult> searchPrograms(Context context, String query,
57            String[] columnNames) {
58        String[] projection = {
59                Programs.COLUMN_CHANNEL_ID,
60                Programs.COLUMN_TITLE,
61                Programs.COLUMN_DESCRIPTION,
62        };
63        return search(context, Programs.CONTENT_URI, projection, query, columnNames);
64    }
65
66    private static List<SearchResult> search(Context context, Uri uri, String[] projection,
67            String query, String[] columnNames) {
68        List<SearchResult> results = new ArrayList<SearchResult>();
69
70        StringBuilder sb = new StringBuilder("1=0");
71        for (String columnName : columnNames) {
72            sb.append(" OR ").append(columnName).append(" like ?");
73        }
74        String selection = sb.toString();
75        String selectionArg = "%" + query + "%";
76        String[] selectionArgs = new String[columnNames.length];
77        for (int i=0; i<selectionArgs.length; ++i) {
78            selectionArgs[i] = selectionArg;
79        }
80
81        Cursor cursor = null;
82        try {
83            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
84                    null);
85            if (cursor != null) {
86                // TODO: Need to add image when available.
87                while (cursor.moveToNext()) {
88                    int id = cursor.getInt(0);
89                    String title = cursor.getString(1);
90                    String description = cursor.getString(2);
91
92                    SearchResult result = SearchResult.builder()
93                            .setTitle(title)
94                            .setDescription(description)
95                            .setIntentAction(Intent.ACTION_VIEW)
96                            .setIntentData(ContentUris.withAppendedId(Channels.CONTENT_URI, id)
97                                    .toString())
98                            .build();
99                    results.add(result);
100                }
101            }
102        } finally {
103            if (cursor != null) {
104                cursor.close();
105            }
106        }
107
108        return results;
109    }
110}
111