add CommandReceiver
[andGMXsms.git] / src / de / ub0r / android / websms / connector / common / ConnectorService.java
blob5432a17dd9daf0a06618b9e0fae7c31f5f5d5b2b
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.app.Service;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.IBinder;
25 import android.util.Log;
27 /**
28 * {@link Service} run by the connectors BroadcastReceiver.
30 * @author flx
32 public abstract class ConnectorService extends Service {
33 /** Tag for output. */
34 private static final String TAG = "WebSMS.IO";
36 /** Wrapper for API5 commands. */
37 private HelperAPI5Service helperAPI5s = null;
39 /** Number of running Tasks. */
40 private int running = 0;
42 /**
43 * {@inheritDoc}
45 @Override
46 public final IBinder onBind(final Intent intent) {
47 return null;
50 /**
51 * {@inheritDoc}
53 @Override
54 public final void onCreate() {
55 super.onCreate();
56 try {
57 this.helperAPI5s = new HelperAPI5Service();
58 if (!this.helperAPI5s.isAvailable()) {
59 this.helperAPI5s = null;
61 } catch (VerifyError e) {
62 this.helperAPI5s = null;
63 Log.d(TAG, "no api5 running", e);
67 /**
68 * Register a IO task.
70 public final synchronized void register() {
71 Log.d(TAG, "register()");
72 Log.d(TAG, "currentIOOps=" + this.running);
73 ++this.running;
74 Log.d(TAG, "currentIOOps=" + this.running);
77 /**
78 * Unregister a IO task.
80 public final synchronized void unregister() {
81 Log.d(TAG, "unregister()");
82 Log.d(TAG, "currentIOOps=" + this.running);
83 --this.running;
84 if (this.running <= 0) {
85 this.stopSelf();
87 Log.d(TAG, "currentIOOps=" + this.running);
90 /**
91 * {@inheritDoc}
93 @Override
94 public final void onStart(final Intent intent, final int startId) {
95 if (intent != null) {
96 final String a = intent.getAction();
97 if (a != null && // .
98 (a.equals(CommandReceiver.ACTION_CONNECTOR_RUN_BOOSTRAP)
99 || a.equals(// .
100 CommandReceiver.ACTION_CONNECTOR_RUN_UPDATE) // .
101 || a.equals(CommandReceiver.ACTION_CONNECTOR_RUN_SEND))) {
102 // TODO: setForeground / startForeground
103 this.startConnectorTask(this, intent);
109 * {@inheritDoc}
111 @Override
112 public final int onStartCommand(final Intent intent, final int flags,
113 final int startId) {
114 this.onStart(intent, startId);
115 return START_NOT_STICKY;
119 * Start a connectorTask.
121 * @param context
122 * context
123 * @param intent
124 * intent
126 protected abstract void startConnectorTask(final Context context,
127 final Intent intent);