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.voicemail.impl.sms;
17 
18 import android.annotation.TargetApi;
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.os.Build.VERSION_CODES;
22 import android.support.annotation.Nullable;
23 import android.telecom.PhoneAccountHandle;
24 import android.telephony.SmsManager;
25 import android.telephony.TelephonyManager;
26 import com.android.voicemail.impl.OmtpConstants;
27 import com.android.voicemail.impl.VvmLog;
28 
29 /**
30  * Send client originated OMTP messages to the OMTP server.
31  *
32  * <p>Uses {@link PendingIntent} instead of a call back to notify when the message is sent. This is
33  * primarily to keep the implementation simple and reuse what the underlying {@link SmsManager}
34  * interface provides.
35  *
36  * <p>Provides simple APIs to send different types of mobile originated OMTP SMS to the VVM server.
37  */
38 @TargetApi(VERSION_CODES.O)
39 public abstract class OmtpMessageSender {
40   protected static final String TAG = "OmtpMessageSender";
41   protected final Context context;
42   protected final PhoneAccountHandle phoneAccountHandle;
43   protected final short applicationPort;
44   protected final String destinationNumber;
45 
OmtpMessageSender( Context context, PhoneAccountHandle phoneAccountHandle, short applicationPort, String destinationNumber)46   public OmtpMessageSender(
47       Context context,
48       PhoneAccountHandle phoneAccountHandle,
49       short applicationPort,
50       String destinationNumber) {
51     this.context = context;
52     this.phoneAccountHandle = phoneAccountHandle;
53     this.applicationPort = applicationPort;
54     this.destinationNumber = destinationNumber;
55   }
56 
57   /**
58    * Sends a request to the VVM server to activate VVM for the current subscriber.
59    *
60    * @param sentIntent If not NULL this PendingIntent is broadcast when the message is successfully
61    *     sent, or failed.
62    */
requestVvmActivation(@ullable PendingIntent sentIntent)63   public void requestVvmActivation(@Nullable PendingIntent sentIntent) {}
64 
65   /**
66    * Sends a request to the VVM server to deactivate VVM for the current subscriber.
67    *
68    * @param sentIntent If not NULL this PendingIntent is broadcast when the message is successfully
69    *     sent, or failed.
70    */
requestVvmDeactivation(@ullable PendingIntent sentIntent)71   public void requestVvmDeactivation(@Nullable PendingIntent sentIntent) {}
72 
73   /**
74    * Send a request to the VVM server to get account status of the current subscriber.
75    *
76    * @param sentIntent If not NULL this PendingIntent is broadcast when the message is successfully
77    *     sent, or failed.
78    */
requestVvmStatus(@ullable PendingIntent sentIntent)79   public void requestVvmStatus(@Nullable PendingIntent sentIntent) {}
80 
sendSms(String text, PendingIntent sentIntent)81   protected void sendSms(String text, PendingIntent sentIntent) {
82 
83     VvmLog.v(
84         TAG, String.format("Sending sms '%s' to %s:%d", text, destinationNumber, applicationPort));
85 
86     context
87         .getSystemService(TelephonyManager.class)
88         .createForPhoneAccountHandle(phoneAccountHandle)
89         .sendVisualVoicemailSms(destinationNumber, applicationPort, text, sentIntent);
90   }
91 
appendField(StringBuilder sb, String field, Object value)92   protected void appendField(StringBuilder sb, String field, Object value) {
93     sb.append(field).append(OmtpConstants.SMS_KEY_VALUE_SEPARATOR).append(value);
94   }
95 }
96