Home | History | Annotate | Download | only in com.example.android.lnotifications
      1 /*
      2 * Copyright 2014 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.lnotifications;
     18 
     19 import android.app.Fragment;
     20 import android.app.Notification;
     21 import android.app.NotificationManager;
     22 import android.app.PendingIntent;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.os.Bundle;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.Button;
     30 import android.widget.CheckBox;
     31 import android.widget.Toast;
     32 
     33 /**
     34  * Fragment that demonstrates options for displaying Heads-Up Notifications.
     35  */
     36 public class HeadsUpNotificationFragment extends Fragment {
     37 
     38     /**
     39      * NotificationId used for the notifications from this Fragment.
     40      */
     41     private static final int NOTIFICATION_ID = 1;
     42 
     43     private NotificationManager mNotificationManager;
     44 
     45     /**
     46      * Button to show a notification.
     47      */
     48     private Button mShowNotificationButton;
     49 
     50     /**
     51      * If checked, notifications that this Fragment creates will be displayed as Heads-Up
     52      * Notifications.
     53      */
     54     private CheckBox mUseHeadsUpCheckbox;
     55 
     56     /**
     57      * Use this factory method to create a new instance of
     58      * this fragment using the provided parameters.
     59      *
     60      * @return A new instance of fragment NotificationFragment.
     61      */
     62     public static HeadsUpNotificationFragment newInstance() {
     63         HeadsUpNotificationFragment fragment = new HeadsUpNotificationFragment();
     64         fragment.setRetainInstance(true);
     65         return fragment;
     66     }
     67 
     68     public HeadsUpNotificationFragment() {
     69         // Required empty public constructor
     70     }
     71 
     72     @Override
     73     public void onCreate(Bundle savedInstanceState) {
     74         super.onCreate(savedInstanceState);
     75         mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
     76                 .NOTIFICATION_SERVICE);
     77     }
     78 
     79     @Override
     80     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     81                              Bundle savedInstanceState) {
     82         // Inflate the layout for this fragment
     83         return inflater.inflate(R.layout.fragment_heads_up_notification, container, false);
     84     }
     85 
     86     @Override
     87     public void onViewCreated(View view, Bundle savedInstanceState) {
     88         super.onViewCreated(view, savedInstanceState);
     89         mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
     90         mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
     91             @Override
     92             public void onClick(View view) {
     93                 mNotificationManager.notify(NOTIFICATION_ID, createNotification(
     94                         mUseHeadsUpCheckbox.isChecked()));
     95                 Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
     96             }
     97         });
     98         mUseHeadsUpCheckbox = (CheckBox) view.findViewById(R.id.use_heads_up_checkbox);
     99     }
    100 
    101     /**
    102      * Creates a new notification depending on the argument.
    103      *
    104      * @param makeHeadsUpNotification A boolean value to indicating whether a notification will be
    105      *                                created as a heads-up notification or not.
    106      *                                <ul>
    107      *                                <li>true : Creates a heads-up notification.</li>
    108      *                                <li>false : Creates a non-heads-up notification.</li>
    109      *                                </ul>
    110      *
    111      * @return A Notification instance.
    112      */
    113     //@VisibleForTesting
    114     Notification createNotification(boolean makeHeadsUpNotification) {
    115         Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
    116                 .setSmallIcon(R.drawable.ic_launcher_notification)
    117                 .setPriority(Notification.PRIORITY_DEFAULT)
    118                 .setCategory(Notification.CATEGORY_MESSAGE)
    119                 .setContentTitle("Sample Notification")
    120                 .setContentText("This is a normal notification.");
    121         if (makeHeadsUpNotification) {
    122             Intent push = new Intent();
    123             push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    124             push.setClass(getActivity(), LNotificationActivity.class);
    125 
    126             PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0,
    127                     push, PendingIntent.FLAG_CANCEL_CURRENT);
    128             notificationBuilder
    129                     .setContentText("Heads-Up Notification on Android L or above.")
    130                     .setFullScreenIntent(fullScreenPendingIntent, true);
    131         }
    132         return notificationBuilder.build();
    133     }
    134 }
    135