17d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon/*
27d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * Copyright (C) 2006 The Android Open Source Project
37d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon *
47d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * Licensed under the Apache License, Version 2.0 (the "License");
57d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * you may not use this file except in compliance with the License.
67d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * You may obtain a copy of the License at
77d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon *
87d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon *      http://www.apache.org/licenses/LICENSE-2.0
97d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon *
107d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * Unless required by applicable law or agreed to in writing, software
117d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * distributed under the License is distributed on an "AS IS" BASIS,
127d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * See the License for the specific language governing permissions and
147d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * limitations under the License.
157d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon */
167d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
177d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonpackage com.android.phone;
187d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
19afca2d6f6a2145586e4a956b1d1b5b8efc11f8f7Ta-wei Yenimport static android.Manifest.permission.READ_PHONE_STATE;
20afca2d6f6a2145586e4a956b1d1b5b8efc11f8f7Ta-wei Yen
217d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.app.Notification;
227d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.app.NotificationManager;
237d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.app.PendingIntent;
247d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.app.StatusBarManager;
257d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.content.ComponentName;
267d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.content.Context;
277d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.content.Intent;
287d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.content.SharedPreferences;
295bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yenimport android.content.pm.ResolveInfo;
3099d0ac2c693849e959601a275128a739569d2efbAndrew Leeimport android.content.pm.UserInfo;
31b4a9270ef0cd337732e6e88d9cf4ff5f72fb94d2Nancy Chenimport android.content.res.Resources;
327d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.net.Uri;
33c31f1f32963534763aaf82ae2544f3a94f13edcaJonathan Basseriimport android.os.PersistableBundle;
347d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.os.SystemProperties;
3599d0ac2c693849e959601a275128a739569d2efbAndrew Leeimport android.os.UserHandle;
3699d0ac2c693849e959601a275128a739569d2efbAndrew Leeimport android.os.UserManager;
377d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.preference.PreferenceManager;
387d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.provider.ContactsContract.PhoneLookup;
396d9bf52509149052f78337d64a91ff076294994cJeff Davidsonimport android.provider.Settings;
405bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yenimport android.telecom.DefaultDialerManager;
414d45d1cf58a2003378fd35912d6d73a00001bf06Tyler Gunnimport android.telecom.PhoneAccount;
42d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Leeimport android.telecom.PhoneAccountHandle;
43d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Leeimport android.telecom.TelecomManager;
443649bdb5813200904d4e887306dfebb1a0beffa8Jonathan Basseriimport android.telephony.CarrierConfigManager;
457d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.telephony.PhoneNumberUtils;
467d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.telephony.ServiceState;
472fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Leeimport android.telephony.SubscriptionInfo;
48a82b820e69f41585c3a2b76678040b93fdf54886Andrew Leeimport android.telephony.SubscriptionManager;
49d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Leeimport android.telephony.TelephonyManager;
507d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.text.TextUtils;
519c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunnimport android.util.ArrayMap;
527d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.util.Log;
537d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport android.widget.Toast;
54b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen
557d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport com.android.internal.telephony.Phone;
562ef9a48d76578caaa46c847bd17889866720cc7aJayachandran Cimport com.android.internal.telephony.PhoneFactory;
577d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordonimport com.android.internal.telephony.TelephonyCapabilities;
588b7620dee4a061bd987254709300e3face3f7b90fionaxuimport com.android.internal.telephony.util.NotificationChannelController;
59bf07f76dd57457da159b5d5504e391afbb4a3592Andrew Leeimport com.android.phone.settings.VoicemailSettingsActivity;
60b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen
619c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunnimport java.util.Iterator;
6299d0ac2c693849e959601a275128a739569d2efbAndrew Leeimport java.util.List;
639c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunnimport java.util.Set;
6499d0ac2c693849e959601a275128a739569d2efbAndrew Lee
657d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon/**
667d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * NotificationManager-related utility code for the Phone app.
677d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon *
687d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * This is a singleton object which acts as the interface to the
697d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * framework's NotificationManager, and is used to display status bar
707d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * icons and control other status bar-related behavior.
717d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon *
727d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon * @see PhoneGlobals.notificationMgr
737d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon */
74312b9c9112948c8ca7f74ea91b5e2585a66482c9Chiao Chengpublic class NotificationMgr {
75a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee    private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
767d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private static final boolean DBG =
777d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
787d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    // Do not check in with VDBG = true, since that may write PII to the system log.
797d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private static final boolean VDBG = false;
807d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
81b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen    private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
82b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen            "mwi_should_check_vvm_configuration_state_";
83b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen
847d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    // notification types
85f68db2ef27f3bdf14f0053c46d73df910bd394f2Santos Cordon    static final int MMI_NOTIFICATION = 1;
86f68db2ef27f3bdf14f0053c46d73df910bd394f2Santos Cordon    static final int NETWORK_SELECTION_NOTIFICATION = 2;
87f68db2ef27f3bdf14f0053c46d73df910bd394f2Santos Cordon    static final int VOICEMAIL_NOTIFICATION = 3;
88f68db2ef27f3bdf14f0053c46d73df910bd394f2Santos Cordon    static final int CALL_FORWARD_NOTIFICATION = 4;
89f68db2ef27f3bdf14f0053c46d73df910bd394f2Santos Cordon    static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
90f68db2ef27f3bdf14f0053c46d73df910bd394f2Santos Cordon    static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
917d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
927d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /** The singleton NotificationMgr instance. */
937d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private static NotificationMgr sInstance;
947d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
957d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private PhoneGlobals mApp;
967d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
977d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private Context mContext;
987d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private NotificationManager mNotificationManager;
997d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private StatusBarManager mStatusBarManager;
10099d0ac2c693849e959601a275128a739569d2efbAndrew Lee    private UserManager mUserManager;
1017d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private Toast mToast;
1022fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee    private SubscriptionManager mSubscriptionManager;
103d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee    private TelecomManager mTelecomManager;
104d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee    private TelephonyManager mTelephonyManager;
1057d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
1067d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    // used to track the notification of selected network unavailable
1077d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private boolean mSelectedUnavailableNotify = false;
1087d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
1099c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn    // used to track whether the message waiting indicator is visible, per subscription id.
1109c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn    private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
1119c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn
1127d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
1137d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Private constructor (this is a singleton).
114f68db2ef27f3bdf14f0053c46d73df910bd394f2Santos Cordon     * @see #init(PhoneGlobals)
1157d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
1167d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private NotificationMgr(PhoneGlobals app) {
1177d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        mApp = app;
1187d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        mContext = app;
1197d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        mNotificationManager =
1207d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
1217d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        mStatusBarManager =
1227d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
12399d0ac2c693849e959601a275128a739569d2efbAndrew Lee        mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
1242fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee        mSubscriptionManager = SubscriptionManager.from(mContext);
125d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee        mTelecomManager = TelecomManager.from(mContext);
126d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee        mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
1277d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
1287d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
1297d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
1307d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Initialize the singleton NotificationMgr instance.
1317d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     *
1327d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * This is only done once, at startup, from PhoneApp.onCreate().
1337d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * From then on, the NotificationMgr instance is available via the
1347d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * PhoneApp's public "notificationMgr" field, which is why there's no
1357d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * getInstance() method here.
1367d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
1377d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /* package */ static NotificationMgr init(PhoneGlobals app) {
1387d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        synchronized (NotificationMgr.class) {
1397d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            if (sInstance == null) {
1407d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                sInstance = new NotificationMgr(app);
1417d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            } else {
1427d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                Log.wtf(LOG_TAG, "init() called multiple times!  sInstance = " + sInstance);
1437d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            }
1447d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            return sInstance;
1457d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        }
1467d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
1477d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
1487d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /** The projection to use when querying the phones table */
1497d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    static final String[] PHONES_PROJECTION = new String[] {
1507d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        PhoneLookup.NUMBER,
1517d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        PhoneLookup.DISPLAY_NAME,
1527d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        PhoneLookup._ID
1537d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    };
1547d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
1557d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
1569c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     * Re-creates the message waiting indicator (voicemail) notification if it is showing.  Used to
1579c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     * refresh the voicemail intent on the indicator when the user changes it via the voicemail
1589c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     * settings screen.  The voicemail notification sound is suppressed.
1599c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     *
1609c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     * @param subId The subscription Id.
1619c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     */
1629c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn    /* package */ void refreshMwi(int subId) {
1639c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn        // In a single-sim device, subId can be -1 which means "no sub id".  In this case we will
1649c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn        // reference the single subid stored in the mMwiVisible map.
165a1390d471ebb739a14a01327729ba423e6a8c3b5Ta-wei Yen        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
1669c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            if (mMwiVisible.keySet().size() == 1) {
1679c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                Set<Integer> keySet = mMwiVisible.keySet();
1689c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                Iterator<Integer> keyIt = keySet.iterator();
1699c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                if (!keyIt.hasNext()) {
1709c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                    return;
1719c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                }
1729c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                subId = keyIt.next();
1739c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            }
1749c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn        }
1759c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn        if (mMwiVisible.containsKey(subId)) {
1769c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            boolean mwiVisible = mMwiVisible.get(subId);
1779c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            if (mwiVisible) {
178263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                mApp.notifier.updatePhoneStateListeners(true);
1799c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            }
1809c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn        }
1819c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn    }
1829c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn
183b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen    public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
184b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
185b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen            Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
186b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen                    + subId);
187b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen            return;
188b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen        }
189b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen
190b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen        PreferenceManager.getDefaultSharedPreferences(mContext).edit()
191b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen                .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
192b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen                .apply();
193b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen    }
194b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen
195b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen    private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
196b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
197b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen            Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
198b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen            return true;
199b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen        }
200b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen        return PreferenceManager
201b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen                .getDefaultSharedPreferences(mContext)
202b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen                .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
203b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen    }
2049c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn    /**
2057d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Updates the message waiting indicator (voicemail) notification.
2067d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     *
2077d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * @param visible true if there are messages waiting
2087d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
2092fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee    /* package */ void updateMwi(int subId, boolean visible) {
210282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen        updateMwi(subId, visible, false /* isRefresh */);
2119c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn    }
2129c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn
2139c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn    /**
2149c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     * Updates the message waiting indicator (voicemail) notification.
2159c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     *
2169c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     * @param subId the subId to update.
2179c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     * @param visible true if there are messages waiting
218282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen     * @param isRefresh {@code true} if the notification is a refresh and the user should not be
219282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen     * notified again.
2209c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn     */
221282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen    void updateMwi(int subId, boolean visible, boolean isRefresh) {
222a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee        if (!PhoneGlobals.sVoiceCapable) {
223a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            // Do not show the message waiting indicator on devices which are not voice capable.
224a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            // These events *should* be blocked at the telephony layer for such devices.
225a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
226a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            return;
227a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee        }
228a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee
2292cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        Phone phone = PhoneGlobals.getPhone(subId);
23067a62a2dd4941b983ed92f53b330a7c902408c5dYorke Lee        Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
231f8ad78f133fc0841d2cd6947f5be3aa94cf2252eAndrew Lee        mMwiVisible.put(subId, visible);
232f8ad78f133fc0841d2cd6947f5be3aa94cf2252eAndrew Lee
2337d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        if (visible) {
2342fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            if (phone == null) {
235d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                Log.w(LOG_TAG, "Found null phone for: " + subId);
236d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                return;
237d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            }
238d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee
239d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
240d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            if (subInfo == null) {
241d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                Log.w(LOG_TAG, "Found null subscription info for: " + subId);
2422fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee                return;
2432fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            }
2442fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee
2457d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            int resId = android.R.drawable.stat_notify_voicemail;
246263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro            if (mTelephonyManager.getPhoneCount() > 1) {
247263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
248263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                        : R.drawable.stat_notify_voicemail_sub2;
249263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro            }
2507d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
2517d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // This Notification can get a lot fancier once we have more
2527d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // information about the current voicemail messages.
2537d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // (For example, the current voicemail system can't tell
2547d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // us the caller-id or timestamp of a message, or tell us the
2557d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // message count.)
2567d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
2577d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // But for now, the UI is ultra-simple: if the MWI indication
2587d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // is supposed to be visible, just show a single generic
2597d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // notification.
2607d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
2617d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
2622fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            String vmNumber = phone.getVoiceMailNumber();
2637d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            if (DBG) log("- got vm number: '" + vmNumber + "'");
2647d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
265a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            // The voicemail number may be null because:
266a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            //   (1) This phone has no voicemail number.
267a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            //   (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
268a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            //       happen when the device first boots if we get a MWI notification when we
269a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee            //       register on the network before the SIM has loaded. In this case, the
2702fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            //       SubscriptionListener in CallNotifier will update this once the SIM is loaded.
2712fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
2727d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
273a82b820e69f41585c3a2b76678040b93fdf54886Andrew Lee                return;
2747d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            }
2757d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
2765dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            Integer vmCount = null;
2775dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee
2782fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
2795dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                vmCount = phone.getVoiceMessageCount();
2807d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
2817d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                notificationTitle = String.format(titleFormat, vmCount);
2827d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            }
2837d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
2849c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
2859c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
2869c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn
2879c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn            Intent intent;
2887d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            String notificationText;
2895dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
2905dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee
2915dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            if (isSettingsIntent) {
2929c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                notificationText = mContext.getString(
2939c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                        R.string.notification_voicemail_no_vm_number);
2949c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn
2959c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                // If the voicemail number if unknown, instead of calling voicemail, take the user
2969c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                // to the voicemail settings.
2979c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                notificationText = mContext.getString(
2989c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                        R.string.notification_voicemail_no_vm_number);
299bf07f76dd57457da159b5d5504e391afbb4a3592Andrew Lee                intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
3009c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
301bf07f76dd57457da159b5d5504e391afbb4a3592Andrew Lee                intent.setClass(mContext, VoicemailSettingsActivity.class);
3027d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            } else {
3039c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                if (mTelephonyManager.getPhoneCount() > 1) {
3049c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                    notificationText = subInfo.getDisplayName().toString();
305d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                } else {
306d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                    notificationText = String.format(
307d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                            mContext.getString(R.string.notification_voicemail_text_format),
308d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                            PhoneNumberUtils.formatNumber(vmNumber));
309d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                }
3109c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                intent = new Intent(
3119c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                        Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
3123649bdb5813200904d4e887306dfebb1a0beffa8Jonathan Basseri                                null));
3139c1071fdb42947afd215b0f4f3e81f871b1ff581Tyler Gunn                intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
3147d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            }
3157d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
3162fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            PendingIntent pendingIntent =
3172fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee                    PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
3187d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
319b4a9270ef0cd337732e6e88d9cf4ff5f72fb94d2Nancy Chen            Resources res = mContext.getResources();
320c31f1f32963534763aaf82ae2544f3a94f13edcaJonathan Basseri            PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
3219b37a873360aedd575d7ed189939fb0ad9f9251cTa-wei Yen                    subId);
3228b7620dee4a061bd987254709300e3face3f7b90fionaxu            Notification.Builder builder = new Notification.Builder(mContext);
3237d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            builder.setSmallIcon(resId)
3247d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                    .setWhen(System.currentTimeMillis())
325d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                    .setColor(subInfo.getIconTint())
3267d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                    .setContentTitle(notificationTitle)
3277d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                    .setContentText(notificationText)
3287d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                    .setContentIntent(pendingIntent)
329b4a9270ef0cd337732e6e88d9cf4ff5f72fb94d2Nancy Chen                    .setColor(res.getColor(R.color.dialer_theme_color))
3303649bdb5813200904d4e887306dfebb1a0beffa8Jonathan Basseri                    .setOngoing(carrierConfig.getBoolean(
33175b66a75f589be674bb56491a2681c55ab9c70a4fionaxu                            CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
332282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen                    .setChannel(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
333282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen                    .setOnlyAlertOnce(isRefresh);
3347d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
33599d0ac2c693849e959601a275128a739569d2efbAndrew Lee            final Notification notification = builder.build();
33699d0ac2c693849e959601a275128a739569d2efbAndrew Lee            List<UserInfo> users = mUserManager.getUsers(true);
33799d0ac2c693849e959601a275128a739569d2efbAndrew Lee            for (int i = 0; i < users.size(); i++) {
338047b1f9f0af3c9a0a285eb944b409a785040707cYorke Lee                final UserInfo user = users.get(i);
339047b1f9f0af3c9a0a285eb944b409a785040707cYorke Lee                final UserHandle userHandle = user.getUserHandle();
34099d0ac2c693849e959601a275128a739569d2efbAndrew Lee                if (!mUserManager.hasUserRestriction(
341047b1f9f0af3c9a0a285eb944b409a785040707cYorke Lee                        UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
3423649bdb5813200904d4e887306dfebb1a0beffa8Jonathan Basseri                        && !user.isManagedProfile()) {
343a71a38b1105a3335b022463ca461abf947c9ffbcTa-wei Yen                    if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
344282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen                            pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
3455dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                        mNotificationManager.notifyAsUser(
3465dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                                Integer.toString(subId) /* tag */,
3475dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                                VOICEMAIL_NOTIFICATION,
3485dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                                notification,
3495dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                                userHandle);
3505dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                    }
35199d0ac2c693849e959601a275128a739569d2efbAndrew Lee                }
3527d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            }
3537d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        } else {
3545bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen            List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
3555bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen            for (int i = 0; i < users.size(); i++) {
3565bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                final UserInfo user = users.get(i);
3575bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                final UserHandle userHandle = user.getUserHandle();
3585bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                if (!mUserManager.hasUserRestriction(
3595bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                        UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
3605bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                        && !user.isManagedProfile()) {
361a71a38b1105a3335b022463ca461abf947c9ffbcTa-wei Yen                    if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
362282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen                            false, userHandle, isRefresh)) {
3635bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                        mNotificationManager.cancelAsUser(
3645bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                                Integer.toString(subId) /* tag */,
3655bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                                VOICEMAIL_NOTIFICATION,
3665bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                                userHandle);
3675bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                    }
3685bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                }
3695dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            }
3705dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee        }
3715dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee    }
3725dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee
3735dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee    /**
3745bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen     * Sends a broadcast with the voicemail notification information to the default dialer. This
3755bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen     * method is also used to indicate to the default dialer when to clear the
3765bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen     * notification. A pending intent can be passed to the default dialer to indicate an action to
3775dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     * be taken as it would by a notification produced in this class.
378a71a38b1105a3335b022463ca461abf947c9ffbcTa-wei Yen     * @param phone The phone the notification is sent from
3795dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     * @param count The number of pending voicemail messages to indicate on the notification. A
3805dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     *              Value of 0 is passed here to indicate that the notification should be cleared.
3815dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     * @param number The voicemail phone number if specified.
3825dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     * @param pendingIntent The intent that should be passed as the action to be taken.
3835dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
3845dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     *                         otherwise, {@code false} to indicate the intent launches voicemail.
3855bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen     * @param userHandle The user to receive the notification. Each user can have their own default
3865bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen     *                   dialer.
3875bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen     * @return {@code true} if the default was notified of the notification.
3885dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee     */
389a71a38b1105a3335b022463ca461abf947c9ffbcTa-wei Yen    private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
390a71a38b1105a3335b022463ca461abf947c9ffbcTa-wei Yen            String number, PendingIntent pendingIntent, boolean isSettingsIntent,
391282a97078762705b5aea04f6d7fb1f131b394879Ta-wei Yen            UserHandle userHandle, boolean isRefresh) {
3925bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen
3935bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
3945bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen            Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
3955dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
3965dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
397a71a38b1105a3335b022463ca461abf947c9ffbcTa-wei Yen            intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
398a71a38b1105a3335b022463ca461abf947c9ffbcTa-wei Yen                    PhoneUtils.makePstnPhoneAccountHandle(phone));
399afca2d6f6a2145586e4a956b1d1b5b8efc11f8f7Ta-wei Yen            intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh);
4005dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            if (count != null) {
4015dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
4025dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            }
4035dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee
4045dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            // Additional information about the voicemail notification beyond the count is only
4055dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            // present when the count not specified or greater than 0. The value of 0 represents
4065dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            // clearing the notification, which does not require additional information.
4075dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            if (count == null || count > 0) {
4085dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                if (!TextUtils.isEmpty(number)) {
4095dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                    intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
4105dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                }
4115dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee
4125dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                if (pendingIntent != null) {
4135dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                    intent.putExtra(isSettingsIntent
4145dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                            ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
4155dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                            : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
4165dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                            pendingIntent);
4175dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee                }
4185dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            }
4195bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen            mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
4205dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee            return true;
4217d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        }
4225dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee
4235dc90848c184a5fd35ad5b7c0e9aa1d50911934bBryce Lee        return false;
4247d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
4257d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
4265bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen    private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
4275bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        String dialerPackage = DefaultDialerManager
4285bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
4295bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
4305bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                .setPackage(dialerPackage);
4315bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen    }
4325bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen
4335bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen    private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
4345bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
4355bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        if (intent == null) {
4365bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen            return false;
4375bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        }
4385bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen
4395bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        List<ResolveInfo> receivers = mContext.getPackageManager()
4405bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen                .queryBroadcastReceivers(intent, 0);
4415bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen        return receivers.size() > 0;
4425bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen    }
4435bb195648911e037df07f3fbc56c43d64dfbdcbeTa-wei Yen
4447d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
4457d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Updates the message call forwarding indicator notification.
4467d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     *
4474baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala     * @param visible true if call forwarding enabled
4487d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
4494baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala
4504baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala     /* package */ void updateCfi(int subId, boolean visible) {
4514baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala        updateCfi(subId, visible, false /* isRefresh */);
4524baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala    }
4534baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala
4544baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala    /**
4554baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala     * Updates the message call forwarding indicator notification.
4564baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala     *
4574baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala     * @param visible true if call forwarding enabled
4584baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala     */
4594baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala    /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
460a584e2c587eacdfc1b06334d0c9d2ff27edf4208Tyler Gunn        logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
4617d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        if (visible) {
4627d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // If Unconditional Call Forwarding (forward all calls) for VOICE
4637d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // is enabled, just show a notification.  We'll default to expanded
4647d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // view for now, so the there is less confusion about the icon.  If
4657d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // it is deemed too weird to have CF indications as expanded views,
4667d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // then we'll flip the flag back.
4677d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
4687d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // TODO: We may want to take a look to see if the notification can
4697d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // display the target to forward calls to.  This will require some
4707d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // effort though, since there are multiple layers of messages that
4717d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            // will need to propagate that information.
4727d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
473d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
474d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            if (subInfo == null) {
475d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                Log.w(LOG_TAG, "Found null subscription info for: " + subId);
476d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                return;
477d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            }
478d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee
479d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            String notificationTitle;
480263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro            int resId = R.drawable.stat_sys_phone_call_forward;
481d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            if (mTelephonyManager.getPhoneCount() > 1) {
482263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                int slotId = SubscriptionManager.getSlotIndex(subId);
483263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
484263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                        : R.drawable.stat_sys_phone_call_forward_sub2;
485d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                notificationTitle = subInfo.getDisplayName().toString();
486d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            } else {
487d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                notificationTitle = mContext.getString(R.string.labelCF);
488d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee            }
489d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee
4908b7620dee4a061bd987254709300e3face3f7b90fionaxu            Notification.Builder builder = new Notification.Builder(mContext)
491263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                    .setSmallIcon(resId)
492d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                    .setColor(subInfo.getIconTint())
493d5165b05c851ecd47d330b600ed2768e0db8b9d1Andrew Lee                    .setContentTitle(notificationTitle)
49499d0ac2c693849e959601a275128a739569d2efbAndrew Lee                    .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
49599d0ac2c693849e959601a275128a739569d2efbAndrew Lee                    .setShowWhen(false)
49675b66a75f589be674bb56491a2681c55ab9c70a4fionaxu                    .setOngoing(true)
4974baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala                    .setChannel(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
4984baf0b9455929098aa50392bc95718bbfa0b26a2Srikanth Chintala                    .setOnlyAlertOnce(isRefresh);
49999d0ac2c693849e959601a275128a739569d2efbAndrew Lee
50099d0ac2c693849e959601a275128a739569d2efbAndrew Lee            Intent intent = new Intent(Intent.ACTION_MAIN);
5012fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
50299d0ac2c693849e959601a275128a739569d2efbAndrew Lee            intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
5032fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee            SubscriptionInfoHelper.addExtrasToIntent(
5042fcb6c372c76a48b7af8f9b0dc7fd518698bbf77Andrew Lee                    intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
50596ceebd42ac48fe1455caed2323dfdee0053152ffionaxu            builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
50696ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                    intent, 0));
50796ceebd42ac48fe1455caed2323dfdee0053152ffionaxu            mNotificationManager.notifyAsUser(
50896ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                    Integer.toString(subId) /* tag */,
50996ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                    CALL_FORWARD_NOTIFICATION,
51096ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                    builder.build(),
51196ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                    UserHandle.ALL);
5127d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        } else {
513263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro            List<UserInfo> users = mUserManager.getUsers(true);
514263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro            for (UserInfo user : users) {
515263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                if (user.isManagedProfile()) {
516263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                    continue;
517263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                }
518263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                UserHandle userHandle = user.getUserHandle();
519263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                mNotificationManager.cancelAsUser(
520263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                        Integer.toString(subId) /* tag */,
521263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                        CALL_FORWARD_NOTIFICATION,
522263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro                        userHandle);
523263737d547dc27601740115f9b7d71d32f4d3940Kazuya Ohshiro            }
5247d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        }
5257d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
5267d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5277d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
5287d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Shows the "data disconnected due to roaming" notification, which
5297d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * appears when you lose data connectivity because you're roaming and
5308783d93a48b87370cd43ce99138ca98da12f6fa6Pengquan Meng     * you have the "data roaming" feature turned off for the given {@code subId}.
5317d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
5328783d93a48b87370cd43ce99138ca98da12f6fa6Pengquan Meng    /* package */ void showDataDisconnectedRoaming(int subId) {
5337d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        if (DBG) log("showDataDisconnectedRoaming()...");
5347d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5357d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        // "Mobile network settings" screen / dialog
5367d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
5376d9bf52509149052f78337d64a91ff076294994cJeff Davidson        intent.putExtra(Settings.EXTRA_SUB_ID, subId);
5388783d93a48b87370cd43ce99138ca98da12f6fa6Pengquan Meng        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
5398783d93a48b87370cd43ce99138ca98da12f6fa6Pengquan Meng        PendingIntent contentIntent = PendingIntent.getActivity(mContext, subId, intent, 0);
5407d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5417d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
5427d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5438b7620dee4a061bd987254709300e3face3f7b90fionaxu        final Notification.Builder builder = new Notification.Builder(mContext)
54499d0ac2c693849e959601a275128a739569d2efbAndrew Lee                .setSmallIcon(android.R.drawable.stat_sys_warning)
545ba6bdefe75ad7b0907d74578fae0c5bbefe84dd1fionaxu                .setContentTitle(mContext.getText(R.string.roaming_notification_title))
54699d0ac2c693849e959601a275128a739569d2efbAndrew Lee                .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
54775b66a75f589be674bb56491a2681c55ab9c70a4fionaxu                .setContentText(contentText)
54896ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                .setChannel(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
54996ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                .setContentIntent(contentIntent);
55096ceebd42ac48fe1455caed2323dfdee0053152ffionaxu        final Notification notif =
55196ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                new Notification.BigTextStyle(builder).bigText(contentText).build();
55296ceebd42ac48fe1455caed2323dfdee0053152ffionaxu        mNotificationManager.notifyAsUser(
55396ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
5547d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
5557d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5567d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
5577d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Turns off the "data disconnected due to roaming" notification.
5587d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
5597d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /* package */ void hideDataDisconnectedRoaming() {
5607d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        if (DBG) log("hideDataDisconnectedRoaming()...");
5617d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
5627d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
5637d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5647d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
5657d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Display the network selection "no service" notification
5667d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * @param operator is the numeric operator number
5672ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C     * @param subId is the subscription ID
5687d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
5692ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C    private void showNetworkSelection(String operator, int subId) {
5707d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        if (DBG) log("showNetworkSelection(" + operator + ")...");
5717d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5728b7620dee4a061bd987254709300e3face3f7b90fionaxu        Notification.Builder builder = new Notification.Builder(mContext)
57399d0ac2c693849e959601a275128a739569d2efbAndrew Lee                .setSmallIcon(android.R.drawable.stat_sys_warning)
57499d0ac2c693849e959601a275128a739569d2efbAndrew Lee                .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
57599d0ac2c693849e959601a275128a739569d2efbAndrew Lee                .setContentText(
57699d0ac2c693849e959601a275128a739569d2efbAndrew Lee                        mContext.getString(R.string.notification_network_selection_text, operator))
57799d0ac2c693849e959601a275128a739569d2efbAndrew Lee                .setShowWhen(false)
57875b66a75f589be674bb56491a2681c55ab9c70a4fionaxu                .setOngoing(true)
5798b7620dee4a061bd987254709300e3face3f7b90fionaxu                .setChannel(NotificationChannelController.CHANNEL_ID_ALERT);
5807d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5817d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        // create the target network operators settings intent
5827d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        Intent intent = new Intent(Intent.ACTION_MAIN);
5837d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
5847d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
58534d4fa5e9fa19feb70da552dfa765b1a90a5c0ddMalcolm Chen        // Use MobileNetworkSettings to handle the selection intent
586be964583a634a57199417e50b36edb10223f7d3eWei Liu        intent.setComponent(new ComponentName(
58734d4fa5e9fa19feb70da552dfa765b1a90a5c0ddMalcolm Chen                mContext.getString(R.string.mobile_network_settings_package),
58834d4fa5e9fa19feb70da552dfa765b1a90a5c0ddMalcolm Chen                mContext.getString(R.string.mobile_network_settings_class)));
5892ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C        intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, subId);
59096ceebd42ac48fe1455caed2323dfdee0053152ffionaxu        builder.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0));
59196ceebd42ac48fe1455caed2323dfdee0053152ffionaxu        mNotificationManager.notifyAsUser(
59296ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                null /* tag */,
59396ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                SELECTED_OPERATOR_FAIL_NOTIFICATION,
59496ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                builder.build(),
59596ceebd42ac48fe1455caed2323dfdee0053152ffionaxu                UserHandle.ALL);
5967d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
5977d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
5987d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
5997d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Turn off the network selection "no service" notification
6007d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
6017d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private void cancelNetworkSelection() {
6027d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        if (DBG) log("cancelNetworkSelection()...");
60399d0ac2c693849e959601a275128a739569d2efbAndrew Lee        mNotificationManager.cancelAsUser(
60499d0ac2c693849e959601a275128a739569d2efbAndrew Lee                null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
6057d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
6067d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
6077d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /**
6087d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * Update notification about no service of user selected operator
6097d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     *
6107d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     * @param serviceState Phone service state
6112ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C     * @param subId The subscription ID
6127d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon     */
6132ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C    void updateNetworkSelection(int serviceState, int subId) {
6142ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C        int phoneId = SubscriptionManager.getPhoneId(subId);
6152ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C        Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
6162ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C                PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
6172ef9a48d76578caaa46c847bd17889866720cc7aJayachandran C        if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
618a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan            if (SubscriptionManager.isValidSubscriptionId(subId)) {
619996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
620996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                String selectedNetworkOperatorName =
621996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                        sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
622996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                // get the shared preference of network_selection.
623996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                // empty is auto mode, otherwise it is the operator alpha name
624996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                // in case there is no operator name, check the operator numeric
625996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                if (TextUtils.isEmpty(selectedNetworkOperatorName)) {
626996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                    selectedNetworkOperatorName =
627996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                            sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
628996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                }
629996a1c343eb1608e7fa56cc4b3728080b22ad34ffionaxu                boolean isManualSelection;
630d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                // if restoring manual selection is controlled by framework, then get network
631d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                // selection from shared preference, otherwise get from real network indicators.
632d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                boolean restoreSelection = !mContext.getResources().getBoolean(
633d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                        com.android.internal.R.bool.skip_restoring_network_selection);
634d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                if (restoreSelection) {
635d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                    isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName);
636d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                } else {
637d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                    isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection();
638a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                }
6397d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
640d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                if (DBG) {
641d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                    log("updateNetworkSelection()..." + "state = " + serviceState + " new network "
642d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                            + (isManualSelection ? selectedNetworkOperatorName : ""));
643d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                }
6447d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
645d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                if (serviceState == ServiceState.STATE_OUT_OF_SERVICE && isManualSelection) {
646d6aac66931461f560cb3f472e507459fc75d32dcfionaxu                    showNetworkSelection(selectedNetworkOperatorName, subId);
6472b846c78ff3b4102e416c81c4a17b976d327ed38Hall Liu                    mSelectedUnavailableNotify = true;
648a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                } else {
649a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                    if (mSelectedUnavailableNotify) {
650a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                        cancelNetworkSelection();
651a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                        mSelectedUnavailableNotify = false;
652a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                    }
6537d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon                }
6547d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            } else {
655a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                if (DBG) log("updateNetworkSelection()..." + "state = " +
656a60be872ccf4fc96584bf99b11129ffa2cbb7c4eAmit Mahajan                        serviceState + " not updating network due to invalid subId " + subId);
6577d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            }
6587d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        }
6597d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
6607d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
6617d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
6627d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        if (mToast != null) {
6637d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon            mToast.cancel();
6647d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        }
6657d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
6667d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
6677d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        mToast.show();
6687d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
6697d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon
6707d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    private void log(String msg) {
6717d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon        Log.d(LOG_TAG, msg);
6727d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon    }
673a584e2c587eacdfc1b06334d0c9d2ff27edf4208Tyler Gunn
674a584e2c587eacdfc1b06334d0c9d2ff27edf4208Tyler Gunn    private void logi(String msg) {
675a584e2c587eacdfc1b06334d0c9d2ff27edf4208Tyler Gunn        Log.i(LOG_TAG, msg);
676a584e2c587eacdfc1b06334d0c9d2ff27edf4208Tyler Gunn    }
6777d4ddf6dc0d7c8158bac3a5dec7936e837e95bddSantos Cordon}
678