org.android.activityminer.app.service
Class BackgroundService

Package class diagram package BackgroundService
java.lang.Object
  extended by android.content.Context
      extended by android.content.ContextWrapper
          extended by android.app.Service
              extended by org.android.activityminer.app.service.BackgroundService
All Implemented Interfaces:
ComponentCallbacks, ComponentCallbacks2, EventObserver<TaskFinishedEvent>

public class BackgroundService
extends Service
implements EventObserver<TaskFinishedEvent>

The service for asynchronous background tasks.


Field Summary
private  IBackgroundService.Stub binder
          the service binder
private  ObservableWorkerThreadWithProgressNotification currentTask
          Reference to an active feature extraction thread
private  Queue<ObservableWorkerThreadWithProgressNotification> queue
          The queue for batch processing.
private  ServiceState state
          The service state
private  TrainingDataDeletionThread trainingDataDeletionThread
          The training data deletion thread.
private  PowerManager.WakeLock wakeLock
          The wake lock.
 
Fields inherited from class android.app.Service
START_CONTINUATION_MASK, START_FLAG_REDELIVERY, START_FLAG_RETRY, START_NOT_STICKY, START_REDELIVER_INTENT, START_STICKY, START_STICKY_COMPATIBILITY
 
Fields inherited from class android.content.Context
ACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APP_OPS_SERVICE, AUDIO_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_WAIVE_PRIORITY, BLUETOOTH_SERVICE, CAPTIONING_SERVICE, CLIPBOARD_SERVICE, CONNECTIVITY_SERVICE, CONSUMER_IR_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, DEVICE_POLICY_SERVICE, DISPLAY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, KEYGUARD_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_ROUTER_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, POWER_SERVICE, PRINT_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, STORAGE_SERVICE, TELEPHONY_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, UI_MODE_SERVICE, USB_SERVICE, USER_SERVICE, VIBRATOR_SERVICE, WALLPAPER_SERVICE, WIFI_P2P_SERVICE, WIFI_SERVICE, WINDOW_SERVICE
 
Fields inherited from interface android.content.ComponentCallbacks2
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
 
Constructor Summary
BackgroundService()
          Constructor
 
Method Summary
protected  boolean batchCreateAndExportTrainingDataDatabase(DBManager dbManager, Configuration baseConfig, long[] windowSizes, long[] sampleRates, double[] gfCutOffs)
          Method to start batch training data creation in the background for any combination of the given window sizes and sample rates
 boolean compareAndSetState(ServiceState expect, ServiceState update)
          Atomically sets the service state to the given updated value if the current value equals the expected value.
protected  boolean createModel(DBManager dbManager, Configuration config, String modelName)
          Method to start model creation in the background
protected  boolean dBRawDataUpdate(DBManager dbManager, Configuration config)
          Method to start DB related updates of raw time series (relevant for older DB versions only!).
protected  boolean evaluate(DBManager dbManager, Configuration config, ParcelableFeatureTypeSets featureSetsToTest, boolean bestResultsOnly)
          Method to start a features set evaluation in the background
protected  boolean exportDatabase(String fileName)
          Method to export database in the background
protected  boolean exportModelFile(String modelName)
          Method to export a model file in the background
protected  boolean exportRawData(DBManager dbManager, Configuration config, String fileName)
          Method to export the raw recorded data to a file in the background
private  boolean exportTrainingData(DBManager dbManager, Configuration config, String fileName)
          Method to export training data to file in the background
protected  boolean extractFeatures(DBManager dbManager, Configuration config, long[] sequenceIds)
          Method to start feature extraction in the background
private  ObservableWorkerThreadWithProgressNotification getCurrentTask()
          Getter for the current task
protected  Class<? extends Activity> getNotificationTargetClass(ServiceState serviceState)
          Does determine the notification result target class by the current service state
private  ServiceState getState()
          Getter for the state
