Home | History | Annotate | Download | only in impl
      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  */
     16 package com.android.voicemail.impl;
     17 
     18 import android.content.Context;
     19 import android.telecom.PhoneAccountHandle;
     20 import android.telephony.PhoneStateListener;
     21 import android.telephony.ServiceState;
     22 import com.android.voicemail.impl.sync.OmtpVvmSyncService;
     23 import com.android.voicemail.impl.sync.SyncTask;
     24 import com.android.voicemail.impl.sync.VoicemailStatusQueryHelper;
     25 import com.android.voicemail.impl.sync.VvmAccountManager;
     26 
     27 /**
     28  * Check if service is lost and indicate this in the voicemail status. TODO(b/35125657): Not used
     29  * for now, restore it.
     30  */
     31 public 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(
     51           TAG,
     52           "onServiceStateChanged on phoneAccount "
     53               + mPhoneAccount
     54               + " with invalid phoneAccountHandle, ignoring");
     55       return;
     56     }
     57 
     58     int state = serviceState.getState();
     59     if (state == mPreviousState
     60         || (state != ServiceState.STATE_IN_SERVICE
     61             && mPreviousState != ServiceState.STATE_IN_SERVICE)) {
     62       // Only interested in state changes or transitioning into or out of "in service".
     63       // Otherwise just quit.
     64       mPreviousState = state;
     65       return;
     66     }
     67 
     68     OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(mContext, mPhoneAccount);
     69 
     70     if (state == ServiceState.STATE_IN_SERVICE) {
     71       VoicemailStatusQueryHelper voicemailStatusQueryHelper =
     72           new VoicemailStatusQueryHelper(mContext);
     73       if (voicemailStatusQueryHelper.isVoicemailSourceConfigured(mPhoneAccount)) {
     74         if (!voicemailStatusQueryHelper.isNotificationsChannelActive(mPhoneAccount)) {
     75           VvmLog.v(TAG, "Notifications channel is active for " + mPhoneAccount);
     76           helper.handleEvent(
     77               VoicemailStatus.edit(mContext, mPhoneAccount), OmtpEvents.NOTIFICATION_IN_SERVICE);
     78         }
     79       }
     80 
     81       if (VvmAccountManager.isAccountActivated(mContext, mPhoneAccount)) {
     82         VvmLog.v(TAG, "Signal returned: requesting resync for " + mPhoneAccount);
     83         // If the source is already registered, run a full sync in case something was missed
     84         // while signal was down.
     85         SyncTask.start(mContext, mPhoneAccount, OmtpVvmSyncService.SYNC_FULL_SYNC);
     86       } else {
     87         VvmLog.v(TAG, "Signal returned: reattempting activation for " + mPhoneAccount);
     88         // Otherwise initiate an activation because this means that an OMTP source was
     89         // recognized but either the activation text was not successfully sent or a response
     90         // was not received.
     91         helper.startActivation();
     92       }
     93     } else {
     94       VvmLog.v(TAG, "Notifications channel is inactive for " + mPhoneAccount);
     95 
     96       if (!VvmAccountManager.isAccountActivated(mContext, mPhoneAccount)) {
     97         return;
     98       }
     99       helper.handleEvent(
    100           VoicemailStatus.edit(mContext, mPhoneAccount), OmtpEvents.NOTIFICATION_SERVICE_LOST);
    101     }
    102     mPreviousState = state;
    103   }
    104 }
    105