blob: e10b3f3bb9f4d618db250b9f1e6c1634494670ab [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
Ta-wei Yen5bb19562016-11-16 11:05:37 -080019import 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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import com.android.internal.telephony.TelephonyCapabilities;
fionaxu75b66a72017-04-19 19:01:56 -070057import com.android.internal.telephony.util.TelephonyNotificationBuilder;
Ta-wei Yen41fb43e2016-08-17 15:16:01 -070058import com.android.phone.settings.VoicemailNotificationSettingsUtil;
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
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070085 static final int MMI_NOTIFICATION = 1;
86 static final int NETWORK_SELECTION_NOTIFICATION = 2;
87 static final int VOICEMAIL_NOTIFICATION = 3;
88 static final int CALL_FORWARD_NOTIFICATION = 4;
89 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
90 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091
92 /** The singleton NotificationMgr instance. */
93 private static NotificationMgr sInstance;
94
95 private PhoneGlobals mApp;
96 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097
98 private Context mContext;
99 private NotificationManager mNotificationManager;
100 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700101 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800103 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800104 private TelecomManager mTelecomManager;
105 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 // used to track the notification of selected network unavailable
108 private boolean mSelectedUnavailableNotify = false;
109
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800110 // used to track whether the message waiting indicator is visible, per subscription id.
111 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
112
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113 /**
114 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700115 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116 */
117 private NotificationMgr(PhoneGlobals app) {
118 mApp = app;
119 mContext = app;
120 mNotificationManager =
121 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
122 mStatusBarManager =
123 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700124 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800125 mPhone = app.mCM.getDefaultPhone();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800126 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800127 mTelecomManager = TelecomManager.from(mContext);
128 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700129
Ta-wei Yenfb4f0502016-05-27 12:15:43 -0700130 mSubscriptionManager.addOnSubscriptionsChangedListener(
131 new OnSubscriptionsChangedListener() {
132 @Override
133 public void onSubscriptionsChanged() {
134 updateActivePhonesMwi();
135 }
136 });
137 }
138
139 public void updateActivePhonesMwi() {
140 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
141
142 if (subInfos == null) {
143 return;
144 }
145
146 for (int i = 0; i < subInfos.size(); i++) {
147 int subId = subInfos.get(i).getSubscriptionId();
148 refreshMwi(subId);
149 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 }
151
152 /**
153 * Initialize the singleton NotificationMgr instance.
154 *
155 * This is only done once, at startup, from PhoneApp.onCreate().
156 * From then on, the NotificationMgr instance is available via the
157 * PhoneApp's public "notificationMgr" field, which is why there's no
158 * getInstance() method here.
159 */
160 /* package */ static NotificationMgr init(PhoneGlobals app) {
161 synchronized (NotificationMgr.class) {
162 if (sInstance == null) {
163 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 } else {
165 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
166 }
167 return sInstance;
168 }
169 }
170
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 /** The projection to use when querying the phones table */
172 static final String[] PHONES_PROJECTION = new String[] {
173 PhoneLookup.NUMBER,
174 PhoneLookup.DISPLAY_NAME,
175 PhoneLookup._ID
176 };
177
178 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800179 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
180 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
181 * settings screen. The voicemail notification sound is suppressed.
182 *
183 * @param subId The subscription Id.
184 */
185 /* package */ void refreshMwi(int subId) {
186 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
187 // reference the single subid stored in the mMwiVisible map.
188 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
189 if (mMwiVisible.keySet().size() == 1) {
190 Set<Integer> keySet = mMwiVisible.keySet();
191 Iterator<Integer> keyIt = keySet.iterator();
192 if (!keyIt.hasNext()) {
193 return;
194 }
195 subId = keyIt.next();
196 }
197 }
198 if (mMwiVisible.containsKey(subId)) {
199 boolean mwiVisible = mMwiVisible.get(subId);
200 if (mwiVisible) {
201 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
202 }
203 }
204 }
205
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700206 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
207 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
208 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
209 + subId);
210 return;
211 }
212
213 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
214 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
215 .apply();
216 }
217
218 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
219 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
220 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
221 return true;
222 }
223 return PreferenceManager
224 .getDefaultSharedPreferences(mContext)
225 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
226 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800227 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 * Updates the message waiting indicator (voicemail) notification.
229 *
230 * @param visible true if there are messages waiting
231 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800232 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800233 updateMwi(subId, visible, true /* enableNotificationSound */);
234 }
235
236 /**
237 * Updates the message waiting indicator (voicemail) notification.
238 *
239 * @param subId the subId to update.
240 * @param visible true if there are messages waiting
241 * @param enableNotificationSound {@code true} if the notification sound should be played.
242 */
243 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800244 if (!PhoneGlobals.sVoiceCapable) {
245 // Do not show the message waiting indicator on devices which are not voice capable.
246 // These events *should* be blocked at the telephony layer for such devices.
247 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
248 return;
249 }
250
Nancy Chen2cf7f292015-05-15 11:00:10 -0700251 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800252 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800253 mMwiVisible.put(subId, visible);
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800256 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800257 Log.w(LOG_TAG, "Found null phone for: " + subId);
258 return;
259 }
260
261 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
262 if (subInfo == null) {
263 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800264 return;
265 }
266
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 int resId = android.R.drawable.stat_notify_voicemail;
268
269 // This Notification can get a lot fancier once we have more
270 // information about the current voicemail messages.
271 // (For example, the current voicemail system can't tell
272 // us the caller-id or timestamp of a message, or tell us the
273 // message count.)
274
275 // But for now, the UI is ultra-simple: if the MWI indication
276 // is supposed to be visible, just show a single generic
277 // notification.
278
279 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800280 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 if (DBG) log("- got vm number: '" + vmNumber + "'");
282
Andrew Leea82b8202014-11-21 16:18:28 -0800283 // The voicemail number may be null because:
284 // (1) This phone has no voicemail number.
285 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
286 // happen when the device first boots if we get a MWI notification when we
287 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800288 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
289 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800291 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292 }
293
Bryce Lee5dc90842015-08-11 07:57:14 -0700294 Integer vmCount = null;
295
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800296 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700297 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
299 notificationTitle = String.format(titleFormat, vmCount);
300 }
301
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800302 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
303 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
304
305 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700307 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
308
309 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800310 notificationText = mContext.getString(
311 R.string.notification_voicemail_no_vm_number);
312
313 // If the voicemail number if unknown, instead of calling voicemail, take the user
314 // to the voicemail settings.
315 notificationText = mContext.getString(
316 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700317 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800318 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700319 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800321 if (mTelephonyManager.getPhoneCount() > 1) {
322 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800323 } else {
324 notificationText = String.format(
325 mContext.getString(R.string.notification_voicemail_text_format),
326 PhoneNumberUtils.formatNumber(vmNumber));
327 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800328 intent = new Intent(
329 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700330 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800331 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 }
333
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800334 PendingIntent pendingIntent =
335 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800336 Uri ringtoneUri = null;
337
338 if (enableNotificationSound) {
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700339 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800340 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341
Nancy Chenb4a92702014-12-04 15:57:29 -0800342 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700343 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700344 subId);
fionaxu75b66a72017-04-19 19:01:56 -0700345 Notification.Builder builder = new TelephonyNotificationBuilder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 builder.setSmallIcon(resId)
347 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800348 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700349 .setContentTitle(notificationTitle)
350 .setContentText(notificationText)
351 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700352 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800353 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700354 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700355 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
356 .setChannel(TelephonyNotificationBuilder.CHANNEL_ID_VOICE_MAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800358 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700359 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700361
362 final Notification notification = builder.build();
363 List<UserInfo> users = mUserManager.getUsers(true);
364 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700365 final UserInfo user = users.get(i);
366 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700367 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700368 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700369 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800370 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800371 pendingIntent, isSettingsIntent, userHandle)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700372 mNotificationManager.notifyAsUser(
373 Integer.toString(subId) /* tag */,
374 VOICEMAIL_NOTIFICATION,
375 notification,
376 userHandle);
377 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700378 }
379 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 } else {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800381 List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
382 for (int i = 0; i < users.size(); i++) {
383 final UserInfo user = users.get(i);
384 final UserHandle userHandle = user.getUserHandle();
385 if (!mUserManager.hasUserRestriction(
386 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
387 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800388 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
389 false, userHandle)) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800390 mNotificationManager.cancelAsUser(
391 Integer.toString(subId) /* tag */,
392 VOICEMAIL_NOTIFICATION,
393 userHandle);
394 }
395 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700396 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 }
398 }
399
400 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800401 * Sends a broadcast with the voicemail notification information to the default dialer. This
402 * method is also used to indicate to the default dialer when to clear the
403 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700404 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800405 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700406 * @param count The number of pending voicemail messages to indicate on the notification. A
407 * Value of 0 is passed here to indicate that the notification should be cleared.
408 * @param number The voicemail phone number if specified.
409 * @param pendingIntent The intent that should be passed as the action to be taken.
410 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
411 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800412 * @param userHandle The user to receive the notification. Each user can have their own default
413 * dialer.
414 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700415 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800416 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
417 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
418 UserHandle userHandle) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800419
420 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
421 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700422 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700423 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800424 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
425 PhoneUtils.makePstnPhoneAccountHandle(phone));
Bryce Lee5dc90842015-08-11 07:57:14 -0700426 if (count != null) {
427 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
428 }
429
430 // Additional information about the voicemail notification beyond the count is only
431 // present when the count not specified or greater than 0. The value of 0 represents
432 // clearing the notification, which does not require additional information.
433 if (count == null || count > 0) {
434 if (!TextUtils.isEmpty(number)) {
435 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
436 }
437
438 if (pendingIntent != null) {
439 intent.putExtra(isSettingsIntent
440 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
441 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
442 pendingIntent);
443 }
444 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800445 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700446 return true;
447 }
448
449 return false;
450 }
451
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800452 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
453 String dialerPackage = DefaultDialerManager
454 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
455 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
456 .setPackage(dialerPackage);
457 }
458
459 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
460 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
461 if (intent == null) {
462 return false;
463 }
464
465 List<ResolveInfo> receivers = mContext.getPackageManager()
466 .queryBroadcastReceivers(intent, 0);
467 return receivers.size() > 0;
468 }
469
Bryce Lee5dc90842015-08-11 07:57:14 -0700470 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700471 * Updates the message call forwarding indicator notification.
472 *
473 * @param visible true if there are messages waiting
474 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800475 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476 if (DBG) log("updateCfi(): " + visible);
477 if (visible) {
478 // If Unconditional Call Forwarding (forward all calls) for VOICE
479 // is enabled, just show a notification. We'll default to expanded
480 // view for now, so the there is less confusion about the icon. If
481 // it is deemed too weird to have CF indications as expanded views,
482 // then we'll flip the flag back.
483
484 // TODO: We may want to take a look to see if the notification can
485 // display the target to forward calls to. This will require some
486 // effort though, since there are multiple layers of messages that
487 // will need to propagate that information.
488
Andrew Leed5165b02014-12-05 15:53:58 -0800489 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
490 if (subInfo == null) {
491 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
492 return;
493 }
494
495 String notificationTitle;
496 if (mTelephonyManager.getPhoneCount() > 1) {
497 notificationTitle = subInfo.getDisplayName().toString();
498 } else {
499 notificationTitle = mContext.getString(R.string.labelCF);
500 }
501
fionaxu75b66a72017-04-19 19:01:56 -0700502 Notification.Builder builder = new TelephonyNotificationBuilder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700503 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800504 .setColor(subInfo.getIconTint())
505 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700506 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
507 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700508 .setOngoing(true)
509 .setChannel(TelephonyNotificationBuilder.CHANNEL_ID_CALL_FORWARD);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700510
Andrew Lee99d0ac22014-10-10 13:18:04 -0700511 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800512 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700513 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800514 SubscriptionInfoHelper.addExtrasToIntent(
515 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
516 PendingIntent contentIntent =
517 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700518
519 List<UserInfo> users = mUserManager.getUsers(true);
520 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800521 final UserInfo user = users.get(i);
522 if (user.isManagedProfile()) {
523 continue;
524 }
525 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700526 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800527 mNotificationManager.notifyAsUser(
528 Integer.toString(subId) /* tag */,
529 CALL_FORWARD_NOTIFICATION,
530 builder.build(),
531 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700532 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700534 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800535 Integer.toString(subId) /* tag */,
536 CALL_FORWARD_NOTIFICATION,
537 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 }
539 }
540
541 /**
542 * Shows the "data disconnected due to roaming" notification, which
543 * appears when you lose data connectivity because you're roaming and
544 * you have the "data roaming" feature turned off.
545 */
546 /* package */ void showDataDisconnectedRoaming() {
547 if (DBG) log("showDataDisconnectedRoaming()...");
548
549 // "Mobile network settings" screen / dialog
550 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700551 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552
553 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
554
fionaxu75b66a72017-04-19 19:01:56 -0700555 final Notification.Builder builder = new TelephonyNotificationBuilder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700556 .setSmallIcon(android.R.drawable.stat_sys_warning)
557 .setContentTitle(mContext.getText(R.string.roaming))
558 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700559 .setContentText(contentText)
560 .setChannel(TelephonyNotificationBuilder.CHANNEL_ID_MOBILE_DATA_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561
Andrew Lee99d0ac22014-10-10 13:18:04 -0700562 List<UserInfo> users = mUserManager.getUsers(true);
563 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800564 final UserInfo user = users.get(i);
565 if (user.isManagedProfile()) {
566 continue;
567 }
568 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700569 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700570 final Notification notif =
571 new Notification.BigTextStyle(builder).bigText(contentText).build();
572 mNotificationManager.notifyAsUser(
573 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
574 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700575 }
576
577 /**
578 * Turns off the "data disconnected due to roaming" notification.
579 */
580 /* package */ void hideDataDisconnectedRoaming() {
581 if (DBG) log("hideDataDisconnectedRoaming()...");
582 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
583 }
584
585 /**
586 * Display the network selection "no service" notification
587 * @param operator is the numeric operator number
588 */
589 private void showNetworkSelection(String operator) {
590 if (DBG) log("showNetworkSelection(" + operator + ")...");
591
fionaxu75b66a72017-04-19 19:01:56 -0700592 Notification.Builder builder = new TelephonyNotificationBuilder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700593 .setSmallIcon(android.R.drawable.stat_sys_warning)
594 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
595 .setContentText(
596 mContext.getString(R.string.notification_network_selection_text, operator))
597 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700598 .setOngoing(true)
599 .setChannel(TelephonyNotificationBuilder.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600
601 // create the target network operators settings intent
602 Intent intent = new Intent(Intent.ACTION_MAIN);
603 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
604 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
605 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700606 intent.setComponent(new ComponentName(
607 mContext.getString(R.string.network_operator_settings_package),
608 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700609 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700610 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611
Andrew Lee99d0ac22014-10-10 13:18:04 -0700612 List<UserInfo> users = mUserManager.getUsers(true);
613 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800614 final UserInfo user = users.get(i);
615 if (user.isManagedProfile()) {
616 continue;
617 }
618 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700619 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700620 mNotificationManager.notifyAsUser(
621 null /* tag */,
622 SELECTED_OPERATOR_FAIL_NOTIFICATION,
623 builder.build(),
624 userHandle);
625 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 }
627
628 /**
629 * Turn off the network selection "no service" notification
630 */
631 private void cancelNetworkSelection() {
632 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700633 mNotificationManager.cancelAsUser(
634 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 }
636
637 /**
638 * Update notification about no service of user selected operator
639 *
640 * @param serviceState Phone service state
641 */
642 void updateNetworkSelection(int serviceState) {
643 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800644 int subId = mPhone.getSubId();
645 if (SubscriptionManager.isValidSubscriptionId(subId)) {
646 // get the shared preference of network_selection.
647 // empty is auto mode, otherwise it is the operator alpha name
648 // in case there is no operator name, check the operator numeric
649 SharedPreferences sp =
650 PreferenceManager.getDefaultSharedPreferences(mContext);
651 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800652 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800653 if (TextUtils.isEmpty(networkSelection)) {
654 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800655 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800656 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657
Amit Mahajana60be872015-01-15 16:05:08 -0800658 if (DBG) log("updateNetworkSelection()..." + "state = " +
659 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700660
Amit Mahajana60be872015-01-15 16:05:08 -0800661 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
662 && !TextUtils.isEmpty(networkSelection)) {
Hall Liu2b846c72016-02-09 18:21:24 -0800663 showNetworkSelection(networkSelection);
664 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800665 } else {
666 if (mSelectedUnavailableNotify) {
667 cancelNetworkSelection();
668 mSelectedUnavailableNotify = false;
669 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700670 }
671 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800672 if (DBG) log("updateNetworkSelection()..." + "state = " +
673 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674 }
675 }
676 }
677
678 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
679 if (mToast != null) {
680 mToast.cancel();
681 }
682
683 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
684 mToast.show();
685 }
686
687 private void log(String msg) {
688 Log.d(LOG_TAG, msg);
689 }
690}