blob: a21bdccf259640787284ff0b408d2157635afb97 [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;
Andrew Leed5165b02014-12-05 15:53:58 -080048import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080050import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070053
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070055import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070057import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070058import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070059
Tyler Gunn9c1071f2014-12-09 10:07:54 -080060import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070061import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080062import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070063
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064/**
65 * NotificationManager-related utility code for the Phone app.
66 *
67 * This is a singleton object which acts as the interface to the
68 * framework's NotificationManager, and is used to display status bar
69 * icons and control other status bar-related behavior.
70 *
71 * @see PhoneGlobals.notificationMgr
72 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070073public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080074 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075 private static final boolean DBG =
76 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
77 // Do not check in with VDBG = true, since that may write PII to the system log.
78 private static final boolean VDBG = false;
79
Ta-wei Yenb29425b2016-09-21 17:28:14 -070080 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
81 "mwi_should_check_vvm_configuration_state_";
82
Ta-wei Yen282a9702017-05-30 17:32:29 -070083 /**
84 * Boolean value representing whether the {@link
85 * TelephonyManager#ACTION_SHOW_VOICEMAIL_NOTIFICATION} is new or a refresh of an existing
86 * notification.
87 *
88 * TODO(b/62202833): make public
89 */
90 private static final String EXTRA_IS_REFRESH = "is_refresh";
91
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070093 static final int MMI_NOTIFICATION = 1;
94 static final int NETWORK_SELECTION_NOTIFICATION = 2;
95 static final int VOICEMAIL_NOTIFICATION = 3;
96 static final int CALL_FORWARD_NOTIFICATION = 4;
97 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
98 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099
100 /** The singleton NotificationMgr instance. */
101 private static NotificationMgr sInstance;
102
103 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104
105 private Context mContext;
106 private NotificationManager mNotificationManager;
107 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700108 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800110 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800111 private TelecomManager mTelecomManager;
112 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 // used to track the notification of selected network unavailable
115 private boolean mSelectedUnavailableNotify = false;
116
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800117 // used to track whether the message waiting indicator is visible, per subscription id.
118 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
119
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 /**
121 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700122 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123 */
124 private NotificationMgr(PhoneGlobals app) {
125 mApp = app;
126 mContext = app;
127 mNotificationManager =
128 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
129 mStatusBarManager =
130 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700131 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800132 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800133 mTelecomManager = TelecomManager.from(mContext);
134 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700135 }
136
137 /**
138 * Initialize the singleton NotificationMgr instance.
139 *
140 * This is only done once, at startup, from PhoneApp.onCreate().
141 * From then on, the NotificationMgr instance is available via the
142 * PhoneApp's public "notificationMgr" field, which is why there's no
143 * getInstance() method here.
144 */
145 /* package */ static NotificationMgr init(PhoneGlobals app) {
146 synchronized (NotificationMgr.class) {
147 if (sInstance == null) {
148 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 } else {
150 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
151 }
152 return sInstance;
153 }
154 }
155
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 /** The projection to use when querying the phones table */
157 static final String[] PHONES_PROJECTION = new String[] {
158 PhoneLookup.NUMBER,
159 PhoneLookup.DISPLAY_NAME,
160 PhoneLookup._ID
161 };
162
163 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800164 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
165 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
166 * settings screen. The voicemail notification sound is suppressed.
167 *
168 * @param subId The subscription Id.
169 */
170 /* package */ void refreshMwi(int subId) {
171 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
172 // reference the single subid stored in the mMwiVisible map.
Ta-wei Yena1390d42017-12-04 15:11:33 -0800173 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800174 if (mMwiVisible.keySet().size() == 1) {
175 Set<Integer> keySet = mMwiVisible.keySet();
176 Iterator<Integer> keyIt = keySet.iterator();
177 if (!keyIt.hasNext()) {
178 return;
179 }
180 subId = keyIt.next();
181 }
182 }
183 if (mMwiVisible.containsKey(subId)) {
184 boolean mwiVisible = mMwiVisible.get(subId);
185 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900186 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800187 }
188 }
189 }
190
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700191 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
192 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
193 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
194 + subId);
195 return;
196 }
197
198 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
199 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
200 .apply();
201 }
202
203 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
204 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
205 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
206 return true;
207 }
208 return PreferenceManager
209 .getDefaultSharedPreferences(mContext)
210 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
211 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800212 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 * Updates the message waiting indicator (voicemail) notification.
214 *
215 * @param visible true if there are messages waiting
216 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800217 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700218 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800219 }
220
221 /**
222 * Updates the message waiting indicator (voicemail) notification.
223 *
224 * @param subId the subId to update.
225 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700226 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
227 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800228 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700229 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800230 if (!PhoneGlobals.sVoiceCapable) {
231 // Do not show the message waiting indicator on devices which are not voice capable.
232 // These events *should* be blocked at the telephony layer for such devices.
233 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
234 return;
235 }
236
Nancy Chen2cf7f292015-05-15 11:00:10 -0700237 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800238 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800239 mMwiVisible.put(subId, visible);
240
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800242 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800243 Log.w(LOG_TAG, "Found null phone for: " + subId);
244 return;
245 }
246
247 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
248 if (subInfo == null) {
249 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800250 return;
251 }
252
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900254 if (mTelephonyManager.getPhoneCount() > 1) {
255 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
256 : R.drawable.stat_notify_voicemail_sub2;
257 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258
259 // This Notification can get a lot fancier once we have more
260 // information about the current voicemail messages.
261 // (For example, the current voicemail system can't tell
262 // us the caller-id or timestamp of a message, or tell us the
263 // message count.)
264
265 // But for now, the UI is ultra-simple: if the MWI indication
266 // is supposed to be visible, just show a single generic
267 // notification.
268
269 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800270 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271 if (DBG) log("- got vm number: '" + vmNumber + "'");
272
Andrew Leea82b8202014-11-21 16:18:28 -0800273 // The voicemail number may be null because:
274 // (1) This phone has no voicemail number.
275 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
276 // happen when the device first boots if we get a MWI notification when we
277 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800278 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
279 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800281 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 }
283
Bryce Lee5dc90842015-08-11 07:57:14 -0700284 Integer vmCount = null;
285
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800286 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700287 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
289 notificationTitle = String.format(titleFormat, vmCount);
290 }
291
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800292 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
293 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
294
295 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700297 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
298
299 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800300 notificationText = mContext.getString(
301 R.string.notification_voicemail_no_vm_number);
302
303 // If the voicemail number if unknown, instead of calling voicemail, take the user
304 // to the voicemail settings.
305 notificationText = mContext.getString(
306 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700307 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800308 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700309 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800311 if (mTelephonyManager.getPhoneCount() > 1) {
312 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800313 } else {
314 notificationText = String.format(
315 mContext.getString(R.string.notification_voicemail_text_format),
316 PhoneNumberUtils.formatNumber(vmNumber));
317 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800318 intent = new Intent(
319 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700320 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800321 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 }
323
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800324 PendingIntent pendingIntent =
325 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326
Nancy Chenb4a92702014-12-04 15:57:29 -0800327 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700328 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700329 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700330 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 builder.setSmallIcon(resId)
332 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800333 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 .setContentTitle(notificationTitle)
335 .setContentText(notificationText)
336 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800337 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700338 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700339 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Ta-wei Yen282a9702017-05-30 17:32:29 -0700340 .setChannel(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
341 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342
Andrew Lee99d0ac22014-10-10 13:18:04 -0700343 final Notification notification = builder.build();
344 List<UserInfo> users = mUserManager.getUsers(true);
345 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700346 final UserInfo user = users.get(i);
347 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700348 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700349 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700350 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800351 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700352 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700353 mNotificationManager.notifyAsUser(
354 Integer.toString(subId) /* tag */,
355 VOICEMAIL_NOTIFICATION,
356 notification,
357 userHandle);
358 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700359 }
360 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361 } else {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800362 List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
363 for (int i = 0; i < users.size(); i++) {
364 final UserInfo user = users.get(i);
365 final UserHandle userHandle = user.getUserHandle();
366 if (!mUserManager.hasUserRestriction(
367 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
368 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800369 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700370 false, userHandle, isRefresh)) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800371 mNotificationManager.cancelAsUser(
372 Integer.toString(subId) /* tag */,
373 VOICEMAIL_NOTIFICATION,
374 userHandle);
375 }
376 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700377 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700378 }
379 }
380
381 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800382 * Sends a broadcast with the voicemail notification information to the default dialer. This
383 * method is also used to indicate to the default dialer when to clear the
384 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700385 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800386 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700387 * @param count The number of pending voicemail messages to indicate on the notification. A
388 * Value of 0 is passed here to indicate that the notification should be cleared.
389 * @param number The voicemail phone number if specified.
390 * @param pendingIntent The intent that should be passed as the action to be taken.
391 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
392 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800393 * @param userHandle The user to receive the notification. Each user can have their own default
394 * dialer.
395 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700396 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800397 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
398 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700399 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800400
401 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
402 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700403 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700404 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800405 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
406 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yen282a9702017-05-30 17:32:29 -0700407 intent.putExtra(EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700408 if (count != null) {
409 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
410 }
411
412 // Additional information about the voicemail notification beyond the count is only
413 // present when the count not specified or greater than 0. The value of 0 represents
414 // clearing the notification, which does not require additional information.
415 if (count == null || count > 0) {
416 if (!TextUtils.isEmpty(number)) {
417 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
418 }
419
420 if (pendingIntent != null) {
421 intent.putExtra(isSettingsIntent
422 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
423 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
424 pendingIntent);
425 }
426 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800427 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700428 return true;
429 }
430
431 return false;
432 }
433
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800434 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
435 String dialerPackage = DefaultDialerManager
436 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
437 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
438 .setPackage(dialerPackage);
439 }
440
441 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
442 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
443 if (intent == null) {
444 return false;
445 }
446
447 List<ResolveInfo> receivers = mContext.getPackageManager()
448 .queryBroadcastReceivers(intent, 0);
449 return receivers.size() > 0;
450 }
451
Bryce Lee5dc90842015-08-11 07:57:14 -0700452 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 * Updates the message call forwarding indicator notification.
454 *
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530455 * @param visible true if call forwarding enabled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700456 */
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530457
458 /* package */ void updateCfi(int subId, boolean visible) {
459 updateCfi(subId, visible, false /* isRefresh */);
460 }
461
462 /**
463 * Updates the message call forwarding indicator notification.
464 *
465 * @param visible true if call forwarding enabled
466 */
467 /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
Tyler Gunn17bffd02017-09-19 11:40:12 -0700468 logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 if (visible) {
470 // If Unconditional Call Forwarding (forward all calls) for VOICE
471 // is enabled, just show a notification. We'll default to expanded
472 // view for now, so the there is less confusion about the icon. If
473 // it is deemed too weird to have CF indications as expanded views,
474 // then we'll flip the flag back.
475
476 // TODO: We may want to take a look to see if the notification can
477 // display the target to forward calls to. This will require some
478 // effort though, since there are multiple layers of messages that
479 // will need to propagate that information.
480
Andrew Leed5165b02014-12-05 15:53:58 -0800481 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
482 if (subInfo == null) {
483 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
484 return;
485 }
486
487 String notificationTitle;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900488 int resId = R.drawable.stat_sys_phone_call_forward;
Andrew Leed5165b02014-12-05 15:53:58 -0800489 if (mTelephonyManager.getPhoneCount() > 1) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900490 int slotId = SubscriptionManager.getSlotIndex(subId);
491 resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
492 : R.drawable.stat_sys_phone_call_forward_sub2;
Andrew Leed5165b02014-12-05 15:53:58 -0800493 notificationTitle = subInfo.getDisplayName().toString();
494 } else {
495 notificationTitle = mContext.getString(R.string.labelCF);
496 }
497
fionaxu8b7620d2017-05-01 16:22:17 -0700498 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900499 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800500 .setColor(subInfo.getIconTint())
501 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700502 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
503 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700504 .setOngoing(true)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530505 .setChannel(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
506 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700507
Andrew Lee99d0ac22014-10-10 13:18:04 -0700508 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800509 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700510 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800511 SubscriptionInfoHelper.addExtrasToIntent(
512 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
513 PendingIntent contentIntent =
514 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700515
516 List<UserInfo> users = mUserManager.getUsers(true);
517 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800518 final UserInfo user = users.get(i);
519 if (user.isManagedProfile()) {
520 continue;
521 }
522 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700523 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800524 mNotificationManager.notifyAsUser(
525 Integer.toString(subId) /* tag */,
526 CALL_FORWARD_NOTIFICATION,
527 builder.build(),
528 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700529 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530 } else {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900531 List<UserInfo> users = mUserManager.getUsers(true);
532 for (UserInfo user : users) {
533 if (user.isManagedProfile()) {
534 continue;
535 }
536 UserHandle userHandle = user.getUserHandle();
537 mNotificationManager.cancelAsUser(
538 Integer.toString(subId) /* tag */,
539 CALL_FORWARD_NOTIFICATION,
540 userHandle);
541 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 }
543 }
544
545 /**
546 * Shows the "data disconnected due to roaming" notification, which
547 * appears when you lose data connectivity because you're roaming and
548 * you have the "data roaming" feature turned off.
549 */
Sanket Padawe336ef362017-07-12 12:12:37 -0700550 void showDataDisconnectedRoaming() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 if (DBG) log("showDataDisconnectedRoaming()...");
552
553 // "Mobile network settings" screen / dialog
554 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700555 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556
557 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
558
fionaxu8b7620d2017-05-01 16:22:17 -0700559 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700560 .setSmallIcon(android.R.drawable.stat_sys_warning)
561 .setContentTitle(mContext.getText(R.string.roaming))
562 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700563 .setContentText(contentText)
fionaxu8b7620d2017-05-01 16:22:17 -0700564 .setChannel(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565
Andrew Lee99d0ac22014-10-10 13:18:04 -0700566 List<UserInfo> users = mUserManager.getUsers(true);
567 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800568 final UserInfo user = users.get(i);
569 if (user.isManagedProfile()) {
570 continue;
571 }
572 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700573 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700574 final Notification notif =
575 new Notification.BigTextStyle(builder).bigText(contentText).build();
576 mNotificationManager.notifyAsUser(
577 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
578 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700579 }
580
581 /**
582 * Turns off the "data disconnected due to roaming" notification.
583 */
584 /* package */ void hideDataDisconnectedRoaming() {
585 if (DBG) log("hideDataDisconnectedRoaming()...");
586 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
587 }
588
589 /**
590 * Display the network selection "no service" notification
591 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700592 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700594 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 if (DBG) log("showNetworkSelection(" + operator + ")...");
596
fionaxu8b7620d2017-05-01 16:22:17 -0700597 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700598 .setSmallIcon(android.R.drawable.stat_sys_warning)
599 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
600 .setContentText(
601 mContext.getString(R.string.notification_network_selection_text, operator))
602 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700603 .setOngoing(true)
fionaxu8b7620d2017-05-01 16:22:17 -0700604 .setChannel(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605
606 // create the target network operators settings intent
607 Intent intent = new Intent(Intent.ACTION_MAIN);
608 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
609 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen39aa7912017-06-05 19:02:16 -0700610 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700611 intent.setComponent(new ComponentName(
Malcolm Chen39aa7912017-06-05 19:02:16 -0700612 mContext.getString(R.string.mobile_network_settings_package),
613 mContext.getString(R.string.mobile_network_settings_class)));
Jayachandran C2ef9a482017-05-12 22:07:47 -0700614 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, subId);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700615 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616
Andrew Lee99d0ac22014-10-10 13:18:04 -0700617 List<UserInfo> users = mUserManager.getUsers(true);
618 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800619 final UserInfo user = users.get(i);
620 if (user.isManagedProfile()) {
621 continue;
622 }
623 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700624 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700625 mNotificationManager.notifyAsUser(
626 null /* tag */,
627 SELECTED_OPERATOR_FAIL_NOTIFICATION,
628 builder.build(),
629 userHandle);
630 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700631 }
632
633 /**
634 * Turn off the network selection "no service" notification
635 */
636 private void cancelNetworkSelection() {
637 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700638 mNotificationManager.cancelAsUser(
639 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 }
641
642 /**
643 * Update notification about no service of user selected operator
644 *
645 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700646 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700648 void updateNetworkSelection(int serviceState, int subId) {
649 int phoneId = SubscriptionManager.getPhoneId(subId);
650 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
651 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
652 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800653 if (SubscriptionManager.isValidSubscriptionId(subId)) {
654 // get the shared preference of network_selection.
655 // empty is auto mode, otherwise it is the operator alpha name
656 // in case there is no operator name, check the operator numeric
657 SharedPreferences sp =
658 PreferenceManager.getDefaultSharedPreferences(mContext);
659 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800660 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800661 if (TextUtils.isEmpty(networkSelection)) {
662 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800663 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800664 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665
Amit Mahajana60be872015-01-15 16:05:08 -0800666 if (DBG) log("updateNetworkSelection()..." + "state = " +
667 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668
Amit Mahajana60be872015-01-15 16:05:08 -0800669 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
670 && !TextUtils.isEmpty(networkSelection)) {
Jayachandran C2ef9a482017-05-12 22:07:47 -0700671 showNetworkSelection(networkSelection, subId);
Hall Liu2b846c72016-02-09 18:21:24 -0800672 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800673 } else {
674 if (mSelectedUnavailableNotify) {
675 cancelNetworkSelection();
676 mSelectedUnavailableNotify = false;
677 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700678 }
679 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800680 if (DBG) log("updateNetworkSelection()..." + "state = " +
681 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 }
683 }
684 }
685
686 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
687 if (mToast != null) {
688 mToast.cancel();
689 }
690
691 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
692 mToast.show();
693 }
694
695 private void log(String msg) {
696 Log.d(LOG_TAG, msg);
697 }
Tyler Gunn17bffd02017-09-19 11:40:12 -0700698
699 private void logi(String msg) {
700 Log.i(LOG_TAG, msg);
701 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700702}