blob: fdbd1a3e6e8fedfa4eb64adbfd24c18830242495 [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;
Ta-wei Yen41fb43e2016-08-17 15:16:01 -070057import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Andrew Leebf07f762015-04-07 19:05:50 -070058import com.android.phone.settings.VoicemailSettingsActivity;
Nancy Chen2cf7f292015-05-15 11:00:10 -070059import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
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);
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700252 if (visible && phone != null && shouldCheckVisualVoicemailConfigurationForMwi(subId)) {
Nancy Chen2cf7f292015-05-15 11:00:10 -0700253 VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
254 PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
Ta-wei Yen41fb43e2016-08-17 15:16:01 -0700255 if (queryHelper.isVoicemailSourceConfigured(phoneAccount)) {
256 Log.v(LOG_TAG, "Source configured for visual voicemail, hiding mwi.");
257 // MWI may not be suppressed if the PIN is not set on VVM3 because it is also a
258 // "Not OK" configuration state. But VVM3 never send a MWI after the service is
259 // activated so this should be fine.
260 // TODO(twyen): once unbundled the client should be able to set a flag to suppress
261 // MWI, instead of letting the NotificationMgr try to interpret the states.
Nancy Chen2cf7f292015-05-15 11:00:10 -0700262 visible = false;
263 }
264 }
265
Yorke Lee67a62a22014-12-15 18:46:17 -0800266 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800267 mMwiVisible.put(subId, visible);
268
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800270 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800271 Log.w(LOG_TAG, "Found null phone for: " + subId);
272 return;
273 }
274
275 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
276 if (subInfo == null) {
277 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800278 return;
279 }
280
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 int resId = android.R.drawable.stat_notify_voicemail;
282
283 // This Notification can get a lot fancier once we have more
284 // information about the current voicemail messages.
285 // (For example, the current voicemail system can't tell
286 // us the caller-id or timestamp of a message, or tell us the
287 // message count.)
288
289 // But for now, the UI is ultra-simple: if the MWI indication
290 // is supposed to be visible, just show a single generic
291 // notification.
292
293 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800294 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700295 if (DBG) log("- got vm number: '" + vmNumber + "'");
296
Andrew Leea82b8202014-11-21 16:18:28 -0800297 // The voicemail number may be null because:
298 // (1) This phone has no voicemail number.
299 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
300 // happen when the device first boots if we get a MWI notification when we
301 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800302 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
303 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800305 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 }
307
Bryce Lee5dc90842015-08-11 07:57:14 -0700308 Integer vmCount = null;
309
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800310 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700311 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
313 notificationTitle = String.format(titleFormat, vmCount);
314 }
315
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800316 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
317 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
318
319 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700321 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
322
323 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800324 notificationText = mContext.getString(
325 R.string.notification_voicemail_no_vm_number);
326
327 // If the voicemail number if unknown, instead of calling voicemail, take the user
328 // to the voicemail settings.
329 notificationText = mContext.getString(
330 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700331 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800332 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700333 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800335 if (mTelephonyManager.getPhoneCount() > 1) {
336 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800337 } else {
338 notificationText = String.format(
339 mContext.getString(R.string.notification_voicemail_text_format),
340 PhoneNumberUtils.formatNumber(vmNumber));
341 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800342 intent = new Intent(
343 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700344 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800345 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 }
347
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800348 PendingIntent pendingIntent =
349 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800350 Uri ringtoneUri = null;
351
352 if (enableNotificationSound) {
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700353 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800354 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355
Nancy Chenb4a92702014-12-04 15:57:29 -0800356 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700357 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700358 subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 Notification.Builder builder = new Notification.Builder(mContext);
360 builder.setSmallIcon(resId)
361 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800362 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363 .setContentTitle(notificationTitle)
364 .setContentText(notificationText)
365 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700366 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800367 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700368 .setOngoing(carrierConfig.getBoolean(
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700369 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800371 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700372 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700374
375 final Notification notification = builder.build();
376 List<UserInfo> users = mUserManager.getUsers(true);
377 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700378 final UserInfo user = users.get(i);
379 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700380 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700381 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700382 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800383 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800384 pendingIntent, isSettingsIntent, userHandle)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700385 mNotificationManager.notifyAsUser(
386 Integer.toString(subId) /* tag */,
387 VOICEMAIL_NOTIFICATION,
388 notification,
389 userHandle);
390 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700391 }
392 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393 } else {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800394 List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
395 for (int i = 0; i < users.size(); i++) {
396 final UserInfo user = users.get(i);
397 final UserHandle userHandle = user.getUserHandle();
398 if (!mUserManager.hasUserRestriction(
399 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
400 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800401 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
402 false, userHandle)) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800403 mNotificationManager.cancelAsUser(
404 Integer.toString(subId) /* tag */,
405 VOICEMAIL_NOTIFICATION,
406 userHandle);
407 }
408 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700409 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 }
411 }
412
413 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800414 * Sends a broadcast with the voicemail notification information to the default dialer. This
415 * method is also used to indicate to the default dialer when to clear the
416 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700417 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800418 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700419 * @param count The number of pending voicemail messages to indicate on the notification. A
420 * Value of 0 is passed here to indicate that the notification should be cleared.
421 * @param number The voicemail phone number if specified.
422 * @param pendingIntent The intent that should be passed as the action to be taken.
423 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
424 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800425 * @param userHandle The user to receive the notification. Each user can have their own default
426 * dialer.
427 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700428 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800429 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
430 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
431 UserHandle userHandle) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800432
433 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
434 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700435 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700436 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800437 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
438 PhoneUtils.makePstnPhoneAccountHandle(phone));
Bryce Lee5dc90842015-08-11 07:57:14 -0700439 if (count != null) {
440 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
441 }
442
443 // Additional information about the voicemail notification beyond the count is only
444 // present when the count not specified or greater than 0. The value of 0 represents
445 // clearing the notification, which does not require additional information.
446 if (count == null || count > 0) {
447 if (!TextUtils.isEmpty(number)) {
448 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
449 }
450
451 if (pendingIntent != null) {
452 intent.putExtra(isSettingsIntent
453 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
454 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
455 pendingIntent);
456 }
457 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800458 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700459 return true;
460 }
461
462 return false;
463 }
464
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800465 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
466 String dialerPackage = DefaultDialerManager
467 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
468 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
469 .setPackage(dialerPackage);
470 }
471
472 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
473 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
474 if (intent == null) {
475 return false;
476 }
477
478 List<ResolveInfo> receivers = mContext.getPackageManager()
479 .queryBroadcastReceivers(intent, 0);
480 return receivers.size() > 0;
481 }
482
Bryce Lee5dc90842015-08-11 07:57:14 -0700483 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484 * Updates the message call forwarding indicator notification.
485 *
486 * @param visible true if there are messages waiting
487 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800488 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700489 if (DBG) log("updateCfi(): " + visible);
490 if (visible) {
491 // If Unconditional Call Forwarding (forward all calls) for VOICE
492 // is enabled, just show a notification. We'll default to expanded
493 // view for now, so the there is less confusion about the icon. If
494 // it is deemed too weird to have CF indications as expanded views,
495 // then we'll flip the flag back.
496
497 // TODO: We may want to take a look to see if the notification can
498 // display the target to forward calls to. This will require some
499 // effort though, since there are multiple layers of messages that
500 // will need to propagate that information.
501
Andrew Leed5165b02014-12-05 15:53:58 -0800502 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
503 if (subInfo == null) {
504 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
505 return;
506 }
507
508 String notificationTitle;
509 if (mTelephonyManager.getPhoneCount() > 1) {
510 notificationTitle = subInfo.getDisplayName().toString();
511 } else {
512 notificationTitle = mContext.getString(R.string.labelCF);
513 }
514
Andrew Lee99d0ac22014-10-10 13:18:04 -0700515 Notification.Builder builder = new Notification.Builder(mContext)
516 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800517 .setColor(subInfo.getIconTint())
518 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700519 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
520 .setShowWhen(false)
521 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522
Andrew Lee99d0ac22014-10-10 13:18:04 -0700523 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800524 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700525 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800526 SubscriptionInfoHelper.addExtrasToIntent(
527 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
528 PendingIntent contentIntent =
529 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700530
531 List<UserInfo> users = mUserManager.getUsers(true);
532 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800533 final UserInfo user = users.get(i);
534 if (user.isManagedProfile()) {
535 continue;
536 }
537 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700538 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800539 mNotificationManager.notifyAsUser(
540 Integer.toString(subId) /* tag */,
541 CALL_FORWARD_NOTIFICATION,
542 builder.build(),
543 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700544 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700545 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700546 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800547 Integer.toString(subId) /* tag */,
548 CALL_FORWARD_NOTIFICATION,
549 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700550 }
551 }
552
553 /**
554 * Shows the "data disconnected due to roaming" notification, which
555 * appears when you lose data connectivity because you're roaming and
556 * you have the "data roaming" feature turned off.
557 */
558 /* package */ void showDataDisconnectedRoaming() {
559 if (DBG) log("showDataDisconnectedRoaming()...");
560
561 // "Mobile network settings" screen / dialog
562 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700563 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700564
565 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
566
Andrew Lee99d0ac22014-10-10 13:18:04 -0700567 final Notification.Builder builder = new Notification.Builder(mContext)
568 .setSmallIcon(android.R.drawable.stat_sys_warning)
569 .setContentTitle(mContext.getText(R.string.roaming))
570 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
571 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572
Andrew Lee99d0ac22014-10-10 13:18:04 -0700573 List<UserInfo> users = mUserManager.getUsers(true);
574 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800575 final UserInfo user = users.get(i);
576 if (user.isManagedProfile()) {
577 continue;
578 }
579 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700580 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700581 final Notification notif =
582 new Notification.BigTextStyle(builder).bigText(contentText).build();
583 mNotificationManager.notifyAsUser(
584 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
585 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700586 }
587
588 /**
589 * Turns off the "data disconnected due to roaming" notification.
590 */
591 /* package */ void hideDataDisconnectedRoaming() {
592 if (DBG) log("hideDataDisconnectedRoaming()...");
593 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
594 }
595
596 /**
597 * Display the network selection "no service" notification
598 * @param operator is the numeric operator number
599 */
600 private void showNetworkSelection(String operator) {
601 if (DBG) log("showNetworkSelection(" + operator + ")...");
602
Andrew Lee99d0ac22014-10-10 13:18:04 -0700603 Notification.Builder builder = new Notification.Builder(mContext)
604 .setSmallIcon(android.R.drawable.stat_sys_warning)
605 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
606 .setContentText(
607 mContext.getString(R.string.notification_network_selection_text, operator))
608 .setShowWhen(false)
609 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700610
611 // create the target network operators settings intent
612 Intent intent = new Intent(Intent.ACTION_MAIN);
613 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
614 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
615 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700616 intent.setComponent(new ComponentName(
617 mContext.getString(R.string.network_operator_settings_package),
618 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700619 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700620 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700621
Andrew Lee99d0ac22014-10-10 13:18:04 -0700622 List<UserInfo> users = mUserManager.getUsers(true);
623 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800624 final UserInfo user = users.get(i);
625 if (user.isManagedProfile()) {
626 continue;
627 }
628 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700629 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700630 mNotificationManager.notifyAsUser(
631 null /* tag */,
632 SELECTED_OPERATOR_FAIL_NOTIFICATION,
633 builder.build(),
634 userHandle);
635 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 }
637
638 /**
639 * Turn off the network selection "no service" notification
640 */
641 private void cancelNetworkSelection() {
642 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700643 mNotificationManager.cancelAsUser(
644 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 }
646
647 /**
648 * Update notification about no service of user selected operator
649 *
650 * @param serviceState Phone service state
651 */
652 void updateNetworkSelection(int serviceState) {
653 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800654 int subId = mPhone.getSubId();
655 if (SubscriptionManager.isValidSubscriptionId(subId)) {
656 // get the shared preference of network_selection.
657 // empty is auto mode, otherwise it is the operator alpha name
658 // in case there is no operator name, check the operator numeric
659 SharedPreferences sp =
660 PreferenceManager.getDefaultSharedPreferences(mContext);
661 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800662 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800663 if (TextUtils.isEmpty(networkSelection)) {
664 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800665 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800666 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667
Amit Mahajana60be872015-01-15 16:05:08 -0800668 if (DBG) log("updateNetworkSelection()..." + "state = " +
669 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700670
Amit Mahajana60be872015-01-15 16:05:08 -0800671 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
672 && !TextUtils.isEmpty(networkSelection)) {
Hall Liu2b846c72016-02-09 18:21:24 -0800673 showNetworkSelection(networkSelection);
674 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800675 } else {
676 if (mSelectedUnavailableNotify) {
677 cancelNetworkSelection();
678 mSelectedUnavailableNotify = false;
679 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 }
681 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800682 if (DBG) log("updateNetworkSelection()..." + "state = " +
683 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700684 }
685 }
686 }
687
688 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
689 if (mToast != null) {
690 mToast.cancel();
691 }
692
693 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
694 mToast.show();
695 }
696
697 private void log(String msg) {
698 Log.d(LOG_TAG, msg);
699 }
700}