Home | History | Annotate | Download | only in notify
      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.notify;
     17 
     18 import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
     19 import static com.android.networkrecommendation.util.NotificationChannelUtil.CHANNEL_ID_NETWORK_AVAILABLE;
     20 
     21 import android.app.Notification;
     22 import android.app.Notification.Action;
     23 import android.app.PendingIntent;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.net.wifi.WifiConfiguration;
     27 import android.os.Bundle;
     28 import com.android.networkrecommendation.R;
     29 import com.android.networkrecommendation.util.NotificationChannelUtil;
     30 
     31 /** Helper class that creates notifications for {@link WifiNotificationController}. */
     32 public class WifiNotificationHelper {
     33     private final Context mContext;
     34 
     35     public WifiNotificationHelper(Context context) {
     36         mContext = context;
     37     }
     38 
     39     /**
     40      * Creates the main open networks notification with two actions. "Options" link to the Wi-Fi
     41      * picker activity, and "Connect" prompts {@link WifiNotificationController} to connect to the
     42      * recommended network. Tapping on the content body connects to the recommended network and
     43      * opens the wifi picker
     44      */
     45     public Notification createMainNotification(WifiConfiguration config) {
     46         PendingIntent allNetworksIntent =
     47                 PendingIntent.getBroadcast(
     48                         mContext,
     49                         0,
     50                         new Intent(WifiNotificationController.ACTION_PICK_WIFI_NETWORK),
     51                         FLAG_UPDATE_CURRENT);
     52         Action allNetworksAction =
     53                 new Action.Builder(
     54                                 null /* icon */,
     55                                 mContext.getText(R.string.wifi_available_action_all_networks),
     56                                 allNetworksIntent)
     57                         .build();
     58         PendingIntent connectIntent =
     59                 PendingIntent.getBroadcast(
     60                         mContext,
     61                         0,
     62                         new Intent(
     63                                 WifiNotificationController.ACTION_CONNECT_TO_RECOMMENDED_NETWORK),
     64                         FLAG_UPDATE_CURRENT);
     65         Action connectAction =
     66                 new Action.Builder(
     67                                 null /* icon */,
     68                                 mContext.getText(R.string.wifi_available_action_connect),
     69                                 connectIntent)
     70                         .build();
     71         PendingIntent connectAndOpenPickerIntent =
     72                 PendingIntent.getBroadcast(
     73                         mContext,
     74                         0,
     75                         new Intent(
     76                                 WifiNotificationController
     77                                         .ACTION_CONNECT_TO_RECOMMENDED_NETWORK_AND_OPEN_SETTINGS),
     78                         FLAG_UPDATE_CURRENT);
     79         return createNotificationBuilder(R.string.wifi_available_title, config.SSID)
     80                 .setContentIntent(connectAndOpenPickerIntent)
     81                 .addAction(connectAction)
     82                 .addAction(allNetworksAction)
     83                 .build();
     84     }
     85 
     86     /**
     87      * Creates the notification that indicates the controller is attempting to connect to the
     88      * recommended network.
     89      */
     90     public Notification createConnectingNotification(WifiConfiguration config) {
     91         return createNotificationBuilder(R.string.wifi_available_title_connecting, config.SSID)
     92                 .setProgress(0 /* max */, 0 /* progress */, true /* indeterminate */)
     93                 .build();
     94     }
     95 
     96     /**
     97      * Creates the notification that indicates the controller successfully connected to the
     98      * recommended network.
     99      */
    100     public Notification createConnectedNotification(WifiConfiguration config) {
    101         return createNotificationBuilder(R.string.wifi_available_title_connected, config.SSID)
    102                 .setAutoCancel(true)
    103                 .build();
    104     }
    105 
    106     /**
    107      * Creates the notification that indicates the controller failed to connect to the recommended
    108      * network. Tapping this notification opens the wifi picker.
    109      */
    110     public Notification createFailedToConnectNotification() {
    111         PendingIntent openWifiPickerAfterFailure =
    112                 PendingIntent.getBroadcast(
    113                         mContext,
    114                         0,
    115                         new Intent(
    116                                 WifiNotificationController
    117                                         .ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE),
    118                         FLAG_UPDATE_CURRENT);
    119         return createNotificationBuilder(
    120                         R.string.wifi_available_title_failed,
    121                         mContext.getString(R.string.wifi_available_content_failed))
    122                 .setContentIntent(openWifiPickerAfterFailure)
    123                 .setAutoCancel(true)
    124                 .build();
    125     }
    126 
    127     private Notification.Builder createNotificationBuilder(int titleRid, String content) {
    128         CharSequence title = mContext.getText(titleRid);
    129         PendingIntent deleteIntent =
    130                 PendingIntent.getBroadcast(
    131                         mContext,
    132                         0,
    133                         new Intent(WifiNotificationController.ACTION_NOTIFICATION_DELETED),
    134                         FLAG_UPDATE_CURRENT);
    135         int smallIcon = R.drawable.ic_signal_wifi_statusbar_not_connected;
    136         Notification.Builder builder =
    137                 new Notification.Builder(mContext)
    138                         .setDeleteIntent(deleteIntent)
    139                         .setSmallIcon(smallIcon)
    140                         .setTicker(title)
    141                         .setContentTitle(title)
    142                         .setColor(mContext.getColor(R.color.color_tint))
    143                         .setContentText(content)
    144                         .setShowWhen(false)
    145                         .setLocalOnly(true)
    146                         .addExtras(getOverrideLabelExtras());
    147         return NotificationChannelUtil.setChannel(builder, CHANNEL_ID_NETWORK_AVAILABLE);
    148     }
    149 
    150     private Bundle getOverrideLabelExtras() {
    151         Bundle extras = new Bundle();
    152         extras.putString(
    153                 Notification.EXTRA_SUBSTITUTE_APP_NAME,
    154                 mContext.getString(R.string.notification_channel_group_name));
    155         return extras;
    156     }
    157 }
    158