1 /* 2 * Copyright (C) 2017 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.google.android.tv.partner.support; 18 19 import android.content.ContentResolver; 20 import android.content.ContentValues; 21 import android.database.Cursor; 22 import android.database.DatabaseUtils; 23 import android.net.Uri; 24 import android.support.annotation.Nullable; 25 import android.support.annotation.WorkerThread; 26 import java.util.Collections; 27 import java.util.HashSet; 28 import java.util.Set; 29 30 /** Static utilities for {@link EpgInput}. */ 31 public final class EpgInputs { 32 // TODO create a fake EpgContentProvider for testing. 33 34 /** Returns {@link EpgInput} for {@code inputId} or null if not found. */ 35 @WorkerThread 36 @Nullable queryEpgInput(ContentResolver contentResolver, String inputId)37 public static EpgInput queryEpgInput(ContentResolver contentResolver, String inputId) { 38 39 for (EpgInput epgInput : queryEpgInputs(contentResolver)) { 40 if (inputId.equals(epgInput.getInputId())) { 41 return epgInput; 42 } 43 } 44 return null; 45 } 46 47 /** Returns all {@link EpgInput}. */ 48 @WorkerThread queryEpgInputs(ContentResolver contentResolver)49 public static Set<EpgInput> queryEpgInputs(ContentResolver contentResolver) { 50 try (Cursor cursor = 51 contentResolver.query(EpgContract.EpgInputs.CONTENT_URI, null, null, null, null)) { 52 if (cursor == null) { 53 return Collections.emptySet(); 54 } 55 HashSet<EpgInput> result = new HashSet<>(cursor.getCount()); 56 while (cursor.moveToNext()) { 57 ContentValues contentValues = new ContentValues(); 58 DatabaseUtils.cursorRowToContentValues(cursor, contentValues); 59 result.add(EpgInput.createEpgChannel(contentValues)); 60 } 61 return result; 62 } catch (Exception e) { 63 return Collections.emptySet(); 64 } 65 } 66 67 /** Insert an {@link EpgInput}. */ 68 @WorkerThread 69 @Nullable insert(ContentResolver contentResolver, EpgInput epgInput)70 public static Uri insert(ContentResolver contentResolver, EpgInput epgInput) { 71 return contentResolver.insert( 72 EpgContract.EpgInputs.CONTENT_URI, epgInput.toContentValues()); 73 } 74 75 /** Update an {@link EpgInput}. */ 76 @WorkerThread update(ContentResolver contentResolver, EpgInput epgInput)77 public static int update(ContentResolver contentResolver, EpgInput epgInput) { 78 return contentResolver.update( 79 EpgContract.EpgInputs.buildUri(epgInput.getId()), 80 epgInput.toContentValues(), 81 null, 82 null); 83 } 84 EpgInputs()85 private EpgInputs() {} 86 } 87