Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 1 | package com.google.android.car.kitchensink.notification; |
| 2 | |
| 3 | import static android.security.KeyStore.getApplicationContext; |
| 4 | |
| 5 | import android.annotation.Nullable; |
| 6 | import android.app.Notification; |
| 7 | import android.app.NotificationChannel; |
| 8 | import android.app.NotificationManager; |
| 9 | import android.app.PendingIntent; |
Selim Cinek | 890675e | 2018-04-05 14:48:26 -0700 | [diff] [blame] | 10 | import android.app.Person; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 11 | import android.app.RemoteInput; |
| 12 | import android.content.Context; |
| 13 | import android.content.Intent; |
| 14 | import android.os.Bundle; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 15 | import android.view.LayoutInflater; |
| 16 | import android.view.View; |
| 17 | import android.view.ViewGroup; |
| 18 | import android.widget.Button; |
| 19 | |
Anthony Chen | d12cd77 | 2018-04-16 16:20:44 -0700 | [diff] [blame^] | 20 | import androidx.fragment.app.Fragment; |
| 21 | |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 22 | import com.google.android.car.kitchensink.KitchenSinkActivity; |
| 23 | import com.google.android.car.kitchensink.R; |
| 24 | |
| 25 | /** |
| 26 | * Test fragment that can send all sorts of notifications. |
| 27 | */ |
| 28 | public class NotificationFragment extends Fragment { |
| 29 | private static final String CHANNEL_ID_1 = "kitchensink.channel1"; |
| 30 | private static final String CHANNEL_ID_2 = "kitchensink.channel2"; |
| 31 | private static final String CHANNEL_ID_3 = "kitchensink.channel3"; |
| 32 | private static final String CHANNEL_ID_4 = "kitchensink.channel4"; |
| 33 | private static final String CHANNEL_ID_5 = "kitchensink.channel5"; |
| 34 | private static final String CHANNEL_ID_6 = "kitchensink.channel6"; |
| 35 | |
| 36 | @Override |
| 37 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
| 38 | @Nullable Bundle savedInstanceState) { |
| 39 | View view = inflater.inflate(R.layout.notification_fragment, container, false); |
| 40 | Button cancelAllButton = view.findViewById(R.id.cancel_all_button); |
| 41 | Button importanceHighButton = view.findViewById(R.id.importance_high_button); |
| 42 | Button importanceHighButton2 = view.findViewById(R.id.importance_high_button_2); |
| 43 | Button importanceLowButton = view.findViewById(R.id.importance_low_button); |
| 44 | Button importanceMinButton = view.findViewById(R.id.importance_min_button); |
| 45 | Button importanceDefaultButton = view.findViewById(R.id.importance_default_button); |
| 46 | Button ongoingButton = view.findViewById(R.id.ongoing_button); |
| 47 | Button messageButton = view.findViewById(R.id.category_message_button); |
| 48 | |
| 49 | NotificationManager manager = |
| 50 | (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE); |
| 51 | |
| 52 | // cancel all button |
| 53 | cancelAllButton.setOnClickListener(v -> manager.cancelAll()); |
| 54 | |
| 55 | // importance high notifications |
| 56 | NotificationChannel highImportanceChannel = |
| 57 | new NotificationChannel( |
| 58 | CHANNEL_ID_1, "Importance High", NotificationManager.IMPORTANCE_HIGH); |
| 59 | manager.createNotificationChannel(highImportanceChannel); |
| 60 | |
| 61 | importanceHighButton.setOnClickListener(v -> { |
| 62 | |
| 63 | Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1) |
| 64 | .setContentTitle("Importance High") |
| 65 | .setContentText("blah") |
| 66 | .setSmallIcon(R.drawable.car_ic_mode) |
| 67 | .build(); |
| 68 | manager.notify(1, notification); |
| 69 | }); |
| 70 | |
| 71 | importanceHighButton2.setOnClickListener(v -> { |
| 72 | Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1) |
| 73 | .setContentTitle("Importance High 2") |
| 74 | .setContentText("blah blah blah") |
| 75 | .setSmallIcon(R.drawable.car_ic_mode) |
| 76 | .build(); |
| 77 | manager.notify(2, notification); |
| 78 | }); |
| 79 | |
| 80 | // importance default |
| 81 | importanceDefaultButton.setOnClickListener(v -> { |
| 82 | NotificationChannel channel = |
| 83 | new NotificationChannel( |
| 84 | CHANNEL_ID_3, |
| 85 | "Importance Default", |
| 86 | NotificationManager.IMPORTANCE_DEFAULT); |
| 87 | manager.createNotificationChannel(channel); |
| 88 | |
| 89 | Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_3) |
| 90 | .setContentTitle("Importance Default") |
| 91 | .setSmallIcon(R.drawable.car_ic_mode) |
| 92 | .build(); |
| 93 | manager.notify(4, notification); |
| 94 | }); |
| 95 | |
| 96 | // importance low |
| 97 | importanceLowButton.setOnClickListener(v -> { |
| 98 | NotificationChannel channel = |
| 99 | new NotificationChannel( |
| 100 | CHANNEL_ID_4, "Importance Low", NotificationManager.IMPORTANCE_LOW); |
| 101 | manager.createNotificationChannel(channel); |
| 102 | |
| 103 | Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_4) |
| 104 | .setContentTitle("Importance Low") |
| 105 | .setContentText("low low low") |
| 106 | .setSmallIcon(R.drawable.car_ic_mode) |
| 107 | .build(); |
| 108 | manager.notify(5, notification); |
| 109 | }); |
| 110 | |
| 111 | // importance min |
| 112 | importanceMinButton.setOnClickListener(v -> { |
| 113 | NotificationChannel channel = |
| 114 | new NotificationChannel( |
| 115 | CHANNEL_ID_5, "Importance Min", NotificationManager.IMPORTANCE_MIN); |
| 116 | manager.createNotificationChannel(channel); |
| 117 | |
| 118 | Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_5) |
| 119 | .setContentTitle("Importance Min") |
| 120 | .setContentText("min min min") |
| 121 | .setSmallIcon(R.drawable.car_ic_mode) |
| 122 | .build(); |
| 123 | manager.notify(6, notification); |
| 124 | }); |
| 125 | |
| 126 | // ongoing |
| 127 | ongoingButton.setOnClickListener(v -> { |
| 128 | NotificationChannel channel = |
| 129 | new NotificationChannel( |
| 130 | CHANNEL_ID_6, "Ongoing", NotificationManager.IMPORTANCE_DEFAULT); |
| 131 | manager.createNotificationChannel(channel); |
| 132 | |
| 133 | Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_6) |
| 134 | .setContentTitle("Playing music or something") |
| 135 | .setSmallIcon(R.drawable.car_ic_mode) |
| 136 | .setOngoing(true) |
| 137 | .build(); |
| 138 | manager.notify(7, notification); |
| 139 | }); |
| 140 | |
| 141 | // category message |
| 142 | messageButton.setOnClickListener(v -> { |
| 143 | NotificationChannel channel = |
| 144 | new NotificationChannel( |
| 145 | CHANNEL_ID_2, "Message", NotificationManager.IMPORTANCE_HIGH); |
| 146 | manager.createNotificationChannel(channel); |
| 147 | |
| 148 | Intent intent = new Intent(getActivity(), KitchenSinkActivity.class); |
| 149 | PendingIntent readIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0); |
| 150 | |
| 151 | RemoteInput remoteInput = new RemoteInput.Builder("voice reply").build(); |
| 152 | PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(), |
| 153 | 12345, |
| 154 | intent, |
| 155 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 156 | |
Selim Cinek | 890675e | 2018-04-05 14:48:26 -0700 | [diff] [blame] | 157 | Person personJohn = new Person.Builder().setName("John Doe").build(); |
| 158 | Person personJane = new Person.Builder().setName("Jane Roe").build(); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 159 | Notification.MessagingStyle messagingStyle = |
| 160 | new Notification.MessagingStyle(personJohn) |
| 161 | .setConversationTitle("Whassup") |
| 162 | .addHistoricMessage( |
| 163 | new Notification.MessagingStyle.Message( |
| 164 | "historic message", |
| 165 | System.currentTimeMillis() - 3600, |
| 166 | personJohn)) |
| 167 | .addMessage(new Notification.MessagingStyle.Message( |
| 168 | "message", System.currentTimeMillis(), personJane)); |
| 169 | |
| 170 | Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_2) |
| 171 | .setContentTitle("Message from someone") |
| 172 | .setContentText("hi") |
| 173 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 174 | .setSmallIcon(R.drawable.car_ic_mode) |
| 175 | .setStyle(messagingStyle) |
| 176 | .setAutoCancel(true) |
| 177 | .addAction( |
| 178 | new Notification.Action.Builder(null, "read", readIntent).build()) |
| 179 | .addAction( |
| 180 | new Notification.Action.Builder(null, "reply", replyIntent) |
| 181 | .addRemoteInput(remoteInput).build()) |
| 182 | .extend(new Notification.CarExtender().setColor(R.color.car_red_500)) |
| 183 | .build(); |
| 184 | manager.notify(3, notification); |
| 185 | }); |
| 186 | |
| 187 | return view; |
| 188 | } |
| 189 | } |