• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.example.android.leanback;
16 
17 import android.app.Notification;
18 import android.app.NotificationManager;
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.graphics.Bitmap;
22 import android.os.Bundle;
23 import android.support.v4.app.NotificationCompat;
24 import android.util.Log;
25 
26 import com.squareup.picasso.Picasso;
27 
28 import java.io.IOException;
29 
30 /*
31  * This class builds recommendations as notifications with videos as inputs.
32  */
33 public class RecommendationBuilder {
34     private static final String TAG = "RecommendationBuilder";
35 
36     private static int CARD_WIDTH = 313;
37     private static int CARD_HEIGHT = 176;
38 
39     public static final String EXTRA_BACKGROUND_IMAGE_URL = "background_image_url";
40     private Context mContext;
41     private NotificationManager mNotificationManager;
42 
43     private int mId;
44     private int mPriority;
45     private int mSmallIcon;
46     private String mTitle;
47     private String mDescription;
48     private String mImageUri;
49     private String mBackgroundUri;
50     private PendingIntent mIntent;
51 
RecommendationBuilder()52     public RecommendationBuilder() {
53     }
54 
setContext(Context context)55     public RecommendationBuilder setContext(Context context) {
56         mContext = context;
57         return this;
58     }
59 
setId(int id)60     public RecommendationBuilder setId(int id) {
61         mId = id;
62         return this;
63     }
64 
setPriority(int priority)65     public RecommendationBuilder setPriority(int priority) {
66         mPriority = priority;
67         return this;
68     }
69 
setTitle(String title)70     public RecommendationBuilder setTitle(String title) {
71         mTitle = title;
72         return this;
73     }
74 
setDescription(String description)75     public RecommendationBuilder setDescription(String description) {
76         mDescription = description;
77         return this;
78     }
79 
setImage(String uri)80     public RecommendationBuilder setImage(String uri) {
81         mImageUri = uri;
82         return this;
83     }
84 
setBackground(String uri)85     public RecommendationBuilder setBackground(String uri) {
86         mBackgroundUri = uri;
87         return this;
88     }
89 
setIntent(PendingIntent intent)90     public RecommendationBuilder setIntent(PendingIntent intent) {
91         mIntent = intent;
92         return this;
93     }
94 
setSmallIcon(int resourceId)95     public RecommendationBuilder setSmallIcon(int resourceId) {
96         mSmallIcon = resourceId;
97         return this;
98     }
99 
build()100     public Notification build() throws IOException {
101 
102         Log.d(TAG, "Building notification - " + this.toString());
103 
104         if (mNotificationManager == null) {
105             mNotificationManager = (NotificationManager) mContext
106                     .getSystemService(Context.NOTIFICATION_SERVICE);
107         }
108 
109         Bundle extras = new Bundle();
110         if (mBackgroundUri != null) {
111             extras.putString(EXTRA_BACKGROUND_IMAGE_URL, mBackgroundUri);
112         }
113 
114         Bitmap image = Picasso.with(mContext)
115                 .load(mImageUri)
116                 .resize(Utils.dpToPx(CARD_WIDTH, mContext), Utils.dpToPx(CARD_HEIGHT, mContext))
117                 .get();
118 
119         Notification notification = new NotificationCompat.BigPictureStyle(
120                 new NotificationCompat.Builder(mContext)
121                         .setContentTitle(mTitle)
122                         .setContentText(mDescription)
123                         .setPriority(mPriority)
124                         .setLocalOnly(true)
125                         .setOngoing(true)
126                         .setColor(mContext.getResources().getColor(R.color.fastlane_background))
127                         // .setCategory(Notification.CATEGORY_RECOMMENDATION)
128                         .setCategory("recommendation")
129                         .setLargeIcon(image)
130                         .setSmallIcon(mSmallIcon)
131                         .setContentIntent(mIntent)
132                         .setExtras(extras))
133                 .build();
134 
135         mNotificationManager.notify(mId, notification);
136         mNotificationManager = null;
137         return notification;
138     }
139 
140     @Override
toString()141     public String toString() {
142         return "RecommendationBuilder{" +
143                 ", mId=" + mId +
144                 ", mPriority=" + mPriority +
145                 ", mSmallIcon=" + mSmallIcon +
146                 ", mTitle='" + mTitle + '\'' +
147                 ", mDescription='" + mDescription + '\'' +
148                 ", mImageUri='" + mImageUri + '\'' +
149                 ", mBackgroundUri='" + mBackgroundUri + '\'' +
150                 ", mIntent=" + mIntent +
151                 '}';
152     }
153 }
154