2 * Copyright (C) 2010 Felix Bechstein
4 * This file is part of WebSMS.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation; either version 3 of the License, or (at your option) any later
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License along with
17 * this program; If not, see <http://www.gnu.org/licenses/>.
19 package de
.ub0r
.android
.websms
.connector
.sms
;
21 import java
.util
.ArrayList
;
23 import android
.content
.BroadcastReceiver
;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.content
.SharedPreferences
;
27 import android
.preference
.PreferenceManager
;
28 import android
.telephony
.gsm
.SmsManager
;
29 import android
.util
.Log
;
30 import android
.widget
.Toast
;
31 import de
.ub0r
.android
.andGMXsms
.R
;
32 import de
.ub0r
.android
.andGMXsms
.Connector
.WebSMSException
;
33 import de
.ub0r
.android
.websms
.connector
.ConnectorCommand
;
34 import de
.ub0r
.android
.websms
.connector
.ConnectorSpec
;
35 import de
.ub0r
.android
.websms
.connector
.Constants
;
36 import de
.ub0r
.android
.websms
.connector
.ConnectorSpec
.SubConnectorSpec
;
39 * Receives commands coming as broadcast from WebSMS.
43 @SuppressWarnings("deprecation")
44 public class CommandReceiverSMS
extends BroadcastReceiver
{
45 /** Tag for debug output. */
46 private static final String TAG
= "WebSMS.sms";
48 /** Preference key: enabled. */
49 private static final String PREFS_ENABLED
= "enable_sms";
51 /** Internal {@link ConnectorSpec}. */
52 private static ConnectorSpec conector
= null;
59 * @return ConnectorSpec
61 private static synchronized ConnectorSpec
getSpecs(final Context context
) {
62 if (conector
== null) {
63 conector
= new ConnectorSpec(TAG
, context
64 .getString(R
.string
.connector_sms_name
));
65 conector
.setAuthor(// .
66 context
.getString(R
.string
.connector_sms_author
));
67 conector
.setBalance(null);
68 conector
.setPrefsIntent(null);
69 conector
.setPrefsTitle(null);
70 conector
.setCapabilities(ConnectorSpec
.CAPABILITIES_SEND
);
71 conector
.addSubConnector(TAG
, conector
.getName(),
72 SubConnectorSpec
.FEATURE_MULTIRECIPIENTS
);
74 final SharedPreferences p
= PreferenceManager
75 .getDefaultSharedPreferences(context
);
76 if (p
.getBoolean(PREFS_ENABLED
, false)) {
79 conector
.setStatus(ConnectorSpec
.STATUS_INACTIVE
);
88 * command comming from intent
89 * @throws WebSMSException
92 private void send(final ConnectorCommand command
) throws WebSMSException
{
94 SmsManager sm
= SmsManager
.getDefault();
95 for (String t
: command
.getRecipients()) {
96 ArrayList
<String
> messages
= sm
97 .divideMessage(command
.getText());
98 sm
.sendMultipartTextMessage(t
, null, messages
, null, null);
99 for (String m
: messages
) {
100 Log
.d(TAG
, "send sms: " + t
+ " text: " + m
);
103 } catch (Exception e
) {
104 throw new WebSMSException(e
.toString());
109 * Send INFO Broadcast back to WebSMS.
114 * {@link ConnectorSpec}; if null, getSpecs() is called to get
117 private void sendInfo(final Context context
, final ConnectorSpec specs
) {
118 ConnectorSpec c
= specs
;
120 c
= getSpecs(context
);
122 final Intent i
= new Intent(Constants
.ACTION_CONNECTOR_INFO
);
124 Log
.d(TAG
, "send broadcast: " + i
.getAction());
125 context
.sendBroadcast(i
);
132 public final void onReceive(final Context context
, final Intent intent
) {
133 final String action
= intent
.getAction();
134 Log
.d(TAG
, "action: " + action
);
135 if (action
== null) {
138 if (Constants
.ACTION_CONNECTOR_UPDATE
.equals(action
)) {
139 this.sendInfo(context
, null);
140 } else if (Constants
.ACTION_CONNECTOR_RUN_SEND
.equals(action
)) {
141 final ConnectorCommand command
= new ConnectorCommand(intent
);
142 if (command
.getType() == ConnectorCommand
.TYPE_SEND
) {
143 final ConnectorSpec origSpecs
= new ConnectorSpec(intent
);
144 final ConnectorSpec specs
= CommandReceiverSMS
146 if (specs
.getID().equals(origSpecs
.getID())
147 && specs
.hasStatus(ConnectorSpec
.STATUS_READY
)) {
148 // check internal status
151 } catch (WebSMSException e
) {
153 Toast
.makeText(context
,
154 specs
.getName() + ": " + e
.getMessage(),
155 Toast
.LENGTH_LONG
).show();
157 this.sendInfo(context
, specs
);