1 /*
2  * Copyright (C) 2020 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.android.car.rotaryplayground;
18 
19 import android.app.Notification;
20 import android.app.NotificationChannel;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 
29 import androidx.annotation.Nullable;
30 import androidx.fragment.app.Fragment;
31 
32 public class HeadsUpNotificationFragment extends Fragment {
33     private static final String NOTIFICATION_CHANNEL_ID = "rotary_notification";
34     private static final int NOTIFICATION_ID = 1;
35 
36     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)37     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
38             @Nullable Bundle savedInstanceState) {
39         View view = inflater.inflate(R.layout.rotary_notification, container, false);
40         NotificationManager notificationManager =
41                 getContext().getSystemService(NotificationManager.class);
42         notificationManager.createNotificationChannel(
43                 new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Rotary Playground",
44                         NotificationManager.IMPORTANCE_HIGH));
45         view.findViewById(R.id.add_notification_button1).setOnClickListener(
46                 v -> notificationManager.notify(NOTIFICATION_ID, createNotification()));
47         view.findViewById(R.id.clear_notification_button1).setOnClickListener(
48                 v -> notificationManager.cancel(NOTIFICATION_ID));
49         view.findViewById(R.id.add_notification_button2).setOnClickListener(
50                 v -> notificationManager.notify(NOTIFICATION_ID, createNotification()));
51         view.findViewById(R.id.clear_notification_button2).setOnClickListener(
52                 v -> notificationManager.cancel(NOTIFICATION_ID));
53 
54         return view;
55     }
56 
57     /**
58      * Creates a notification with CATEGORY_CALL in a channel with IMPORTANCE_HIGH. This will
59      * produce a heads-up notification even for non-system apps that aren't privileged and aren't
60      * signed with the platform key. The notification includes three actions which appear as buttons
61      * in the HUN.
62      */
createNotification()63     private Notification createNotification() {
64         Intent intent = new Intent(getContext(), RotaryActivity.class);
65         PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
66         return new Notification.Builder(getContext(), NOTIFICATION_CHANNEL_ID)
67                 .setContentTitle("Example heads-up notification")
68                 .setContentText("Try nudging up to HUN")
69                 .setSmallIcon(R.drawable.ic_launcher)
70                 .addAction(new Notification.Action.Builder(null, "Action1", pendingIntent).build())
71                 .addAction(new Notification.Action.Builder(null, "Action2", pendingIntent).build())
72                 .addAction(new Notification.Action.Builder(null, "Action3", pendingIntent).build())
73                 .setColor(getContext().getColor(android.R.color.holo_red_light))
74                 .setCategory(Notification.CATEGORY_CALL)
75                 .build();
76     }
77 }
78