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.android.tv.testing; 18 19 import android.content.ContentUris; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.database.Cursor; 23 import android.database.sqlite.SQLiteException; 24 import android.media.tv.TvContract; 25 import android.media.tv.TvContract.Programs; 26 import android.net.Uri; 27 import android.util.Log; 28 29 import com.android.tv.common.TvContentRatingCache; 30 31 import java.util.ArrayList; 32 import java.util.concurrent.TimeUnit; 33 34 public class ProgramUtils { 35 private static final String TAG = "ProgramUtils"; 36 private static final boolean DEBUG = false; 37 38 // Populate program data for a week. 39 private static final long PROGRAM_INSERT_DURATION_MS = TimeUnit.DAYS.toMillis(7); 40 private static final int MAX_DB_INSERT_COUNT_AT_ONCE = 500; 41 42 /** 43 * Populate programs by repeating given program information. 44 * This method will populate programs without any gap nor overlapping 45 * starting from the current time. 46 */ populatePrograms(Context context, Uri channelUri, ProgramInfo program)47 public static void populatePrograms(Context context, Uri channelUri, ProgramInfo program) { 48 ContentValues values = new ContentValues(); 49 long channelId = ContentUris.parseId(channelUri); 50 51 values.put(Programs.COLUMN_CHANNEL_ID, channelId); 52 values.put(Programs.COLUMN_SHORT_DESCRIPTION, program.description); 53 values.put(Programs.COLUMN_CONTENT_RATING, 54 TvContentRatingCache.contentRatingsToString(program.contentRatings)); 55 56 long currentTimeMs = System.currentTimeMillis(); 57 long targetEndTimeMs = currentTimeMs + PROGRAM_INSERT_DURATION_MS; 58 long timeMs = getLastProgramEndTimeMs(context, channelUri, currentTimeMs, targetEndTimeMs); 59 if (timeMs <= 0) { 60 timeMs = currentTimeMs; 61 } 62 int index = program.getIndex(timeMs, channelId); 63 timeMs = program.getStartTimeMs(index, channelId); 64 65 ArrayList<ContentValues> list = new ArrayList<>(); 66 while (timeMs < targetEndTimeMs) { 67 ProgramInfo programAt = program.build(context, index++); 68 values.put(Programs.COLUMN_TITLE, programAt.title); 69 values.put(Programs.COLUMN_EPISODE_TITLE, programAt.episode); 70 if (programAt.seasonNumber != 0) { 71 values.put(Programs.COLUMN_SEASON_NUMBER, programAt.seasonNumber); 72 } 73 if (programAt.episodeNumber != 0) { 74 values.put(Programs.COLUMN_EPISODE_NUMBER, programAt.episodeNumber); 75 } 76 values.put(Programs.COLUMN_POSTER_ART_URI, programAt.posterArtUri); 77 values.put(Programs.COLUMN_START_TIME_UTC_MILLIS, timeMs); 78 values.put(Programs.COLUMN_END_TIME_UTC_MILLIS, timeMs + programAt.durationMs); 79 values.put(Programs.COLUMN_CANONICAL_GENRE, programAt.genre); 80 values.put(Programs.COLUMN_POSTER_ART_URI, programAt.posterArtUri); 81 list.add(new ContentValues(values)); 82 timeMs += programAt.durationMs; 83 84 if (list.size() >= MAX_DB_INSERT_COUNT_AT_ONCE 85 || timeMs >= targetEndTimeMs) { 86 try { 87 context.getContentResolver().bulkInsert(Programs.CONTENT_URI, 88 list.toArray(new ContentValues[list.size()])); 89 } catch (SQLiteException e) { 90 Log.e(TAG, "Can't insert EPG.", e); 91 return; 92 } 93 if (DEBUG) Log.d(TAG, "Inserted " + list.size() + " programs for " + channelUri); 94 list.clear(); 95 } 96 } 97 } 98 getLastProgramEndTimeMs( Context context, Uri channelUri, long startTimeMs, long endTimeMs)99 private static long getLastProgramEndTimeMs( 100 Context context, Uri channelUri, long startTimeMs, long endTimeMs) { 101 Uri uri = TvContract.buildProgramsUriForChannel(channelUri, startTimeMs, endTimeMs); 102 String[] projection = {Programs.COLUMN_END_TIME_UTC_MILLIS}; 103 try (Cursor cursor = 104 context.getContentResolver().query(uri, projection, null, null, null)) { 105 if (cursor != null && cursor.moveToLast()) { 106 return cursor.getLong(0); 107 } 108 } 109 return 0; 110 } 111 ProgramUtils()112 private ProgramUtils() {} 113 } 114