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