move some code
[andGMXsms.git] / src / de / ub0r / android / andGMXsms / ConnectorCherrySMS.java
blobfcfd504fa6cf83312783545305231a0dd91094fb
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.net.URLEncoder;
25 import org.apache.http.HttpResponse;
27 import android.util.Log;
28 import de.ub0r.android.websms.connector.common.WebSMSException;
30 /**
31 * AsyncTask to manage IO to cherry-sms.com API.
33 * @author flx
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;
45 /**
46 * Create an CherrySMS Connector..
48 * @param u
49 * user
50 * @param p
51 * password
52 * @param con
53 * connector type
55 public ConnectorCherrySMS(final String u, final String p, final short con) {
56 super(null); // FIXME:
57 switch (con) {
58 case CHERRY_WO_SENDER:
59 this.sendWithSender = false;
60 break;
61 case CHERRY_W_SENDER:
62 this.sendWithSender = true;
63 break;
64 default:
65 this.sendWithSender = false;
66 break;
70 /**
71 * Check return code from cherry-sms.com.
73 * @param ret
74 * return code
75 * @return true if no error code
76 * @throws WebSMSException
77 * WebSMSException
79 private boolean checkReturnCode(final int ret) throws WebSMSException {
80 Log.d(TAG, "ret=" + ret);
81 switch (ret) {
82 case 100:
83 return true;
84 case 10:
85 throw new WebSMSException(this.context,
86 R.string.log_error_cherry_10);
87 case 20:
88 throw new WebSMSException(this.context,
89 R.string.log_error_cherry_20);
90 case 30:
91 throw new WebSMSException(this.context,
92 R.string.log_error_cherry_30);
93 case 31:
94 throw new WebSMSException(this.context,
95 R.string.log_error_cherry_31);
96 case 40:
97 throw new WebSMSException(this.context,
98 R.string.log_error_cherry_40);
99 case 50:
100 throw new WebSMSException(this.context,
101 R.string.log_error_cherry_50);
102 case 60:
103 throw new WebSMSException(this.context,
104 R.string.log_error_cherry_60);
105 case 70:
106 throw new WebSMSException(this.context,
107 R.string.log_error_cherry_70);
108 case 71:
109 throw new WebSMSException(this.context,
110 R.string.log_error_cherry_71);
111 case 80:
112 throw new WebSMSException(this.context,
113 R.string.log_error_cherry_80);
114 case 90:
115 throw new WebSMSException(this.context,
116 R.string.log_error_cherry_90);
117 default:
118 throw new WebSMSException(this.context, R.string.log_error,
119 " code: " + ret);
124 * Send data.
126 * @return successful?
127 * @throws WebSMSException
128 * WebSMSException
130 private boolean sendData() throws WebSMSException {
131 // do IO
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));
146 url.append("&to=");
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++) {
152 toBuf.append(";");
153 toBuf.append(international2oldformat(recvs[j]));
155 url.append(toBuf.toString());
156 toBuf = null;
157 } else {
158 url.append("&check=guthaben");
160 // send data
161 HttpResponse response = getHttpClient(url.toString(), null, null,
162 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())
169 .trim();
170 String[] lines = htmlText.split("\n");
171 Log.d(TAG, htmlText);
172 htmlText = null;
173 int l = lines.length;
174 // FIXME: WebSMS.SMS_BALANCE[CHERRY] = lines[l - 1].trim();
175 this.pushMessage(WebSMS.MESSAGE_FREECOUNT, null);
176 if (l > 1) {
177 final int ret = Integer.parseInt(lines[0].trim());
178 return this.checkReturnCode(ret);
180 } catch (IOException e) {
181 Log.e(TAG, null, e);
182 throw new WebSMSException(e.getMessage());
184 return true;
188 * {@inheritDoc}
190 @Override
191 protected final boolean updateMessages() throws WebSMSException {
192 return this.sendData();
196 * {@inheritDoc}
198 @Override
199 protected final boolean sendMessage() throws WebSMSException {
200 if (!this.sendData()) {
201 // failed!
202 throw new WebSMSException(this.context, R.string.log_error);
203 } else {
204 // result: ok
205 return true;