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.telecom.PhoneAccountHandle;
     20 import android.telephony.PhoneStateListener;
     21 import android.telephony.ServiceState;
     22 import android.telephony.SubscriptionManager;
     23 import com.android.phone.PhoneGlobals;
     24 import com.android.phone.PhoneUtils;
     25 import com.android.phone.VoicemailStatus;
     26 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
     27 import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
     28 import com.android.phone.vvm.omtp.sync.SyncTask;
     29 import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
     30 import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter;
     31 
     32 /**
     33  * Check if service is lost and indicate this in the voicemail status.
     34  */
     35 public 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                     PhoneGlobals.getInstance().clearMwiIndicator(subId);
     79                 }
     80             }
     81 
     82             if (OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
     83                 VvmLog
     84                         .v(TAG, "Signal returned: requesting resync for " + subId);
     85                 // If the source is already registered, run a full sync in case something was missed
     86                 // while signal was down.
     87                 SyncTask.start(mContext, mPhoneAccount, OmtpVvmSyncService.SYNC_FULL_SYNC);
     88             } else {
     89                 VvmLog.v(TAG,
     90                         "Signal returned: reattempting activation for " + subId);
     91                 // Otherwise initiate an activation because this means that an OMTP source was
     92                 // recognized but either the activation text was not successfully sent or a response
     93                 // was not received.
     94                 helper.startActivation();
     95             }
     96         } else {
     97             VvmLog.v(TAG, "Notifications channel is inactive for " + subId);
     98 
     99             if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
    100                 return;
    101             }
    102             helper.handleEvent(VoicemailStatus.edit(mContext, mPhoneAccount),
    103                 OmtpEvents.NOTIFICATION_SERVICE_LOST);
    104         }
    105         mPreviousState = state;
    106     }
    107 }
    108