• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.Build.VERSION_CODES;
24 import android.os.Bundle;
25 import android.support.annotation.Nullable;
26 import android.telecom.PhoneAccountHandle;
27 import android.telephony.TelephonyManager;
28 import android.telephony.VisualVoicemailSms;
29 import com.android.dialer.callintent.CallInitiationType;
30 import com.android.dialer.callintent.CallIntentBuilder;
31 import com.android.dialer.precall.PreCall;
32 import com.android.voicemail.VoicemailClient;
33 import com.android.voicemail.impl.OmtpConstants;
34 import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
35 import com.android.voicemail.impl.VvmLog;
36 
37 /**
38  * Class ot handle voicemail SMS under legacy mode
39  *
40  * @see OmtpVvmCarrierConfigHelper#isLegacyModeEnabled()
41  */
42 @TargetApi(VERSION_CODES.O)
43 public class LegacyModeSmsHandler {
44 
45   private static final String TAG = "LegacyModeSmsHandler";
46 
47   private static final int CALL_VOICEMAIL_REQUEST_CODE = 1;
48   private static final int LAUNCH_VOICEMAIL_SETTINGS_REQUEST_CODE = 2;
49 
handle(Context context, VisualVoicemailSms sms)50   public static void handle(Context context, VisualVoicemailSms sms) {
51     VvmLog.i(TAG, "processing VVM SMS on legacy mode");
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);
58       VvmLog.i(
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:
64           sendLegacyVoicemailNotification(context, handle, message.getNewMessageCount());
65 
66           break;
67         default:
68           break;
69       }
70     }
71   }
72 
sendLegacyVoicemailNotification( Context context, PhoneAccountHandle phoneAccountHandle, int messageCount)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());
83     intent.putExtra(VoicemailClient.EXTRA_IS_LEGACY_MODE, true);
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,
98               PreCall.getIntent(
99                   context,
100                   CallIntentBuilder.forVoicemail(
101                       phoneAccountHandle, CallInitiationType.Type.LEGACY_VOICEMAIL_NOTIFICATION)),
102               PendingIntent.FLAG_UPDATE_CURRENT);
103     } else {
104       Intent launchVoicemailSettingsIntent =
105           new Intent(TelephonyManager.ACTION_CONFIGURE_VOICEMAIL);
106       launchVoicemailSettingsIntent.putExtra(TelephonyManager.EXTRA_HIDE_PUBLIC_SETTINGS, true);
107       launchVoicemailSettingsIntent.putExtra(
108           TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
109 
110       launchVoicemailSettingsPendingIntent =
111           PendingIntent.getActivity(
112               context,
113               LAUNCH_VOICEMAIL_SETTINGS_REQUEST_CODE,
114               launchVoicemailSettingsIntent,
115               PendingIntent.FLAG_UPDATE_CURRENT);
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
getVoicemailNumber(Context context, PhoneAccountHandle phoneAccountHandle)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   }
138 }
139