NotificationService.java revision 181d72c27e8560d7b3e84e08b28af27a3db31a26
1/*
2 * Copyright (C) 2013 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
17package com.android.example.notificationshowcase;
18
19import android.app.IntentService;
20import android.app.Notification;
21import android.app.NotificationManager;
22import android.app.PendingIntent;
23import android.content.Context;
24import android.content.Intent;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.Typeface;
28import android.graphics.drawable.BitmapDrawable;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
31import android.support.v4.app.NotificationCompat;
32import android.text.SpannableString;
33import android.text.style.StyleSpan;
34import android.view.View;
35
36import java.util.ArrayList;
37
38public class NotificationService extends IntentService {
39
40    private static final String TAG = "NotificationService";
41
42    public static final String ACTION_CREATE = "create";
43    public static final int NOTIFICATION_ID = 31338;
44
45    public NotificationService() {
46        super(TAG);
47    }
48
49    public NotificationService(String name) {
50        super(name);
51    }
52
53    private static Bitmap getBitmap(Context context, int resId) {
54        int largeIconWidth = (int) context.getResources()
55                .getDimension(R.dimen.notification_large_icon_width);
56        int largeIconHeight = (int) context.getResources()
57                .getDimension(R.dimen.notification_large_icon_height);
58        Drawable d = context.getResources().getDrawable(resId);
59        Bitmap b = Bitmap.createBitmap(largeIconWidth, largeIconHeight, Bitmap.Config.ARGB_8888);
60        Canvas c = new Canvas(b);
61        d.setBounds(0, 0, largeIconWidth, largeIconHeight);
62        d.draw(c);
63        return b;
64    }
65
66    private static PendingIntent makeEmailIntent(Context context, String who) {
67        final Intent intent = new Intent(android.content.Intent.ACTION_SENDTO,
68                Uri.parse("mailto:" + who));
69        return PendingIntent.getActivity(
70                context, 0, intent,
71                PendingIntent.FLAG_CANCEL_CURRENT);
72    }
73
74    public static Notification makeBigTextNotification(Context context, int update, int id,
75            long when) {
76        String addendum = update > 0 ? "(updated) " : "";
77        String longSmsText = "Hey, looks like\nI'm getting kicked out of this conference" +
78                " room";
79        if (update > 1) {
80            longSmsText += ", so stay in the hangout and I'll rejoin in about 5-10 minutes" +
81                    ". If you don't see me, assume I got pulled into another meeting. And" +
82                    " now \u2026 I have to find my shoes.";
83        }
84        if (update > 2) {
85            when = System.currentTimeMillis();
86        }
87        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
88        bigTextStyle.bigText(addendum + longSmsText);
89        Notification bigText = new NotificationCompat.Builder(context)
90                .setContentTitle(addendum + "Mike Cleron")
91                .setContentIntent(ToastService.getPendingIntent(context, "Clicked on bigText"))
92                .setContentText(addendum + longSmsText)
93                .setTicker(addendum + "Mike Cleron: " + longSmsText)
94                .setWhen(when)
95                .setLargeIcon(getBitmap(context, R.drawable.bucket))
96                .setPriority(NotificationCompat.PRIORITY_HIGH)
97                .setDefaults(Notification.DEFAULT_SOUND)
98                .addAction(R.drawable.ic_media_next,
99                        "update: " + update,
100                        UpdateService.getPendingIntent(context, update + 1, id, when))
101                .setSmallIcon(R.drawable.stat_notify_talk_text)
102                .setStyle(bigTextStyle)
103                .build();
104        return bigText;
105    }
106
107    public static Notification makeUploadNotification(Context context, int progress, long when) {
108        Notification uploadNotification = new NotificationCompat.Builder(context)
109                .setContentTitle("File Upload")
110                .setContentText("foo.txt")
111                .setPriority(NotificationCompat.PRIORITY_MIN)
112                .setContentIntent(ToastService.getPendingIntent(context, "Clicked on Upload"))
113                .setDeleteIntent(ProgressService.getSilencePendingIntent(context))
114                .setWhen(when)
115                .setSmallIcon(R.drawable.ic_menu_upload)
116                .setProgress(100, Math.min(progress, 100), false)
117                .build();
118        return uploadNotification;
119    }
120
121    @Override
122    protected void onHandleIntent(Intent intent) {
123        ArrayList<Notification> mNotifications = new ArrayList<Notification>();
124        NotificationManager noMa =
125                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
126
127        int bigtextId = mNotifications.size();
128        mNotifications.add(makeBigTextNotification(this, 0, bigtextId, System.currentTimeMillis()));
129
130        int uploadId = mNotifications.size();
131        long uploadWhen = System.currentTimeMillis();
132        mNotifications.add(makeUploadNotification(this, 0, uploadWhen));
133
134        int phoneId = mNotifications.size();
135        final PendingIntent fullscreenIntent = FullScreenActivity.getPendingIntent(this, phoneId);
136        Notification phoneCall = new NotificationCompat.Builder(this)
137                .setContentTitle("Incoming call")
138                .setContentText("Matias Duarte")
139                .setLargeIcon(getBitmap(this, R.drawable.matias_hed))
140                .setSmallIcon(R.drawable.stat_sys_phone_call)
141                .setDefaults(Notification.DEFAULT_SOUND)
142                .setPriority(NotificationCompat.PRIORITY_MAX)
143                .setContentIntent(fullscreenIntent)
144                .setFullScreenIntent(fullscreenIntent, true)
145                .addAction(R.drawable.ic_dial_action_call, "Answer",
146                        PhoneService.getPendingIntent(this, phoneId, PhoneService.ACTION_ANSWER))
147                .addAction(R.drawable.ic_end_call, "Ignore",
148                        PhoneService.getPendingIntent(this, phoneId, PhoneService.ACTION_IGNORE))
149                .setOngoing(true)
150                .build();
151        mNotifications.add(phoneCall);
152
153        mNotifications.add(new NotificationCompat.Builder(this)
154                .setContentTitle("Stopwatch PRO")
155                .setContentText("Counting up")
156                .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Stopwatch"))
157                .setSmallIcon(R.drawable.stat_notify_alarm)
158                .setUsesChronometer(true)
159                .build());
160
161        mNotifications.add(new NotificationCompat.Builder(this)
162                .setContentTitle("J Planning")
163                .setContentText("The Botcave")
164                .setWhen(System.currentTimeMillis())
165                .setSmallIcon(R.drawable.stat_notify_calendar)
166                .setContentIntent(ToastService.getPendingIntent(this, "Clicked on calendar event"))
167                .setContentInfo("7PM")
168                .addAction(R.drawable.stat_notify_snooze, "+10 min",
169                        ToastService.getPendingIntent(this, "snoozed 10 min"))
170                .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour",
171                        ToastService.getPendingIntent(this, "snoozed 1 hr"))
172                .addAction(R.drawable.stat_notify_email, "Email",
173                        makeEmailIntent(this,
174                                "gabec@example.com,mcleron@example.com,dsandler@example.com"))
175                .build());
176
177        BitmapDrawable d =
178                (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway);
179        mNotifications.add(new NotificationCompat.BigPictureStyle(
180                new NotificationCompat.Builder(this)
181                        .setContentTitle("Romain Guy")
182                        .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area "
183                                + "store last week but I had not been able to try it in the field "
184                                + "until tonight. After a few days of rain the sky finally cleared "
185                                + "up. Rockaway Beach did not disappoint and I was finally able to "
186                                + "see what my new camera feels like when shooting landscapes.")
187                        .setSmallIcon(R.drawable.ic_stat_gplus)
188                        .setContentIntent(
189                                ToastService.getPendingIntent(this, "Clicked on bigPicture"))
190                        .setLargeIcon(getBitmap(this, R.drawable.romainguy_hed))
191                        .addAction(R.drawable.add, "Add to Gallery",
192                                ToastService.getPendingIntent(this, "added! (just kidding)"))
193                        .setSubText("talk rocks!"))
194                .bigPicture(d.getBitmap())
195                .build());
196
197        // Note: this may conflict with real email notifications
198        StyleSpan bold = new StyleSpan(Typeface.BOLD);
199        SpannableString line1 = new SpannableString("Alice: hey there!");
200        line1.setSpan(bold, 0, 5, 0);
201        SpannableString line2 = new SpannableString("Bob: hi there!");
202        line2.setSpan(bold, 0, 3, 0);
203        SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!");
204        line3.setSpan(bold, 0, 7, 0);
205        mNotifications.add(new NotificationCompat.InboxStyle(
206                new NotificationCompat.Builder(this)
207                        .setContentTitle("24 new messages")
208                        .setContentText("You have mail!")
209                        .setSubText("test.hugo2@gmail.com")
210                        .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Email"))
211                        .setSmallIcon(R.drawable.stat_notify_email))
212                .setSummaryText("+21 more")
213                .addLine(line1)
214                .addLine(line2)
215                .addLine(line3)
216                .build());
217
218        mNotifications.add(new NotificationCompat.Builder(this)
219                .setContentTitle("Twitter")
220                .setContentText("New mentions")
221                .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Twitter"))
222                .setSmallIcon(R.drawable.twitter_icon)
223                .setNumber(15)
224                .setPriority(NotificationCompat.PRIORITY_LOW)
225                .build());
226
227
228        for (int i=0; i<mNotifications.size(); i++) {
229            noMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
230        }
231
232        ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0);
233    }
234}
235