blob: abe539732797436d8129ab4355492cf964ff87d8 [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 Yen5bb19562016-11-16 11:05:37 -0800383 if (!maybeSendVoicemailNotificationUsingDefaultDialer(vmCount, vmNumber,
384 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()) {
401 if (!maybeSendVoicemailNotificationUsingDefaultDialer(0, null, null, false,
402 userHandle)) {
403 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.
418 * @param count The number of pending voicemail messages to indicate on the notification. A
419 * Value of 0 is passed here to indicate that the notification should be cleared.
420 * @param number The voicemail phone number if specified.
421 * @param pendingIntent The intent that should be passed as the action to be taken.
422 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
423 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800424 * @param userHandle The user to receive the notification. Each user can have their own default
425 * dialer.
426 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700427 */
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800428 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Integer count, String number,
429 PendingIntent pendingIntent, boolean isSettingsIntent, UserHandle userHandle) {
430
431 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
432 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700433 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700434 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
435
436 if (count != null) {
437 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
438 }
439
440 // Additional information about the voicemail notification beyond the count is only
441 // present when the count not specified or greater than 0. The value of 0 represents
442 // clearing the notification, which does not require additional information.
443 if (count == null || count > 0) {
444 if (!TextUtils.isEmpty(number)) {
445 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
446 }
447
448 if (pendingIntent != null) {
449 intent.putExtra(isSettingsIntent
450 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
451 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
452 pendingIntent);
453 }
454 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800455 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700456 return true;
457 }
458
459 return false;
460 }
461
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800462 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
463 String dialerPackage = DefaultDialerManager
464 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
465 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
466 .setPackage(dialerPackage);
467 }
468
469 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
470 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
471 if (intent == null) {
472 return false;
473 }
474
475 List<ResolveInfo> receivers = mContext.getPackageManager()
476 .queryBroadcastReceivers(intent, 0);
477 return receivers.size() > 0;
478 }
479
Bryce Lee5dc90842015-08-11 07:57:14 -0700480 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481 * Updates the message call forwarding indicator notification.
482 *
483 * @param visible true if there are messages waiting
484 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800485 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 if (DBG) log("updateCfi(): " + visible);
487 if (visible) {
488 // If Unconditional Call Forwarding (forward all calls) for VOICE
489 // is enabled, just show a notification. We'll default to expanded
490 // view for now, so the there is less confusion about the icon. If
491 // it is deemed too weird to have CF indications as expanded views,
492 // then we'll flip the flag back.
493
494 // TODO: We may want to take a look to see if the notification can
495 // display the target to forward calls to. This will require some
496 // effort though, since there are multiple layers of messages that
497 // will need to propagate that information.
498
Andrew Leed5165b02014-12-05 15:53:58 -0800499 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
500 if (subInfo == null) {
501 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
502 return;
503 }
504
505 String notificationTitle;
506 if (mTelephonyManager.getPhoneCount() > 1) {
507 notificationTitle = subInfo.getDisplayName().toString();
508 } else {
509 notificationTitle = mContext.getString(R.string.labelCF);
510 }
511
Andrew Lee99d0ac22014-10-10 13:18:04 -0700512 Notification.Builder builder = new Notification.Builder(mContext)
513 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800514 .setColor(subInfo.getIconTint())
515 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700516 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
517 .setShowWhen(false)
518 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519
Andrew Lee99d0ac22014-10-10 13:18:04 -0700520 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800521 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700522 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800523 SubscriptionInfoHelper.addExtrasToIntent(
524 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
525 PendingIntent contentIntent =
526 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700527
528 List<UserInfo> users = mUserManager.getUsers(true);
529 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800530 final UserInfo user = users.get(i);
531 if (user.isManagedProfile()) {
532 continue;
533 }
534 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700535 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800536 mNotificationManager.notifyAsUser(
537 Integer.toString(subId) /* tag */,
538 CALL_FORWARD_NOTIFICATION,
539 builder.build(),
540 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700543 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800544 Integer.toString(subId) /* tag */,
545 CALL_FORWARD_NOTIFICATION,
546 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700547 }
548 }
549
550 /**
551 * Shows the "data disconnected due to roaming" notification, which
552 * appears when you lose data connectivity because you're roaming and
553 * you have the "data roaming" feature turned off.
554 */
555 /* package */ void showDataDisconnectedRoaming() {
556 if (DBG) log("showDataDisconnectedRoaming()...");
557
558 // "Mobile network settings" screen / dialog
559 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700560 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700561
562 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
563
Andrew Lee99d0ac22014-10-10 13:18:04 -0700564 final Notification.Builder builder = new Notification.Builder(mContext)
565 .setSmallIcon(android.R.drawable.stat_sys_warning)
566 .setContentTitle(mContext.getText(R.string.roaming))
567 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
568 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700569
Andrew Lee99d0ac22014-10-10 13:18:04 -0700570 List<UserInfo> users = mUserManager.getUsers(true);
571 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800572 final UserInfo user = users.get(i);
573 if (user.isManagedProfile()) {
574 continue;
575 }
576 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700577 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700578 final Notification notif =
579 new Notification.BigTextStyle(builder).bigText(contentText).build();
580 mNotificationManager.notifyAsUser(
581 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
582 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 }
584
585 /**
586 * Turns off the "data disconnected due to roaming" notification.
587 */
588 /* package */ void hideDataDisconnectedRoaming() {
589 if (DBG) log("hideDataDisconnectedRoaming()...");
590 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
591 }
592
593 /**
594 * Display the network selection "no service" notification
595 * @param operator is the numeric operator number
596 */
597 private void showNetworkSelection(String operator) {
598 if (DBG) log("showNetworkSelection(" + operator + ")...");
599
Andrew Lee99d0ac22014-10-10 13:18:04 -0700600 Notification.Builder builder = new Notification.Builder(mContext)
601 .setSmallIcon(android.R.drawable.stat_sys_warning)
602 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
603 .setContentText(
604 mContext.getString(R.string.notification_network_selection_text, operator))
605 .setShowWhen(false)
606 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607
608 // create the target network operators settings intent
609 Intent intent = new Intent(Intent.ACTION_MAIN);
610 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
611 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
612 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700613 intent.setComponent(new ComponentName(
614 mContext.getString(R.string.network_operator_settings_package),
615 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700616 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700617 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618
Andrew Lee99d0ac22014-10-10 13:18:04 -0700619 List<UserInfo> users = mUserManager.getUsers(true);
620 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800621 final UserInfo user = users.get(i);
622 if (user.isManagedProfile()) {
623 continue;
624 }
625 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700626 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700627 mNotificationManager.notifyAsUser(
628 null /* tag */,
629 SELECTED_OPERATOR_FAIL_NOTIFICATION,
630 builder.build(),
631 userHandle);
632 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700633 }
634
635 /**
636 * Turn off the network selection "no service" notification
637 */
638 private void cancelNetworkSelection() {
639 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700640 mNotificationManager.cancelAsUser(
641 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 }
643
644 /**
645 * Update notification about no service of user selected operator
646 *
647 * @param serviceState Phone service state
648 */
649 void updateNetworkSelection(int serviceState) {
650 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800651 int subId = mPhone.getSubId();
652 if (SubscriptionManager.isValidSubscriptionId(subId)) {
653 // get the shared preference of network_selection.
654 // empty is auto mode, otherwise it is the operator alpha name
655 // in case there is no operator name, check the operator numeric
656 SharedPreferences sp =
657 PreferenceManager.getDefaultSharedPreferences(mContext);
658 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800659 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800660 if (TextUtils.isEmpty(networkSelection)) {
661 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800662 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800663 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664
Amit Mahajana60be872015-01-15 16:05:08 -0800665 if (DBG) log("updateNetworkSelection()..." + "state = " +
666 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667
Amit Mahajana60be872015-01-15 16:05:08 -0800668 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
669 && !TextUtils.isEmpty(networkSelection)) {
Hall Liu2b846c72016-02-09 18:21:24 -0800670 showNetworkSelection(networkSelection);
671 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800672 } else {
673 if (mSelectedUnavailableNotify) {
674 cancelNetworkSelection();
675 mSelectedUnavailableNotify = false;
676 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700677 }
678 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800679 if (DBG) log("updateNetworkSelection()..." + "state = " +
680 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700681 }
682 }
683 }
684
685 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
686 if (mToast != null) {
687 mToast.cancel();
688 }
689
690 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
691 mToast.show();
692 }
693
694 private void log(String msg) {
695 Log.d(LOG_TAG, msg);
696 }
697}