12cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen/*
22cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * Copyright (C) 2015 The Android Open Source Project
32cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen *
42cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * Licensed under the Apache License, Version 2.0 (the "License");
52cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * you may not use this file except in compliance with the License.
62cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * You may obtain a copy of the License at
72cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen *
82cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen *      http://www.apache.org/licenses/LICENSE-2.0
92cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen *
102cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * Unless required by applicable law or agreed to in writing, software
112cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * distributed under the License is distributed on an "AS IS" BASIS,
122cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * See the License for the specific language governing permissions and
142cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * limitations under the License
152cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen */
16bb49d410b5266d288f65bbb0f2584ac7c2abf6ddNancy Chenpackage com.android.phone.vvm.omtp;
172cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen
182cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.content.Context;
192cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.telecom.PhoneAccountHandle;
202cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.telephony.PhoneStateListener;
212cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.telephony.ServiceState;
227df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yenimport android.telephony.SubscriptionManager;
23b29425b49c3ed353a114291712ef4d484ef645c5Ta-wei Yen
24bb49d410b5266d288f65bbb0f2584ac7c2abf6ddNancy Chenimport com.android.phone.PhoneUtils;
25016df6b594d4a5aba8c1e325a646a0bcf5d6d263Ta-wei Yenimport com.android.phone.VoicemailStatus;
26ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chenimport com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
27ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chenimport com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
28ccb523db571855a8bcffe8e1a719d0221e01919bTa-wei Yenimport com.android.phone.vvm.omtp.sync.SyncTask;
29ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chenimport com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
30f5c5d934a418a6c28313f284eec4f98a6fbbdc5cTa-wei Yenimport com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter;
312cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen
322cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen/**
332cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * Check if service is lost and indicate this in the voicemail status.
342cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen */
352cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenpublic class VvmPhoneStateListener extends PhoneStateListener {
36f5c5d934a418a6c28313f284eec4f98a6fbbdc5cTa-wei Yen
3787d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen    private static final String TAG = "VvmPhoneStateListener";
385aef02b75aeade526de52d5c907932b97fbead52Nancy Chen
392cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    private PhoneAccountHandle mPhoneAccount;
402cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    private Context mContext;
4187d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen    private int mPreviousState = -1;
4287d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen
432cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    public VvmPhoneStateListener(Context context, PhoneAccountHandle accountHandle) {
442cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        super(PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle));
452cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        mContext = context;
462cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        mPhoneAccount = accountHandle;
472cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    }
482cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen
492cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    @Override
502cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    public void onServiceStateChanged(ServiceState serviceState) {
517df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yen        int subId = PhoneAccountHandleConverter.toSubId(mPhoneAccount);
527df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yen        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
537df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yen            VvmLog.e(TAG, "onServiceStateChanged on phoneAccount " + mPhoneAccount
547df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yen                    + " with invalid subId, ignoring");
557df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yen            return;
567df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yen        }
577df340d3e6dc0277c0a8307e34d40837e8106403Ta-wei Yen
5887d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen        int state = serviceState.getState();
5987d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen        if (state == mPreviousState || (state != ServiceState.STATE_IN_SERVICE
6087d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen                && mPreviousState != ServiceState.STATE_IN_SERVICE)) {
6187d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen            // Only interested in state changes or transitioning into or out of "in service".
6287d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen            // Otherwise just quit.
6387d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen            mPreviousState = state;
6487d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen            return;
6587d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen        }
6687d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen
67f5c5d934a418a6c28313f284eec4f98a6fbbdc5cTa-wei Yen        OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(mContext, subId);
68f5c5d934a418a6c28313f284eec4f98a6fbbdc5cTa-wei Yen
6987d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen        if (state == ServiceState.STATE_IN_SERVICE) {
706057b1b6a078db56d02fe6d3731785ee1812e18fNancy Chen            VoicemailStatusQueryHelper voicemailStatusQueryHelper =
716057b1b6a078db56d02fe6d3731785ee1812e18fNancy Chen                    new VoicemailStatusQueryHelper(mContext);
725aef02b75aeade526de52d5c907932b97fbead52Nancy Chen            if (voicemailStatusQueryHelper.isVoicemailSourceConfigured(mPhoneAccount)) {
735aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                if (!voicemailStatusQueryHelper.isNotificationsChannelActive(mPhoneAccount)) {
7499282e0eb8f04d03c9464bea94fa2d8959edbd13Ta-wei Yen                    VvmLog
7599282e0eb8f04d03c9464bea94fa2d8959edbd13Ta-wei Yen                            .v(TAG, "Notifications channel is active for " + subId);
76016df6b594d4a5aba8c1e325a646a0bcf5d6d263Ta-wei Yen                    helper.handleEvent(VoicemailStatus.edit(mContext, mPhoneAccount),
77016df6b594d4a5aba8c1e325a646a0bcf5d6d263Ta-wei Yen                        OmtpEvents.NOTIFICATION_IN_SERVICE);
785aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                }
7912085b7479893e4cd1b8d5305a1a56023139f00eNancy Chen            }
8012085b7479893e4cd1b8d5305a1a56023139f00eNancy Chen
81ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chen            if (OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
8299282e0eb8f04d03c9464bea94fa2d8959edbd13Ta-wei Yen                VvmLog
8399282e0eb8f04d03c9464bea94fa2d8959edbd13Ta-wei Yen                        .v(TAG, "Signal returned: requesting resync for " + subId);
84ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chen                // If the source is already registered, run a full sync in case something was missed
85ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chen                // while signal was down.
86ccb523db571855a8bcffe8e1a719d0221e01919bTa-wei Yen                SyncTask.start(mContext, mPhoneAccount, OmtpVvmSyncService.SYNC_FULL_SYNC);
87ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chen            } else {
8899282e0eb8f04d03c9464bea94fa2d8959edbd13Ta-wei Yen                VvmLog.v(TAG,
8999282e0eb8f04d03c9464bea94fa2d8959edbd13Ta-wei Yen                        "Signal returned: reattempting activation for " + subId);
90ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chen                // Otherwise initiate an activation because this means that an OMTP source was
91ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chen                // recognized but either the activation text was not successfully sent or a response
92ea5d3f4426fb2a79aecd59c19ba2021b6ea42ae9Nancy Chen                // was not received.
93f5c5d934a418a6c28313f284eec4f98a6fbbdc5cTa-wei Yen                helper.startActivation();
946057b1b6a078db56d02fe6d3731785ee1812e18fNancy Chen            }
952cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        } else {
9699282e0eb8f04d03c9464bea94fa2d8959edbd13Ta-wei Yen            VvmLog.v(TAG, "Notifications channel is inactive for " + subId);
9787d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen
98d627a26cef1f3e7bf454af4aa49b9dc624316fb2Yorke Lee            if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
99d627a26cef1f3e7bf454af4aa49b9dc624316fb2Yorke Lee                return;
100d627a26cef1f3e7bf454af4aa49b9dc624316fb2Yorke Lee            }
101016df6b594d4a5aba8c1e325a646a0bcf5d6d263Ta-wei Yen            helper.handleEvent(VoicemailStatus.edit(mContext, mPhoneAccount),
102016df6b594d4a5aba8c1e325a646a0bcf5d6d263Ta-wei Yen                OmtpEvents.NOTIFICATION_SERVICE_LOST);
1032cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        }
10487d7c2164e219bca5dfaaf2065767ab0e036061fNancy Chen        mPreviousState = state;
1052cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    }
1062cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen}
107