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/>.
19 package de
.ub0r
.android
.websms
.connector
.common
;
21 import android
.content
.Intent
;
22 import android
.os
.AsyncTask
;
23 import android
.util
.Log
;
24 import android
.widget
.Toast
;
27 * {@link AsyncTask} run by the Connector's {@link ConnectorService}.
31 final class ConnectorTask
extends AsyncTask
<Void
, Void
, Void
> {
33 /** Intent comming from outside. */
34 private final Intent intent
;
35 /** Connector class which will do the actual IO. */
36 private final Connector receiver
;
37 /** Used connector. */
38 private final ConnectorSpec connector
;
39 /** Command running. */
40 private final ConnectorCommand command
;
41 /** {@link ConnectorService}. */
42 private final ConnectorService service
;
45 * Create a connector task.
48 * intent holding {@link ConnectorSpec} and
49 * {@link ConnectorCommand}
53 * {@link ConnectorService}
55 public ConnectorTask(final Intent i
, final Connector r
,
56 final ConnectorService s
) {
58 this.connector
= new ConnectorSpec(i
);
59 this.command
= new ConnectorCommand(i
);
68 protected Void
doInBackground(final Void
... arg0
) {
70 switch (this.command
.getType()) {
71 case ConnectorCommand
.TYPE_BOOTSTRAP
:
72 this.receiver
.doBootstrap(this.service
, this.intent
);
74 case ConnectorCommand
.TYPE_UPDATE
:
75 this.receiver
.doUpdate(this.service
, this.intent
);
77 case ConnectorCommand
.TYPE_SEND
:
78 this.receiver
.doSend(this.service
, this.intent
);
83 } catch (WebSMSException e
) {
84 Log
.d(this.connector
.getID(), "error in AsyncTask", e
);
85 // put error message to ConnectorSpec
86 this.connector
.setErrorMessage(e
);
87 } catch (Exception e
) {
88 Log
.e(this.connector
.getID(), "error in AsyncTask", e
);
89 // put error message to ConnectorSpec
90 this.connector
.setErrorMessage(e
);
99 protected void onPostExecute(final Void result
) {
100 final String e
= this.connector
.getErrorMessage();
102 Toast
.makeText(this.service
, e
, Toast
.LENGTH_LONG
).show();
104 final Intent i
= this.connector
.setToIntent(null);
105 this.command
.setToIntent(i
);
106 this.service
.sendBroadcast(i
);
107 this.service
.unregister(i
);