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