blob: c36b75e8318ac84b7335af9deb9d8a7ed1e12a05 [file] [log] [blame]
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001/*
Eric Erfanian8369df02017-05-03 10:27:13 -07002 * Copyright (C) 2016 The Android Open Source Project
Eric Erfaniand5e47f62017-03-15 14:41:07 -07003 *
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
17package com.android.voicemail.impl.sms;
18
Eric Erfanian8369df02017-05-03 10:27:13 -070019import android.annotation.TargetApi;
20import android.app.PendingIntent;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070021import android.content.Context;
Eric Erfanian8369df02017-05-03 10:27:13 -070022import android.content.Intent;
Eric Erfanian8369df02017-05-03 10:27:13 -070023import android.os.Build.VERSION_CODES;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070024import android.os.Bundle;
Eric Erfanian8369df02017-05-03 10:27:13 -070025import android.support.annotation.Nullable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070026import android.telecom.PhoneAccountHandle;
Eric Erfanian8369df02017-05-03 10:27:13 -070027import android.telephony.TelephonyManager;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070028import android.telephony.VisualVoicemailSms;
twyenc2ea6952017-11-06 16:56:11 -080029import com.android.dialer.callintent.CallInitiationType;
30import com.android.dialer.callintent.CallIntentBuilder;
twyen0efc8402017-11-07 15:39:15 -080031import com.android.dialer.precall.PreCall;
Eric Erfanian8369df02017-05-03 10:27:13 -070032import com.android.voicemail.VoicemailClient;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070033import com.android.voicemail.impl.OmtpConstants;
34import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070035import com.android.voicemail.impl.VvmLog;
36
37/**
38 * Class ot handle voicemail SMS under legacy mode
39 *
40 * @see OmtpVvmCarrierConfigHelper#isLegacyModeEnabled()
41 */
Eric Erfanian8369df02017-05-03 10:27:13 -070042@TargetApi(VERSION_CODES.O)
Eric Erfaniand5e47f62017-03-15 14:41:07 -070043public class LegacyModeSmsHandler {
44
45 private static final String TAG = "LegacyModeSmsHandler";
46
Eric Erfanian8369df02017-05-03 10:27:13 -070047 private static final int CALL_VOICEMAIL_REQUEST_CODE = 1;
48 private static final int LAUNCH_VOICEMAIL_SETTINGS_REQUEST_CODE = 2;
49
Eric Erfaniand5e47f62017-03-15 14:41:07 -070050 public static void handle(Context context, VisualVoicemailSms sms) {
Eric Erfanian8369df02017-05-03 10:27:13 -070051 VvmLog.i(TAG, "processing VVM SMS on legacy mode");
Eric Erfaniand5e47f62017-03-15 14:41:07 -070052 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 Erfanian8369df02017-05-03 10:27:13 -070058 VvmLog.i(
Eric Erfaniand5e47f62017-03-15 14:41:07 -070059 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 Erfanian8369df02017-05-03 10:27:13 -070064 sendLegacyVoicemailNotification(context, handle, message.getNewMessageCount());
Eric Erfaniand5e47f62017-03-15 14:41:07 -070065
Eric Erfaniand5e47f62017-03-15 14:41:07 -070066 break;
67 default:
68 break;
69 }
70 }
71 }
Eric Erfanian8369df02017-05-03 10:27:13 -070072
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 Erfanian2ca43182017-08-31 06:57:16 -070083 intent.putExtra(VoicemailClient.EXTRA_IS_LEGACY_MODE, true);
Eric Erfanian8369df02017-05-03 10:27:13 -070084 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,
twyen0efc8402017-11-07 15:39:15 -080098 PreCall.getIntent(
99 context,
100 CallIntentBuilder.forVoicemail(
101 phoneAccountHandle, CallInitiationType.Type.LEGACY_VOICEMAIL_NOTIFICATION)),
Varun Berry2d937db2021-11-05 11:04:39 -0700102 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Eric Erfanian8369df02017-05-03 10:27:13 -0700103 } else {
104 Intent launchVoicemailSettingsIntent =
105 new Intent(TelephonyManager.ACTION_CONFIGURE_VOICEMAIL);
106 launchVoicemailSettingsIntent.putExtra(TelephonyManager.EXTRA_HIDE_PUBLIC_SETTINGS, true);
twyenbf900242017-09-08 11:10:16 -0700107 launchVoicemailSettingsIntent.putExtra(
108 TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Eric Erfanian8369df02017-05-03 10:27:13 -0700109
110 launchVoicemailSettingsPendingIntent =
111 PendingIntent.getActivity(
112 context,
113 LAUNCH_VOICEMAIL_SETTINGS_REQUEST_CODE,
114 launchVoicemailSettingsIntent,
Varun Berry2d937db2021-11-05 11:04:39 -0700115 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Eric Erfanian8369df02017-05-03 10:27:13 -0700116 }
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 Erfaniand5e47f62017-03-15 14:41:07 -0700138}