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
.sloono
;
21 import java
.io
.IOException
;
22 import java
.net
.HttpURLConnection
;
23 import java
.net
.URLEncoder
;
25 import org
.apache
.http
.HttpResponse
;
27 import android
.content
.Context
;
28 import android
.content
.Intent
;
29 import android
.content
.SharedPreferences
;
30 import android
.preference
.PreferenceManager
;
31 import android
.util
.Log
;
32 import de
.ub0r
.android
.websms
.connector
.common
.Connector
;
33 import de
.ub0r
.android
.websms
.connector
.common
.ConnectorCommand
;
34 import de
.ub0r
.android
.websms
.connector
.common
.ConnectorSpec
;
35 import de
.ub0r
.android
.websms
.connector
.common
.Utils
;
36 import de
.ub0r
.android
.websms
.connector
.common
.WebSMSException
;
37 import de
.ub0r
.android
.websms
.connector
.common
.ConnectorSpec
.SubConnectorSpec
;
40 * AsyncTask to manage IO to sloono.de API.
44 public class ConnectorSloono
extends Connector
{
45 /** Tag for output. */
46 private static final String TAG
= "WebSMS.sloono";
47 /** {@link SubConnectorSpec} ID: basic. */
48 private static final String ID_BASIC
= "1";
49 /** {@link SubConnectorSpec} ID: discount. */
50 private static final String ID_DISCOUNT
= "0";
51 /** {@link SubConnectorSpec} ID: pro. */
52 private static final String ID_PRO
= "2";
53 /** {@link SubConnectorSpec} ID: flash. */
54 private static final String ID_FLASH
= "3";
56 /** Preferences intent action. */
57 private static final String PREFS_INTENT_ACTION
= "de.ub0r.android."
58 + "websms.connectors.sloono.PREFS";
60 /** Sloono Gateway URL. */
61 private static final String URL_SEND
= // .
62 "http://www.sloono.de/API/httpsms.php";
63 /** Sloono Gateway URL. */
64 private static final String URL_BALANCE
= // .
65 "http://www.sloono.de/API/httpkonto.php";
71 public final ConnectorSpec
initSpec(final Context context
) {
72 final String name
= context
.getString(R
.string
.connector_sloono_name
);
73 ConnectorSpec c
= new ConnectorSpec(TAG
, name
);
75 context
.getString(R
.string
.connector_sloono_author
));
77 c
.setPrefsIntent(PREFS_INTENT_ACTION
);
78 c
.setPrefsTitle(context
.getString(R
.string
.settings_sloono
));
79 c
.setCapabilities(ConnectorSpec
.CAPABILITIES_UPDATE
80 | ConnectorSpec
.CAPABILITIES_SEND
);
81 final short f
= (short) (SubConnectorSpec
.FEATURE_MULTIRECIPIENTS
82 | SubConnectorSpec
.FEATURE_FLASHSMS
| // .
83 SubConnectorSpec
.FEATURE_SENDLATER
);
84 c
.addSubConnector(ID_BASIC
, context
.getString(R
.string
.basic
), f
);
85 c
.addSubConnector(ID_DISCOUNT
, context
.getString(R
.string
.discount
), f
);
86 c
.addSubConnector(ID_PRO
, context
.getString(R
.string
.pro
), f
);
94 public final ConnectorSpec
updateSpec(final Context context
,
95 final ConnectorSpec connectorSpec
) {
96 final SharedPreferences p
= PreferenceManager
97 .getDefaultSharedPreferences(context
);
98 if (p
.getBoolean(Preferences
.PREFS_ENABLED
, false)) {
99 if (p
.getString(Preferences
.PREFS_USER
, "").length() > 0
100 && p
.getString(Preferences
.PREFS_PASSWORD
, "")// .
102 connectorSpec
.setReady();
104 connectorSpec
.setStatus(ConnectorSpec
.STATUS_ENABLED
);
107 connectorSpec
.setStatus(ConnectorSpec
.STATUS_INACTIVE
);
109 return connectorSpec
;
113 * Check return code from sloono.de.
119 * @return true if no error code
120 * @throws WebSMSException
123 private boolean checkReturnCode(final Context context
, final int ret
)
124 throws WebSMSException
{
125 Log
.d(TAG
, "ret=" + ret
);
128 } else if (ret
< 300) {
129 throw new WebSMSException(context
, R
.string
.error_input
);
132 throw new WebSMSException(context
, R
.string
.error_pw
);
134 throw new WebSMSException(context
, R
.string
.error_server
, // .
145 * {@link ConnectorCommand}
146 * @throws WebSMSException
149 private void sendData(final Context context
, final ConnectorCommand command
)
150 throws WebSMSException
{
152 try { // get Connection
153 final String text
= command
.getText();
154 final boolean checkOnly
= (text
== null || text
.length() == 0);
155 final StringBuilder url
= new StringBuilder();
156 final ConnectorSpec cs
= this.getSpec(context
);
157 final SharedPreferences p
= PreferenceManager
158 .getDefaultSharedPreferences(context
);
160 url
.append(URL_BALANCE
);
162 url
.append(URL_SEND
);
164 url
.append("?user=");
165 url
.append(p
.getString(Preferences
.PREFS_USER
, ""));
166 url
.append("&password=");
167 url
.append(Utils
.md5(p
.getString(Preferences
.PREFS_PASSWORD
, "")));
171 if (command
.getFlashSMS()) {
172 url
.append(ID_FLASH
);
174 url
.append(command
.getSelectedSubConnector());
176 final long sendLater
= command
.getSendLater();
178 url
.append("×tamp=");
179 url
.append(sendLater
/ 1000);
181 url
.append("&text=");
182 url
.append(URLEncoder
.encode(text
));
184 url
.append(Utils
.joinRecipientsNumbers(command
.getRecipients(), ",", true));
187 HttpResponse response
= Utils
.getHttpClient(url
.toString(), null,
189 int resp
= response
.getStatusLine().getStatusCode();
190 if (resp
!= HttpURLConnection
.HTTP_OK
) {
191 this.checkReturnCode(context
, resp
);
192 throw new WebSMSException(context
, R
.string
.error_http
, " "
195 String htmlText
= Utils
.stream2str(
196 response
.getEntity().getContent()).trim();
197 String
[] lines
= htmlText
.split("\n");
198 Log
.d(TAG
, "--HTTP RESPONSE--");
199 Log
.d(TAG
, htmlText
);
200 Log
.d(TAG
, "--HTTP RESPONSE--");
202 for (String s
: lines
) {
203 if (s
.startsWith("Kontostand: ")) {
204 cs
.setBalance(s
.split(" ")[1].trim() + "\u20AC");
207 } catch (IOException e
) {
209 throw new WebSMSException(e
.getMessage());
217 protected final void doUpdate(final Context context
, final Intent intent
)
218 throws WebSMSException
{
219 this.sendData(context
, new ConnectorCommand(intent
));
226 protected final void doSend(final Context context
, final Intent intent
)
227 throws WebSMSException
{
228 this.sendData(context
, new ConnectorCommand(intent
));