improve some code
[andGMXsms.git] / connectors / sloono / src / de / ub0r / android / websms / connector / sloono / ConnectorSloono.java
blobe41a33d961fc986985544667f3f43ce93dbba849
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.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;
39 /**
40 * AsyncTask to manage IO to sloono.de API.
42 * @author flx
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";
67 /**
68 * {@inheritDoc}
70 @Override
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);
74 c.setAuthor(// .
75 context.getString(R.string.connector_sloono_author));
76 c.setBalance(null);
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);
87 return c;
90 /**
91 * {@inheritDoc}
93 @Override
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, "")// .
101 .length() > 0) {
102 connectorSpec.setReady();
103 } else {
104 connectorSpec.setStatus(ConnectorSpec.STATUS_ENABLED);
106 } else {
107 connectorSpec.setStatus(ConnectorSpec.STATUS_INACTIVE);
109 return connectorSpec;
113 * Check return code from sloono.de.
115 * @param context
116 * {@link Context}
117 * @param ret
118 * return code
119 * @return true if no error code
120 * @throws WebSMSException
121 * WebSMSException
123 private boolean checkReturnCode(final Context context, final int ret)
124 throws WebSMSException {
125 Log.d(TAG, "ret=" + ret);
126 if (ret < 200) {
127 return true;
128 } else if (ret < 300) {
129 throw new WebSMSException(context, R.string.error_input);
130 } else {
131 if (ret == 401) {
132 throw new WebSMSException(context, R.string.error_pw);
134 throw new WebSMSException(context, R.string.error_server, // .
135 " " + ret);
140 * Send data.
142 * @param context
143 * {@link Context}
144 * @param command
145 * {@link ConnectorCommand}
146 * @throws WebSMSException
147 * WebSMSException
149 private void sendData(final Context context, final ConnectorCommand command)
150 throws WebSMSException {
151 // do IO
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);
159 if (checkOnly) {
160 url.append(URL_BALANCE);
161 } else {
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, "")));
169 if (!checkOnly) {
170 url.append("&typ=");
171 if (command.getFlashSMS()) {
172 url.append(ID_FLASH);
173 } else {
174 url.append(command.getSelectedSubConnector());
176 final long sendLater = command.getSendLater();
177 if (sendLater > 0) {
178 url.append("&timestamp=");
179 url.append(sendLater / 1000);
181 url.append("&text=");
182 url.append(URLEncoder.encode(text));
183 url.append("&to=");
184 url.append(Utils.joinRecipientsNumbers(command.getRecipients(), ",", true));
186 // send data
187 HttpResponse response = Utils.getHttpClient(url.toString(), null,
188 null, null, 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, " "
193 + resp);
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--");
201 htmlText = null;
202 for (String s : lines) {
203 if (s.startsWith("Kontostand: ")) {
204 cs.setBalance(s.split(" ")[1].trim() + "\u20AC");
207 } catch (IOException e) {
208 Log.e(TAG, null, e);
209 throw new WebSMSException(e.getMessage());
214 * {@inheritDoc}
216 @Override
217 protected final void doUpdate(final Context context, final Intent intent)
218 throws WebSMSException {
219 this.sendData(context, new ConnectorCommand(intent));
223 * {@inheritDoc}
225 @Override
226 protected final void doSend(final Context context, final Intent intent)
227 throws WebSMSException {
228 this.sendData(context, new ConnectorCommand(intent));