1*90c8c64dSAndroid Build Coastguard Worker /*
2*90c8c64dSAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*90c8c64dSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
4*90c8c64dSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
5*90c8c64dSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
6*90c8c64dSAndroid Build Coastguard Worker  *
7*90c8c64dSAndroid Build Coastguard Worker  *       http://www.apache.org/licenses/LICENSE-2.0
8*90c8c64dSAndroid Build Coastguard Worker  *
9*90c8c64dSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
10*90c8c64dSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
11*90c8c64dSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*90c8c64dSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
13*90c8c64dSAndroid Build Coastguard Worker  * limitations under the License.
14*90c8c64dSAndroid Build Coastguard Worker  */
15*90c8c64dSAndroid Build Coastguard Worker 
16*90c8c64dSAndroid Build Coastguard Worker package com.example.android.customnotifications;
17*90c8c64dSAndroid Build Coastguard Worker 
18*90c8c64dSAndroid Build Coastguard Worker import android.app.Activity;
19*90c8c64dSAndroid Build Coastguard Worker import android.app.Notification;
20*90c8c64dSAndroid Build Coastguard Worker import android.app.NotificationManager;
21*90c8c64dSAndroid Build Coastguard Worker import android.app.PendingIntent;
22*90c8c64dSAndroid Build Coastguard Worker import android.content.Intent;
23*90c8c64dSAndroid Build Coastguard Worker import android.os.Build;
24*90c8c64dSAndroid Build Coastguard Worker import android.os.Bundle;
25*90c8c64dSAndroid Build Coastguard Worker import android.support.v4.app.NotificationCompat;
26*90c8c64dSAndroid Build Coastguard Worker import android.view.View;
27*90c8c64dSAndroid Build Coastguard Worker import android.widget.RemoteViews;
28*90c8c64dSAndroid Build Coastguard Worker 
29*90c8c64dSAndroid Build Coastguard Worker import java.text.DateFormat;
30*90c8c64dSAndroid Build Coastguard Worker import java.util.Date;
31*90c8c64dSAndroid Build Coastguard Worker 
32*90c8c64dSAndroid Build Coastguard Worker public class MainActivity extends Activity {
33*90c8c64dSAndroid Build Coastguard Worker     /**
34*90c8c64dSAndroid Build Coastguard Worker      * This sample demonstrates notifications with custom content views.
35*90c8c64dSAndroid Build Coastguard Worker      *
36*90c8c64dSAndroid Build Coastguard Worker      * <p>On API level 16 and above a big content view is also defined that is used for the
37*90c8c64dSAndroid Build Coastguard Worker      * 'expanded' notification. The notification is created by the NotificationCompat.Builder.
38*90c8c64dSAndroid Build Coastguard Worker      * The expanded content view is set directly on the {@link android.app.Notification} once it has been build.
39*90c8c64dSAndroid Build Coastguard Worker      * (See {@link android.app.Notification#bigContentView}.) </p>
40*90c8c64dSAndroid Build Coastguard Worker      *
41*90c8c64dSAndroid Build Coastguard Worker      * <p>The content views are inflated as {@link android.widget.RemoteViews} directly from their XML layout
42*90c8c64dSAndroid Build Coastguard Worker      * definitions using {@link android.widget.RemoteViews#RemoteViews(String, int)}.</p>
43*90c8c64dSAndroid Build Coastguard Worker      */
createNotification()44*90c8c64dSAndroid Build Coastguard Worker     private void createNotification() {
45*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE(notificationCompat)
46*90c8c64dSAndroid Build Coastguard Worker         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
47*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(notificationCompat)
48*90c8c64dSAndroid Build Coastguard Worker 
49*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE(intent)
50*90c8c64dSAndroid Build Coastguard Worker         //Create Intent to launch this Activity again if the notification is clicked.
51*90c8c64dSAndroid Build Coastguard Worker         Intent i = new Intent(this, MainActivity.class);
52*90c8c64dSAndroid Build Coastguard Worker         i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
53*90c8c64dSAndroid Build Coastguard Worker         PendingIntent intent = PendingIntent.getActivity(this, 0, i,
54*90c8c64dSAndroid Build Coastguard Worker                 PendingIntent.FLAG_UPDATE_CURRENT);
55*90c8c64dSAndroid Build Coastguard Worker         builder.setContentIntent(intent);
56*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(intent)
57*90c8c64dSAndroid Build Coastguard Worker 
58*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE(ticker)
59*90c8c64dSAndroid Build Coastguard Worker         // Sets the ticker text
60*90c8c64dSAndroid Build Coastguard Worker         builder.setTicker(getResources().getString(R.string.custom_notification));
61*90c8c64dSAndroid Build Coastguard Worker 
62*90c8c64dSAndroid Build Coastguard Worker         // Sets the small icon for the ticker
63*90c8c64dSAndroid Build Coastguard Worker         builder.setSmallIcon(R.drawable.ic_stat_custom);
64*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(ticker)
65*90c8c64dSAndroid Build Coastguard Worker 
66*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE(buildNotification)
67*90c8c64dSAndroid Build Coastguard Worker         // Cancel the notification when clicked
68*90c8c64dSAndroid Build Coastguard Worker         builder.setAutoCancel(true);
69*90c8c64dSAndroid Build Coastguard Worker 
70*90c8c64dSAndroid Build Coastguard Worker         // Build the notification
71*90c8c64dSAndroid Build Coastguard Worker         Notification notification = builder.build();
72*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(buildNotification)
73*90c8c64dSAndroid Build Coastguard Worker 
74*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE(customLayout)
75*90c8c64dSAndroid Build Coastguard Worker         // Inflate the notification layout as RemoteViews
76*90c8c64dSAndroid Build Coastguard Worker         RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
77*90c8c64dSAndroid Build Coastguard Worker 
78*90c8c64dSAndroid Build Coastguard Worker         // Set text on a TextView in the RemoteViews programmatically.
79*90c8c64dSAndroid Build Coastguard Worker         final String time = DateFormat.getTimeInstance().format(new Date()).toString();
80*90c8c64dSAndroid Build Coastguard Worker         final String text = getResources().getString(R.string.collapsed, time);
81*90c8c64dSAndroid Build Coastguard Worker         contentView.setTextViewText(R.id.textView, text);
82*90c8c64dSAndroid Build Coastguard Worker 
83*90c8c64dSAndroid Build Coastguard Worker         /* Workaround: Need to set the content view here directly on the notification.
84*90c8c64dSAndroid Build Coastguard Worker          * NotificationCompatBuilder contains a bug that prevents this from working on platform
85*90c8c64dSAndroid Build Coastguard Worker          * versions HoneyComb.
86*90c8c64dSAndroid Build Coastguard Worker          * See https://code.google.com/p/android/issues/detail?id=30495
87*90c8c64dSAndroid Build Coastguard Worker          */
88*90c8c64dSAndroid Build Coastguard Worker         notification.contentView = contentView;
89*90c8c64dSAndroid Build Coastguard Worker 
90*90c8c64dSAndroid Build Coastguard Worker         // Add a big content view to the notification if supported.
91*90c8c64dSAndroid Build Coastguard Worker         // Support for expanded notifications was added in API level 16.
92*90c8c64dSAndroid Build Coastguard Worker         // (The normal contentView is shown when the notification is collapsed, when expanded the
93*90c8c64dSAndroid Build Coastguard Worker         // big content view set here is displayed.)
94*90c8c64dSAndroid Build Coastguard Worker         if (Build.VERSION.SDK_INT >= 16) {
95*90c8c64dSAndroid Build Coastguard Worker             // Inflate and set the layout for the expanded notification view
96*90c8c64dSAndroid Build Coastguard Worker             RemoteViews expandedView =
97*90c8c64dSAndroid Build Coastguard Worker                     new RemoteViews(getPackageName(), R.layout.notification_expanded);
98*90c8c64dSAndroid Build Coastguard Worker             notification.bigContentView = expandedView;
99*90c8c64dSAndroid Build Coastguard Worker         }
100*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(customLayout)
101*90c8c64dSAndroid Build Coastguard Worker 
102*90c8c64dSAndroid Build Coastguard Worker         // START_INCLUDE(notify)
103*90c8c64dSAndroid Build Coastguard Worker         // Use the NotificationManager to show the notification
104*90c8c64dSAndroid Build Coastguard Worker         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
105*90c8c64dSAndroid Build Coastguard Worker         nm.notify(0, notification);
106*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(notify)
107*90c8c64dSAndroid Build Coastguard Worker     }
108*90c8c64dSAndroid Build Coastguard Worker 
109*90c8c64dSAndroid Build Coastguard Worker     @Override
onCreate(Bundle savedInstanceState)110*90c8c64dSAndroid Build Coastguard Worker     protected void onCreate(Bundle savedInstanceState) {
111*90c8c64dSAndroid Build Coastguard Worker         super.onCreate(savedInstanceState);
112*90c8c64dSAndroid Build Coastguard Worker         setContentView(R.layout.sample_main);
113*90c8c64dSAndroid Build Coastguard Worker     }
114*90c8c64dSAndroid Build Coastguard Worker 
115*90c8c64dSAndroid Build Coastguard Worker     /**
116*90c8c64dSAndroid Build Coastguard Worker      * Create and show a notification with a custom layout.
117*90c8c64dSAndroid Build Coastguard Worker      * This callback is defined through the 'onClick' attribute of the
118*90c8c64dSAndroid Build Coastguard Worker      * 'Show Notification' button in the XML layout.
119*90c8c64dSAndroid Build Coastguard Worker      *
120*90c8c64dSAndroid Build Coastguard Worker      * @param v
121*90c8c64dSAndroid Build Coastguard Worker      */
showNotificationClicked(View v)122*90c8c64dSAndroid Build Coastguard Worker     public void showNotificationClicked(View v) {
123*90c8c64dSAndroid Build Coastguard Worker         createNotification();
124*90c8c64dSAndroid Build Coastguard Worker     }
125*90c8c64dSAndroid Build Coastguard Worker }
126