| /* |
| * Copyright (C) 2014 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| * in compliance with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software distributed under the License |
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| * or implied. See the License for the specific language governing permissions and limitations under |
| * the License. |
| */ |
| |
| package com.example.android.leanback; |
| |
| import android.app.IntentService; |
| import android.app.PendingIntent; |
| import android.app.TaskStackBuilder; |
| import android.content.Intent; |
| import android.util.Log; |
| |
| import java.io.IOException; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| /* |
| * This class builds up to MAX_RECOMMMENDATIONS of recommendations and defines what happens |
| * when they're clicked from Recommendations seciton on Home screen |
| */ |
| public class UpdateRecommendationsService extends IntentService { |
| private static final String TAG = "UpdateRecommendationsService"; |
| private static final int MAX_RECOMMENDATIONS = 3; |
| |
| public UpdateRecommendationsService() { |
| super("RecommendationService"); |
| } |
| |
| @Override |
| protected void onHandleIntent(Intent intent) { |
| Log.d(TAG, "Updating recommendation cards"); |
| HashMap<String, List<Movie>> recommendations = VideoProvider.getMovieList(); |
| |
| int count = 0; |
| |
| try { |
| RecommendationBuilder builder = new RecommendationBuilder() |
| .setContext(getApplicationContext()) |
| .setSmallIcon(R.drawable.videos_by_google_icon); |
| |
| for (Map.Entry<String, List<Movie>> entry : recommendations.entrySet()) |
| { |
| for (int i = 0; i < entry.getValue().size(); i++) { |
| Movie movie = entry.getValue().get(i); |
| Log.d(TAG, "Recommendation - " + movie.getTitle()); |
| |
| builder.setBackground(movie.getCardImageUrl()) |
| .setId(count + 1) |
| .setPriority(MAX_RECOMMENDATIONS - count) |
| .setTitle(movie.getTitle()) |
| .setDescription(getString(R.string.popular_header)) |
| .setImage(movie.getCardImageUrl()) |
| .setIntent(buildPendingIntent(movie)) |
| .build(); |
| |
| if (++count >= MAX_RECOMMENDATIONS) { |
| break; |
| } |
| } |
| if (++count >= MAX_RECOMMENDATIONS) { |
| break; |
| } |
| } |
| } catch (IOException e) { |
| Log.e(TAG, "Unable to update recommendation", e); |
| } |
| } |
| |
| private PendingIntent buildPendingIntent(Movie movie) { |
| Intent detailsIntent = new Intent(this, DetailsActivity.class); |
| detailsIntent.putExtra("Movie", movie); |
| |
| TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); |
| stackBuilder.addParentStack(DetailsActivity.class); |
| stackBuilder.addNextIntent(detailsIntent); |
| // Ensure a unique PendingIntents, otherwise all recommendations end up with the same |
| // PendingIntent |
| detailsIntent.setAction(Long.toString(movie.getId())); |
| |
| PendingIntent intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); |
| return intent; |
| } |
| } |