move connectors
[andGMXsms.git] / connectors / innosend / src / de / ub0r / android / websms / connector / innosend / ConnectorInnosend.java
blob4979b0085ecae4ca25da51e77955eef13e416dc0
1 /*
2 * Copyright (C) 2010 Felix Bechstein
3 *
4 * This file is part of WebSMS.
5 *
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
9 * version.
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
14 * details.
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;
32 /**
33 * AsyncTask to manage IO to Innosend.de API.
35 * @author flx
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 = "";
56 /**
57 * Create an Innosend.de.
59 * @param u
60 * user
61 * @param p
62 * password
63 * @param con
64 * connector type
66 public ConnectorInnosend(final String u, final String p, final short con) {
67 super(null); // FIXME:
68 switch (con) {
69 case INNOSEND_FREE:
70 this.connector = 2;
71 this.free = true;
72 break;
73 case INNOSEND_WO_SENDER:
74 this.connector = 2;
75 this.free = false;
76 break;
77 case INNOSEND_W_SENDER:
78 this.free = false;
79 this.connector = 4;
80 break;
81 default:
82 this.connector = 2;
83 this.free = false;
84 break;
88 /**
89 * Check return code from innosend.de.
91 * @param ret
92 * return code
93 * @param more
94 * more text
95 * @param failOnError
96 * fail if return code is not 10*
97 * @return true if no error code
98 * @throws WebSMSException
99 * WebSMSException
101 private boolean checkReturnCode(final int ret, final String more,
102 final boolean failOnError) throws WebSMSException {
103 switch (ret) {
104 case 100:
105 case 101:
106 // this.pushMessage(WebSMS.MESSAGE_LOG, more + " "
107 // + this.context.getString(R.string.log_remain_free));
108 return true;
109 case 161:
110 if (!failOnError) {
111 if (this.balance.length() > 0) {
112 this.balance += "/";
114 this.balance += this.context
115 .getString(R.string.innosend_next_free)
116 + " " + more;
117 // FIXME: WebSMS.SMS_BALANCE[INNOSEND] = this.balance;
118 this.pushMessage(WebSMS.MESSAGE_FREECOUNT, null);
119 return true;
121 throw new WebSMSException(this.context,
122 R.string.log_error_innosend_161, " " + more);
123 default:
124 throw new WebSMSException(this.context, R.string.log_error,
125 " code: " + ret + " " + more);
130 * Check return code from innosend.de.
132 * @param ret
133 * return code
134 * @return true if no error code
135 * @throws WebSMSException
136 * WebSMSException
138 private boolean checkReturnCode(final int ret) throws WebSMSException {
139 switch (ret) {
140 case 100:
141 case 101:
142 return true;
143 case 111:
144 throw new WebSMSException(this.context,
145 R.string.log_error_innosend_111);
146 case 112:
147 throw new WebSMSException(this.context, R.string.log_error_pw);
148 case 120:
149 throw new WebSMSException(this.context,
150 R.string.log_error_innosend_111);
151 case 121:
152 throw new WebSMSException(this.context,
153 R.string.log_error_innosend_121);
154 case 122:
155 throw new WebSMSException(this.context,
156 R.string.log_error_innosend_122);
157 case 123:
158 throw new WebSMSException(this.context,
159 R.string.log_error_innosend_123);
160 case 129:
161 throw new WebSMSException(this.context,
162 R.string.log_error_innosend_129);
163 case 130:
164 throw new WebSMSException(this.context,
165 R.string.log_error_innosend_130);
166 case 140:
167 throw new WebSMSException(this.context,
168 R.string.log_error_innosend_140);
169 case 150:
170 throw new WebSMSException(this.context,
171 R.string.log_error_innosend_150);
172 case 170:
173 throw new WebSMSException(this.context,
174 R.string.log_error_innosend_170);
175 case 171:
176 throw new WebSMSException(this.context,
177 R.string.log_error_innosend_171);
178 case 172:
179 throw new WebSMSException(this.context,
180 R.string.log_error_innosend_172);
181 case 173:
182 throw new WebSMSException(this.context,
183 R.string.log_error_innosend_173);
184 default:
185 throw new WebSMSException(this.context, R.string.log_error,
186 " code: " + ret);
191 * Send data.
193 * @param updateFree
194 * update free sms
195 * @return successful?
196 * @throws WebSMSException
197 * WebSMSException
199 private boolean sendData(final boolean updateFree) throws WebSMSException {
200 // do IO
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) {
205 if (this.free) {
206 url.append("free.php");
207 d.add(new BasicNameValuePair("app", "1"));
208 d.add(new BasicNameValuePair("was", "iphone"));
209 } else {
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)));
220 } else {
222 .add(new BasicNameValuePair("absender",
223 this.customSender));
226 if (this.flashSMS) {
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()));
236 } else {
237 if (updateFree) {
238 url.append("free.php");
239 d.add(new BasicNameValuePair("app", "1"));
240 d.add(new BasicNameValuePair("was", "iphone"));
241 } else {
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,
248 null, null);
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())
255 .trim();
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);
261 } else {
262 i = htmlText.indexOf("<br>");
263 int ret;
264 Log.d(TAG, url.toString());
265 if (i < 0) {
266 ret = Integer.parseInt(htmlText);
267 return updateFree || this.checkReturnCode(ret);
268 } else {
269 ret = Integer.parseInt(htmlText.substring(0, i));
270 return this.checkReturnCode(ret, htmlText.substring(i + 4)
271 .trim(), !updateFree);
274 } catch (IOException e) {
275 Log.e(TAG, null, e);
276 throw new WebSMSException(e.getMessage());
278 return true;
282 * {@inheritDoc}
284 @Override
285 protected final boolean updateMessages() throws WebSMSException {
286 return this.sendData(false) && this.sendData(true);
290 * {@inheritDoc}
292 @Override
293 protected final boolean sendMessage() throws WebSMSException {
294 if (!this.sendData(false)) {
295 // failed!
296 throw new WebSMSException(this.context, R.string.log_error);
297 } else {
298 // result: ok
299 return true;
304 * {@inheritDoc}
306 @Override
307 protected final boolean supportFlashsms() {
308 return (this.connector == 2 && !this.free);
312 * {@inheritDoc}
314 @Override
315 protected final boolean supportCustomsender() {
316 return (this.connector != 2 && !this.free);
320 * {@inheritDoc}
322 @Override
323 protected final boolean supportSendLater() {
324 return !this.free;