1/*
2 * Copyright (C) 2015 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 */
16package com.android.phone.vvm.omtp;
17
18import android.content.Context;
19import android.telecom.PhoneAccountHandle;
20import android.telephony.PhoneStateListener;
21import android.telephony.ServiceState;
22import android.telephony.SubscriptionManager;
23
24import com.android.phone.PhoneUtils;
25import com.android.phone.VoicemailStatus;
26import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
27import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
28import com.android.phone.vvm.omtp.sync.SyncTask;
29import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
30import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter;
31
32/**
33 * Check if service is lost and indicate this in the voicemail status.
34 */
35public class VvmPhoneStateListener extends PhoneStateListener {
36
37    private static final String TAG = "VvmPhoneStateListener";
38
39    private PhoneAccountHandle mPhoneAccount;
40    private Context mContext;
41    private int mPreviousState = -1;
42
43    public VvmPhoneStateListener(Context context, PhoneAccountHandle accountHandle) {
44        super(PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle));
45        mContext = context;
46        mPhoneAccount = accountHandle;
47    }
48
49    @Override
50    public void onServiceStateChanged(ServiceState serviceState) {
51        int subId = PhoneAccountHandleConverter.toSubId(mPhoneAccount);
52        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
53            VvmLog.e(TAG, "onServiceStateChanged on phoneAccount " + mPhoneAccount
54                    + " with invalid subId, ignoring");
55            return;
56        }
57
58        int state = serviceState.getState();
59        if (state == mPreviousState || (state != ServiceState.STATE_IN_SERVICE
60                && mPreviousState != ServiceState.STATE_IN_SERVICE)) {
61            // Only interested in state changes or transitioning into or out of "in service".
62            // Otherwise just quit.
63            mPreviousState = state;
64            return;
65        }
66
67        OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(mContext, subId);
68
69        if (state == ServiceState.STATE_IN_SERVICE) {
70            VoicemailStatusQueryHelper voicemailStatusQueryHelper =
71                    new VoicemailStatusQueryHelper(mContext);
72            if (voicemailStatusQueryHelper.isVoicemailSourceConfigured(mPhoneAccount)) {
73                if (!voicemailStatusQueryHelper.isNotificationsChannelActive(mPhoneAccount)) {
74                    VvmLog
75                            .v(TAG, "Notifications channel is active for " + subId);
76                    helper.handleEvent(VoicemailStatus.edit(mContext, mPhoneAccount),
77                        OmtpEvents.NOTIFICATION_IN_SERVICE);
78                }
79            }
80
81            if (OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
82                VvmLog
83                        .v(TAG, "Signal returned: requesting resync for " + subId);
84                // If the source is already registered, run a full sync in case something was missed
85                // while signal was down.
86                SyncTask.start(mContext, mPhoneAccount, OmtpVvmSyncService.SYNC_FULL_SYNC);
87            } else {
88                VvmLog.v(TAG,
89                        "Signal returned: reattempting activation for " + subId);
90                // Otherwise initiate an activation because this means that an OMTP source was
91                // recognized but either the activation text was not successfully sent or a response
92                // was not received.
93                helper.startActivation();
94            }
95        } else {
96            VvmLog.v(TAG, "Notifications channel is inactive for " + subId);
97
98            if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
99                return;
100            }
101            helper.handleEvent(VoicemailStatus.edit(mContext, mPhoneAccount),
102                OmtpEvents.NOTIFICATION_SERVICE_LOST);
103        }
104        mPreviousState = state;
105    }
106}
107