summaryrefslogtreecommitdiff
path: root/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java
blob: b19f25c549ccee91cfc5839c71b59ebf104fb3cb (plain)
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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.networkrecommendation.notify;

import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static com.android.networkrecommendation.util.NotificationChannelUtil.CHANNEL_ID_NETWORK_AVAILABLE;

import android.app.Notification;
import android.app.Notification.Action;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import com.android.networkrecommendation.R;
import com.android.networkrecommendation.util.NotificationChannelUtil;

/** Helper class that creates notifications for {@link WifiNotificationController}. */
public class WifiNotificationHelper {
    private final Context mContext;

    public WifiNotificationHelper(Context context) {
        mContext = context;
    }

    /**
     * Creates the main open networks notification with two actions. "Options" link to the Wi-Fi
     * picker activity, and "Connect" prompts {@link WifiNotificationController} to connect to the
     * recommended network. Tapping on the content body connects to the recommended network and
     * opens the wifi picker
     */
    public Notification createMainNotification(WifiConfiguration config) {
        PendingIntent allNetworksIntent =
                PendingIntent.getBroadcast(
                        mContext,
                        0,
                        new Intent(WifiNotificationController.ACTION_PICK_WIFI_NETWORK),
                        FLAG_UPDATE_CURRENT);
        Action allNetworksAction =
                new Action.Builder(
                                null /* icon */,
                                mContext.getText(R.string.wifi_available_action_all_networks),
                                allNetworksIntent)
                        .build();
        PendingIntent connectIntent =
                PendingIntent.getBroadcast(
                        mContext,
                        0,
                        new Intent(
                                WifiNotificationController.ACTION_CONNECT_TO_RECOMMENDED_NETWORK),
                        FLAG_UPDATE_CURRENT);
        Action connectAction =
                new Action.Builder(
                                null /* icon */,
                                mContext.getText(R.string.wifi_available_action_connect),
                                connectIntent)
                        .build();
        PendingIntent connectAndOpenPickerIntent =
                PendingIntent.getBroadcast(
                        mContext,
                        0,
                        new Intent(
                                WifiNotificationController
                                        .ACTION_CONNECT_TO_RECOMMENDED_NETWORK_AND_OPEN_SETTINGS),
                        FLAG_UPDATE_CURRENT);
        return createNotificationBuilder(R.string.wifi_available_title, config.SSID)
                .setContentIntent(connectAndOpenPickerIntent)
                .addAction(connectAction)
                .addAction(allNetworksAction)
                .build();
    }

    /**
     * Creates the notification that indicates the controller is attempting to connect to the
     * recommended network.
     */
    public Notification createConnectingNotification(WifiConfiguration config) {
        return createNotificationBuilder(R.string.wifi_available_title_connecting, config.SSID)
                .setProgress(0 /* max */, 0 /* progress */, true /* indeterminate */)
                .build();
    }

    /**
     * Creates the notification that indicates the controller successfully connected to the
     * recommended network.
     */
    public Notification createConnectedNotification(WifiConfiguration config) {
        return createNotificationBuilder(R.string.wifi_available_title_connected, config.SSID)
                .setAutoCancel(true)
                .build();
    }

    /**
     * Creates the notification that indicates the controller failed to connect to the recommended
     * network. Tapping this notification opens the wifi picker.
     */
    public Notification createFailedToConnectNotification() {
        PendingIntent openWifiPickerAfterFailure =
                PendingIntent.getBroadcast(
                        mContext,
                        0,
                        new Intent(
                                WifiNotificationController
                                        .ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE),
                        FLAG_UPDATE_CURRENT);
        return createNotificationBuilder(
                        R.string.wifi_available_title_failed,
                        mContext.getString(R.string.wifi_available_content_failed))
                .setContentIntent(openWifiPickerAfterFailure)
                .setAutoCancel(true)
                .build();
    }

    private Notification.Builder createNotificationBuilder(int titleRid, String content) {
        CharSequence title = mContext.getText(titleRid);
        PendingIntent deleteIntent =
                PendingIntent.getBroadcast(
                        mContext,
                        0,
                        new Intent(WifiNotificationController.ACTION_NOTIFICATION_DELETED),
                        FLAG_UPDATE_CURRENT);
        int smallIcon = R.drawable.ic_signal_wifi_statusbar_not_connected;
        Notification.Builder builder =
                new Notification.Builder(mContext)
                        .setDeleteIntent(deleteIntent)
                        .setSmallIcon(smallIcon)
                        .setTicker(title)
                        .setContentTitle(title)
                        .setColor(mContext.getColor(R.color.color_tint))
                        .setContentText(content)
                        .setShowWhen(false)
                        .setLocalOnly(true)
                        .addExtras(getOverrideLabelExtras());
        return NotificationChannelUtil.setChannel(builder, CHANNEL_ID_NETWORK_AVAILABLE);
    }

    private Bundle getOverrideLabelExtras() {
        Bundle extras = new Bundle();
        extras.putString(
                Notification.EXTRA_SUBSTITUTE_APP_NAME,
                mContext.getString(R.string.notification_channel_group_name));
        return extras;
    }
}