2 * Copyright (C) 2010 Felix Bechstein
4 * This file is part of WebSMS.
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
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
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
;
28 * A Command send to a Connector.
32 public final class ConnectorCommand
{
34 /** Key to find command in a Bundle. */
35 private static final String EXTRAS_COMMAND
= "command";
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";
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
;
64 * Create command with type update.
66 * @return created command
68 public static ConnectorCommand
update() {
69 return new ConnectorCommand(TYPE_UPDATE
);
73 * Create command with type bootstrap.
75 * @return created command
77 public static ConnectorCommand
bootstrap() {
78 return new ConnectorCommand(TYPE_BOOTSTRAP
);
82 * Create Command with type "send".
95 * timestamp for sending
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
,
106 ret
.setSendLater(timestamp
);
107 ret
.setCustomSender(customSender
);
112 * Create Command with type "send".
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) {
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.
155 private ConnectorCommand(final Short type
) {
156 this.bundle
= new Bundle();
157 this.bundle
.putShort(TYPE
, type
);
161 * Create Command from {@link Bundle}.
166 private ConnectorCommand(final Bundle b
) {
171 * Create Command from {@link Intent}.
176 public ConnectorCommand(final Intent i
) {
177 Bundle e
= i
.getExtras();
179 this.bundle
= e
.getBundle(EXTRAS_COMMAND
);
181 this.bundle
= new Bundle();
186 * Set this {@link ConnectorCommand} to an {@link Intent}. Creates new
191 * @return the same {@link Intent}
193 public Intent
setToIntent(final Intent intent
) {
196 switch (this.getType()) {
198 i
= new Intent(CommandReceiver
.ACTION_CONNECTOR_RUN_BOOSTRAP
);
201 i
= new Intent(CommandReceiver
.ACTION_CONNECTOR_RUN_UPDATE
);
204 i
= new Intent(CommandReceiver
.ACTION_CONNECTOR_RUN_SEND
);
210 i
.putExtra(EXTRAS_COMMAND
, this.getBundle());
215 * @return internal bundle
217 public Bundle
getBundle() {
224 public short getType() {
225 if (this.bundle
!= null) {
226 return this.bundle
.getShort(TYPE
);
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
);
249 public String
[] getRecipients() {
250 return this.bundle
.getStringArray(RECIPIENTS
);
256 public String
getText() {
257 return this.bundle
.getString(TEXT
);
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.
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
);
294 * @param customSender
297 public void setCustomSender(final String customSender
) {
298 this.bundle
.putString(CUSTOMSENDER
, customSender
);