summaryrefslogtreecommitdiffstats
blob: 7a26f932bc9995869c4143f82d96fbaad6cb696e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.google.android.car.kitchensink.notification;

import static android.security.KeyStore.getApplicationContext;

import android.annotation.Nullable;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Person;
import android.app.RemoteInput;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.fragment.app.Fragment;

import com.google.android.car.kitchensink.KitchenSinkActivity;
import com.google.android.car.kitchensink.R;

/**
 * Test fragment that can send all sorts of notifications.
 */
public class NotificationFragment extends Fragment {
    private static final String CHANNEL_ID_1 = "kitchensink.channel1";
    private static final String CHANNEL_ID_2 = "kitchensink.channel2";
    private static final String CHANNEL_ID_3 = "kitchensink.channel3";
    private static final String CHANNEL_ID_4 = "kitchensink.channel4";
    private static final String CHANNEL_ID_5 = "kitchensink.channel5";
    private static final String CHANNEL_ID_6 = "kitchensink.channel6";

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.notification_fragment, container, false);
        Button cancelAllButton = view.findViewById(R.id.cancel_all_button);
        Button importanceHighButton = view.findViewById(R.id.importance_high_button);
        Button importanceHighButton2 = view.findViewById(R.id.importance_high_button_2);
        Button importanceLowButton = view.findViewById(R.id.importance_low_button);
        Button importanceMinButton = view.findViewById(R.id.importance_min_button);
        Button importanceDefaultButton = view.findViewById(R.id.importance_default_button);
        Button ongoingButton = view.findViewById(R.id.ongoing_button);
        Button messageButton = view.findViewById(R.id.category_message_button);

        NotificationManager manager =
                (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

        // cancel all button
        cancelAllButton.setOnClickListener(v -> manager.cancelAll());

        // importance high notifications
        NotificationChannel highImportanceChannel =
                new NotificationChannel(
                        CHANNEL_ID_1, "Importance High", NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(highImportanceChannel);

        importanceHighButton.setOnClickListener(v -> {

            Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1)
                    .setContentTitle("Importance High")
                    .setContentText("blah")
                    .setSmallIcon(R.drawable.car_ic_mode)
                    .build();
            manager.notify(1, notification);
        });

        importanceHighButton2.setOnClickListener(v -> {
            Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1)
                    .setContentTitle("Importance High 2")
                    .setContentText("blah blah blah")
                    .setSmallIcon(R.drawable.car_ic_mode)
                    .build();
            manager.notify(2, notification);
        });

        // importance default
        importanceDefaultButton.setOnClickListener(v -> {
            NotificationChannel channel =
                    new NotificationChannel(
                            CHANNEL_ID_3,
                            "Importance Default",
                            NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);

            Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_3)
                    .setContentTitle("Importance Default")
                    .setSmallIcon(R.drawable.car_ic_mode)
                    .build();
            manager.notify(4, notification);
        });

        // importance low
        importanceLowButton.setOnClickListener(v -> {
            NotificationChannel channel =
                    new NotificationChannel(
                            CHANNEL_ID_4, "Importance Low", NotificationManager.IMPORTANCE_LOW);
            manager.createNotificationChannel(channel);

            Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_4)
                    .setContentTitle("Importance Low")
                    .setContentText("low low low")
                    .setSmallIcon(R.drawable.car_ic_mode)
                    .build();
            manager.notify(5, notification);
        });

        // importance min
        importanceMinButton.setOnClickListener(v -> {
            NotificationChannel channel =
                    new NotificationChannel(
                            CHANNEL_ID_5, "Importance Min", NotificationManager.IMPORTANCE_MIN);
            manager.createNotificationChannel(channel);

            Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_5)
                    .setContentTitle("Importance Min")
                    .setContentText("min min min")
                    .setSmallIcon(R.drawable.car_ic_mode)
                    .build();
            manager.notify(6, notification);
        });

        // ongoing
        ongoingButton.setOnClickListener(v -> {
            NotificationChannel channel =
                    new NotificationChannel(
                            CHANNEL_ID_6, "Ongoing", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);

            Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_6)
                    .setContentTitle("Playing music or something")
                    .setSmallIcon(R.drawable.car_ic_mode)
                    .setOngoing(true)
                    .build();
            manager.notify(7, notification);
        });

        // category message
        messageButton.setOnClickListener(v -> {
            NotificationChannel channel =
                    new NotificationChannel(
                            CHANNEL_ID_2, "Message", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);

            Intent intent = new Intent(getActivity(), KitchenSinkActivity.class);
            PendingIntent readIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);

            RemoteInput remoteInput = new RemoteInput.Builder("voice reply").build();
            PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(),
                    12345,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            Person personJohn = new Person.Builder().setName("John Doe").build();
            Person personJane = new Person.Builder().setName("Jane Roe").build();
            Notification.MessagingStyle messagingStyle =
                    new Notification.MessagingStyle(personJohn)
                            .setConversationTitle("Whassup")
                            .addHistoricMessage(
                                    new Notification.MessagingStyle.Message(
                                            "historic message",
                                            System.currentTimeMillis() - 3600,
                                            personJohn))
                            .addMessage(new Notification.MessagingStyle.Message(
                                    "message", System.currentTimeMillis(), personJane));

            Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_2)
                    .setContentTitle("Message from someone")
                    .setContentText("hi")
                    .setCategory(Notification.CATEGORY_MESSAGE)
                    .setSmallIcon(R.drawable.car_ic_mode)
                    .setStyle(messagingStyle)
                    .setAutoCancel(true)
                    .addAction(
                            new Notification.Action.Builder(null, "read", readIntent).build())
                    .addAction(
                            new Notification.Action.Builder(null, "reply", replyIntent)
                                    .addRemoteInput(remoteInput).build())
                    .extend(new Notification.CarExtender().setColor(R.color.car_red_500))
                    .build();
            manager.notify(3, notification);
        });

        return view;
    }
}