blob: fbb6dc5426594278212d1707a1e5aad756beb588 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 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.phone;
18
Malcolm Chen39aa7912017-06-05 19:02:16 -070019import static android.Manifest.permission.READ_PHONE_STATE;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.Notification;
22import android.app.NotificationManager;
23import android.app.PendingIntent;
24import android.app.StatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.Context;
27import android.content.Intent;
28import android.content.SharedPreferences;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080029import android.content.pm.ResolveInfo;
Andrew Lee99d0ac22014-10-10 13:18:04 -070030import android.content.pm.UserInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080031import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070035import android.os.UserHandle;
36import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.provider.ContactsContract.PhoneLookup;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080039import android.telecom.DefaultDialerManager;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070040import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080041import android.telecom.PhoneAccountHandle;
42import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070043import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.PhoneNumberUtils;
45import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080046import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080047import android.telephony.SubscriptionManager;
Ta-wei Yenfb4f0502016-05-27 12:15:43 -070048import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Andrew Leed5165b02014-12-05 15:53:58 -080049import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080051import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070054
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070056import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070058import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070059import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070060
Tyler Gunn9c1071f2014-12-09 10:07:54 -080061import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070062import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080063import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070064
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065/**
66 * NotificationManager-related utility code for the Phone app.
67 *
68 * This is a singleton object which acts as the interface to the
69 * framework's NotificationManager, and is used to display status bar
70 * icons and control other status bar-related behavior.
71 *
72 * @see PhoneGlobals.notificationMgr
73 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070074public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080075 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 private static final boolean DBG =
77 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
78 // Do not check in with VDBG = true, since that may write PII to the system log.
79 private static final boolean VDBG = false;
80
Ta-wei Yenb29425b2016-09-21 17:28:14 -070081 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
82 "mwi_should_check_vvm_configuration_state_";
83
Ta-wei Yen282a9702017-05-30 17:32:29 -070084 /**
85 * Boolean value representing whether the {@link
86 * TelephonyManager#ACTION_SHOW_VOICEMAIL_NOTIFICATION} is new or a refresh of an existing
87 * notification.
88 *
89 * TODO(b/62202833): make public
90 */
91 private static final String EXTRA_IS_REFRESH = "is_refresh";
92
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070094 static final int MMI_NOTIFICATION = 1;
95 static final int NETWORK_SELECTION_NOTIFICATION = 2;
96 static final int VOICEMAIL_NOTIFICATION = 3;
97 static final int CALL_FORWARD_NOTIFICATION = 4;
98 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
99 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100
101 /** The singleton NotificationMgr instance. */
102 private static NotificationMgr sInstance;
103
104 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 private Context mContext;
107 private NotificationManager mNotificationManager;
108 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700109 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800111 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800112 private TelecomManager mTelecomManager;
113 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 // used to track the notification of selected network unavailable
116 private boolean mSelectedUnavailableNotify = false;
117
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800118 // used to track whether the message waiting indicator is visible, per subscription id.
119 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
120
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 /**
122 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700123 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 */
125 private NotificationMgr(PhoneGlobals app) {
126 mApp = app;
127 mContext = app;
128 mNotificationManager =
129 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
130 mStatusBarManager =
131 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700132 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800133 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800134 mTelecomManager = TelecomManager.from(mContext);
135 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136 }
137
138 /**
139 * Initialize the singleton NotificationMgr instance.
140 *
141 * This is only done once, at startup, from PhoneApp.onCreate().
142 * From then on, the NotificationMgr instance is available via the
143 * PhoneApp's public "notificationMgr" field, which is why there's no
144 * getInstance() method here.
145 */
146 /* package */ static NotificationMgr init(PhoneGlobals app) {
147 synchronized (NotificationMgr.class) {
148 if (sInstance == null) {
149 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 } else {
151 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
152 }
153 return sInstance;
154 }
155 }
156
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157 /** The projection to use when querying the phones table */
158 static final String[] PHONES_PROJECTION = new String[] {
159 PhoneLookup.NUMBER,
160 PhoneLookup.DISPLAY_NAME,
161 PhoneLookup._ID
162 };
163
164 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800165 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
166 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
167 * settings screen. The voicemail notification sound is suppressed.
168 *
169 * @param subId The subscription Id.
170 */
171 /* package */ void refreshMwi(int subId) {
172 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
173 // reference the single subid stored in the mMwiVisible map.
174 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
175 if (mMwiVisible.keySet().size() == 1) {
176 Set<Integer> keySet = mMwiVisible.keySet();
177 Iterator<Integer> keyIt = keySet.iterator();
178 if (!keyIt.hasNext()) {
179 return;
180 }
181 subId = keyIt.next();
182 }
183 }
184 if (mMwiVisible.containsKey(subId)) {
185 boolean mwiVisible = mMwiVisible.get(subId);
186 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900187 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800188 }
189 }
190 }
191
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700192 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
193 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
194 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
195 + subId);
196 return;
197 }
198
199 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
200 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
201 .apply();
202 }
203
204 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
205 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
206 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
207 return true;
208 }
209 return PreferenceManager
210 .getDefaultSharedPreferences(mContext)
211 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
212 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800213 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 * Updates the message waiting indicator (voicemail) notification.
215 *
216 * @param visible true if there are messages waiting
217 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800218 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700219 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800220 }
221
222 /**
223 * Updates the message waiting indicator (voicemail) notification.
224 *
225 * @param subId the subId to update.
226 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700227 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
228 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800229 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700230 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800231 if (!PhoneGlobals.sVoiceCapable) {
232 // Do not show the message waiting indicator on devices which are not voice capable.
233 // These events *should* be blocked at the telephony layer for such devices.
234 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
235 return;
236 }
237
Nancy Chen2cf7f292015-05-15 11:00:10 -0700238 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800239 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800240 mMwiVisible.put(subId, visible);
241
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800243 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800244 Log.w(LOG_TAG, "Found null phone for: " + subId);
245 return;
246 }
247
248 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
249 if (subInfo == null) {
250 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800251 return;
252 }
253
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900255 if (mTelephonyManager.getPhoneCount() > 1) {
256 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
257 : R.drawable.stat_notify_voicemail_sub2;
258 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259
260 // This Notification can get a lot fancier once we have more
261 // information about the current voicemail messages.
262 // (For example, the current voicemail system can't tell
263 // us the caller-id or timestamp of a message, or tell us the
264 // message count.)
265
266 // But for now, the UI is ultra-simple: if the MWI indication
267 // is supposed to be visible, just show a single generic
268 // notification.
269
270 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800271 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 if (DBG) log("- got vm number: '" + vmNumber + "'");
273
Andrew Leea82b8202014-11-21 16:18:28 -0800274 // The voicemail number may be null because:
275 // (1) This phone has no voicemail number.
276 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
277 // happen when the device first boots if we get a MWI notification when we
278 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800279 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
280 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800282 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283 }
284
Bryce Lee5dc90842015-08-11 07:57:14 -0700285 Integer vmCount = null;
286
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800287 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700288 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
290 notificationTitle = String.format(titleFormat, vmCount);
291 }
292
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800293 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
294 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
295
296 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700298 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
299
300 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800301 notificationText = mContext.getString(
302 R.string.notification_voicemail_no_vm_number);
303
304 // If the voicemail number if unknown, instead of calling voicemail, take the user
305 // to the voicemail settings.
306 notificationText = mContext.getString(
307 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700308 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800309 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700310 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800312 if (mTelephonyManager.getPhoneCount() > 1) {
313 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800314 } else {
315 notificationText = String.format(
316 mContext.getString(R.string.notification_voicemail_text_format),
317 PhoneNumberUtils.formatNumber(vmNumber));
318 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800319 intent = new Intent(
320 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700321 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800322 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 }
324
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800325 PendingIntent pendingIntent =
326 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700327
Nancy Chenb4a92702014-12-04 15:57:29 -0800328 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700329 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700330 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700331 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 builder.setSmallIcon(resId)
333 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800334 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700335 .setContentTitle(notificationTitle)
336 .setContentText(notificationText)
337 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800338 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700339 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700340 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Ta-wei Yen282a9702017-05-30 17:32:29 -0700341 .setChannel(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
342 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343
Andrew Lee99d0ac22014-10-10 13:18:04 -0700344 final Notification notification = builder.build();
345 List<UserInfo> users = mUserManager.getUsers(true);
346 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700347 final UserInfo user = users.get(i);
348 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700349 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700350 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700351 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800352 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700353 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700354 mNotificationManager.notifyAsUser(
355 Integer.toString(subId) /* tag */,
356 VOICEMAIL_NOTIFICATION,
357 notification,
358 userHandle);
359 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700360 }
361 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 } else {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800363 List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
364 for (int i = 0; i < users.size(); i++) {
365 final UserInfo user = users.get(i);
366 final UserHandle userHandle = user.getUserHandle();
367 if (!mUserManager.hasUserRestriction(
368 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
369 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800370 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700371 false, userHandle, isRefresh)) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800372 mNotificationManager.cancelAsUser(
373 Integer.toString(subId) /* tag */,
374 VOICEMAIL_NOTIFICATION,
375 userHandle);
376 }
377 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700378 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700379 }
380 }
381
382 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800383 * Sends a broadcast with the voicemail notification information to the default dialer. This
384 * method is also used to indicate to the default dialer when to clear the
385 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700386 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800387 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700388 * @param count The number of pending voicemail messages to indicate on the notification. A
389 * Value of 0 is passed here to indicate that the notification should be cleared.
390 * @param number The voicemail phone number if specified.
391 * @param pendingIntent The intent that should be passed as the action to be taken.
392 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
393 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800394 * @param userHandle The user to receive the notification. Each user can have their own default
395 * dialer.
396 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700397 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800398 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
399 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700400 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800401
402 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
403 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700404 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700405 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800406 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
407 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yen282a9702017-05-30 17:32:29 -0700408 intent.putExtra(EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700409 if (count != null) {
410 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
411 }
412
413 // Additional information about the voicemail notification beyond the count is only
414 // present when the count not specified or greater than 0. The value of 0 represents
415 // clearing the notification, which does not require additional information.
416 if (count == null || count > 0) {
417 if (!TextUtils.isEmpty(number)) {
418 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
419 }
420
421 if (pendingIntent != null) {
422 intent.putExtra(isSettingsIntent
423 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
424 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
425 pendingIntent);
426 }
427 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800428 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700429 return true;
430 }
431
432 return false;
433 }
434
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800435 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
436 String dialerPackage = DefaultDialerManager
437 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
438 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
439 .setPackage(dialerPackage);
440 }
441
442 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
443 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
444 if (intent == null) {
445 return false;
446 }
447
448 List<ResolveInfo> receivers = mContext.getPackageManager()
449 .queryBroadcastReceivers(intent, 0);
450 return receivers.size() > 0;
451 }
452
Bryce Lee5dc90842015-08-11 07:57:14 -0700453 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700454 * Updates the message call forwarding indicator notification.
455 *
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530456 * @param visible true if call forwarding enabled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457 */
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530458
459 /* package */ void updateCfi(int subId, boolean visible) {
460 updateCfi(subId, visible, false /* isRefresh */);
461 }
462
463 /**
464 * Updates the message call forwarding indicator notification.
465 *
466 * @param visible true if call forwarding enabled
467 */
468 /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
Tyler Gunn17bffd02017-09-19 11:40:12 -0700469 logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 if (visible) {
471 // If Unconditional Call Forwarding (forward all calls) for VOICE
472 // is enabled, just show a notification. We'll default to expanded
473 // view for now, so the there is less confusion about the icon. If
474 // it is deemed too weird to have CF indications as expanded views,
475 // then we'll flip the flag back.
476
477 // TODO: We may want to take a look to see if the notification can
478 // display the target to forward calls to. This will require some
479 // effort though, since there are multiple layers of messages that
480 // will need to propagate that information.
481
Andrew Leed5165b02014-12-05 15:53:58 -0800482 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
483 if (subInfo == null) {
484 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
485 return;
486 }
487
488 String notificationTitle;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900489 int resId = R.drawable.stat_sys_phone_call_forward;
Andrew Leed5165b02014-12-05 15:53:58 -0800490 if (mTelephonyManager.getPhoneCount() > 1) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900491 int slotId = SubscriptionManager.getSlotIndex(subId);
492 resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
493 : R.drawable.stat_sys_phone_call_forward_sub2;
Andrew Leed5165b02014-12-05 15:53:58 -0800494 notificationTitle = subInfo.getDisplayName().toString();
495 } else {
496 notificationTitle = mContext.getString(R.string.labelCF);
497 }
498
fionaxu8b7620d2017-05-01 16:22:17 -0700499 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900500 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800501 .setColor(subInfo.getIconTint())
502 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700503 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
504 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700505 .setOngoing(true)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530506 .setChannel(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
507 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508
Andrew Lee99d0ac22014-10-10 13:18:04 -0700509 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800510 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700511 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800512 SubscriptionInfoHelper.addExtrasToIntent(
513 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
514 PendingIntent contentIntent =
515 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700516
517 List<UserInfo> users = mUserManager.getUsers(true);
518 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800519 final UserInfo user = users.get(i);
520 if (user.isManagedProfile()) {
521 continue;
522 }
523 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700524 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800525 mNotificationManager.notifyAsUser(
526 Integer.toString(subId) /* tag */,
527 CALL_FORWARD_NOTIFICATION,
528 builder.build(),
529 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 } else {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900532 List<UserInfo> users = mUserManager.getUsers(true);
533 for (UserInfo user : users) {
534 if (user.isManagedProfile()) {
535 continue;
536 }
537 UserHandle userHandle = user.getUserHandle();
538 mNotificationManager.cancelAsUser(
539 Integer.toString(subId) /* tag */,
540 CALL_FORWARD_NOTIFICATION,
541 userHandle);
542 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 }
544 }
545
546 /**
547 * Shows the "data disconnected due to roaming" notification, which
548 * appears when you lose data connectivity because you're roaming and
549 * you have the "data roaming" feature turned off.
550 */
Sanket Padawe336ef362017-07-12 12:12:37 -0700551 void showDataDisconnectedRoaming() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552 if (DBG) log("showDataDisconnectedRoaming()...");
553
554 // "Mobile network settings" screen / dialog
555 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700556 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557
558 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
559
fionaxu8b7620d2017-05-01 16:22:17 -0700560 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700561 .setSmallIcon(android.R.drawable.stat_sys_warning)
562 .setContentTitle(mContext.getText(R.string.roaming))
563 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700564 .setContentText(contentText)
fionaxu8b7620d2017-05-01 16:22:17 -0700565 .setChannel(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566
Andrew Lee99d0ac22014-10-10 13:18:04 -0700567 List<UserInfo> users = mUserManager.getUsers(true);
568 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800569 final UserInfo user = users.get(i);
570 if (user.isManagedProfile()) {
571 continue;
572 }
573 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700574 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700575 final Notification notif =
576 new Notification.BigTextStyle(builder).bigText(contentText).build();
577 mNotificationManager.notifyAsUser(
578 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
579 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 }
581
582 /**
583 * Turns off the "data disconnected due to roaming" notification.
584 */
585 /* package */ void hideDataDisconnectedRoaming() {
586 if (DBG) log("hideDataDisconnectedRoaming()...");
587 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
588 }
589
590 /**
591 * Display the network selection "no service" notification
592 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700593 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700595 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 if (DBG) log("showNetworkSelection(" + operator + ")...");
597
fionaxu8b7620d2017-05-01 16:22:17 -0700598 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700599 .setSmallIcon(android.R.drawable.stat_sys_warning)
600 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
601 .setContentText(
602 mContext.getString(R.string.notification_network_selection_text, operator))
603 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700604 .setOngoing(true)
fionaxu8b7620d2017-05-01 16:22:17 -0700605 .setChannel(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606
607 // create the target network operators settings intent
608 Intent intent = new Intent(Intent.ACTION_MAIN);
609 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
610 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen39aa7912017-06-05 19:02:16 -0700611 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700612 intent.setComponent(new ComponentName(
Malcolm Chen39aa7912017-06-05 19:02:16 -0700613 mContext.getString(R.string.mobile_network_settings_package),
614 mContext.getString(R.string.mobile_network_settings_class)));
Jayachandran C2ef9a482017-05-12 22:07:47 -0700615 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, subId);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700616 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617
Andrew Lee99d0ac22014-10-10 13:18:04 -0700618 List<UserInfo> users = mUserManager.getUsers(true);
619 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800620 final UserInfo user = users.get(i);
621 if (user.isManagedProfile()) {
622 continue;
623 }
624 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700625 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700626 mNotificationManager.notifyAsUser(
627 null /* tag */,
628 SELECTED_OPERATOR_FAIL_NOTIFICATION,
629 builder.build(),
630 userHandle);
631 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700632 }
633
634 /**
635 * Turn off the network selection "no service" notification
636 */
637 private void cancelNetworkSelection() {
638 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700639 mNotificationManager.cancelAsUser(
640 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700641 }
642
643 /**
644 * Update notification about no service of user selected operator
645 *
646 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700647 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700648 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700649 void updateNetworkSelection(int serviceState, int subId) {
650 int phoneId = SubscriptionManager.getPhoneId(subId);
651 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
652 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
653 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800654 if (SubscriptionManager.isValidSubscriptionId(subId)) {
655 // get the shared preference of network_selection.
656 // empty is auto mode, otherwise it is the operator alpha name
657 // in case there is no operator name, check the operator numeric
658 SharedPreferences sp =
659 PreferenceManager.getDefaultSharedPreferences(mContext);
660 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800661 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800662 if (TextUtils.isEmpty(networkSelection)) {
663 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800664 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800665 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700666
Amit Mahajana60be872015-01-15 16:05:08 -0800667 if (DBG) log("updateNetworkSelection()..." + "state = " +
668 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669
Amit Mahajana60be872015-01-15 16:05:08 -0800670 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
671 && !TextUtils.isEmpty(networkSelection)) {
Jayachandran C2ef9a482017-05-12 22:07:47 -0700672 showNetworkSelection(networkSelection, subId);
Hall Liu2b846c72016-02-09 18:21:24 -0800673 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800674 } else {
675 if (mSelectedUnavailableNotify) {
676 cancelNetworkSelection();
677 mSelectedUnavailableNotify = false;
678 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 }
680 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800681 if (DBG) log("updateNetworkSelection()..." + "state = " +
682 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700683 }
684 }
685 }
686
687 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
688 if (mToast != null) {
689 mToast.cancel();
690 }
691
692 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
693 mToast.show();
694 }
695
696 private void log(String msg) {
697 Log.d(LOG_TAG, msg);
698 }
Tyler Gunn17bffd02017-09-19 11:40:12 -0700699
700 private void logi(String msg) {
701 Log.i(LOG_TAG, msg);
702 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700703}