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