add CommandReceiver
[andGMXsms.git] / src / de / ub0r / android / websms / connector / common / ConnectorCommand.java
blob2a1f746a4c378b27a824ca42bf5fbea20e7c9daf
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/>.
20 package de.ub0r.android.websms.connector.common;
22 import java.util.ArrayList;
24 import android.content.Intent;
25 import android.os.Bundle;
27 /**
28 * A Command send to a Connector.
30 * @author flx
32 public final class ConnectorCommand {
34 /** Key to find command in a Bundle. */
35 private static final String EXTRAS_COMMAND = "command";
37 /** Command: type. */
38 private static final String TYPE = "command_type";
39 /** Command: type - bootstrap. */
40 public static final short TYPE_BOOTSTRAP = 1;
41 /** Command: type - update. */
42 public static final short TYPE_UPDATE = 2;
43 /** Command: type - send. */
44 public static final short TYPE_SEND = 4;
45 /** Command: default sender. */
46 private static final String DEFSENDER = "command_defsender";
47 /** Command: default prefix. */
48 private static final String DEFPREFIX = "command_defprefix";
49 /** Command: recipients. */
50 private static final String RECIPIENTS = "command_reciepients";
51 /** Command: text. */
52 private static final String TEXT = "command_text";
53 /** Command: flashsms. */
54 private static final String FLASHSMS = "command_flashsms";
55 /** Command: timestamp. */
56 private static final String TIMESTAMP = "command_timestamp";
57 /** Command: custom sender. */
58 private static final String CUSTOMSENDER = "command_customsender";
60 /** {@link Bundle} represents the ConnectorSpec. */
61 private final Bundle bundle;
63 /**
64 * Create command with type update.
66 * @return created command
68 public static ConnectorCommand update() {
69 return new ConnectorCommand(TYPE_UPDATE);
72 /**
73 * Create command with type bootstrap.
75 * @return created command
77 public static ConnectorCommand bootstrap() {
78 return new ConnectorCommand(TYPE_BOOTSTRAP);
81 /**
82 * Create Command with type "send".
84 * @param defPrefix
85 * default prefix
86 * @param defSender
87 * default sender
88 * @param recipients
89 * reciepients
90 * @param text
91 * text
92 * @param flashSMS
93 * flashsms
94 * @param timestamp
95 * timestamp for sending
96 * @param customSender
97 * custom sender
98 * @return created command
100 public static ConnectorCommand send(final String defPrefix,
101 final String defSender, final String[] recipients,
102 final String text, final boolean flashSMS, final long timestamp,
103 final String customSender) {
104 ConnectorCommand ret = send(defPrefix, defSender, recipients, text,
105 flashSMS);
106 ret.setSendLater(timestamp);
107 ret.setCustomSender(customSender);
108 return ret;
112 * Create Command with type "send".
114 * @param defPrefix
115 * default prefix
116 * @param defSender
117 * default sender
118 * @param recipients
119 * reciepients
120 * @param text
121 * text
122 * @param flashSMS
123 * flashsms
124 * @return created command
126 public static ConnectorCommand send(final String defPrefix,
127 final String defSender, final String[] recipients,
128 final String text, final boolean flashSMS) {
129 final Bundle b = new Bundle();
130 b.putShort(TYPE, TYPE_SEND);
131 b.putString(DEFPREFIX, defPrefix);
132 b.putString(DEFSENDER, defSender);
133 final int l = recipients.length;
134 ArrayList<String> r = new ArrayList<String>(l);
135 for (int i = 0; i < l; i++) {
136 final String s = recipients[i];
137 if (s != null && s.trim().length() > 0) {
138 r.add(s);
141 b.putStringArray(RECIPIENTS, r.toArray(new String[0]));
142 b.putString(TEXT, text);
143 b.putBoolean(FLASHSMS, flashSMS);
144 b.putLong(TIMESTAMP, -1);
145 b.putString(CUSTOMSENDER, null);
146 return new ConnectorCommand(b);
150 * Create Command with type.
152 * @param type
153 * type
155 private ConnectorCommand(final Short type) {
156 this.bundle = new Bundle();
157 this.bundle.putShort(TYPE, type);
161 * Create Command from {@link Bundle}.
163 * @param b
164 * Bundle
166 private ConnectorCommand(final Bundle b) {
167 this.bundle = b;
171 * Create Command from {@link Intent}.
173 * @param i
174 * Intent
176 public ConnectorCommand(final Intent i) {
177 Bundle e = i.getExtras();
178 if (e != null) {
179 this.bundle = e.getBundle(EXTRAS_COMMAND);
180 } else {
181 this.bundle = new Bundle();
186 * Set this {@link ConnectorCommand} to an {@link Intent}. Creates new
187 * Intent if needed.
189 * @param intent
190 * {@link Intent}.
191 * @return the same {@link Intent}
193 public Intent setToIntent(final Intent intent) {
194 Intent i = intent;
195 if (i == null) {
196 switch (this.getType()) {
197 case TYPE_BOOTSTRAP:
198 i = new Intent(CommandReceiver.ACTION_CONNECTOR_RUN_BOOSTRAP);
199 break;
200 case TYPE_UPDATE:
201 i = new Intent(CommandReceiver.ACTION_CONNECTOR_RUN_UPDATE);
202 break;
203 case TYPE_SEND:
204 i = new Intent(CommandReceiver.ACTION_CONNECTOR_RUN_SEND);
205 break;
206 default:
207 return null;
210 i.putExtra(EXTRAS_COMMAND, this.getBundle());
211 return i;
215 * @return internal bundle
217 public Bundle getBundle() {
218 return this.bundle;
222 * @return type
224 public short getType() {
225 if (this.bundle != null) {
226 return this.bundle.getShort(TYPE);
227 } else {
228 return 0;
233 * @return default sender
235 public String getDefSender() {
236 return this.bundle.getString(DEFSENDER);
240 * @return default prefix
242 public String getDefPrefix() {
243 return this.bundle.getString(DEFPREFIX);
247 * @return recipients
249 public String[] getRecipients() {
250 return this.bundle.getStringArray(RECIPIENTS);
254 * @return text
256 public String getText() {
257 return this.bundle.getString(TEXT);
261 * @return flashsms
263 public boolean getFlashSMS() {
264 return this.bundle.getBoolean(FLASHSMS, false);
268 * @return timestamp for sending
270 public long getSendLater() {
271 return this.bundle.getLong(TIMESTAMP, -1);
275 * Set timestamp for sending later.
277 * @param timestamp
278 * timestamp
280 public void setSendLater(final long timestamp) {
281 this.bundle.putLong(TIMESTAMP, timestamp);
285 * @return custom sender
287 public String getCustomSender() {
288 return this.bundle.getString(CUSTOMSENDER);
292 * Set custom sender.
294 * @param customSender
295 * custom sender
297 public void setCustomSender(final String customSender) {
298 this.bundle.putString(CUSTOMSENDER, customSender);