Home | History | Annotate | Download | only in sms
      1 /*
      2  * Copyright (C) 2016 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 
     17 package com.android.voicemail.impl.sms;
     18 
     19 import android.annotation.TargetApi;
     20 import android.app.PendingIntent;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.net.Uri;
     24 import android.os.Build.VERSION_CODES;
     25 import android.os.Bundle;
     26 import android.support.annotation.Nullable;
     27 import android.telecom.PhoneAccount;
     28 import android.telecom.PhoneAccountHandle;
     29 import android.telephony.TelephonyManager;
     30 import android.telephony.VisualVoicemailSms;
     31 import com.android.voicemail.VoicemailClient;
     32 import com.android.voicemail.impl.OmtpConstants;
     33 import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
     34 import com.android.voicemail.impl.VvmLog;
     35 
     36 /**
     37  * Class ot handle voicemail SMS under legacy mode
     38  *
     39  * @see OmtpVvmCarrierConfigHelper#isLegacyModeEnabled()
     40  */
     41 @TargetApi(VERSION_CODES.O)
     42 public class LegacyModeSmsHandler {
     43 
     44   private static final String TAG = "LegacyModeSmsHandler";
     45 
     46   private static final int CALL_VOICEMAIL_REQUEST_CODE = 1;
     47   private static final int LAUNCH_VOICEMAIL_SETTINGS_REQUEST_CODE = 2;
     48 
     49   public static void handle(Context context, VisualVoicemailSms sms) {
     50     VvmLog.i(TAG, "processing VVM SMS on legacy mode");
     51     String eventType = sms.getPrefix();
     52     Bundle data = sms.getFields();
     53     PhoneAccountHandle handle = sms.getPhoneAccountHandle();
     54 
     55     if (eventType.equals(OmtpConstants.SYNC_SMS_PREFIX)) {
     56       SyncMessage message = new SyncMessage(data);
     57       VvmLog.i(
     58           TAG, "Received SYNC sms for " + handle + " with event " + message.getSyncTriggerEvent());
     59 
     60       switch (message.getSyncTriggerEvent()) {
     61         case OmtpConstants.NEW_MESSAGE:
     62         case OmtpConstants.MAILBOX_UPDATE:
     63           sendLegacyVoicemailNotification(context, handle, message.getNewMessageCount());
     64 
     65           break;
     66         default:
     67           break;
     68       }
     69     }
     70   }
     71 
     72   private static void sendLegacyVoicemailNotification(
     73       Context context, PhoneAccountHandle phoneAccountHandle, int messageCount) {
     74     // The user has called into the voicemail and the new message count could
     75     // change.
     76     // For some carriers new message count could be set to 0 even if there are still
     77     // unread messages, to clear the message waiting indicator.
     78 
     79     VvmLog.i(TAG, "sending voicemail notification");
     80     Intent intent = new Intent(VoicemailClient.ACTION_SHOW_LEGACY_VOICEMAIL);
     81     intent.setPackage(context.getPackageName());
     82     intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
     83     // Setting voicemail message count to non-zero will show the telephony voicemail
     84     // notification, and zero will clear it.
     85     intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, messageCount);
     86 
     87     String voicemailNumber = getVoicemailNumber(context, phoneAccountHandle);
     88     PendingIntent callVoicemailPendingIntent = null;
     89     PendingIntent launchVoicemailSettingsPendingIntent = null;
     90 
     91     if (voicemailNumber != null) {
     92       callVoicemailPendingIntent =
     93           PendingIntent.getActivity(
     94               context,
     95               CALL_VOICEMAIL_REQUEST_CODE,
     96               new Intent(
     97                   Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", null)),
     98               PendingIntent.FLAG_UPDATE_CURRENT);
     99     } else {
    100       Intent launchVoicemailSettingsIntent =
    101           new Intent(TelephonyManager.ACTION_CONFIGURE_VOICEMAIL);
    102       launchVoicemailSettingsIntent.putExtra(TelephonyManager.EXTRA_HIDE_PUBLIC_SETTINGS, true);
    103 
    104       launchVoicemailSettingsPendingIntent =
    105           PendingIntent.getActivity(
    106               context,
    107               LAUNCH_VOICEMAIL_SETTINGS_REQUEST_CODE,
    108               launchVoicemailSettingsIntent,
    109               PendingIntent.FLAG_UPDATE_CURRENT);
    110     }
    111 
    112     intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, voicemailNumber);
    113     intent.putExtra(TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT, callVoicemailPendingIntent);
    114     intent.putExtra(
    115         TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT,
    116         launchVoicemailSettingsPendingIntent);
    117 
    118     context.sendBroadcast(intent);
    119   }
    120 
    121   @Nullable
    122   private static String getVoicemailNumber(Context context, PhoneAccountHandle phoneAccountHandle) {
    123     TelephonyManager telephonyManager =
    124         context
    125             .getSystemService(TelephonyManager.class)
    126             .createForPhoneAccountHandle(phoneAccountHandle);
    127     if (telephonyManager == null) {
    128       return null;
    129     }
    130     return telephonyManager.getVoiceMailNumber();
    131   }
    132 }
    133