1 /*
2  * Copyright (C) 2020 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 
17 package com.android.server.wifi;
18 
19 import android.app.Notification;
20 import android.app.PendingIntent;
21 import android.content.Intent;
22 import android.graphics.drawable.Icon;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
26 import com.android.wifi.resources.R;
27 
28 /**
29  * Helper class for SoftApManager to generator notification.
30  */
31 public class SoftApNotifier {
32     private static final String TAG = "SoftApNotifier";
33 
34     @VisibleForTesting
35     public static final String ACTION_HOTSPOT_PREFERENCES =
36             "com.android.settings.WIFI_TETHER_SETTINGS";
37 
38     @VisibleForTesting
39     public static final int NOTIFICATION_ID_SOFTAP_AUTO_DISABLED =
40             SystemMessage.NOTE_SOFTAP_AUTO_DISABLED;
41 
42     private final WifiContext mContext;
43     private final FrameworkFacade mFrameworkFacade;
44     private final WifiNotificationManager mNotificationManager;
45 
SoftApNotifier(WifiContext context, FrameworkFacade framework, WifiNotificationManager wifiNotificationManager)46     public SoftApNotifier(WifiContext context, FrameworkFacade framework,
47             WifiNotificationManager wifiNotificationManager) {
48         mContext = context;
49         mFrameworkFacade = framework;
50         mNotificationManager = wifiNotificationManager;
51     }
52 
53     /**
54      * Show notification to notify user softap disable because auto shutdown timeout expired.
55      */
showSoftApShutdownTimeoutExpiredNotification()56     public void showSoftApShutdownTimeoutExpiredNotification() {
57         mNotificationManager.notify(NOTIFICATION_ID_SOFTAP_AUTO_DISABLED,
58                 buildSoftApShutdownTimeoutExpiredNotification());
59     }
60 
61     /**
62      * Dismiss notification which used to notify user softap disable because auto shutdown
63      * timeout expired.
64      */
dismissSoftApShutdownTimeoutExpiredNotification()65     public void dismissSoftApShutdownTimeoutExpiredNotification() {
66         mNotificationManager.cancel(NOTIFICATION_ID_SOFTAP_AUTO_DISABLED);
67     }
68 
buildSoftApShutdownTimeoutExpiredNotification()69     private Notification buildSoftApShutdownTimeoutExpiredNotification() {
70         String title = mContext.getResources().getString(
71                 R.string.wifi_softap_auto_shutdown_timeout_expired_title);
72         String contentSummary = mContext.getResources().getString(
73                 R.string.wifi_softap_auto_shutdown_timeout_expired_summary);
74 
75         return mFrameworkFacade.makeNotificationBuilder(mContext,
76                 WifiService.NOTIFICATION_NETWORK_STATUS)
77                 .setSmallIcon(Icon.createWithResource(mContext.getWifiOverlayApkPkgName(),
78                         R.drawable.ic_wifi_settings))
79                 .setContentTitle(title)
80                 .setContentText(contentSummary)
81                 .setContentIntent(launchWifiTetherSettings())
82                 .setTicker(title)
83                 .setShowWhen(false)
84                 .setLocalOnly(true)
85                 .setColor(mContext.getResources().getColor(
86                         android.R.color.system_notification_accent_color, mContext.getTheme()))
87                 .setAutoCancel(true)
88                 .build();
89     }
90 
launchWifiTetherSettings()91     private PendingIntent launchWifiTetherSettings() {
92         Intent intent = new Intent(ACTION_HOTSPOT_PREFERENCES)
93                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
94                 .setPackage(mFrameworkFacade.getSettingsPackageName(mContext));
95         return mFrameworkFacade.getActivity(mContext, 0, intent,
96                 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
97     }
98 
99 
100 }
101