1 /*
2  * Copyright (C) 2007 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 
17 package com.example.android.apis.app;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22 
23 import android.app.Notification;
24 import android.app.NotificationManager;
25 import android.app.PendingIntent;
26 import android.app.Service;
27 import android.content.Intent;
28 import android.os.Binder;
29 import android.os.ConditionVariable;
30 import android.os.IBinder;
31 import android.os.Parcel;
32 import android.os.RemoteException;
33 
34 /**
35  * This is an example of service that will update its status bar balloon
36  * every 5 seconds for a minute.
37  *
38  */
39 public class NotifyingService extends Service {
40 
41     // Use a layout id for a unique identifier
42     private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
43 
44     // variable which controls the notification thread
45     private ConditionVariable mCondition;
46 
47     @Override
onCreate()48     public void onCreate() {
49         mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
50 
51         // Start up the thread running the service.  Note that we create a
52         // separate thread because the service normally runs in the process's
53         // main thread, which we don't want to block.
54         Thread notifyingThread = new Thread(null, mTask, "NotifyingService");
55         mCondition = new ConditionVariable(false);
56         notifyingThread.start();
57     }
58 
59     @Override
onDestroy()60     public void onDestroy() {
61         // Cancel the persistent notification.
62         mNM.cancel(MOOD_NOTIFICATIONS);
63         // Stop the thread from generating further notifications
64         mCondition.open();
65     }
66 
67     private Runnable mTask = new Runnable() {
68         public void run() {
69             for (int i = 0; i < 4; ++i) {
70                 showNotification(R.drawable.stat_happy,
71                         R.string.status_bar_notifications_happy_message);
72                 if (mCondition.block(5 * 1000))
73                     break;
74                 showNotification(R.drawable.stat_neutral,
75                         R.string.status_bar_notifications_ok_message);
76                 if (mCondition.block(5 * 1000))
77                     break;
78                 showNotification(R.drawable.stat_sad,
79                         R.string.status_bar_notifications_sad_message);
80                 if (mCondition.block(5 * 1000))
81                     break;
82             }
83             // Done with our work...  stop the service!
84             NotifyingService.this.stopSelf();
85         }
86     };
87 
88     @Override
onBind(Intent intent)89     public IBinder onBind(Intent intent) {
90         return mBinder;
91     }
92 
showNotification(int moodId, int textId)93     private void showNotification(int moodId, int textId) {
94         // In this sample, we'll use the same text for the ticker and the expanded notification
95         CharSequence text = getText(textId);
96 
97         // The PendingIntent to launch our activity if the user selects this notification
98         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
99                 new Intent(this, NotifyingController.class), 0);
100 
101         // Set the icon and timestamp.
102         // Note that in this example, we do not set the tickerText.  We update the icon enough that
103         // it is distracting to show the ticker text every time it changes.  We strongly suggest
104         // that you do this as well.  (Think of of the "New hardware found" or "Network connection
105         // changed" messages that always pop up)
106         // Set the info for the views that show in the notification panel.
107         Notification notification = new Notification.Builder(this)
108                 .setSmallIcon(moodId)
109                 .setWhen(System.currentTimeMillis())
110                 .setContentTitle(getText(R.string.status_bar_notifications_mood_title))
111                 .setContentText(text)  // the contents of the entry
112                 .setContentIntent(contentIntent)  // The intent to send when the entry is clicked
113                 .build();
114 
115         // Send the notification.
116         // We use a layout id because it is a unique number.  We use it later to cancel.
117         mNM.notify(MOOD_NOTIFICATIONS, notification);
118     }
119 
120     // This is the object that receives interactions from clients.  See
121     // RemoteService for a more complete example.
122     private final IBinder mBinder = new Binder() {
123         @Override
124         protected boolean onTransact(int code, Parcel data, Parcel reply,
125                 int flags) throws RemoteException {
126             return super.onTransact(code, data, reply, flags);
127         }
128     };
129 
130     private NotificationManager mNM;
131 }
132