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
.andGMXsms
;
21 import java
.io
.IOException
;
22 import java
.net
.HttpURLConnection
;
23 import java
.util
.ArrayList
;
25 import org
.apache
.http
.HttpResponse
;
26 import org
.apache
.http
.message
.BasicNameValuePair
;
28 import android
.text
.format
.DateFormat
;
29 import android
.util
.Log
;
30 import de
.ub0r
.android
.websms
.connector
.common
.WebSMSException
;
33 * AsyncTask to manage IO to Innosend.de API.
37 public class ConnectorInnosend
extends Connector
{
38 /** Tag for output. */
39 private static final String TAG
= "WebSMS.inno";
41 /** Innosend Gateway URL. */
42 private static final String URL
= "https://www.innosend.de/gateway/";
44 /** Custom Dateformater. */
45 private static final String DATEFORMAT
= "dd.MM.yyyy-kk:mm";
47 /** Try to send free sms. */
48 private final boolean free
;
50 /** Innosend connector. */
51 private final short connector
;
53 /** Innosend balance. */
54 private String balance
= "";
57 * Create an Innosend.de.
66 public ConnectorInnosend(final String u
, final String p
, final short con
) {
67 super(null); // FIXME:
73 case INNOSEND_WO_SENDER
:
77 case INNOSEND_W_SENDER
:
89 * Check return code from innosend.de.
96 * fail if return code is not 10*
97 * @return true if no error code
98 * @throws WebSMSException
101 private boolean checkReturnCode(final int ret
, final String more
,
102 final boolean failOnError
) throws WebSMSException
{
106 // this.pushMessage(WebSMS.MESSAGE_LOG, more + " "
107 // + this.context.getString(R.string.log_remain_free));
111 if (this.balance
.length() > 0) {
114 this.balance
+= this.context
115 .getString(R
.string
.innosend_next_free
)
117 // FIXME: WebSMS.SMS_BALANCE[INNOSEND] = this.balance;
118 this.pushMessage(WebSMS
.MESSAGE_FREECOUNT
, null);
121 throw new WebSMSException(this.context
,
122 R
.string
.log_error_innosend_161
, " " + more
);
124 throw new WebSMSException(this.context
, R
.string
.log_error
,
125 " code: " + ret
+ " " + more
);
130 * Check return code from innosend.de.
134 * @return true if no error code
135 * @throws WebSMSException
138 private boolean checkReturnCode(final int ret
) throws WebSMSException
{
144 throw new WebSMSException(this.context
,
145 R
.string
.log_error_innosend_111
);
147 throw new WebSMSException(this.context
, R
.string
.log_error_pw
);
149 throw new WebSMSException(this.context
,
150 R
.string
.log_error_innosend_111
);
152 throw new WebSMSException(this.context
,
153 R
.string
.log_error_innosend_121
);
155 throw new WebSMSException(this.context
,
156 R
.string
.log_error_innosend_122
);
158 throw new WebSMSException(this.context
,
159 R
.string
.log_error_innosend_123
);
161 throw new WebSMSException(this.context
,
162 R
.string
.log_error_innosend_129
);
164 throw new WebSMSException(this.context
,
165 R
.string
.log_error_innosend_130
);
167 throw new WebSMSException(this.context
,
168 R
.string
.log_error_innosend_140
);
170 throw new WebSMSException(this.context
,
171 R
.string
.log_error_innosend_150
);
173 throw new WebSMSException(this.context
,
174 R
.string
.log_error_innosend_170
);
176 throw new WebSMSException(this.context
,
177 R
.string
.log_error_innosend_171
);
179 throw new WebSMSException(this.context
,
180 R
.string
.log_error_innosend_172
);
182 throw new WebSMSException(this.context
,
183 R
.string
.log_error_innosend_173
);
185 throw new WebSMSException(this.context
, R
.string
.log_error
,
195 * @return successful?
196 * @throws WebSMSException
199 private boolean sendData(final boolean updateFree
) throws WebSMSException
{
201 try { // get Connection
202 final StringBuilder url
= new StringBuilder(URL
);
203 ArrayList
<BasicNameValuePair
> d
= new ArrayList
<BasicNameValuePair
>();
204 if (this.text
!= null && this.to
!= null && this.to
.length
> 0) {
206 url
.append("free.php");
207 d
.add(new BasicNameValuePair("app", "1"));
208 d
.add(new BasicNameValuePair("was", "iphone"));
210 url
.append("sms.php");
212 d
.add(new BasicNameValuePair("text", this.text
));
213 d
.add(new BasicNameValuePair("type", this.connector
+ ""));
214 d
.add(new BasicNameValuePair("empfaenger",
215 international2oldformat(this.to
[0])));
217 if (this.customSender
== null) {
218 // FIXME: d.add(new BasicNameValuePair("absender",
219 // international2national(WebSMS.prefsSender)));
222 .add(new BasicNameValuePair("absender",
227 d
.add(new BasicNameValuePair("flash", "1"));
229 if (this.sendLater
> 0) {
230 if (this.sendLater
<= 0) {
231 this.sendLater
= System
.currentTimeMillis();
233 d
.add(new BasicNameValuePair("termin", DateFormat
.format(
234 DATEFORMAT
, this.sendLater
).toString()));
238 url
.append("free.php");
239 d
.add(new BasicNameValuePair("app", "1"));
240 d
.add(new BasicNameValuePair("was", "iphone"));
242 url
.append("konto.php");
245 // FIXME: d.add(new BasicNameValuePair("id", this.user));
246 // FIXME: d.add(new BasicNameValuePair("pw", this.password));
247 HttpResponse response
= getHttpClient(url
.toString(), null, d
,
249 int resp
= response
.getStatusLine().getStatusCode();
250 if (resp
!= HttpURLConnection
.HTTP_OK
) {
251 throw new WebSMSException(this.context
,
252 R
.string
.log_error_http
, "" + resp
);
254 String htmlText
= stream2str(response
.getEntity().getContent())
256 int i
= htmlText
.indexOf(',');
257 if (i
> 0 && !updateFree
) {
258 this.balance
= htmlText
.substring(0, i
+ 3) + "\u20AC";
259 // FIXME: WebSMS.SMS_BALANCE[INNOSEND] = this.balance;
260 this.pushMessage(WebSMS
.MESSAGE_FREECOUNT
, null);
262 i
= htmlText
.indexOf("<br>");
264 Log
.d(TAG
, url
.toString());
266 ret
= Integer
.parseInt(htmlText
);
267 return updateFree
|| this.checkReturnCode(ret
);
269 ret
= Integer
.parseInt(htmlText
.substring(0, i
));
270 return this.checkReturnCode(ret
, htmlText
.substring(i
+ 4)
271 .trim(), !updateFree
);
274 } catch (IOException e
) {
276 throw new WebSMSException(e
.getMessage());
285 protected final boolean updateMessages() throws WebSMSException
{
286 return this.sendData(false) && this.sendData(true);
293 protected final boolean sendMessage() throws WebSMSException
{
294 if (!this.sendData(false)) {
296 throw new WebSMSException(this.context
, R
.string
.log_error
);
307 protected final boolean supportFlashsms() {
308 return (this.connector
== 2 && !this.free
);
315 protected final boolean supportCustomsender() {
316 return (this.connector
!= 2 && !this.free
);
323 protected final boolean supportSendLater() {