• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Google Inc. All Rights Reserved.
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.phone.vvm.omtp.sms;
17 
18 import android.annotation.Nullable;
19 import android.app.PendingIntent;
20 import android.telephony.SmsManager;
21 
22 import com.android.phone.vvm.omtp.OmtpConstants;
23 import com.android.phone.vvm.omtp.VvmLog;
24 
25 import java.io.UnsupportedEncodingException;
26 import java.util.Locale;
27 
28 /**
29  * Send client originated OMTP messages to the OMTP server.
30  * <p>
31  * Uses {@link PendingIntent} instead of a call back to notify when the message is
32  * sent. This is primarily to keep the implementation simple and reuse what the underlying
33  * {@link SmsManager} interface provides.
34  * <p>
35  * Provides simple APIs to send different types of mobile originated OMTP SMS to the VVM server.
36  */
37 public abstract class OmtpMessageSender {
38     protected static final String TAG = "OmtpMessageSender";
39     protected short mApplicationPort;
40     protected String mDestinationNumber;
41     protected SmsManager mSmsManager;
42 
OmtpMessageSender(SmsManager smsManager, short applicationPort, String destinationNumber)43     public OmtpMessageSender(SmsManager smsManager, short applicationPort,
44             String destinationNumber) {
45         mSmsManager = smsManager;
46         mApplicationPort = applicationPort;
47         mDestinationNumber = destinationNumber;
48     }
49 
50     /**
51      * Sends a request to the VVM server to activate VVM for the current subscriber.
52      *
53      * @param sentIntent If not NULL this PendingIntent is broadcast when the message is
54      *            successfully sent, or failed.
55      */
requestVvmActivation(@ullable PendingIntent sentIntent)56     public void requestVvmActivation(@Nullable PendingIntent sentIntent) {}
57 
58     /**
59      * Sends a request to the VVM server to deactivate VVM for the current subscriber.
60      *
61      * @param sentIntent If not NULL this PendingIntent is broadcast when the message is
62      *            successfully sent, or failed.
63      */
requestVvmDeactivation(@ullable PendingIntent sentIntent)64     public void requestVvmDeactivation(@Nullable PendingIntent sentIntent) {}
65 
66     /**
67      * Send a request to the VVM server to get account status of the current subscriber.
68      *
69      * @param sentIntent If not NULL this PendingIntent is broadcast when the message is
70      *            successfully sent, or failed.
71      */
requestVvmStatus(@ullable PendingIntent sentIntent)72     public void requestVvmStatus(@Nullable PendingIntent sentIntent) {}
73 
sendSms(String text, PendingIntent sentIntent)74     protected void sendSms(String text, PendingIntent sentIntent) {
75         // If application port is set to 0 then send simple text message, else send data message.
76         if (mApplicationPort == 0) {
77             VvmLog
78                     .v(TAG, String.format("Sending TEXT sms '%s' to %s", text, mDestinationNumber));
79             mSmsManager.sendTextMessageWithSelfPermissions(mDestinationNumber, null, text,
80                     sentIntent, null, false);
81         } else {
82             byte[] data;
83             try {
84                 data = text.getBytes("UTF-8");
85             } catch (UnsupportedEncodingException e) {
86                 throw new IllegalStateException("Failed to encode: " + text);
87             }
88             VvmLog.v(TAG,
89                     String.format(Locale.US, "Sending BINARY sms '%s' to %s:%d", text,
90                             mDestinationNumber, mApplicationPort));
91             mSmsManager.sendDataMessageWithSelfPermissions(mDestinationNumber, null,
92                     mApplicationPort, data, sentIntent, null);
93         }
94     }
95 
appendField(StringBuilder sb, String field, Object value)96     protected void appendField(StringBuilder sb, String field, Object value) {
97         sb.append(field).append(OmtpConstants.SMS_KEY_VALUE_SEPARATOR).append(value);
98     }
99 }
100