diff options
Diffstat (limited to 'src/com/android/tv/search/DataManagerSearch.java')
-rw-r--r-- | src/com/android/tv/search/DataManagerSearch.java | 185 |
1 files changed, 113 insertions, 72 deletions
diff --git a/src/com/android/tv/search/DataManagerSearch.java b/src/com/android/tv/search/DataManagerSearch.java index d90908f1..82fb5016 100644 --- a/src/com/android/tv/search/DataManagerSearch.java +++ b/src/com/android/tv/search/DataManagerSearch.java @@ -26,17 +26,14 @@ import android.os.SystemClock; import android.support.annotation.MainThread; import android.text.TextUtils; import android.util.Log; - -import com.android.tv.ApplicationSingletons; -import com.android.tv.TvApplication; -import com.android.tv.data.Channel; +import com.android.tv.TvSingletons; import com.android.tv.data.ChannelDataManager; import com.android.tv.data.Program; import com.android.tv.data.ProgramDataManager; +import com.android.tv.data.api.Channel; import com.android.tv.search.LocalSearchProvider.SearchResult; import com.android.tv.util.MainThreadExecutor; import com.android.tv.util.Utils; - import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -47,11 +44,11 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; /** - * An implementation of {@link SearchInterface} to search query from {@link ChannelDataManager} - * and {@link ProgramDataManager}. + * An implementation of {@link SearchInterface} to search query from {@link ChannelDataManager} and + * {@link ProgramDataManager}. */ public class DataManagerSearch implements SearchInterface { - private static final String TAG = "TvProviderSearch"; + private static final String TAG = "DataManagerSearch"; private static final boolean DEBUG = false; private final Context mContext; @@ -62,20 +59,22 @@ public class DataManagerSearch implements SearchInterface { DataManagerSearch(Context context) { mContext = context; mTvInputManager = (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE); - ApplicationSingletons appSingletons = TvApplication.getSingletons(context); - mChannelDataManager = appSingletons.getChannelDataManager(); - mProgramDataManager = appSingletons.getProgramDataManager(); + TvSingletons tvSingletons = TvSingletons.getSingletons(context); + mChannelDataManager = tvSingletons.getChannelDataManager(); + mProgramDataManager = tvSingletons.getProgramDataManager(); } @Override public List<SearchResult> search(final String query, final int limit, final int action) { - Future<List<SearchResult>> future = MainThreadExecutor.getInstance() - .submit(new Callable<List<SearchResult>>() { - @Override - public List<SearchResult> call() throws Exception { - return searchFromDataManagers(query, limit, action); - } - }); + Future<List<SearchResult>> future = + MainThreadExecutor.getInstance() + .submit( + new Callable<List<SearchResult>>() { + @Override + public List<SearchResult> call() throws Exception { + return searchFromDataManagers(query, limit, action); + } + }); try { return future.get(); @@ -90,12 +89,12 @@ public class DataManagerSearch implements SearchInterface { @MainThread private List<SearchResult> searchFromDataManagers(String query, int limit, int action) { + // TODO(b/72499165): add a test. List<SearchResult> results = new ArrayList<>(); if (!mChannelDataManager.isDbLoadFinished()) { return results; } - if (action == ACTION_TYPE_SWITCH_CHANNEL - || action == ACTION_TYPE_SWITCH_INPUT) { + if (action == ACTION_TYPE_SWITCH_CHANNEL || action == ACTION_TYPE_SWITCH_INPUT) { // Voice search query should be handled by the a system TV app. return results; } @@ -114,9 +113,14 @@ public class DataManagerSearch implements SearchInterface { } if (results.size() >= limit) { if (DEBUG) { - Log.d(TAG, "Found " + results.size() + " channels. Elapsed time for" + - " searching channels: " + (SystemClock.elapsedRealtime() - time) + - "(msec)"); + Log.d( + TAG, + "Found " + + results.size() + + " channels. Elapsed time for" + + " searching channels: " + + (SystemClock.elapsedRealtime() - time) + + "(msec)"); } return results; } @@ -133,16 +137,27 @@ public class DataManagerSearch implements SearchInterface { } if (results.size() >= limit) { if (DEBUG) { - Log.d(TAG, "Found " + results.size() + " channels. Elapsed time for" + - " searching channels: " + (SystemClock.elapsedRealtime() - time) + - "(msec)"); + Log.d( + TAG, + "Found " + + results.size() + + " channels. Elapsed time for" + + " searching channels: " + + (SystemClock.elapsedRealtime() - time) + + "(msec)"); } return results; } } if (DEBUG) { - Log.d(TAG, "Found " + results.size() + " channels. Elapsed time for" + - " searching channels: " + (SystemClock.elapsedRealtime() - time) + "(msec)"); + Log.d( + TAG, + "Found " + + results.size() + + " channels. Elapsed time for" + + " searching channels: " + + (SystemClock.elapsedRealtime() - time) + + "(msec)"); } int channelResult = results.size(); if (DEBUG) Log.d(TAG, "Searching programs: '" + query + "'"); @@ -161,9 +176,14 @@ public class DataManagerSearch implements SearchInterface { } if (results.size() >= limit) { if (DEBUG) { - Log.d(TAG, "Found " + (results.size() - channelResult) + " programs. Elapsed" + - " time for searching programs: " + - (SystemClock.elapsedRealtime() - time) + "(msec)"); + Log.d( + TAG, + "Found " + + (results.size() - channelResult) + + " programs. Elapsed" + + " time for searching programs: " + + (SystemClock.elapsedRealtime() - time) + + "(msec)"); } return results; } @@ -182,16 +202,27 @@ public class DataManagerSearch implements SearchInterface { } if (results.size() >= limit) { if (DEBUG) { - Log.d(TAG, "Found " + (results.size() - channelResult) + " programs. Elapsed" + - " time for searching programs: " + - (SystemClock.elapsedRealtime() - time) + "(msec)"); + Log.d( + TAG, + "Found " + + (results.size() - channelResult) + + " programs. Elapsed" + + " time for searching programs: " + + (SystemClock.elapsedRealtime() - time) + + "(msec)"); } return results; } } if (DEBUG) { - Log.d(TAG, "Found " + (results.size() - channelResult) + " programs. Elapsed time for" + - " searching programs: " + (SystemClock.elapsedRealtime() - time) + "(msec)"); + Log.d( + TAG, + "Found " + + (results.size() - channelResult) + + " programs. Elapsed time for" + + " searching programs: " + + (SystemClock.elapsedRealtime() - time) + + "(msec)"); } return results; } @@ -201,11 +232,9 @@ public class DataManagerSearch implements SearchInterface { return string != null && string.toLowerCase().contains(query); } - /** - * If query is matched to channel, {@code program} should be null. - */ - private void addResult(List<SearchResult> results, Set<Long> channelsFound, Channel channel, - Program program) { + /** If query is matched to channel, {@code program} should be null. */ + private void addResult( + List<SearchResult> results, Set<Long> channelsFound, Channel channel, Program program) { if (program == null) { program = mProgramDataManager.getCurrentProgram(channel.getId()); if (program != null && isRatingBlocked(program.getContentRatings())) { @@ -213,47 +242,58 @@ public class DataManagerSearch implements SearchInterface { } } - SearchResult result = new SearchResult(); + SearchResult.Builder result = SearchResult.builder(); long channelId = channel.getId(); - result.channelId = channelId; - result.channelNumber = channel.getDisplayNumber(); + result.setChannelId(channelId); + result.setChannelNumber(channel.getDisplayNumber()); if (program == null) { - result.title = channel.getDisplayName(); - result.description = channel.getDescription(); - result.imageUri = TvContract.buildChannelLogoUri(channelId).toString(); - result.intentAction = Intent.ACTION_VIEW; - result.intentData = buildIntentData(channelId); - result.contentType = Programs.CONTENT_ITEM_TYPE; - result.isLive = true; - result.progressPercentage = LocalSearchProvider.PROGRESS_PERCENTAGE_HIDE; + result.setTitle(channel.getDisplayName()); + result.setDescription(channel.getDescription()); + result.setImageUri(TvContract.buildChannelLogoUri(channelId).toString()); + result.setIntentAction(Intent.ACTION_VIEW); + result.setIntentData(buildIntentData(channelId)); + result.setContentType(Programs.CONTENT_ITEM_TYPE); + result.setIsLive(true); + result.setProgressPercentage(LocalSearchProvider.PROGRESS_PERCENTAGE_HIDE); } else { - result.title = program.getTitle(); - result.description = buildProgramDescription(channel.getDisplayNumber(), - channel.getDisplayName(), program.getStartTimeUtcMillis(), - program.getEndTimeUtcMillis()); - result.imageUri = program.getPosterArtUri(); - result.intentAction = Intent.ACTION_VIEW; - result.intentData = buildIntentData(channelId); - result.contentType = Programs.CONTENT_ITEM_TYPE; - result.isLive = true; - result.videoWidth = program.getVideoWidth(); - result.videoHeight = program.getVideoHeight(); - result.duration = program.getDurationMillis(); - result.progressPercentage = getProgressPercentage( - program.getStartTimeUtcMillis(), program.getEndTimeUtcMillis()); + result.setTitle(program.getTitle()); + result.setDescription( + buildProgramDescription( + channel.getDisplayNumber(), + channel.getDisplayName(), + program.getStartTimeUtcMillis(), + program.getEndTimeUtcMillis())); + result.setImageUri(program.getPosterArtUri()); + result.setIntentAction(Intent.ACTION_VIEW); + result.setIntentData(buildIntentData(channelId)); + result.setIntentExtraData(TvContract.buildProgramUri(program.getId()).toString()); + result.setContentType(Programs.CONTENT_ITEM_TYPE); + result.setIsLive(true); + result.setVideoWidth(program.getVideoWidth()); + result.setVideoHeight(program.getVideoHeight()); + result.setDuration(program.getDurationMillis()); + result.setProgressPercentage( + getProgressPercentage( + program.getStartTimeUtcMillis(), program.getEndTimeUtcMillis())); } if (DEBUG) { Log.d(TAG, "Add a result : channel=" + channel + " program=" + program); } - results.add(result); + results.add(result.build()); channelsFound.add(channel.getId()); } - private String buildProgramDescription(String channelNumber, String channelName, - long programStartUtcMillis, long programEndUtcMillis) { + private String buildProgramDescription( + String channelNumber, + String channelName, + long programStartUtcMillis, + long programEndUtcMillis) { return Utils.getDurationString(mContext, programStartUtcMillis, programEndUtcMillis, false) - + System.lineSeparator() + channelNumber + " " + channelName; + + System.lineSeparator() + + channelNumber + + " " + + channelName; } private int getProgressPercentage(long startUtcMillis, long endUtcMillis) { @@ -261,7 +301,7 @@ public class DataManagerSearch implements SearchInterface { if (startUtcMillis > current || endUtcMillis <= current) { return LocalSearchProvider.PROGRESS_PERCENTAGE_HIDE; } - return (int)(100 * (current - startUtcMillis) / (endUtcMillis - startUtcMillis)); + return (int) (100 * (current - startUtcMillis) / (endUtcMillis - startUtcMillis)); } private String buildIntentData(long channelId) { @@ -269,7 +309,8 @@ public class DataManagerSearch implements SearchInterface { } private boolean isRatingBlocked(TvContentRating[] ratings) { - if (ratings == null || ratings.length == 0 + if (ratings == null + || ratings.length == 0 || !mTvInputManager.isParentalControlsEnabled()) { return false; } |