split RUN broadcasts
[andGMXsms.git] / src / de / ub0r / android / websms / connector / ConnectorService.java
blob7443167e9cbc3be8011caee57676e1538114bb93
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;
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;
26 import de.ub0r.android.andGMXsms.HelperAPI5Service;
28 /**
29 * {@link Service} run by the connectors BroadcastReceiver.
31 * @author flx
33 public abstract class ConnectorService extends Service {
34 /** Tag for output. */
35 private static final String TAG = "WebSMS.IO";
37 /** Wrapper for API5 commands. */
38 private HelperAPI5Service helperAPI5s = null;
40 /** Number of running Tasks. */
41 private int running = 0;
43 /**
44 * {@inheritDoc}
46 @Override
47 public final IBinder onBind(final Intent intent) {
48 return null;
51 /**
52 * {@inheritDoc}
54 @Override
55 public final void onCreate() {
56 super.onCreate();
57 try {
58 this.helperAPI5s = new HelperAPI5Service();
59 if (!this.helperAPI5s.isAvailable()) {
60 this.helperAPI5s = null;
62 } catch (VerifyError e) {
63 this.helperAPI5s = null;
64 Log.d(TAG, "no api5 running", e);
68 /**
69 * Register a IO task.
71 public final synchronized void register() {
72 Log.d(TAG, "register()");
73 Log.d(TAG, "currentIOOps=" + this.running);
74 ++this.running;
75 Log.d(TAG, "currentIOOps=" + this.running);
78 /**
79 * Unregister a IO task.
81 public final synchronized void unregister() {
82 Log.d(TAG, "unregister()");
83 Log.d(TAG, "currentIOOps=" + this.running);
84 --this.running;
85 if (this.running <= 0) {
86 this.stopSelf();
88 Log.d(TAG, "currentIOOps=" + this.running);
91 /**
92 * {@inheritDoc}
94 @Override
95 public final void onStart(final Intent intent, final int startId) {
96 if (intent != null) {
97 final String a = intent.getAction();
98 if (a != null && // .
99 (a.equals(Constants.ACTION_CONNECTOR_RUN_BOOSTRAP)
100 || a.equals(// .
101 Constants.ACTION_CONNECTOR_RUN_UPDATE) // .
102 || a.equals(Constants.ACTION_CONNECTOR_RUN_SEND))) {
103 // TODO: setForeground / startForeground
104 this.startConnectorTask(this, intent);
110 * {@inheritDoc}
112 @Override
113 public final int onStartCommand(final Intent intent, final int flags,
114 final int startId) {
115 this.onStart(intent, startId);
116 return START_NOT_STICKY;
120 * Start a connectorTask.
122 * @param context
123 * context
124 * @param intent
125 * intent
127 protected abstract void startConnectorTask(final Context context,
128 final Intent intent);