• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.Button;
28 import android.widget.RadioGroup;
29 import android.widget.Toast;
30 
31 import java.util.Random;
32 
33 
34 /**
35  * Fragment that demonstrates how notifications with different visibility metadata differ on
36  * a lockscreen.
37  */
38 public class VisibilityMetadataFragment extends Fragment {
39 
40     private NotificationManager mNotificationManager;
41 
42     /**
43      * {@link RadioGroup} that has Visibility RadioButton in its children.
44      */
45     private RadioGroup mRadioGroup;
46 
47     /**
48      * Incremental Integer used for ID for notifications so that each notification will be
49      * treated differently.
50      */
51     private Integer mIncrementalNotificationId = Integer.valueOf(0);
52 
53     /**
54      * Button to show a notification.
55      */
56     private Button mShowNotificationButton;
57 
58     /**
59      * Use this factory method to create a new instance of
60      * this fragment using the provided parameters.
61      *
62      * @return A new instance of fragment NotificationFragment.
63      */
newInstance()64     public static VisibilityMetadataFragment newInstance() {
65         VisibilityMetadataFragment fragment = new VisibilityMetadataFragment();
66         fragment.setRetainInstance(true);
67         return fragment;
68     }
69 
VisibilityMetadataFragment()70     public VisibilityMetadataFragment() {
71         // Required empty public constructor
72     }
73 
74     @Override
onCreate(Bundle savedInstanceState)75     public void onCreate(Bundle savedInstanceState) {
76         super.onCreate(savedInstanceState);
77         mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
78                 .NOTIFICATION_SERVICE);
79     }
80 
81     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)82     public View onCreateView(LayoutInflater inflater, ViewGroup container,
83                              Bundle savedInstanceState) {
84         // Inflate the layout for this fragment
85         return inflater.inflate(R.layout.fragment_visibility_metadata_notification, container, false);
86     }
87 
88     @Override
onViewCreated(View view, Bundle savedInstanceState)89     public void onViewCreated(View view, Bundle savedInstanceState) {
90         super.onViewCreated(view, savedInstanceState);
91         mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
92         mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
93             @Override
94             public void onClick(View view) {
95                 NotificationVisibility visibility = getVisibilityFromSelectedRadio(mRadioGroup);
96                 showNotificationClicked(visibility);
97             }
98         });
99         mRadioGroup = (RadioGroup) view.findViewById(R.id.visibility_radio_group);
100     }
101 
102     /**
103      * Creates a new notification with a different visibility level.
104      *
105      * @param visibility The visibility of the notification to be created.
106      *
107      * @return A Notification instance.
108      */
109     //@VisibleForTesting
createNotification(NotificationVisibility visibility)110     Notification createNotification(NotificationVisibility visibility) {
111         Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
112                 .setContentTitle("Notification for Visibility metadata");
113 
114         notificationBuilder.setVisibility(visibility.getVisibility());
115         notificationBuilder.setContentText(String.format("Visibility : %s",
116                 visibility.getDescription()));
117         notificationBuilder.setSmallIcon(visibility.getNotificationIconId());
118 
119         return notificationBuilder.build();
120     }
121 
122     /**
123      * Returns a {@link NotificationVisibility} depending on which RadioButton in the radiogroup
124      * is selected.
125      *
126      * @param radiogroup The RadioGroup.
127      * @return The instance of {@link NotificationVisibility} corresponding to RadioButton.
128      */
getVisibilityFromSelectedRadio(RadioGroup radiogroup)129     private NotificationVisibility getVisibilityFromSelectedRadio(RadioGroup radiogroup) {
130         switch (radiogroup.getCheckedRadioButtonId()) {
131             case R.id.visibility_public_radio_button:
132                 return NotificationVisibility.PUBLIC;
133             case R.id.visibility_private_radio_button:
134                 return NotificationVisibility.PRIVATE;
135             case R.id.visibility_secret_radio_button:
136                 return NotificationVisibility.SECRET;
137             default:
138                 //If not selected, returns PUBLIC as default.
139                 return NotificationVisibility.PUBLIC;
140         }
141     }
142 
143     /**
144      * Invoked when {@link #mShowNotificationButton} is clicked.
145      * Creates a new notification with a different visibility level.
146      *
147      * @param visibility The visibility of the notification to be created.
148      */
showNotificationClicked(NotificationVisibility visibility)149     private void showNotificationClicked(NotificationVisibility visibility) {
150         // Assigns a unique (incremented) notification ID in order to treat each notification as a
151         // different one. This helps demonstrate how a notification with a different visibility
152         // level differs on the lockscreen.
153         mIncrementalNotificationId = new Integer(mIncrementalNotificationId + 1);
154         mNotificationManager.notify(mIncrementalNotificationId, createNotification(visibility));
155         Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
156     }
157 
158     /**
159      * Enum indicating possible visibility levels for notifications and related data(String
160      * representation of visibility levels, an icon ID to create a notification) to
161      * create a notification.
162      */
163     //@VisibleForTesting
164     static enum NotificationVisibility {
165         PUBLIC(Notification.VISIBILITY_PUBLIC, "Public", R.drawable.ic_public_notification),
166         PRIVATE(Notification.VISIBILITY_PRIVATE, "Private", R.drawable.ic_private_notification),
167         SECRET(Notification.VISIBILITY_SECRET, "Secret", R.drawable.ic_secret_notification);
168 
169         /**
170          * Visibility level of the notification.
171          */
172         private final int mVisibility;
173 
174         /**
175          * String representation of the visibility.
176          */
177         private final String mDescription;
178 
179         /**
180          * Id of an icon used for notifications created from the visibility.
181          */
182         private final int mNotificationIconId;
183 
NotificationVisibility(int visibility, String description, int notificationIconId)184         NotificationVisibility(int visibility, String description, int notificationIconId) {
185             mVisibility = visibility;
186             mDescription = description;
187             mNotificationIconId = notificationIconId;
188         }
189 
getVisibility()190         public int getVisibility() {
191             return mVisibility;
192         }
193 
getDescription()194         public String getDescription() {
195             return mDescription;
196         }
197 
getNotificationIconId()198         public int getNotificationIconId() {
199             return mNotificationIconId;
200         }
201     }
202 }
203