blob: 69c0871bbae3abd8efc1568834d7e1af7e82473e [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;
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
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070084 static final int MMI_NOTIFICATION = 1;
85 static final int NETWORK_SELECTION_NOTIFICATION = 2;
86 static final int VOICEMAIL_NOTIFICATION = 3;
87 static final int CALL_FORWARD_NOTIFICATION = 4;
88 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
89 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090
91 /** The singleton NotificationMgr instance. */
92 private static NotificationMgr sInstance;
93
94 private PhoneGlobals mApp;
95 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096
97 private Context mContext;
98 private NotificationManager mNotificationManager;
99 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700100 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800102 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800103 private TelecomManager mTelecomManager;
104 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106 // used to track the notification of selected network unavailable
107 private boolean mSelectedUnavailableNotify = false;
108
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800109 // used to track whether the message waiting indicator is visible, per subscription id.
110 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
111
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112 /**
113 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700114 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 */
116 private NotificationMgr(PhoneGlobals app) {
117 mApp = app;
118 mContext = app;
119 mNotificationManager =
120 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
121 mStatusBarManager =
122 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700123 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800124 mPhone = app.mCM.getDefaultPhone();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800125 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800126 mTelecomManager = TelecomManager.from(mContext);
127 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700128
Ta-wei Yenfb4f0502016-05-27 12:15:43 -0700129 mSubscriptionManager.addOnSubscriptionsChangedListener(
130 new OnSubscriptionsChangedListener() {
131 @Override
132 public void onSubscriptionsChanged() {
133 updateActivePhonesMwi();
134 }
135 });
136 }
137
138 public void updateActivePhonesMwi() {
139 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
140
141 if (subInfos == null) {
142 return;
143 }
144
145 for (int i = 0; i < subInfos.size(); i++) {
146 int subId = subInfos.get(i).getSubscriptionId();
147 refreshMwi(subId);
148 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 }
150
151 /**
152 * Initialize the singleton NotificationMgr instance.
153 *
154 * This is only done once, at startup, from PhoneApp.onCreate().
155 * From then on, the NotificationMgr instance is available via the
156 * PhoneApp's public "notificationMgr" field, which is why there's no
157 * getInstance() method here.
158 */
159 /* package */ static NotificationMgr init(PhoneGlobals app) {
160 synchronized (NotificationMgr.class) {
161 if (sInstance == null) {
162 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163 } else {
164 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
165 }
166 return sInstance;
167 }
168 }
169
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170 /** The projection to use when querying the phones table */
171 static final String[] PHONES_PROJECTION = new String[] {
172 PhoneLookup.NUMBER,
173 PhoneLookup.DISPLAY_NAME,
174 PhoneLookup._ID
175 };
176
177 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800178 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
179 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
180 * settings screen. The voicemail notification sound is suppressed.
181 *
182 * @param subId The subscription Id.
183 */
184 /* package */ void refreshMwi(int subId) {
185 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
186 // reference the single subid stored in the mMwiVisible map.
187 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
188 if (mMwiVisible.keySet().size() == 1) {
189 Set<Integer> keySet = mMwiVisible.keySet();
190 Iterator<Integer> keyIt = keySet.iterator();
191 if (!keyIt.hasNext()) {
192 return;
193 }
194 subId = keyIt.next();
195 }
196 }
197 if (mMwiVisible.containsKey(subId)) {
198 boolean mwiVisible = mMwiVisible.get(subId);
199 if (mwiVisible) {
200 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
201 }
202 }
203 }
204
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700205 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
206 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
207 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
208 + subId);
209 return;
210 }
211
212 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
213 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
214 .apply();
215 }
216
217 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
218 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
219 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
220 return true;
221 }
222 return PreferenceManager
223 .getDefaultSharedPreferences(mContext)
224 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
225 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800226 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 * Updates the message waiting indicator (voicemail) notification.
228 *
229 * @param visible true if there are messages waiting
230 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800231 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800232 updateMwi(subId, visible, true /* enableNotificationSound */);
233 }
234
235 /**
236 * Updates the message waiting indicator (voicemail) notification.
237 *
238 * @param subId the subId to update.
239 * @param visible true if there are messages waiting
240 * @param enableNotificationSound {@code true} if the notification sound should be played.
241 */
242 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800243 if (!PhoneGlobals.sVoiceCapable) {
244 // Do not show the message waiting indicator on devices which are not voice capable.
245 // These events *should* be blocked at the telephony layer for such devices.
246 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
247 return;
248 }
249
Nancy Chen2cf7f292015-05-15 11:00:10 -0700250 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800251 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800252 mMwiVisible.put(subId, visible);
253
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800255 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800256 Log.w(LOG_TAG, "Found null phone for: " + subId);
257 return;
258 }
259
260 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
261 if (subInfo == null) {
262 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800263 return;
264 }
265
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 int resId = android.R.drawable.stat_notify_voicemail;
267
268 // This Notification can get a lot fancier once we have more
269 // information about the current voicemail messages.
270 // (For example, the current voicemail system can't tell
271 // us the caller-id or timestamp of a message, or tell us the
272 // message count.)
273
274 // But for now, the UI is ultra-simple: if the MWI indication
275 // is supposed to be visible, just show a single generic
276 // notification.
277
278 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800279 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 if (DBG) log("- got vm number: '" + vmNumber + "'");
281
Andrew Leea82b8202014-11-21 16:18:28 -0800282 // The voicemail number may be null because:
283 // (1) This phone has no voicemail number.
284 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
285 // happen when the device first boots if we get a MWI notification when we
286 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800287 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
288 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800290 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 }
292
Bryce Lee5dc90842015-08-11 07:57:14 -0700293 Integer vmCount = null;
294
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800295 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700296 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
298 notificationTitle = String.format(titleFormat, vmCount);
299 }
300
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800301 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
302 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
303
304 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700306 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
307
308 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800309 notificationText = mContext.getString(
310 R.string.notification_voicemail_no_vm_number);
311
312 // If the voicemail number if unknown, instead of calling voicemail, take the user
313 // to the voicemail settings.
314 notificationText = mContext.getString(
315 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700316 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800317 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700318 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800320 if (mTelephonyManager.getPhoneCount() > 1) {
321 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800322 } else {
323 notificationText = String.format(
324 mContext.getString(R.string.notification_voicemail_text_format),
325 PhoneNumberUtils.formatNumber(vmNumber));
326 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800327 intent = new Intent(
328 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700329 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800330 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 }
332
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800333 PendingIntent pendingIntent =
334 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700335
Nancy Chenb4a92702014-12-04 15:57:29 -0800336 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700337 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700338 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700339 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 builder.setSmallIcon(resId)
341 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800342 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 .setContentTitle(notificationTitle)
344 .setContentText(notificationText)
345 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800346 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700347 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700348 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
fionaxu8b7620d2017-05-01 16:22:17 -0700349 .setChannel(NotificationChannelController.CHANNEL_ID_VOICE_MAIL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350
Andrew Lee99d0ac22014-10-10 13:18:04 -0700351 final Notification notification = builder.build();
352 List<UserInfo> users = mUserManager.getUsers(true);
353 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700354 final UserInfo user = users.get(i);
355 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700356 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700357 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700358 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800359 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800360 pendingIntent, isSettingsIntent, userHandle)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700361 mNotificationManager.notifyAsUser(
362 Integer.toString(subId) /* tag */,
363 VOICEMAIL_NOTIFICATION,
364 notification,
365 userHandle);
366 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700367 }
368 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 } else {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800370 List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
371 for (int i = 0; i < users.size(); i++) {
372 final UserInfo user = users.get(i);
373 final UserHandle userHandle = user.getUserHandle();
374 if (!mUserManager.hasUserRestriction(
375 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
376 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800377 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
378 false, userHandle)) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800379 mNotificationManager.cancelAsUser(
380 Integer.toString(subId) /* tag */,
381 VOICEMAIL_NOTIFICATION,
382 userHandle);
383 }
384 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700385 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 }
387 }
388
389 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800390 * Sends a broadcast with the voicemail notification information to the default dialer. This
391 * method is also used to indicate to the default dialer when to clear the
392 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700393 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800394 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700395 * @param count The number of pending voicemail messages to indicate on the notification. A
396 * Value of 0 is passed here to indicate that the notification should be cleared.
397 * @param number The voicemail phone number if specified.
398 * @param pendingIntent The intent that should be passed as the action to be taken.
399 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
400 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800401 * @param userHandle The user to receive the notification. Each user can have their own default
402 * dialer.
403 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700404 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800405 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
406 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
407 UserHandle userHandle) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800408
409 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
410 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700411 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700412 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800413 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
414 PhoneUtils.makePstnPhoneAccountHandle(phone));
Bryce Lee5dc90842015-08-11 07:57:14 -0700415 if (count != null) {
416 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
417 }
418
419 // Additional information about the voicemail notification beyond the count is only
420 // present when the count not specified or greater than 0. The value of 0 represents
421 // clearing the notification, which does not require additional information.
422 if (count == null || count > 0) {
423 if (!TextUtils.isEmpty(number)) {
424 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
425 }
426
427 if (pendingIntent != null) {
428 intent.putExtra(isSettingsIntent
429 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
430 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
431 pendingIntent);
432 }
433 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800434 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700435 return true;
436 }
437
438 return false;
439 }
440
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800441 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
442 String dialerPackage = DefaultDialerManager
443 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
444 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
445 .setPackage(dialerPackage);
446 }
447
448 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
449 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
450 if (intent == null) {
451 return false;
452 }
453
454 List<ResolveInfo> receivers = mContext.getPackageManager()
455 .queryBroadcastReceivers(intent, 0);
456 return receivers.size() > 0;
457 }
458
Bryce Lee5dc90842015-08-11 07:57:14 -0700459 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700460 * Updates the message call forwarding indicator notification.
461 *
462 * @param visible true if there are messages waiting
463 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800464 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465 if (DBG) log("updateCfi(): " + visible);
466 if (visible) {
467 // If Unconditional Call Forwarding (forward all calls) for VOICE
468 // is enabled, just show a notification. We'll default to expanded
469 // view for now, so the there is less confusion about the icon. If
470 // it is deemed too weird to have CF indications as expanded views,
471 // then we'll flip the flag back.
472
473 // TODO: We may want to take a look to see if the notification can
474 // display the target to forward calls to. This will require some
475 // effort though, since there are multiple layers of messages that
476 // will need to propagate that information.
477
Andrew Leed5165b02014-12-05 15:53:58 -0800478 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
479 if (subInfo == null) {
480 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
481 return;
482 }
483
484 String notificationTitle;
485 if (mTelephonyManager.getPhoneCount() > 1) {
486 notificationTitle = subInfo.getDisplayName().toString();
487 } else {
488 notificationTitle = mContext.getString(R.string.labelCF);
489 }
490
fionaxu8b7620d2017-05-01 16:22:17 -0700491 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700492 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800493 .setColor(subInfo.getIconTint())
494 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700495 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
496 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700497 .setOngoing(true)
fionaxu8b7620d2017-05-01 16:22:17 -0700498 .setChannel(NotificationChannelController.CHANNEL_ID_CALL_FORWARD);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499
Andrew Lee99d0ac22014-10-10 13:18:04 -0700500 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800501 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700502 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800503 SubscriptionInfoHelper.addExtrasToIntent(
504 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
505 PendingIntent contentIntent =
506 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700507
508 List<UserInfo> users = mUserManager.getUsers(true);
509 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800510 final UserInfo user = users.get(i);
511 if (user.isManagedProfile()) {
512 continue;
513 }
514 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700515 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800516 mNotificationManager.notifyAsUser(
517 Integer.toString(subId) /* tag */,
518 CALL_FORWARD_NOTIFICATION,
519 builder.build(),
520 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700523 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800524 Integer.toString(subId) /* tag */,
525 CALL_FORWARD_NOTIFICATION,
526 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700527 }
528 }
529
530 /**
531 * Shows the "data disconnected due to roaming" notification, which
532 * appears when you lose data connectivity because you're roaming and
533 * you have the "data roaming" feature turned off.
534 */
535 /* package */ void showDataDisconnectedRoaming() {
536 if (DBG) log("showDataDisconnectedRoaming()...");
537
538 // "Mobile network settings" screen / dialog
539 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700540 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541
542 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
543
fionaxu8b7620d2017-05-01 16:22:17 -0700544 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700545 .setSmallIcon(android.R.drawable.stat_sys_warning)
546 .setContentTitle(mContext.getText(R.string.roaming))
547 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700548 .setContentText(contentText)
fionaxu8b7620d2017-05-01 16:22:17 -0700549 .setChannel(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700550
Andrew Lee99d0ac22014-10-10 13:18:04 -0700551 List<UserInfo> users = mUserManager.getUsers(true);
552 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800553 final UserInfo user = users.get(i);
554 if (user.isManagedProfile()) {
555 continue;
556 }
557 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700558 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700559 final Notification notif =
560 new Notification.BigTextStyle(builder).bigText(contentText).build();
561 mNotificationManager.notifyAsUser(
562 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
563 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564 }
565
566 /**
567 * Turns off the "data disconnected due to roaming" notification.
568 */
569 /* package */ void hideDataDisconnectedRoaming() {
570 if (DBG) log("hideDataDisconnectedRoaming()...");
571 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
572 }
573
574 /**
575 * Display the network selection "no service" notification
576 * @param operator is the numeric operator number
577 */
578 private void showNetworkSelection(String operator) {
579 if (DBG) log("showNetworkSelection(" + operator + ")...");
580
fionaxu8b7620d2017-05-01 16:22:17 -0700581 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700582 .setSmallIcon(android.R.drawable.stat_sys_warning)
583 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
584 .setContentText(
585 mContext.getString(R.string.notification_network_selection_text, operator))
586 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700587 .setOngoing(true)
fionaxu8b7620d2017-05-01 16:22:17 -0700588 .setChannel(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589
590 // create the target network operators settings intent
591 Intent intent = new Intent(Intent.ACTION_MAIN);
592 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
593 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
594 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700595 intent.setComponent(new ComponentName(
596 mContext.getString(R.string.network_operator_settings_package),
597 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700598 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700599 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600
Andrew Lee99d0ac22014-10-10 13:18:04 -0700601 List<UserInfo> users = mUserManager.getUsers(true);
602 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800603 final UserInfo user = users.get(i);
604 if (user.isManagedProfile()) {
605 continue;
606 }
607 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700608 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700609 mNotificationManager.notifyAsUser(
610 null /* tag */,
611 SELECTED_OPERATOR_FAIL_NOTIFICATION,
612 builder.build(),
613 userHandle);
614 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700615 }
616
617 /**
618 * Turn off the network selection "no service" notification
619 */
620 private void cancelNetworkSelection() {
621 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700622 mNotificationManager.cancelAsUser(
623 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700624 }
625
626 /**
627 * Update notification about no service of user selected operator
628 *
629 * @param serviceState Phone service state
630 */
631 void updateNetworkSelection(int serviceState) {
632 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800633 int subId = mPhone.getSubId();
634 if (SubscriptionManager.isValidSubscriptionId(subId)) {
635 // get the shared preference of network_selection.
636 // empty is auto mode, otherwise it is the operator alpha name
637 // in case there is no operator name, check the operator numeric
638 SharedPreferences sp =
639 PreferenceManager.getDefaultSharedPreferences(mContext);
640 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800641 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800642 if (TextUtils.isEmpty(networkSelection)) {
643 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800644 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800645 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646
Amit Mahajana60be872015-01-15 16:05:08 -0800647 if (DBG) log("updateNetworkSelection()..." + "state = " +
648 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700649
Amit Mahajana60be872015-01-15 16:05:08 -0800650 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
651 && !TextUtils.isEmpty(networkSelection)) {
Hall Liu2b846c72016-02-09 18:21:24 -0800652 showNetworkSelection(networkSelection);
653 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800654 } else {
655 if (mSelectedUnavailableNotify) {
656 cancelNetworkSelection();
657 mSelectedUnavailableNotify = false;
658 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700659 }
660 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800661 if (DBG) log("updateNetworkSelection()..." + "state = " +
662 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700663 }
664 }
665 }
666
667 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
668 if (mToast != null) {
669 mToast.cancel();
670 }
671
672 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
673 mToast.show();
674 }
675
676 private void log(String msg) {
677 Log.d(LOG_TAG, msg);
678 }
679}