fix some code
[andGMXsms.git] / connectors / sipgate / src / de / ub0r / android / websms / connector / sipgate / ConnectorSipgate.java
blob146b8b950a74eaa23a68c6a4400dabee0ebec487
1 /*
2 * Copyright (C) 2010 Mirko Weber, 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.sipgate;
21 import java.io.Serializable;
22 import java.util.Hashtable;
23 import java.util.Map;
24 import java.util.Vector;
26 import org.xmlrpc.android.XMLRPCClient;
27 import org.xmlrpc.android.XMLRPCException;
28 import org.xmlrpc.android.XMLRPCFault;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.SharedPreferences;
33 import android.preference.PreferenceManager;
34 import android.util.Log;
35 import de.ub0r.android.websms.connector.common.Connector;
36 import de.ub0r.android.websms.connector.common.ConnectorCommand;
37 import de.ub0r.android.websms.connector.common.ConnectorSpec;
38 import de.ub0r.android.websms.connector.common.Utils;
39 import de.ub0r.android.websms.connector.common.WebSMSException;
40 import de.ub0r.android.websms.connector.common.ConnectorSpec.SubConnectorSpec;
42 /**
43 * AsyncTask to manage XMLRPC-Calls to sipgate.de remote-API.
45 * @author Mirko Weber
47 public class ConnectorSipgate extends Connector {
48 /** Tag for output. */
49 private static final String TAG = "WebSMS.Sipg";
51 /** Preferences intent action. */
52 private static final String PREFS_INTENT_ACTION = "de.ub0r.android."
53 + "websms.connectors.sipgate.PREFS";
55 /** Sipgate.de API URL. */
56 private static final String SIPGATE_URL = "https://"
57 + "samurai.sipgate.net/RPC2";
58 /** Sipfate.de TEAM-API URL. */
59 private static final String SIPGATE_TEAM_URL = "https://"
60 + "api.sipgate.net/RPC2";
62 /**
63 * {@inheritDoc}
65 @Override
66 public final ConnectorSpec initSpec(final Context context) {
67 final String name = context.getString(R.string.connector_sipgate_name);
68 ConnectorSpec c = new ConnectorSpec(TAG, name);
69 c.setAuthor(// .
70 context.getString(R.string.connector_sipgate_author));
71 c.setBalance(null);
72 c.setPrefsIntent(PREFS_INTENT_ACTION);
73 c.setPrefsTitle(context
74 .getString(R.string.connector_sipgate_preferences));
75 c.setCapabilities(ConnectorSpec.CAPABILITIES_UPDATE
76 | ConnectorSpec.CAPABILITIES_SEND);
77 c.addSubConnector(TAG, c.getName(),
78 SubConnectorSpec.FEATURE_MULTIRECIPIENTS);
79 return c;
82 /**
83 * {@inheritDoc}
85 @Override
86 public final ConnectorSpec updateSpec(final Context context,
87 final ConnectorSpec connectorSpec) {
88 final SharedPreferences p = PreferenceManager
89 .getDefaultSharedPreferences(context);
90 if (p.getBoolean(Preferences.PREFS_ENABLE_SIPGATE, false)) {
91 if (p.getString(Preferences.PREFS_USER_SIPGATE, "").length() > 0
92 && p.getString(Preferences.PREFS_PASSWORD_SIPGATE, "") // .
93 .length() > 0) {
94 connectorSpec.setReady();
95 } else {
96 connectorSpec.setStatus(ConnectorSpec.STATUS_ENABLED);
98 } else {
99 connectorSpec.setStatus(ConnectorSpec.STATUS_INACTIVE);
101 return connectorSpec;
105 * {@inheritDoc}
107 @Override
108 protected final void doSend(final Context context, final Intent intent)
109 throws WebSMSException {
110 Log.d(TAG, "doSend()");
111 Object back;
112 try {
113 XMLRPCClient client = this.init(context);
114 Vector<String> remoteUris = new Vector<String>();
115 ConnectorCommand command = new ConnectorCommand(intent);
117 for (String t : command.getRecipients()) {
118 if (t != null && t.length() > 1) {
119 // TODO: force international number?
120 final String u = "sip:"
121 + Utils.getRecipientsNumber(t)
122 .replaceAll("\\+", "") + "@sipgate.net";
123 remoteUris.add(u);
124 Log.d(TAG, "Mobile number: " + u);
127 Hashtable<String, Serializable> params = // .
128 new Hashtable<String, Serializable>();
129 if (command.getDefSender().length() > 6) {
130 String localUri = "sip:"
131 + command.getDefSender().replaceAll("\\+", "")
132 + "@sipgate.net";
133 params.put("LocalUri", localUri);
135 params.put("RemoteUri", remoteUris);
136 params.put("TOS", "text");
137 params.put("Content", command.getText());
138 back = client.call("samurai.SessionInitiateMulti", params);
139 Log.d(TAG, back.toString());
140 } catch (XMLRPCFault e) {
141 Log.e(TAG, null, e);
142 if (e.getFaultCode() == Utils.HTTP_SERVICE_UNAUTHORIZED) {
143 throw new WebSMSException(context, R.string.error_pw);
145 throw new WebSMSException(e);
146 } catch (XMLRPCException e) {
147 Log.e(TAG, null, e);
148 throw new WebSMSException(e);
153 * {@inheritDoc}
155 @SuppressWarnings("unchecked")
156 @Override
157 protected final void doUpdate(final Context context, final Intent intent)
158 throws WebSMSException {
159 Log.d(TAG, "doUpdate()");
160 Map<String, Object> back = null;
161 try {
162 XMLRPCClient client = this.init(context);
163 back = (Map<String, Object>) client.call("samurai.BalanceGet");
164 Log.d(TAG, back.toString());
165 if (back.get("StatusCode").equals(
166 new Integer(Utils.HTTP_SERVICE_OK))) {
167 final String b = String.format("%.2f \u20AC",
168 ((Double) ((Map<String, Object>) back
169 .get("CurrentBalance"))
170 .get("TotalIncludingVat")));
171 this.getSpecs(context).setBalance(b);
174 } catch (XMLRPCFault e) {
175 Log.e(TAG, null, e);
176 if (e.getFaultCode() == Utils.HTTP_SERVICE_UNAUTHORIZED) {
177 throw new WebSMSException(context, R.string.error_pw);
179 throw new WebSMSException(e);
180 } catch (XMLRPCException e) {
181 Log.e(TAG, null, e);
182 throw new WebSMSException(e);
187 * Sets up and instance of {@link XMLRPCClient}.
189 * @param context
190 * {@link Context}
191 * @return the initialized {@link XMLRPCClient}
192 * @throws XMLRPCException
193 * XMLRPCException
195 private XMLRPCClient init(final Context context) throws XMLRPCException {
196 Log.d(TAG, "init()");
197 final String version = context.getString(R.string.app_version);
198 final String vendor = context
199 .getString(R.string.connector_sipgate_author);
200 XMLRPCClient client = null;
201 final SharedPreferences p = PreferenceManager
202 .getDefaultSharedPreferences(context);
203 if (p.getBoolean(Preferences.PREFS_ENABLE_SIPGATE_TEAM, false)) {
204 client = new XMLRPCClient(SIPGATE_TEAM_URL);
205 } else {
206 client = new XMLRPCClient(SIPGATE_URL);
209 client.setBasicAuthentication(p.getString(
210 Preferences.PREFS_USER_SIPGATE, ""), p.getString(
211 Preferences.PREFS_PASSWORD_SIPGATE, ""));
212 Object back;
213 try {
214 Hashtable<String, String> ident = new Hashtable<String, String>();
215 ident.put("ClientName", TAG);
216 ident.put("ClientVersion", version);
217 ident.put("ClientVendor", vendor);
218 back = client.call("samurai.ClientIdentify", ident);
219 Log.d(TAG, back.toString());
220 return client;
221 } catch (XMLRPCException e) {
222 Log.e(TAG, "XMLRPCExceptin in init()", e);
223 throw e;