protected  boolean importDatabase(String fileName)
          Method to export database in the background
 IBinder onBind(Intent intent)
           
 void onCreate()
           
 void onDestroy()
           
 void onEvent(ObservableEventSource<? extends TaskFinishedEvent> eventSource, TaskFinishedEvent observedEvent)
           
 boolean runNextBatchJob()
           
protected  boolean searchForBestFeatureSubset(DBManager dbManager, Configuration config, ParcelableFeatureTypeSets featureSets, int size)
          Method to start a search for the best feature subset of a given size.
private  void setCurrentTask(ObservableWorkerThreadWithProgressNotification thread)
          Setter for the current task
private  void setState(ServiceState state)
          Setter for the state
protected  boolean updateLinearAccelerationsInDatabase(DBManager dbManager, Configuration config)
          Method to start an update of linear acceleration values in the raw data samples for the case that the own linear acceleration implementation is used.
 
Methods inherited from class android.app.Service
dump, getApplication, onConfigurationChanged, onLowMemory, onRebind, onStart, onStartCommand, onTaskRemoved, onTrimMemory, onUnbind, startForeground, stopForeground, stopSelf, stopSelf, stopSelfResult
 
Methods inherited from class android.content.ContextWrapper
attachBaseContext, bindService, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkUriPermission, checkUriPermission, clearWallpaper, createConfigurationContext, createDisplayContext, createPackageContext, databaseList, deleteDatabase, deleteFile, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getBaseContext, getCacheDir, getClassLoader, getContentResolver, getDatabasePath, getDir, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getFilesDir, getFileStreamPath, getMainLooper, getObbDir, getObbDirs, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getSystemService, getTheme, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isRestricted, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setTheme, setWallpaper, setWallpaper, startActivities, startActivities, startActivity, startActivity, startInstrumentation, startIntentSender, startIntentSender, startService, stopService, unbindService, unregisterReceiver
 
Methods inherited from class android.content.Context
getString, getString, getText, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, registerComponentCallbacks, unregisterComponentCallbacks
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentTask

private ObservableWorkerThreadWithProgressNotification currentTask
Reference to an active feature extraction thread


queue

private final Queue<ObservableWorkerThreadWithProgressNotification> queue
The queue for batch processing.


binder

private final IBackgroundService.Stub binder
the service binder


state

private ServiceState state
The service state


wakeLock

private PowerManager.WakeLock wakeLock
The wake lock.


trainingDataDeletionThread

private TrainingDataDeletionThread trainingDataDeletionThread
The training data deletion thread.

Constructor Detail

BackgroundService

public BackgroundService()
Constructor

Method Detail

onCreate

public final void onCreate()
Overrides:
onCreate in class Service

onDestroy

public void onDestroy()
Overrides:
onDestroy in class Service

setCurrentTask

private final void setCurrentTask(ObservableWorkerThreadWithProgressNotification thread)
Setter for the current task

Parameters:
thread - the current task to set

getCurrentTask

private final ObservableWorkerThreadWithProgressNotification getCurrentTask()
Getter for the current task

Returns:
the current task

setState

private final void setState(ServiceState state)
Setter for the state

Parameters:
state - the state to set

getState

private final ServiceState getState()
Getter for the state

Returns:
the state

compareAndSetState

public final boolean compareAndSetState(ServiceState expect,
                                        ServiceState update)
Atomically sets the service state to the given updated value if the current value equals the expected value.

Parameters:
expect - the expected value
update - the update value
Returns:
true if successful. False return indicates that the actual value was not equal to the expected value.

onBind

public final IBinder onBind(Intent intent)
Specified by:
onBind in class Service

extractFeatures

protected final boolean extractFeatures(DBManager dbManager,
                                        Configuration config,
                                        long[] sequenceIds)
Method to start feature extraction in the background

Parameters:
dbManager - the database manager to use
config - the configuration to use
sequenceIds - a list with the identifiers of the continuous recording sequences to extract features from
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

