1 /* 2 * Copyright (C) 2020 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.android.car.ui.core; 18 19 import android.content.ContentProvider; 20 import android.content.ContentUris; 21 import android.content.ContentValues; 22 import android.content.Context; 23 import android.database.Cursor; 24 import android.database.MatrixCursor; 25 import android.net.Uri; 26 27 import androidx.annotation.NonNull; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * Content provider for displaying search results. The url looks like: 34 * "content://${applicationId}.SearchResultsProvide/search_results" 35 */ 36 public class SearchResultsProvider extends ContentProvider { 37 public static final String ITEM_ID = "primaryId"; 38 public static final String SECONDARY_IMAGE_ID = "secondary"; 39 public static final String PRIMARY_IMAGE_BLOB = "primary_image"; 40 public static final String SECONDARY_IMAGE_BLOB = "secondary_image"; 41 public static final String TITLE = "title"; 42 public static final String SUBTITLE = "subtitle"; 43 44 private final List<ContentValues> mSearchResults = new ArrayList<>(); 45 46 /** 47 * Database specific constant declarations 48 */ 49 private static final String SEARCH_RESULTS_TABLE_NAME = "search_results"; 50 51 /** 52 * Gets the authority of this Content Provider. 53 * @param packageName The package name of the current application. 54 */ 55 @NonNull getAuthority(@onNull String packageName)56 public static String getAuthority(@NonNull String packageName) { 57 return "content://" + packageName + "." + SearchResultsProvider.class.getSimpleName(); 58 } 59 60 /** 61 * Gets the Uri to the search results table. 62 * @param context A context used to get the package name of the app. 63 */ 64 @NonNull getSearchResultsTableUri(@onNull Context context)65 public static Uri getSearchResultsTableUri(@NonNull Context context) { 66 return Uri.parse(getAuthority(context.getPackageName()) + "/" + SEARCH_RESULTS_TABLE_NAME); 67 } 68 69 @Override onCreate()70 public boolean onCreate() { 71 return true; 72 } 73 74 @Override insert(Uri uri, ContentValues values)75 public Uri insert(Uri uri, ContentValues values) { 76 mSearchResults.add(values); 77 78 Uri contentUri = getSearchResultsTableUri(getContext()); 79 contentUri = ContentUris.withAppendedId(contentUri, mSearchResults.size() - 1); 80 getContext().getContentResolver().notifyChange(contentUri, null); 81 return contentUri; 82 } 83 84 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)85 public Cursor query(Uri uri, String[] projection, 86 String selection, String[] selectionArgs, String sortOrder) { 87 MatrixCursor cursor = new MatrixCursor(new String[]{ 88 ITEM_ID, 89 SECONDARY_IMAGE_ID, 90 PRIMARY_IMAGE_BLOB, 91 SECONDARY_IMAGE_BLOB, 92 TITLE, 93 SUBTITLE 94 }); 95 96 for (ContentValues values : mSearchResults) { 97 cursor.addRow(new Object[]{ 98 values.get(ITEM_ID), 99 values.get(SECONDARY_IMAGE_ID), 100 values.get(PRIMARY_IMAGE_BLOB), 101 values.get(SECONDARY_IMAGE_BLOB), 102 values.get(TITLE), 103 values.get(SUBTITLE), 104 }); 105 } 106 return cursor; 107 } 108 109 @Override delete(Uri uri, String selection, String[] selectionArgs)110 public int delete(Uri uri, String selection, String[] selectionArgs) { 111 mSearchResults.clear(); 112 return 0; 113 } 114 115 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)116 public int update(Uri uri, ContentValues values, 117 String selection, String[] selectionArgs) { 118 return 0; 119 } 120 121 @Override getType(Uri uri)122 public String getType(Uri uri) { 123 return null; 124 } 125 } 126