• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.example.android.activenotifications;
2 /*
3 * Copyright 2015 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 import android.app.Notification;
19 import android.app.NotificationManager;
20 import android.app.PendingIntent;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.service.notification.StatusBarNotification;
25 import android.support.v4.app.Fragment;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.TextView;
30 
31 import com.example.android.common.logger.Log;
32 
33 /**
34  * A fragment that enables display of notifications.
35  */
36 public class ActiveNotificationFragment extends Fragment {
37 
38     private static final String TAG = "ActiveNotificationFragment";
39 
40     private NotificationManager mNotificationManager;
41     private TextView mNumberOfNotifications;
42 
43     // Every notification needs a unique ID otherwise the previous one would be overwritten.
44     private int mNotificationId = 0;
45     private PendingIntent mDeletePendingIntent;
46 
47     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)48     public View onCreateView(LayoutInflater inflater, ViewGroup container,
49             Bundle savedInstanceState) {
50         return inflater.inflate(R.layout.fragment_notification_builder, container, false);
51     }
52 
53     @Override
onResume()54     public void onResume() {
55         super.onResume();
56         updateNumberOfNotifications();
57     }
58 
59     @Override
onViewCreated(View view, Bundle savedInstanceState)60     public void onViewCreated(View view, Bundle savedInstanceState) {
61         super.onViewCreated(view, savedInstanceState);
62         mNotificationManager = (NotificationManager) getActivity().getSystemService(
63                 Context.NOTIFICATION_SERVICE);
64         mNumberOfNotifications = (TextView) view.findViewById(R.id.number_of_notifications);
65 
66         // Supply actions to the button that is displayed on screen.
67         View.OnClickListener onClickListener = new View.OnClickListener() {
68             @Override
69             public void onClick(View v) {
70                 switch (v.getId()) {
71                     case R.id.add_notification: {
72                         addNotificationAndReadNumber();
73                         break;
74                     }
75                 }
76             }
77         };
78         view.findViewById(R.id.add_notification).setOnClickListener(onClickListener);
79 
80         // [BEGIN create_pending_intent_for_deletion]
81         // Create a PendingIntent to be fired upon deletion of a Notification.
82         Intent deleteIntent = new Intent(ActiveNotificationActivity.ACTION_NOTIFICATION_DELETE);
83         mDeletePendingIntent = PendingIntent.getBroadcast(getActivity(),
84                 2323 /* requestCode */, deleteIntent, 0);
85         // [END create_pending_intent_for_deletion]
86     }
87 
88     /**
89      * Add a new {@link Notification} with sample data and send it to the system.
90      * Then read the current number of displayed notifications for this application.
91      */
addNotificationAndReadNumber()92     private void addNotificationAndReadNumber() {
93         // [BEGIN create_notification]
94         // Create a Notification and notify the system.
95         final Notification.Builder builder = new Notification.Builder(getActivity())
96                 .setSmallIcon(R.mipmap.ic_notification)
97                 .setContentTitle(getString(R.string.app_name))
98                 .setContentText(getString(R.string.sample_notification_content))
99                 .setAutoCancel(true)
100                 .setDeleteIntent(mDeletePendingIntent);
101 
102         final Notification notification = builder.build();
103         mNotificationManager.notify(++mNotificationId, notification);
104         // [END create_notification]
105         Log.i(TAG, "Add a notification");
106         updateNumberOfNotifications();
107     }
108 
109     /**
110      * Request the current number of notifications from the {@link NotificationManager} and
111      * display them to the user.
112      */
updateNumberOfNotifications()113     protected void updateNumberOfNotifications() {
114         // [BEGIN get_active_notifications]
115         // Query the currently displayed notifications.
116         final StatusBarNotification[] activeNotifications = mNotificationManager
117                 .getActiveNotifications();
118         // [END get_active_notifications]
119         final int numberOfNotifications = activeNotifications.length;
120         mNumberOfNotifications.setText(getString(R.string.active_notifications,
121                 numberOfNotifications));
122         Log.i(TAG, getString(R.string.active_notifications, numberOfNotifications));
123     }
124 }
125