batchCreateAndExportTrainingDataDatabase

protected boolean batchCreateAndExportTrainingDataDatabase(DBManager dbManager,
                                                           Configuration baseConfig,
                                                           long[] windowSizes,
                                                           long[] sampleRates,
                                                           double[] gfCutOffs)
Method to start batch training data creation in the background for any combination of the given window sizes and sample rates

Parameters:
dbManager - the database manager to use
baseConfig - the base configuration
windowSizes - the window sizes
sampleRates - the sample rates
gfCutOffs - cut-off frequencies for the own gravitation filter
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

runNextBatchJob

public boolean runNextBatchJob()
Returns:
true if a job was started, false otherwise

evaluate

protected final boolean evaluate(DBManager dbManager,
                                 Configuration config,
                                 ParcelableFeatureTypeSets featureSetsToTest,
                                 boolean bestResultsOnly)
Method to start a features set evaluation in the background

Parameters:
dbManager - the database manager to use
config - the configuration to use
featureSetsToTest - the set with the feature sets to test
bestResultsOnly - flag to limit the result list to the best results (ranked by overall prediction accuracy)
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

searchForBestFeatureSubset

protected final boolean searchForBestFeatureSubset(DBManager dbManager,
                                                   Configuration config,
                                                   ParcelableFeatureTypeSets featureSets,
                                                   int size)
Method to start a search for the best feature subset of a given size.

Parameters:
dbManager - the database manager to use
config - the configuration to use
featureSets - the set with the feature set to search in
size - the subset size
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

createModel

protected final boolean createModel(DBManager dbManager,
                                    Configuration config,
                                    String modelName)
Method to start model creation in the background

Parameters:
dbManager - the database manager to use
config - the configuration to use
modelName - the model name
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

exportTrainingData

private final boolean exportTrainingData(DBManager dbManager,
                                         Configuration config,
                                         String fileName)
Method to export training data to file in the background

Parameters:
dbManager - the database manager to use
config - the current configuration
fileName - the destination file name
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

exportRawData

protected boolean exportRawData(DBManager dbManager,
                                Configuration config,
                                String fileName)
Method to export the raw recorded data to a file in the background

Parameters:
dbManager - the database manager to use
config - the current configuration
fileName - the destination file name
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

updateLinearAccelerationsInDatabase

protected boolean updateLinearAccelerationsInDatabase(DBManager dbManager,
                                                      Configuration config)
Method to start an update of linear acceleration values in the raw data samples for the case that the own linear acceleration implementation is used.

Parameters:
dbManager - the database manager to use
config - the current configuration
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running or the device linear sensor is used )

dBRawDataUpdate

protected boolean dBRawDataUpdate(DBManager dbManager,
                                  Configuration config)
Method to start DB related updates of raw time series (relevant for older DB versions only!).

Parameters:
dbManager - the database manager to use
config - the current configuration
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running or the device linear sensor is used )

importDatabase

protected boolean importDatabase(String fileName)
Method to export database in the background

Parameters:
fileName - the destination file name
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

exportDatabase

protected boolean exportDatabase(String fileName)
Method to export database in the background

Parameters:
fileName - the destination file name
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

exportModelFile

protected boolean exportModelFile(String modelName)
Method to export a model file in the background

Parameters:
modelName - the model name
Returns:
true if request can be performed, false otherwise (e.g. if another task is already running)

onEvent

public final void onEvent(ObservableEventSource<? extends TaskFinishedEvent> eventSource,
                          TaskFinishedEvent observedEvent)
Specified by:
onEvent in interface EventObserver<TaskFinishedEvent>

getNotificationTargetClass

protected Class<? extends Activity> getNotificationTargetClass(ServiceState serviceState)
Does determine the notification result target class by the current service state

Parameters:
serviceState - the service state
Returns:
the target class for the pending notification