in Leanback/app/src/main/java/com/example/android/tvleanback/recommendation/UpdateRecommendationsService.java [67:123]
protected void onHandleIntent(Intent intent) {
// Generate recommendations, but only if recommendations are enabled
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if (!sharedPreferences.getBoolean(getString(R.string.pref_key_recommendations), true)) {
Log.d(TAG, "Recommendations disabled");
mNotifManager.cancelAll();
return;
}
Resources res = getResources();
int cardWidth = res.getDimensionPixelSize(R.dimen.card_width);
int cardHeight = res.getDimensionPixelSize(R.dimen.card_height);
ContentRecommendation.Builder builder = new ContentRecommendation.Builder()
.setBadgeIcon(R.drawable.videos_by_google_icon);
Cursor cursor = getContentResolver().query(
VideoContract.VideoEntry.CONTENT_URI,
null, // projection
null, // selection
null, // selection clause
"RANDOM() LIMIT " + MAX_RECOMMENDATIONS // sort order
);
if (cursor != null && cursor.moveToNext()) {
try {
do {
Video video = (Video) mVideoCursorMapper.convert(cursor);
int id = Long.valueOf(video.id).hashCode();
builder.setIdTag("Video" + id)
.setTitle(video.title)
.setText(getString(R.string.popular_header))
.setContentIntentData(ContentRecommendation.INTENT_TYPE_ACTIVITY,
buildPendingIntent(video, id), 0, null);
Bitmap bitmap = Glide.with(getApplication())
.asBitmap()
.load(video.cardImageUrl)
.submit(cardWidth, cardHeight) // Only use for synchronous .get()
.get();
builder.setContentImage(bitmap);
// Create an object holding all the information used to recommend the content.
ContentRecommendation rec = builder.build();
Notification notification = rec.getNotificationObject(getApplicationContext());
if (BuildConfig.DEBUG) Log.d(TAG, "Recommending video " + video.title);
// Recommend the content by publishing the notification.
mNotifManager.notify(id, notification);
} while (cursor.moveToNext());
} catch (InterruptedException | ExecutionException e) {
Log.e(TAG, "Could not create recommendation.", e);
} finally {
cursor.close();
}
}
}