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 */
16package com.android.phone.vvm.omtp.sms;
17
18import android.annotation.Nullable;
19import android.app.PendingIntent;
20import android.telephony.SmsManager;
21import android.text.TextUtils;
22
23import com.android.phone.vvm.omtp.OmtpConstants;
24
25/**
26 * A implementation of the OmtpMessageSender using the standard OMTP sms protocol.
27 */
28public class OmtpStandardMessageSender extends OmtpMessageSender {
29    private final String mClientType;
30    private final String mProtocolVersion;
31    private final String mClientPrefix;
32
33    /**
34     * Creates a new instance of OmtpStandardMessageSender.
35     *
36     * @param smsManager SMS sending library. There is a different SmsManager for each SIM.
37     * @param applicationPort If set to a value > 0 then a binary sms is sent to this port number.
38     *            Otherwise, a standard text SMS is sent.
39     * @param destinationNumber Destination number to be used.
40     * @param clientType The "ct" field to be set in the MO message. This is the value used by the
41     *            VVM server to identify the client. Certain VVM servers require a specific agreed
42     *            value for this field.
43     * @param protocolVersion OMTP protocol version.
44     * @param clientPrefix The client prefix requested to be used by the server in its MT messages.
45     */
46    public OmtpStandardMessageSender(SmsManager smsManager, short applicationPort,
47            String destinationNumber, String clientType, String protocolVersion,
48            String clientPrefix) {
49        super(smsManager, applicationPort, destinationNumber);
50        mClientType = clientType;
51        mProtocolVersion = protocolVersion;
52        mClientPrefix = clientPrefix;
53    }
54
55    // Activate message:
56    // V1.1: Activate:pv=<value>;ct=<value>
57    // V1.2: Activate:pv=<value>;ct=<value>;pt=<value>;<Clientprefix>
58    // V1.3: Activate:pv=<value>;ct=<value>;pt=<value>;<Clientprefix>
59    @Override
60    public void requestVvmActivation(@Nullable PendingIntent sentIntent) {
61        StringBuilder sb = new StringBuilder().append(OmtpConstants.ACTIVATE_REQUEST);
62
63        appendProtocolVersionAndClientType(sb);
64        if (TextUtils.equals(mProtocolVersion, OmtpConstants.PROTOCOL_VERSION1_2) ||
65                TextUtils.equals(mProtocolVersion, OmtpConstants.PROTOCOL_VERSION1_3)) {
66            appendApplicationPort(sb);
67            appendClientPrefix(sb);
68        }
69
70        sendSms(sb.toString(), sentIntent);
71    }
72
73    // Deactivate message:
74    // V1.1: Deactivate:pv=<value>;ct=<string>
75    // V1.2: Deactivate:pv=<value>;ct=<string>
76    // V1.3: Deactivate:pv=<value>;ct=<string>
77    @Override
78    public void requestVvmDeactivation(@Nullable PendingIntent sentIntent) {
79        StringBuilder sb = new StringBuilder().append(OmtpConstants.DEACTIVATE_REQUEST);
80        appendProtocolVersionAndClientType(sb);
81
82        sendSms(sb.toString(), sentIntent);
83    }
84
85    // Status message:
86    // V1.1: STATUS
87    // V1.2: STATUS
88    // V1.3: STATUS:pv=<value>;ct=<value>;pt=<value>;<Clientprefix>
89    @Override
90    public void requestVvmStatus(@Nullable PendingIntent sentIntent) {
91        StringBuilder sb = new StringBuilder().append(OmtpConstants.STATUS_REQUEST);
92
93        if (TextUtils.equals(mProtocolVersion, OmtpConstants.PROTOCOL_VERSION1_3)) {
94            appendProtocolVersionAndClientType(sb);
95            appendApplicationPort(sb);
96            appendClientPrefix(sb);
97        }
98
99        sendSms(sb.toString(), sentIntent);
100    }
101
102    private void appendProtocolVersionAndClientType(StringBuilder sb) {
103        sb.append(OmtpConstants.SMS_PREFIX_SEPARATOR);
104        appendField(sb, OmtpConstants.PROTOCOL_VERSION, mProtocolVersion);
105        sb.append(OmtpConstants.SMS_FIELD_SEPARATOR);
106        appendField(sb, OmtpConstants.CLIENT_TYPE, mClientType);
107    }
108
109    private void appendApplicationPort(StringBuilder sb) {
110        sb.append(OmtpConstants.SMS_FIELD_SEPARATOR);
111        appendField(sb, OmtpConstants.APPLICATION_PORT, mApplicationPort);
112    }
113
114    private void appendClientPrefix(StringBuilder sb) {
115        sb.append(OmtpConstants.SMS_FIELD_SEPARATOR);
116        sb.append(mClientPrefix);
117    }
118}
119