1 /* 2 * Copyright (C) 2015 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 17 package com.example.android.tvleanback.data; 18 19 import android.app.SearchManager; 20 import android.content.ContentProvider; 21 import android.content.ContentResolver; 22 import android.content.ContentValues; 23 import android.content.UriMatcher; 24 import android.database.Cursor; 25 import android.net.Uri; 26 import android.provider.BaseColumns; 27 import android.util.Log; 28 29 /** 30 * Provides access to the video database. 31 */ 32 public class VideoContentProvider extends ContentProvider { 33 private static String TAG = "VideoContentProvider"; 34 public static String AUTHORITY = "com.example.android.tvleanback"; 35 36 // UriMatcher stuff 37 private static final int SEARCH_SUGGEST = 0; 38 private static final int REFRESH_SHORTCUT = 1; 39 private static final UriMatcher URI_MATCHER = buildUriMatcher(); 40 41 private VideoDatabase mVideoDatabase; 42 43 /** 44 * Builds up a UriMatcher for search suggestion and shortcut refresh queries. 45 */ 46 private static UriMatcher buildUriMatcher() { 47 UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); 48 // to get suggestions... 49 Log.d(TAG, "suggest_uri_path_query: " + SearchManager.SUGGEST_URI_PATH_QUERY); 50 matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); 51 matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); 52 return matcher; 53 } 54 55 @Override 56 public boolean onCreate() { 57 Log.d(TAG, "onCreate"); 58 mVideoDatabase = new VideoDatabase(getContext()); 59 return true; 60 } 61 62 /** 63 * Handles all the video searches and suggestion queries from the Search Manager. 64 * When requesting a specific word, the uri alone is required. 65 * When searching all of the video for matches, the selectionArgs argument must carry 66 * the search query as the first element. 67 * All other arguments are ignored. 68 */ 69 @Override 70 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 71 String sortOrder) { 72 // Use the UriMatcher to see what kind of query we have and format the db query accordingly 73 switch (URI_MATCHER.match(uri)) { 74 case SEARCH_SUGGEST: 75 Log.d(TAG, "search suggest: " + selectionArgs[0] + " URI: " + uri); 76 if (selectionArgs == null) { 77 throw new IllegalArgumentException( 78 "selectionArgs must be provided for the Uri: " + uri); 79 } 80 return getSuggestions(selectionArgs[0]); 81 default: 82 throw new IllegalArgumentException("Unknown Uri: " + uri); 83 } 84 } 85 86 private Cursor getSuggestions(String query) { 87 query = query.toLowerCase(); 88 String[] columns = new String[]{ 89 BaseColumns._ID, 90 VideoDatabase.KEY_NAME, 91 VideoDatabase.KEY_DESCRIPTION, 92 VideoDatabase.KEY_ICON, 93 VideoDatabase.KEY_DATA_TYPE, 94 VideoDatabase.KEY_IS_LIVE, 95 VideoDatabase.KEY_VIDEO_WIDTH, 96 VideoDatabase.KEY_VIDEO_HEIGHT, 97 VideoDatabase.KEY_AUDIO_CHANNEL_CONFIG, 98 VideoDatabase.KEY_PURCHASE_PRICE, 99 VideoDatabase.KEY_RENTAL_PRICE, 100 VideoDatabase.KEY_RATING_STYLE, 101 VideoDatabase.KEY_RATING_SCORE, 102 VideoDatabase.KEY_PRODUCTION_YEAR, 103 VideoDatabase.KEY_COLUMN_DURATION, 104 VideoDatabase.KEY_ACTION, 105 SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID 106 }; 107 return mVideoDatabase.getWordMatch(query, columns); 108 } 109 110 /** 111 * This method is required in order to query the supported types. 112 * It's also useful in our own query() method to determine the type of Uri received. 113 */ 114 @Override 115 public String getType(Uri uri) { 116 switch (URI_MATCHER.match(uri)) { 117 case SEARCH_SUGGEST: 118 return SearchManager.SUGGEST_MIME_TYPE; 119 case REFRESH_SHORTCUT: 120 return SearchManager.SHORTCUT_MIME_TYPE; 121 default: 122 throw new IllegalArgumentException("Unknown URL " + uri); 123 } 124 } 125 126 // Other required implementations... 127 128 @Override 129 public Uri insert(Uri uri, ContentValues values) { 130 throw new UnsupportedOperationException(); 131 } 132 133 @Override 134 public int delete(Uri uri, String selection, String[] selectionArgs) { 135 throw new UnsupportedOperationException(); 136 } 137 138 @Override 139 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 140 throw new UnsupportedOperationException(); 141 } 142 } 143