From 270952695dfc40ba80c3d260bcaeae69ced03623 Mon Sep 17 00:00:00 2001 From: Felix Bechstein Date: Sun, 17 Jan 2010 13:45:42 +0100 Subject: [PATCH] add CommandReceiver --- .../websms/connector/test/CommandReceiverTest.java | 103 +++++------- src/de/ub0r/android/andGMXsms/WebSMS.java | 4 +- src/de/ub0r/android/andGMXsms/WebSMSReceiver.java | 4 +- .../websms/connector/common/CommandReceiver.java | 183 +++++++++++++++++++++ .../websms/connector/common/ConnectorCommand.java | 7 +- .../websms/connector/common/ConnectorService.java | 6 +- .../websms/connector/common/ConnectorSpec.java | 38 ++++- .../websms/connector/common/ConnectorTask.java | 3 +- .../android/websms/connector/common/Constants.java | 67 -------- .../websms/connector/gmx/CommandReceiverGMX.java | 96 ++++------- .../websms/connector/sms/CommandReceiverSMS.java | 83 +++------- 11 files changed, 331 insertions(+), 263 deletions(-) create mode 100644 src/de/ub0r/android/websms/connector/common/CommandReceiver.java delete mode 100644 src/de/ub0r/android/websms/connector/common/Constants.java diff --git a/connectors/test/src/de/ub0r/android/websms/connector/test/CommandReceiverTest.java b/connectors/test/src/de/ub0r/android/websms/connector/test/CommandReceiverTest.java index 0e0bec4..9f0065d 100644 --- a/connectors/test/src/de/ub0r/android/websms/connector/test/CommandReceiverTest.java +++ b/connectors/test/src/de/ub0r/android/websms/connector/test/CommandReceiverTest.java @@ -18,16 +18,15 @@ */ package de.ub0r.android.websms.connector.test; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; import android.widget.Toast; +import de.ub0r.android.websms.connector.common.CommandReceiver; import de.ub0r.android.websms.connector.common.ConnectorCommand; import de.ub0r.android.websms.connector.common.ConnectorSpec; -import de.ub0r.android.websms.connector.common.Constants; import de.ub0r.android.websms.connector.common.WebSMSException; import de.ub0r.android.websms.connector.common.ConnectorSpec.SubConnectorSpec; @@ -36,7 +35,7 @@ import de.ub0r.android.websms.connector.common.ConnectorSpec.SubConnectorSpec; * * @author flx */ -public class CommandReceiverTest extends BroadcastReceiver { +public class CommandReceiverTest extends CommandReceiver { /** Tag for debug output. */ private static final String TAG = "WebSMS.test"; @@ -44,69 +43,43 @@ public class CommandReceiverTest extends BroadcastReceiver { private static final String PREFS_INTENT_ACTION = "de.ub0r.android." + "websms.connectors.test.PREFS"; - /** Internal {@link ConnectorSpec}. */ - private static ConnectorSpec conector = null; + /** + * {@inheritDoc} + */ + @Override + public final ConnectorSpec initSpec(// . + final Context context) { + final String name = context.getString(R.string.connector_test_name); + final ConnectorSpec c = new ConnectorSpec(TAG, name); + + c.setAuthor(context.getString(R.string.connector_test_author)); + c.setBalance(null); + c.setPrefsIntent(PREFS_INTENT_ACTION); + c.setPrefsTitle(context.getString(R.string.connector_test_preferences)); + c.setCapabilities(ConnectorSpec.CAPABILITIES_BOOSTRAP + | ConnectorSpec.CAPABILITIES_UPDATE + | ConnectorSpec.CAPABILITIES_SEND); + c.addSubConnector(TAG, name, SubConnectorSpec.FEATURE_MULTIRECIPIENTS + | SubConnectorSpec.FEATURE_CUSTOMSENDER + | SubConnectorSpec.FEATURE_SENDLATER); + return c; + } /** - * Init ConnectorSpec. - * - * @param context - * context - * @return ConnectorSpec + * {@inheritDoc} */ - private static synchronized ConnectorSpec getSpecs(final Context context) { - if (conector == null) { - conector = new ConnectorSpec(TAG, context - .getString(R.string.connector_test_name)); - conector.setAuthor(// . - context.getString(R.string.connector_test_author)); - conector.setBalance(null); - conector.setPrefsIntent(PREFS_INTENT_ACTION); - conector.setPrefsTitle(context - .getString(R.string.connector_test_preferences)); - conector.setCapabilities(ConnectorSpec.CAPABILITIES_BOOSTRAP - | ConnectorSpec.CAPABILITIES_UPDATE - | ConnectorSpec.CAPABILITIES_SEND); - conector.addSubConnector(TAG, conector.getName(), - SubConnectorSpec.FEATURE_MULTIRECIPIENTS - | SubConnectorSpec.FEATURE_CUSTOMSENDER - | SubConnectorSpec.FEATURE_SENDLATER); - } + @Override + public final ConnectorSpec updateSpec(final Context context, + final ConnectorSpec connectorSpec) { final SharedPreferences p = PreferenceManager .getDefaultSharedPreferences(context); if (p.getBoolean(Preferences.PREFS_ENABLED, false)) { - conector.setReady(); + connectorSpec.setReady(); } else { - conector.setStatus(ConnectorSpec.STATUS_INACTIVE); - } - return conector; - } - - /** - * Send INFO Broadcast back to WebSMS. - * - * @param context - * context - * @param specs - * {@link ConnectorSpec}; if null, getSpecs() is called to get - * them - * @param command - * send back the {@link ConnectorCommand} which was done - */ - private void sendInfo(final Context context, final ConnectorSpec specs, - final ConnectorCommand command) { - ConnectorSpec c = specs; - if (c == null) { - c = getSpecs(context); - } - final Intent i = new Intent(Constants.ACTION_CONNECTOR_INFO); - c.setToIntent(i); - if (command != null) { - command.setToIntent(i); + connectorSpec.setStatus(ConnectorSpec.STATUS_INACTIVE); } - Log.d(TAG, "send broadcast: " + i.getAction()); - context.sendBroadcast(i); + return connectorSpec; } /** @@ -119,14 +92,13 @@ public class CommandReceiverTest extends BroadcastReceiver { if (action == null) { return; } - if (Constants.ACTION_CONNECTOR_UPDATE.equals(action)) { + if (CommandReceiver.ACTION_CONNECTOR_UPDATE.equals(action)) { this.sendInfo(context, null, null); - } else if (Constants.ACTION_CONNECTOR_RUN_SEND.equals(action)) { + } else if (CommandReceiver.ACTION_CONNECTOR_RUN_SEND.equals(action)) { final ConnectorCommand command = new ConnectorCommand(intent); if (command.getType() == ConnectorCommand.TYPE_SEND) { final ConnectorSpec origSpecs = new ConnectorSpec(intent); - final ConnectorSpec specs = CommandReceiverTest - .getSpecs(context); + final ConnectorSpec specs = getSpecs(context); if (specs.getID().equals(origSpecs.getID()) && specs.hasStatus(ConnectorSpec.STATUS_READY)) { // check internal status @@ -144,11 +116,12 @@ public class CommandReceiverTest extends BroadcastReceiver { // if nothing went wrong, info was send from inside. } } - } else if (Constants.ACTION_CONNECTOR_RUN_BOOSTRAP.equals(action)) { - final ConnectorSpec specs = CommandReceiverTest.getSpecs(context); + } else if (// . + CommandReceiver.ACTION_CONNECTOR_RUN_BOOSTRAP.equals(action)) { + final ConnectorSpec specs = getSpecs(context); this.sendInfo(context, specs, null); - } else if (Constants.ACTION_CONNECTOR_RUN_UPDATE.equals(action)) { - final ConnectorSpec specs = CommandReceiverTest.getSpecs(context); + } else if (CommandReceiver.ACTION_CONNECTOR_RUN_UPDATE.equals(action)) { + final ConnectorSpec specs = getSpecs(context); specs.setBalance("13,37\u20AC"); this.sendInfo(context, specs, null); } diff --git a/src/de/ub0r/android/andGMXsms/WebSMS.java b/src/de/ub0r/android/andGMXsms/WebSMS.java index e002786..6dd6622 100644 --- a/src/de/ub0r/android/andGMXsms/WebSMS.java +++ b/src/de/ub0r/android/andGMXsms/WebSMS.java @@ -66,9 +66,9 @@ import android.widget.Toast; import com.admob.android.ads.AdView; +import de.ub0r.android.websms.connector.common.CommandReceiver; import de.ub0r.android.websms.connector.common.ConnectorCommand; import de.ub0r.android.websms.connector.common.ConnectorSpec; -import de.ub0r.android.websms.connector.common.Constants; /** * Main Activity. @@ -464,7 +464,7 @@ public class WebSMS extends Activity implements OnClickListener, } // query for connectors - final Intent i = new Intent(Constants.ACTION_CONNECTOR_UPDATE); + final Intent i = new Intent(CommandReceiver.ACTION_CONNECTOR_UPDATE); Log.d(TAG, "send broadcast: " + i.getAction()); this.sendBroadcast(i); } diff --git a/src/de/ub0r/android/andGMXsms/WebSMSReceiver.java b/src/de/ub0r/android/andGMXsms/WebSMSReceiver.java index 13202c4..817a240 100644 --- a/src/de/ub0r/android/andGMXsms/WebSMSReceiver.java +++ b/src/de/ub0r/android/andGMXsms/WebSMSReceiver.java @@ -29,9 +29,9 @@ import android.content.SharedPreferences; import android.net.Uri; import android.preference.PreferenceManager; import android.util.Log; +import de.ub0r.android.websms.connector.common.CommandReceiver; import de.ub0r.android.websms.connector.common.ConnectorCommand; import de.ub0r.android.websms.connector.common.ConnectorSpec; -import de.ub0r.android.websms.connector.common.Constants; /** * Fetch all incomming Broadcasts and forward them to WebSMS. @@ -75,7 +75,7 @@ public final class WebSMSReceiver extends BroadcastReceiver { if (action == null) { return; } - if (Constants.ACTION_CONNECTOR_INFO.equals(action)) { + if (CommandReceiver.ACTION_CONNECTOR_INFO.equals(action)) { final ConnectorSpec specs = new ConnectorSpec(intent); final ConnectorCommand command = new ConnectorCommand(intent); WebSMS.addConnector(specs); diff --git a/src/de/ub0r/android/websms/connector/common/CommandReceiver.java b/src/de/ub0r/android/websms/connector/common/CommandReceiver.java new file mode 100644 index 0000000..a6afaf9 --- /dev/null +++ b/src/de/ub0r/android/websms/connector/common/CommandReceiver.java @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2010 Felix Bechstein + * + * This file is part of WebSMS. + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; If not, see . + */ +package de.ub0r.android.websms.connector.common; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; +import android.widget.Toast; + +/** + * Receives commands coming as broadcast from WebSMS. + * + * @author flx + */ +public abstract class CommandReceiver extends BroadcastReceiver { + /** Tag for debug output. */ + private static final String TAG = "WebSMS.cbcr"; + + /** Common Action prefix. */ + private static final String ACTION_PREFIX = "de.ub0r." + + "android.websms.connector."; + + /** + * Action to start a connector's {@link Service}. This should include a + * {@link ConnectorCommand}: boostrap. + */ + public static final String ACTION_CONNECTOR_RUN_BOOSTRAP = ACTION_PREFIX + + "RUN_BOOTSTRAP"; + + /** + * Action to start a connector's {@link Service}. This should include a + * {@link ConnectorCommand}: update. + */ + public static final String ACTION_CONNECTOR_RUN_UPDATE = ACTION_PREFIX + + "RUN_UPDATE"; + + /** + * Action to start a connector's {@link Service}. This should include a + * {@link ConnectorCommand}: send. + */ + public static final String ACTION_CONNECTOR_RUN_SEND = ACTION_PREFIX + + "RUN_SEND"; + + /** Broadcast Action requesting update of {@link ConnectorSpec}'s status. */ + public static final String ACTION_CONNECTOR_UPDATE = ACTION_PREFIX + + "UPDATE"; + + /** + * Broadcast Action sending updated {@link ConnectorSpec} informations back + * to WebSMS. This should include a {@link ConnectorSpec}. + */ + public static final String ACTION_CONNECTOR_INFO = ACTION_PREFIX + "INFO"; + + /** Internal {@link ConnectorSpec}. */ + private static ConnectorSpec connector = null; + + /** Sync access to connector. */ + private static final Object syncUpdate = new Object(); + + /** + * Init {@link ConnectorSpec}. This is only run once. Changing properties + * should be set in updateSpec(). Default implementation does nothing at + * all. + * + * @param context + * context + * @return updated {@link ConnectorSpec} + */ + public ConnectorSpec initSpec(final Context context) { + return new ConnectorSpec(TAG, "noname"); + } + + /** + * Update {@link ConnectorSpec}. Default implementation does nothing at all. + * + * @param context + * context + * @param connectorSpec + * {@link ConnectorSpec} + * @return updated {@link ConnectorSpec} + */ + public ConnectorSpec updateSpec(final Context context, + final ConnectorSpec connectorSpec) { + return connectorSpec; + } + + /** + * Init {@link ConnectorSpec}. + * + * @param context + * context + * @return ConnectorSpec + */ + public final synchronized ConnectorSpec getSpecs(final Context context) { + synchronized (syncUpdate) { + if (connector == null) { + connector = this.initSpec(context); + } + return this.updateSpec(context, connector); + } + } + + /** + * Send INFO Broadcast back to WebSMS. + * + * @param context + * context + * @param specs + * {@link ConnectorSpec}; if null, getSpecs() is called to get + * them + * @param command + * send back the {@link ConnectorCommand} which was done + */ + public final void sendInfo(final Context context, + final ConnectorSpec specs, final ConnectorCommand command) { + ConnectorSpec c = specs; + if (c == null) { + c = this.getSpecs(context); + } + final Intent i = new Intent(CommandReceiver.ACTION_CONNECTOR_INFO); + c.setToIntent(i); + if (command != null) { + command.setToIntent(i); + } + Log.d("WebSMS." + this.getSpecs(context), "-> broadcast: " + + i.getAction()); + context.sendBroadcast(i); + } + + /** + * {@inheritDoc} //TODO: change me. + */ + @Override + public void onReceive(final Context context, final Intent intent) { + final String action = intent.getAction(); + Log.d(TAG, "action: " + action); + if (action == null) { + return; + } + if (CommandReceiver.ACTION_CONNECTOR_UPDATE.equals(action)) { + this.sendInfo(context, null, null); + } else if (CommandReceiver.ACTION_CONNECTOR_RUN_SEND.equals(action)) { + final ConnectorCommand command = new ConnectorCommand(intent); + if (command.getType() == ConnectorCommand.TYPE_SEND) { + final ConnectorSpec origSpecs = new ConnectorSpec(intent); + final ConnectorSpec specs = this.getSpecs(context); + if (specs.getID().equals(origSpecs.getID()) + && specs.hasStatus(ConnectorSpec.STATUS_READY)) { + // check internal status + try { + // FIXME: this.send(command); + throw new WebSMSException("fixme"); + } catch (WebSMSException e) { + Log.e(TAG, null, e); + Toast.makeText(context, + specs.getName() + ": " + e.getMessage(), + Toast.LENGTH_LONG).show(); + specs.setErrorMessage(e.getMessage()); + this.sendInfo(context, specs, command); + } + // if nothing went wrong, info was send from inside. + } + } + } + } +} diff --git a/src/de/ub0r/android/websms/connector/common/ConnectorCommand.java b/src/de/ub0r/android/websms/connector/common/ConnectorCommand.java index 7f8cc1c..2a1f746 100644 --- a/src/de/ub0r/android/websms/connector/common/ConnectorCommand.java +++ b/src/de/ub0r/android/websms/connector/common/ConnectorCommand.java @@ -21,7 +21,6 @@ package de.ub0r.android.websms.connector.common; import java.util.ArrayList; - import android.content.Intent; import android.os.Bundle; @@ -196,13 +195,13 @@ public final class ConnectorCommand { if (i == null) { switch (this.getType()) { case TYPE_BOOTSTRAP: - i = new Intent(Constants.ACTION_CONNECTOR_RUN_BOOSTRAP); + i = new Intent(CommandReceiver.ACTION_CONNECTOR_RUN_BOOSTRAP); break; case TYPE_UPDATE: - i = new Intent(Constants.ACTION_CONNECTOR_RUN_UPDATE); + i = new Intent(CommandReceiver.ACTION_CONNECTOR_RUN_UPDATE); break; case TYPE_SEND: - i = new Intent(Constants.ACTION_CONNECTOR_RUN_SEND); + i = new Intent(CommandReceiver.ACTION_CONNECTOR_RUN_SEND); break; default: return null; diff --git a/src/de/ub0r/android/websms/connector/common/ConnectorService.java b/src/de/ub0r/android/websms/connector/common/ConnectorService.java index f4739b8..5432a17 100644 --- a/src/de/ub0r/android/websms/connector/common/ConnectorService.java +++ b/src/de/ub0r/android/websms/connector/common/ConnectorService.java @@ -95,10 +95,10 @@ public abstract class ConnectorService extends Service { if (intent != null) { final String a = intent.getAction(); if (a != null && // . - (a.equals(Constants.ACTION_CONNECTOR_RUN_BOOSTRAP) + (a.equals(CommandReceiver.ACTION_CONNECTOR_RUN_BOOSTRAP) || a.equals(// . - Constants.ACTION_CONNECTOR_RUN_UPDATE) // . - || a.equals(Constants.ACTION_CONNECTOR_RUN_SEND))) { + CommandReceiver.ACTION_CONNECTOR_RUN_UPDATE) // . + || a.equals(CommandReceiver.ACTION_CONNECTOR_RUN_SEND))) { // TODO: setForeground / startForeground this.startConnectorTask(this, intent); } diff --git a/src/de/ub0r/android/websms/connector/common/ConnectorSpec.java b/src/de/ub0r/android/websms/connector/common/ConnectorSpec.java index 999e940..8950653 100644 --- a/src/de/ub0r/android/websms/connector/common/ConnectorSpec.java +++ b/src/de/ub0r/android/websms/connector/common/ConnectorSpec.java @@ -70,7 +70,7 @@ public final class ConnectorSpec { public static final short CAPABILITIES_SEND = 4; /** Connector: Balance. */ private static final String BALANCE = "connector_balance"; - /** Connector: Error message */ + /** Connector: Error message. */ private static final String ERRORMESSAGE = "connector_errormessage"; // Subconnectors @@ -216,6 +216,42 @@ public final class ConnectorSpec { } /** + * @return name. + */ + @Override + public String toString() { + return this.getName(); + } + + /** + * Does nothing. {@inheritDoc} + */ + @Override + public int hashCode() { + return 0; + } + + /** + * @param connector + * {@link ConnectorSpec} or {@link String} + * @return true if this connector has the same id as connector + */ + @Override + public boolean equals(final Object connector) { + if (this == connector) { + return true; + } else if (connector == null) { + return false; + } else if (connector instanceof ConnectorSpec) { + return this.getID().equals(((ConnectorSpec) connector).getID()); + } else if (connector instanceof String) { + return this.getID().equals(connector); + } else { + return false; + } + } + + /** * Set this {@link ConnectorSpec} to an {@link Intent}. * * @param intent diff --git a/src/de/ub0r/android/websms/connector/common/ConnectorTask.java b/src/de/ub0r/android/websms/connector/common/ConnectorTask.java index 4bd9109..b1502fd 100644 --- a/src/de/ub0r/android/websms/connector/common/ConnectorTask.java +++ b/src/de/ub0r/android/websms/connector/common/ConnectorTask.java @@ -78,7 +78,8 @@ public class ConnectorTask extends AsyncTask { */ @Override protected final void onPostExecute(final Void result) { - final Intent intent = new Intent(Constants.ACTION_CONNECTOR_UPDATE); + final Intent intent = new Intent( + CommandReceiver.ACTION_CONNECTOR_UPDATE); this.connector.getConnector().setToIntent(intent); this.service.sendBroadcast(intent); this.service.unregister(); diff --git a/src/de/ub0r/android/websms/connector/common/Constants.java b/src/de/ub0r/android/websms/connector/common/Constants.java deleted file mode 100644 index 188f441..0000000 --- a/src/de/ub0r/android/websms/connector/common/Constants.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2010 Felix Bechstein - * - * This file is part of WebSMS. - * - * This program is free software; you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation; either version 3 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program; If not, see . - */ -package de.ub0r.android.websms.connector.common; - - -/** - * @author flx - */ -public final class Constants { - - /** No Constructor needed here. */ - private Constants() { - // do nothing - return; - } - - /** Common Action prefix. */ - private static final String ACTION_PREFIX = "de.ub0r." - + "android.websms.connector."; - - /** - * Action to start a connector's {@link Service}. This should include a - * {@link ConnectorCommand}: boostrap. - */ - public static final String ACTION_CONNECTOR_RUN_BOOSTRAP = ACTION_PREFIX - + "RUN_BOOTSTRAP"; - - /** - * Action to start a connector's {@link Service}. This should include a - * {@link ConnectorCommand}: update. - */ - public static final String ACTION_CONNECTOR_RUN_UPDATE = ACTION_PREFIX - + "RUN_UPDATE"; - - /** - * Action to start a connector's {@link Service}. This should include a - * {@link ConnectorCommand}: send. - */ - public static final String ACTION_CONNECTOR_RUN_SEND = ACTION_PREFIX - + "RUN_SEND"; - - /** Broadcast Action requesting update of {@link ConnectorSpec}'s status. */ - public static final String ACTION_CONNECTOR_UPDATE = ACTION_PREFIX - + "UPDATE"; - - /** - * Broadcast Action sending updated {@link ConnectorSpec} informations back - * to WebSMS. This should include a {@link ConnectorSpec}. - */ - public static final String ACTION_CONNECTOR_INFO = ACTION_PREFIX + "INFO"; -} diff --git a/src/de/ub0r/android/websms/connector/gmx/CommandReceiverGMX.java b/src/de/ub0r/android/websms/connector/gmx/CommandReceiverGMX.java index 91ce035..afe300b 100644 --- a/src/de/ub0r/android/websms/connector/gmx/CommandReceiverGMX.java +++ b/src/de/ub0r/android/websms/connector/gmx/CommandReceiverGMX.java @@ -18,7 +18,6 @@ */ package de.ub0r.android.websms.connector.gmx; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -26,9 +25,9 @@ import android.preference.PreferenceManager; import android.util.Log; import android.widget.Toast; import de.ub0r.android.andGMXsms.R; +import de.ub0r.android.websms.connector.common.CommandReceiver; import de.ub0r.android.websms.connector.common.ConnectorCommand; import de.ub0r.android.websms.connector.common.ConnectorSpec; -import de.ub0r.android.websms.connector.common.Constants; import de.ub0r.android.websms.connector.common.WebSMSException; import de.ub0r.android.websms.connector.common.ConnectorSpec.SubConnectorSpec; @@ -37,7 +36,7 @@ import de.ub0r.android.websms.connector.common.ConnectorSpec.SubConnectorSpec; * * @author flx */ -public class CommandReceiverGMX extends BroadcastReceiver { +public class CommandReceiverGMX extends CommandReceiver { /** Tag for debug output. */ private static final String TAG = "WebSMS.GMX"; @@ -45,74 +44,48 @@ public class CommandReceiverGMX extends BroadcastReceiver { private static final String PREFS_INTENT_ACTION = "de.ub0r.android." + "websms.connectors.gmx.PREFS"; - /** Internal {@link ConnectorSpec}. */ - private static ConnectorSpec conector = null; + /** + * {@inheritDoc} + */ + @Override + public final ConnectorSpec initSpec(final Context context) { + final String name = context.getString(R.string.connector_gmx_name); + ConnectorSpec c = new ConnectorSpec(TAG, name); + c.setAuthor(// . + context.getString(R.string.connector_gmx_author)); + c.setBalance(null); + c.setPrefsIntent(PREFS_INTENT_ACTION); + c.setPrefsTitle(context.getString(R.string.connector_gmx_preferences)); + c.setCapabilities(ConnectorSpec.CAPABILITIES_BOOSTRAP + | ConnectorSpec.CAPABILITIES_UPDATE + | ConnectorSpec.CAPABILITIES_SEND); + c.addSubConnector(TAG, c.getName(), + SubConnectorSpec.FEATURE_MULTIRECIPIENTS + | SubConnectorSpec.FEATURE_CUSTOMSENDER + | SubConnectorSpec.FEATURE_SENDLATER); + return c; + } /** - * Init ConnectorSpec. - * - * @param context - * context - * @return ConnectorSpec + * {@inheritDoc} */ - private static synchronized ConnectorSpec getSpecs(final Context context) { - if (conector == null) { - conector = new ConnectorSpec(TAG, context - .getString(R.string.connector_gmx_name)); - conector.setAuthor(// . - context.getString(R.string.connector_gmx_author)); - conector.setBalance(null); - conector.setPrefsIntent(PREFS_INTENT_ACTION); - conector.setPrefsTitle(context - .getString(R.string.connector_gmx_preferences)); - conector.setCapabilities(ConnectorSpec.CAPABILITIES_BOOSTRAP - | ConnectorSpec.CAPABILITIES_UPDATE - | ConnectorSpec.CAPABILITIES_SEND); - conector.addSubConnector(TAG, conector.getName(), - SubConnectorSpec.FEATURE_MULTIRECIPIENTS - | SubConnectorSpec.FEATURE_CUSTOMSENDER - | SubConnectorSpec.FEATURE_SENDLATER); - } + @Override + public final ConnectorSpec updateSpec(final Context context, + final ConnectorSpec connectorSpec) { final SharedPreferences p = PreferenceManager .getDefaultSharedPreferences(context); if (p.getBoolean(Preferences.PREFS_ENABLED, false)) { if (p.getString(Preferences.PREFS_MAIL, "").length() > 0 && p.getString(Preferences.PREFS_PASSWORD, "") // . .length() > 0) { - conector.setReady(); + connectorSpec.setReady(); } else { - conector.setStatus(ConnectorSpec.STATUS_ENABLED); + connectorSpec.setStatus(ConnectorSpec.STATUS_ENABLED); } } else { - conector.setStatus(ConnectorSpec.STATUS_INACTIVE); - } - return conector; - } - - /** - * Send INFO Broadcast back to WebSMS. - * - * @param context - * context - * @param specs - * {@link ConnectorSpec}; if null, getSpecs() is called to get - * them - * @param command - * send back the {@link ConnectorCommand} which was done - */ - private void sendInfo(final Context context, final ConnectorSpec specs, - final ConnectorCommand command) { - ConnectorSpec c = specs; - if (c == null) { - c = getSpecs(context); - } - final Intent i = new Intent(Constants.ACTION_CONNECTOR_INFO); - c.setToIntent(i); - if (command != null) { - command.setToIntent(i); + connectorSpec.setStatus(ConnectorSpec.STATUS_INACTIVE); } - Log.d(TAG, "send broadcast: " + i.getAction()); - context.sendBroadcast(i); + return connectorSpec; } /** @@ -125,14 +98,13 @@ public class CommandReceiverGMX extends BroadcastReceiver { if (action == null) { return; } - if (Constants.ACTION_CONNECTOR_UPDATE.equals(action)) { + if (ACTION_CONNECTOR_UPDATE.equals(action)) { this.sendInfo(context, null, null); - } else if (Constants.ACTION_CONNECTOR_RUN_SEND.equals(action)) { + } else if (ACTION_CONNECTOR_RUN_SEND.equals(action)) { final ConnectorCommand command = new ConnectorCommand(intent); if (command.getType() == ConnectorCommand.TYPE_SEND) { final ConnectorSpec origSpecs = new ConnectorSpec(intent); - final ConnectorSpec specs = CommandReceiverGMX - .getSpecs(context); + final ConnectorSpec specs = this.getSpecs(context); if (specs.getID().equals(origSpecs.getID()) && specs.hasStatus(ConnectorSpec.STATUS_READY)) { // check internal status diff --git a/src/de/ub0r/android/websms/connector/sms/CommandReceiverSMS.java b/src/de/ub0r/android/websms/connector/sms/CommandReceiverSMS.java index ed08bdc..8a124f9 100644 --- a/src/de/ub0r/android/websms/connector/sms/CommandReceiverSMS.java +++ b/src/de/ub0r/android/websms/connector/sms/CommandReceiverSMS.java @@ -20,7 +20,6 @@ package de.ub0r.android.websms.connector.sms; import java.util.ArrayList; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -29,9 +28,9 @@ import android.telephony.gsm.SmsManager; import android.util.Log; import android.widget.Toast; import de.ub0r.android.andGMXsms.R; +import de.ub0r.android.websms.connector.common.CommandReceiver; import de.ub0r.android.websms.connector.common.ConnectorCommand; import de.ub0r.android.websms.connector.common.ConnectorSpec; -import de.ub0r.android.websms.connector.common.Constants; import de.ub0r.android.websms.connector.common.Utils; import de.ub0r.android.websms.connector.common.WebSMSException; import de.ub0r.android.websms.connector.common.ConnectorSpec.SubConnectorSpec; @@ -42,44 +41,43 @@ import de.ub0r.android.websms.connector.common.ConnectorSpec.SubConnectorSpec; * @author flx */ @SuppressWarnings("deprecation") -public class CommandReceiverSMS extends BroadcastReceiver { +public class CommandReceiverSMS extends CommandReceiver { /** Tag for debug output. */ private static final String TAG = "WebSMS.sms"; /** Preference key: enabled. */ private static final String PREFS_ENABLED = "enable_sms"; - /** Internal {@link ConnectorSpec}. */ - private static ConnectorSpec conector = null; + /** + * {@inheritDoc} + */ + @Override + public final ConnectorSpec initSpec(final Context context) { + final String name = context.getString(R.string.connector_sms_name); + final ConnectorSpec c = new ConnectorSpec(TAG, name); + c.setAuthor(context.getString(R.string.connector_sms_author)); + c.setBalance(null); + c.setPrefsIntent(null); + c.setPrefsTitle(null); + c.setCapabilities(ConnectorSpec.CAPABILITIES_SEND); + c.addSubConnector(TAG, name, SubConnectorSpec.FEATURE_MULTIRECIPIENTS); + return c; + } /** - * Init ConnectorSpec. - * - * @param context - * context - * @return ConnectorSpec + * {@inheritDoc} */ - private static synchronized ConnectorSpec getSpecs(final Context context) { - if (conector == null) { - conector = new ConnectorSpec(TAG, context - .getString(R.string.connector_sms_name)); - conector.setAuthor(// . - context.getString(R.string.connector_sms_author)); - conector.setBalance(null); - conector.setPrefsIntent(null); - conector.setPrefsTitle(null); - conector.setCapabilities(ConnectorSpec.CAPABILITIES_SEND); - conector.addSubConnector(TAG, conector.getName(), - SubConnectorSpec.FEATURE_MULTIRECIPIENTS); - } + @Override + public final ConnectorSpec updateSpec(final Context context, + final ConnectorSpec connectorSpec) { final SharedPreferences p = PreferenceManager .getDefaultSharedPreferences(context); if (p.getBoolean(PREFS_ENABLED, false)) { - conector.setReady(); + connectorSpec.setReady(); } else { - conector.setStatus(ConnectorSpec.STATUS_INACTIVE); + connectorSpec.setStatus(ConnectorSpec.STATUS_INACTIVE); } - return conector; + return connectorSpec; } /** @@ -108,32 +106,6 @@ public class CommandReceiverSMS extends BroadcastReceiver { } /** - * Send INFO Broadcast back to WebSMS. - * - * @param context - * context - * @param specs - * {@link ConnectorSpec}; if null, getSpecs() is called to get - * them - * @param command - * send back the {@link ConnectorCommand} which was done - */ - private void sendInfo(final Context context, final ConnectorSpec specs, - final ConnectorCommand command) { - ConnectorSpec c = specs; - if (c == null) { - c = getSpecs(context); - } - final Intent i = new Intent(Constants.ACTION_CONNECTOR_INFO); - c.setToIntent(i); - if (command != null) { - command.setToIntent(i); - } - Log.d(TAG, "send broadcast: " + i.getAction()); - context.sendBroadcast(i); - } - - /** * {@inheritDoc} */ @Override @@ -143,14 +115,13 @@ public class CommandReceiverSMS extends BroadcastReceiver { if (action == null) { return; } - if (Constants.ACTION_CONNECTOR_UPDATE.equals(action)) { + if (ACTION_CONNECTOR_UPDATE.equals(action)) { this.sendInfo(context, null, null); - } else if (Constants.ACTION_CONNECTOR_RUN_SEND.equals(action)) { + } else if (ACTION_CONNECTOR_RUN_SEND.equals(action)) { final ConnectorCommand command = new ConnectorCommand(intent); if (command.getType() == ConnectorCommand.TYPE_SEND) { final ConnectorSpec origSpecs = new ConnectorSpec(intent); - final ConnectorSpec specs = CommandReceiverSMS - .getSpecs(context); + final ConnectorSpec specs = this.getSpecs(context); if (specs.getID().equals(origSpecs.getID()) && specs.hasStatus(ConnectorSpec.STATUS_READY)) { // check internal status -- 2.11.4.GIT