ConnectToNetworkNotificationBuilder.java revision 20361dcf2d45262685c8dec334c11f0b2c987702
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 17package com.android.server.wifi; 18 19import android.app.Notification; 20import android.app.PendingIntent; 21import android.content.Context; 22import android.content.Intent; 23import android.content.res.Resources; 24import android.net.wifi.ScanResult; 25 26import com.android.internal.R; 27import com.android.internal.notification.SystemNotificationChannels; 28 29/** 30 * Helper to create notifications for {@link OpenNetworkNotifier}. 31 */ 32public class ConnectToNetworkNotificationBuilder { 33 34 /** Intent when user dismissed the "Connect to Network" notification. */ 35 public static final String ACTION_USER_DISMISSED_NOTIFICATION = 36 "com.android.server.wifi.ConnectToNetworkNotification.USER_DISMISSED_NOTIFICATION"; 37 38 /** Intent when user tapped the "Connect to Network" notification. */ 39 public static final String ACTION_USER_TAPPED_CONTENT = 40 "com.android.server.wifi.ConnectToNetworkNotification.USER_TAPPED_CONTENT"; 41 42 /** Intent when user tapped action button to connect to recommended network. */ 43 public static final String ACTION_CONNECT_TO_NETWORK = 44 "com.android.server.wifi.ConnectToNetworkNotification.CONNECT_TO_NETWORK"; 45 46 /** Intent when user tapped action button to open Wi-Fi Settings. */ 47 public static final String ACTION_PICK_WIFI_NETWORK = 48 "com.android.server.wifi.ConnectToNetworkNotification.PICK_WIFI_NETWORK"; 49 50 /** Intent when user tapped "Failed to connect" notification to open Wi-Fi Settings. */ 51 public static final String ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE = 52 "com.android.server.wifi.ConnectToNetworkNotification.PICK_NETWORK_AFTER_FAILURE"; 53 54 private Context mContext; 55 private Resources mResources; 56 private FrameworkFacade mFrameworkFacade; 57 58 public ConnectToNetworkNotificationBuilder( 59 Context context, 60 FrameworkFacade framework) { 61 mContext = context; 62 mResources = context.getResources(); 63 mFrameworkFacade = framework; 64 } 65 66 /** 67 * Creates the connect to network notification that alerts users of a recommended connectable 68 * network. 69 * 70 * @param numNetworks Number of available open networks nearby 71 */ 72 public Notification createConnectToNetworkNotification(int numNetworks) { 73 74 CharSequence title = mResources.getQuantityText( 75 com.android.internal.R.plurals.wifi_available, numNetworks); 76 CharSequence content = mResources.getQuantityText( 77 com.android.internal.R.plurals.wifi_available_detailed, numNetworks); 78 79 return createNotificationBuilder(title, content) 80 .setContentIntent(getPrivateBroadcast(ACTION_USER_TAPPED_CONTENT)) 81 .build(); 82 } 83 84 /** 85 * Creates the notification that indicates the controller is attempting to connect to the 86 * recommended network. 87 * 88 * @param network The network to be recommended 89 */ 90 public Notification createNetworkConnectingNotification(ScanResult network) { 91 return createNotificationBuilder( 92 mContext.getText(R.string.wifi_available_title_connecting), network.SSID) 93 .setProgress(0 /* max */, 0 /* progress */, true /* indeterminate */) 94 .build(); 95 } 96 97 /** 98 * Creates the notification that indicates the controller successfully connected to the 99 * recommended network. 100 * 101 * @param network The network to be recommended 102 */ 103 public Notification createNetworkConnectedNotification(ScanResult network) { 104 return createNotificationBuilder( 105 mContext.getText(R.string.wifi_available_title_connected), network.SSID) 106 .build(); 107 } 108 109 /** 110 * Creates the notification that indicates the controller failed to connect to the recommended 111 * network. Tapping this notification opens the wifi picker. 112 */ 113 public Notification createNetworkFailedNotification() { 114 return createNotificationBuilder( 115 mContext.getText(R.string.wifi_available_title_failed_to_connect), 116 mContext.getText(R.string.wifi_available_content_failed_to_connect)) 117 .setContentIntent( 118 getPrivateBroadcast(ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE)) 119 .build(); 120 } 121 122 private Notification.Builder createNotificationBuilder( 123 CharSequence title, CharSequence content) { 124 return mFrameworkFacade.makeNotificationBuilder(mContext, 125 SystemNotificationChannels.NETWORK_AVAILABLE) 126 .setSmallIcon(R.drawable.stat_notify_wifi_in_range) 127 .setAutoCancel(true) 128 .setTicker(title) 129 .setContentTitle(title) 130 .setContentText(content) 131 .setDeleteIntent(getPrivateBroadcast(ACTION_USER_DISMISSED_NOTIFICATION)) 132 .setShowWhen(false) 133 .setLocalOnly(true) 134 .setColor(mResources.getColor(R.color.system_notification_accent_color, 135 mContext.getTheme())); 136 } 137 138 private PendingIntent getPrivateBroadcast(String action) { 139 Intent intent = new Intent(action).setPackage("android"); 140 return mFrameworkFacade.getBroadcast( 141 mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 142 } 143} 144