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
.net
.URLEncoder
;
25 import org
.apache
.http
.HttpResponse
;
27 import android
.util
.Log
;
28 import de
.ub0r
.android
.websms
.connector
.common
.WebSMSException
;
31 * AsyncTask to manage IO to cherry-sms.com API.
35 public class ConnectorCherrySMS
extends Connector
{
36 /** Tag for output. */
37 private static final String TAG
= "WebSMS.cherry";
39 /** CherrySMS Gateway URL. */
40 private static final String URL
= "https://gw.cherry-sms.com/";
42 /** CherrySMS connector. */
43 private final boolean sendWithSender
;
46 * Create an CherrySMS Connector..
55 public ConnectorCherrySMS(final String u
, final String p
, final short con
) {
56 super(null); // FIXME:
58 case CHERRY_WO_SENDER
:
59 this.sendWithSender
= false;
62 this.sendWithSender
= true;
65 this.sendWithSender
= false;
71 * Check return code from cherry-sms.com.
75 * @return true if no error code
76 * @throws WebSMSException
79 private boolean checkReturnCode(final int ret
) throws WebSMSException
{
80 Log
.d(TAG
, "ret=" + ret
);
85 throw new WebSMSException(this.context
,
86 R
.string
.log_error_cherry_10
);
88 throw new WebSMSException(this.context
,
89 R
.string
.log_error_cherry_20
);
91 throw new WebSMSException(this.context
,
92 R
.string
.log_error_cherry_30
);
94 throw new WebSMSException(this.context
,
95 R
.string
.log_error_cherry_31
);
97 throw new WebSMSException(this.context
,
98 R
.string
.log_error_cherry_40
);
100 throw new WebSMSException(this.context
,
101 R
.string
.log_error_cherry_50
);
103 throw new WebSMSException(this.context
,
104 R
.string
.log_error_cherry_60
);
106 throw new WebSMSException(this.context
,
107 R
.string
.log_error_cherry_70
);
109 throw new WebSMSException(this.context
,
110 R
.string
.log_error_cherry_71
);
112 throw new WebSMSException(this.context
,
113 R
.string
.log_error_cherry_80
);
115 throw new WebSMSException(this.context
,
116 R
.string
.log_error_cherry_90
);
118 throw new WebSMSException(this.context
, R
.string
.log_error
,
126 * @return successful?
127 * @throws WebSMSException
130 private boolean sendData() throws WebSMSException
{
132 try { // get Connection
133 final StringBuilder url
= new StringBuilder(URL
);
134 url
.append("?user=");
135 // FIXME: url.append(this.user);
136 url
.append("&password=");
137 // FIXME: url.append(this.password);
139 if (this.text
!= null && this.to
!= null && this.to
.length
> 0) {
140 Log
.d(TAG
, "send with sender = " + this.sendWithSender
);
141 if (this.sendWithSender
) {
142 url
.append("&from=1");
144 url
.append("&message=");
145 url
.append(URLEncoder
.encode(this.text
));
147 String
[] recvs
= this.to
;
148 final int e
= recvs
.length
;
149 StringBuilder toBuf
= new StringBuilder(
150 international2oldformat(recvs
[0]));
151 for (int j
= 1; j
< e
; j
++) {
153 toBuf
.append(international2oldformat(recvs
[j
]));
155 url
.append(toBuf
.toString());
158 url
.append("&check=guthaben");
161 HttpResponse response
= getHttpClient(url
.toString(), null, null,
163 int resp
= response
.getStatusLine().getStatusCode();
164 if (resp
!= HttpURLConnection
.HTTP_OK
) {
165 throw new WebSMSException(this.context
,
166 R
.string
.log_error_http
, "" + resp
);
168 String htmlText
= stream2str(response
.getEntity().getContent())
170 String
[] lines
= htmlText
.split("\n");
171 Log
.d(TAG
, htmlText
);
173 int l
= lines
.length
;
174 // FIXME: WebSMS.SMS_BALANCE[CHERRY] = lines[l - 1].trim();
175 this.pushMessage(WebSMS
.MESSAGE_FREECOUNT
, null);
177 final int ret
= Integer
.parseInt(lines
[0].trim());
178 return this.checkReturnCode(ret
);
180 } catch (IOException e
) {
182 throw new WebSMSException(e
.getMessage());
191 protected final boolean updateMessages() throws WebSMSException
{
192 return this.sendData();
199 protected final boolean sendMessage() throws WebSMSException
{
200 if (!this.sendData()) {
202 throw new WebSMSException(this.context
, R
.string
.log_error
);