fix some code
[andGMXsms.git] / src / de / ub0r / android / websms / connector / common / ConnectorTask.java
bloba211117a155924f0f1a8b7a055c479897b68ca1f
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.common;
21 import android.content.Intent;
22 import android.os.AsyncTask;
23 import android.util.Log;
24 import android.widget.Toast;
26 /**
27 * {@link AsyncTask} run by the Connector's {@link ConnectorService}.
29 * @author flx
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;
44 /**
45 * Create a connector task.
47 * @param i
48 * intent holding {@link ConnectorSpec} and
49 * {@link ConnectorCommand}
50 * @param r
51 * {@link Connector}
52 * @param s
53 * {@link ConnectorService}
55 public ConnectorTask(final Intent i, final Connector r,
56 final ConnectorService s) {
57 this.intent = i;
58 this.connector = new ConnectorSpec(i);
59 this.command = new ConnectorCommand(i);
60 this.receiver = r;
61 this.service = s;
64 /**
65 * {@inheritDoc}
67 @Override
68 protected Void doInBackground(final Void... arg0) {
69 try {
70 switch (this.command.getType()) {
71 case ConnectorCommand.TYPE_BOOTSTRAP:
72 this.receiver.doBootstrap(this.service, this.intent);
73 break;
74 case ConnectorCommand.TYPE_UPDATE:
75 this.receiver.doUpdate(this.service, this.intent);
76 break;
77 case ConnectorCommand.TYPE_SEND:
78 this.receiver.doSend(this.service, this.intent);
79 break;
80 default:
81 break;
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);
92 return null;
95 /**
96 * {@inheritDoc}
98 @Override
99 protected void onPostExecute(final Void result) {
100 final String e = this.connector.getErrorMessage();
101 if (e != null) {
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);