VvmPhoneStateListener.java revision 12085b7479893e4cd1b8d5305a1a56023139f00e
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 */
162cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenpackage com.android.phone.vvm.omtp.sync;
172cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen
182cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.content.Context;
196057b1b6a078db56d02fe6d3731785ee1812e18fNancy Chenimport android.content.Intent;
202cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.provider.VoicemailContract;
212cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.telecom.PhoneAccountHandle;
222cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.telephony.PhoneStateListener;
232cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport android.telephony.ServiceState;
242cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen
252cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenimport com.android.phone.PhoneUtils;
265aef02b75aeade526de52d5c907932b97fbead52Nancy Chenimport com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper;
272cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen
282cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen/**
292cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen * Check if service is lost and indicate this in the voicemail status.
302cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen */
312cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chenpublic class VvmPhoneStateListener extends PhoneStateListener {
325aef02b75aeade526de52d5c907932b97fbead52Nancy Chen
332cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    private PhoneAccountHandle mPhoneAccount;
342cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    private Context mContext;
352cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    public VvmPhoneStateListener(Context context, PhoneAccountHandle accountHandle) {
362cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        super(PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle));
372cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        mContext = context;
382cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        mPhoneAccount = accountHandle;
392cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    }
402cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen
412cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    @Override
422cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    public void onServiceStateChanged(ServiceState serviceState) {
432cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
446057b1b6a078db56d02fe6d3731785ee1812e18fNancy Chen            VoicemailStatusQueryHelper voicemailStatusQueryHelper =
456057b1b6a078db56d02fe6d3731785ee1812e18fNancy Chen                    new VoicemailStatusQueryHelper(mContext);
465aef02b75aeade526de52d5c907932b97fbead52Nancy Chen            if (voicemailStatusQueryHelper.isVoicemailSourceConfigured(mPhoneAccount)) {
475aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                if (!voicemailStatusQueryHelper.isNotificationsChannelActive(mPhoneAccount)) {
485aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                    VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
495aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                            VoicemailContract.Status.CONFIGURATION_STATE_OK,
505aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                            VoicemailContract.Status.DATA_CHANNEL_STATE_OK,
515aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                            VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_OK);
525aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                    // Run a full sync in case something was missed while signal was down.
536337d1be5db67c7b81e7998988ca53da50ab44c2Nancy Chen                    Intent serviceIntent = OmtpVvmSyncService.getSyncIntent(
546337d1be5db67c7b81e7998988ca53da50ab44c2Nancy Chen                            mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
556337d1be5db67c7b81e7998988ca53da50ab44c2Nancy Chen                            true /* firstAttempt */);
565aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                    mContext.startService(serviceIntent);
575aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                }
5812085b7479893e4cd1b8d5305a1a56023139f00eNancy Chen            }
5912085b7479893e4cd1b8d5305a1a56023139f00eNancy Chen
6012085b7479893e4cd1b8d5305a1a56023139f00eNancy Chen            if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
615aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                OmtpVvmCarrierConfigHelper carrierConfigHelper = new OmtpVvmCarrierConfigHelper(
625aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                        mContext, PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount));
635aef02b75aeade526de52d5c907932b97fbead52Nancy Chen                carrierConfigHelper.startActivation();
646057b1b6a078db56d02fe6d3731785ee1812e18fNancy Chen            }
652cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        } else {
662cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen            VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
672cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen                    VoicemailContract.Status.CONFIGURATION_STATE_OK,
682cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen                    VoicemailContract.Status.DATA_CHANNEL_STATE_NO_CONNECTION,
692cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen                    VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);
702cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen        }
712cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen    }
722cf7f2935c71b0ddbdda86fa6bc18b33db2dbf99Nancy Chen}
73