switching connectors is working
[andGMXsms.git] / src / de / ub0r / android / websms / connector / gmx / CommandReceiverGMX.java
blobaf89ceede28118bdc92e1fa2ad0cde66aad86328
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.gmx;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.preference.PreferenceManager;
26 import android.util.Log;
27 import android.widget.Toast;
28 import de.ub0r.android.andGMXsms.R;
29 import de.ub0r.android.andGMXsms.Connector.WebSMSException;
30 import de.ub0r.android.websms.connector.ConnectorCommand;
31 import de.ub0r.android.websms.connector.ConnectorSpec;
32 import de.ub0r.android.websms.connector.Constants;
33 import de.ub0r.android.websms.connector.ConnectorSpec.SubConnectorSpec;
35 /**
36 * Receives commands coming as broadcast from WebSMS.
38 * @author flx
40 public class CommandReceiverGMX extends BroadcastReceiver {
41 /** Tag for debug output. */
42 private static final String TAG = "WebSMS.GMX";
44 /** Preferences intent action. */
45 private static final String PREFS_INTENT_ACTION = "de.ub0r.android."
46 + "websms.connectors.gmx.PREFS";
48 /** Internal {@link ConnectorSpec}. */
49 private static ConnectorSpec conector = null;
51 /**
52 * Init ConnectorSpec.
54 * @param context
55 * context
56 * @return ConnectorSpec
58 private static synchronized ConnectorSpec getSpecs(final Context context) {
59 if (conector == null) {
60 conector = new ConnectorSpec(TAG, context
61 .getString(R.string.connector_gmx_name));
62 conector.setAuthor(// .
63 context.getString(R.string.connector_gmx_author));
64 conector.setBalance(null);
65 conector.setPrefsIntent(PREFS_INTENT_ACTION);
66 conector.setPrefsTitle(context
67 .getString(R.string.connector_gmx_preferences));
68 conector.setCapabilities(ConnectorSpec.CAPABILITIES_BOOSTRAP
69 | ConnectorSpec.CAPABILITIES_UPDATE
70 | ConnectorSpec.CAPABILITIES_SEND);
71 conector.addSubConnector(TAG, conector.getName(),
72 SubConnectorSpec.FEATURE_MULTIRECIPIENTS
73 | SubConnectorSpec.FEATURE_CUSTOMSENDER
74 | SubConnectorSpec.FEATURE_SENDLATER);
76 final SharedPreferences p = PreferenceManager
77 .getDefaultSharedPreferences(context);
78 if (p.getBoolean(Preferences.PREFS_ENABLED, false)) {
79 if (p.getString(Preferences.PREFS_MAIL, "").length() > 0
80 && p.getString(Preferences.PREFS_PASSWORD, "") // .
81 .length() > 0) {
82 conector.setReady();
83 } else {
84 conector.setStatus(ConnectorSpec.STATUS_ENABLED);
86 } else {
87 conector.setStatus(ConnectorSpec.STATUS_INACTIVE);
89 return conector;
92 /**
93 * Send INFO Broadcast back to WebSMS.
95 * @param context
96 * context
97 * @param specs
98 * {@link ConnectorSpec}; if null, getSpecs() is called to get
99 * them
101 private void sendInfo(final Context context, final ConnectorSpec specs) {
102 ConnectorSpec c = specs;
103 if (c == null) {
104 c = getSpecs(context);
106 final Intent i = new Intent(Constants.ACTION_CONNECTOR_INFO);
107 c.setToIntent(i);
108 Log.d(TAG, "send broadcast: " + i.getAction());
109 context.sendBroadcast(i);
113 * {@inheritDoc}
115 @Override
116 public final void onReceive(final Context context, final Intent intent) {
117 final String action = intent.getAction();
118 Log.d(TAG, "action: " + action);
119 if (action == null) {
120 return;
122 if (Constants.ACTION_CONNECTOR_UPDATE.equals(action)) {
123 this.sendInfo(context, null);
124 } else if (Constants.ACTION_CONNECTOR_RUN_SEND.equals(action)) {
125 final ConnectorCommand command = new ConnectorCommand(intent);
126 if (command.getType() == ConnectorCommand.TYPE_SEND) {
127 final ConnectorSpec origSpecs = new ConnectorSpec(intent);
128 final ConnectorSpec specs = CommandReceiverGMX
129 .getSpecs(context);
130 if (specs.getID().equals(origSpecs.getID())
131 && specs.hasStatus(ConnectorSpec.STATUS_READY)) {
132 // check internal status
133 try {
134 // FIXME: this.send(command);
135 throw new WebSMSException("fixme");
136 } catch (WebSMSException e) {
137 Log.e(TAG, null, e);
138 Toast.makeText(context,
139 specs.getName() + ": " + e.getMessage(),
140 Toast.LENGTH_LONG).show();
141 } finally {
142 this.sendInfo(context, specs);