• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.networkrecommendation.util;
17 
18 import android.app.Notification.Builder;
19 import android.app.NotificationChannel;
20 import android.app.NotificationChannelGroup;
21 import android.app.NotificationManager;
22 import android.content.Context;
23 import com.android.networkrecommendation.R;
24 
25 /**
26  * Util class for managing {@link android.app.NotificationChannel}s for our notifications.
27  * TODO(netrec): b/36486429 Add tests for this class once relevant class constructors are supported
28  * for Robolectric.
29  */
30 public class NotificationChannelUtil {
31 
32     private static final String CHANNEL_GROUP_ID =
33             "com.android.networkrecommendation.Notifications.WifiMessageGroup";
34     public static final String CHANNEL_ID_NETWORK_AVAILABLE =
35             "com.android.networkrecommendation.Notifications.WifiMessageGroup.NetworkAvailableChannel";
36     public static final String CHANNEL_ID_WAKEUP =
37             "com.android.networkrecommendation.Notifications.WifiMessageGroup.WakeupChannel";
38 
39     /** Configures the {@link android.app.NotificationChannel}s for our module. */
configureNotificationChannels( NotificationManager notificationManager, Context context)40     public static void configureNotificationChannels(
41             NotificationManager notificationManager, Context context) {
42         NotificationChannelGroup notificationChannelGroup =
43                 new NotificationChannelGroup(
44                         CHANNEL_GROUP_ID,
45                         context.getString(R.string.notification_channel_group_name));
46         notificationManager.createNotificationChannelGroup(notificationChannelGroup);
47 
48         NotificationChannel networkAvailableChannel =
49                 new NotificationChannel(
50                         CHANNEL_ID_NETWORK_AVAILABLE,
51                         context.getString(R.string.notification_channel_network_available),
52                         NotificationManager.IMPORTANCE_LOW);
53         networkAvailableChannel.setGroup(CHANNEL_GROUP_ID);
54         notificationManager.createNotificationChannel(networkAvailableChannel);
55 
56         NotificationChannel wakeupChannel =
57                 new NotificationChannel(
58                         CHANNEL_ID_WAKEUP,
59                         context.getString(R.string.notification_channel_wakeup_name),
60                         NotificationManager.IMPORTANCE_LOW);
61         wakeupChannel.setGroup(CHANNEL_GROUP_ID);
62         notificationManager.createNotificationChannel(wakeupChannel);
63     }
64 
65     /** Wraps Notification.Builder.setChannel. */
setChannel(Builder notificationBuilder, String channelId)66     public static Builder setChannel(Builder notificationBuilder, String channelId) {
67         return notificationBuilder.setChannelId(channelId);
68     }
69 }
70