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