1 /* 2 * Copyright (C) 2019 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.providers.settings; 17 18 import android.app.ActivityManager; 19 import android.app.Notification; 20 import android.app.NotificationChannel; 21 import android.app.NotificationManager; 22 import android.app.PendingIntent; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.content.res.Resources; 28 import android.os.UserHandle; 29 import android.provider.Settings; 30 31 import com.android.internal.messages.nano.SystemMessageProto; 32 import com.android.internal.notification.SystemNotificationChannels; 33 34 import java.util.List; 35 36 /** 37 * Helper class for sending notifications when the user's Soft AP config was changed upon restore. 38 */ 39 public class WifiSoftApConfigChangedNotifier { WifiSoftApConfigChangedNotifier()40 private WifiSoftApConfigChangedNotifier() {} 41 42 /** 43 * Send a notification informing the user that their' Soft AP Config was changed upon restore. 44 * When the user taps on the notification, they are taken to the Wifi Tethering page in 45 * Settings. 46 */ notifyUserOfConfigConversion(Context context)47 public static void notifyUserOfConfigConversion(Context context) { 48 NotificationManager notificationManager = 49 context.getSystemService(NotificationManager.class); 50 51 // create channel, or update it if it already exists 52 NotificationChannel channel = new NotificationChannel( 53 SystemNotificationChannels.NETWORK_STATUS, 54 context.getString( 55 com.android.internal.R.string.notification_channel_network_status), 56 NotificationManager.IMPORTANCE_LOW); 57 notificationManager.createNotificationChannel(channel); 58 59 notificationManager.notify( 60 SystemMessageProto.SystemMessage.NOTE_SOFTAP_CONFIG_CHANGED, 61 createConversionNotification(context)); 62 } 63 createConversionNotification(Context context)64 private static Notification createConversionNotification(Context context) { 65 Resources resources = context.getResources(); 66 CharSequence title = resources.getText(R.string.wifi_softap_config_change); 67 CharSequence contentSummary = resources.getText(R.string.wifi_softap_config_change_summary); 68 int color = resources.getColor( 69 android.R.color.system_notification_accent_color, context.getTheme()); 70 71 return new Notification.Builder(context, SystemNotificationChannels.NETWORK_STATUS) 72 .setSmallIcon(R.drawable.ic_wifi_settings) 73 .setPriority(Notification.PRIORITY_HIGH) 74 .setCategory(Notification.CATEGORY_SYSTEM) 75 .setContentTitle(title) 76 .setContentText(contentSummary) 77 .setContentIntent(getPendingActivity(context)) 78 .setTicker(title) 79 .setShowWhen(false) 80 .setLocalOnly(true) 81 .setColor(color) 82 .setStyle(new Notification.BigTextStyle() 83 .setBigContentTitle(title) 84 .setSummaryText(contentSummary)) 85 .setAutoCancel(true) 86 .build(); 87 } 88 getPendingActivity(Context context)89 private static PendingIntent getPendingActivity(Context context) { 90 Intent intent = new Intent("com.android.settings.WIFI_TETHER_SETTINGS") 91 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 92 .setPackage(getSettingsPackageName(context)); 93 return PendingIntent.getActivity(context, 0, intent, 94 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); 95 } 96 97 /** 98 * @return Get settings package name. 99 */ getSettingsPackageName(Context context)100 private static String getSettingsPackageName(Context context) { 101 if (context == null) return null; 102 103 Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); 104 List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivitiesAsUser( 105 intent, PackageManager.MATCH_SYSTEM_ONLY | PackageManager.MATCH_DEFAULT_ONLY, 106 UserHandle.of(ActivityManager.getCurrentUser())); 107 if (resolveInfos == null || resolveInfos.isEmpty()) { 108 return "com.android.settings"; 109 } 110 return resolveInfos.get(0).activityInfo.packageName; 111 } 112 } 113