Home | History | Annotate | Download | only in omtp
      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.phone.vvm.omtp;
     17 
     18 import android.content.Context;
     19 import android.content.Intent;
     20 import android.provider.VoicemailContract;
     21 import android.telecom.PhoneAccountHandle;
     22 import android.telephony.PhoneStateListener;
     23 import android.telephony.ServiceState;
     24 import android.util.Log;
     25 
     26 import com.android.phone.PhoneGlobals;
     27 import com.android.phone.PhoneUtils;
     28 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
     29 import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
     30 import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
     31 
     32 /**
     33  * Check if service is lost and indicate this in the voicemail status.
     34  */
     35 public class VvmPhoneStateListener extends PhoneStateListener {
     36     private static final String TAG = "VvmPhoneStateListener";
     37 
     38     private PhoneAccountHandle mPhoneAccount;
     39     private Context mContext;
     40     private int mPreviousState = -1;
     41 
     42     public VvmPhoneStateListener(Context context, PhoneAccountHandle accountHandle) {
     43         super(PhoneUtils.getSubIdForPhoneAccountHandle(accountHandle));
     44         mContext = context;
     45         mPhoneAccount = accountHandle;
     46     }
     47 
     48     @Override
     49     public void onServiceStateChanged(ServiceState serviceState) {
     50         int state = serviceState.getState();
     51         if (state == mPreviousState || (state != ServiceState.STATE_IN_SERVICE
     52                 && mPreviousState != ServiceState.STATE_IN_SERVICE)) {
     53             // Only interested in state changes or transitioning into or out of "in service".
     54             // Otherwise just quit.
     55             mPreviousState = state;
     56             return;
     57         }
     58 
     59         if (state == ServiceState.STATE_IN_SERVICE) {
     60             VoicemailStatusQueryHelper voicemailStatusQueryHelper =
     61                     new VoicemailStatusQueryHelper(mContext);
     62             if (voicemailStatusQueryHelper.isVoicemailSourceConfigured(mPhoneAccount)) {
     63                 if (!voicemailStatusQueryHelper.isNotificationsChannelActive(mPhoneAccount)) {
     64                     Log.v(TAG, "Notifications channel is active for " + mPhoneAccount.getId());
     65                     VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
     66                             VoicemailContract.Status.CONFIGURATION_STATE_OK,
     67                             VoicemailContract.Status.DATA_CHANNEL_STATE_OK,
     68                             VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_OK);
     69                     PhoneGlobals.getInstance().clearMwiIndicator(
     70                             PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount));
     71                 }
     72             }
     73 
     74             if (OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
     75                 Log.v(TAG, "Signal returned: requesting resync for " + mPhoneAccount.getId());
     76                 LocalLogHelper.log(TAG,
     77                         "Signal returned: requesting resync for " + mPhoneAccount.getId());
     78                 // If the source is already registered, run a full sync in case something was missed
     79                 // while signal was down.
     80                 Intent serviceIntent = OmtpVvmSyncService.getSyncIntent(
     81                         mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
     82                         true /* firstAttempt */);
     83                 mContext.startService(serviceIntent);
     84             } else {
     85                 Log.v(TAG, "Signal returned: reattempting activation for " + mPhoneAccount.getId());
     86                 LocalLogHelper.log(TAG,
     87                         "Signal returned: reattempting activation for " + mPhoneAccount.getId());
     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                 OmtpVvmCarrierConfigHelper carrierConfigHelper = new OmtpVvmCarrierConfigHelper(
     92                         mContext, PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount));
     93                 carrierConfigHelper.startActivation();
     94             }
     95         } else {
     96             Log.v(TAG, "Notifications channel is inactive for " + mPhoneAccount.getId());
     97             mContext.stopService(OmtpVvmSyncService.getSyncIntent(
     98                     mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
     99                     true /* firstAttempt */));
    100 
    101             if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
    102                 return;
    103             }
    104 
    105             VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
    106                     VoicemailContract.Status.CONFIGURATION_STATE_OK,
    107                     VoicemailContract.Status.DATA_CHANNEL_STATE_NO_CONNECTION,
    108                     VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);
    109         }
    110         mPreviousState = state;
    111     }
    112 }
    113