package com.movisens.gattsensorexample.application;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import com.movisens.gattsensorexample.R;
import com.movisens.gattsensorexample.activities.Preferences;
import com.movisens.gattsensorexample.model.CurrentSensorData;
import com.movisens.gattsensorexample.services.SamplingService;
import com.movisens.movisensgattlib.characteristics.MetLevel;
import com.movisens.movisensgattlib.characteristics.MovementAcceleration;
import com.movisens.movisensgattlib.characteristics.StepCount;
import com.movisens.smartgattlib.characteristics.BatteryLevel;
import de.greenrobot.event.EventBus;
public class App extends Application {
private static final String TAG = "MovisensGattSensorExample";
private static App instance = null;
private Handler handler;
private CurrentSensorData sensorData = new CurrentSensorData();
private BatteryLevel batteryLevel = null;
public static App app() {
return instance;
}
public void showToast(final CharSequence text, final int duration) {
handler.post(new Runnable() {
public void run() {
Toast.makeText(app(), text, duration).show();
}
});
}
@Override
public void onCreate() {
Log.i(TAG, "Initialize MovisensGattSensorExample Application");
handler = new Handler(Looper.getMainLooper());
instance = this;
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
EventBus.getDefault().register(this);
Boolean wasSamplingRunning = wasSamplingRunning();
Log.d(TAG, "Sampling_Started was " + wasSamplingRunning);
if (wasSamplingRunning) {
SamplingService.start(instance);
}
super.onCreate();
}
public SharedPreferences getPrefs() {
return PreferenceManager.getDefaultSharedPreferences(this);
}
public boolean isSamplingRunning() {
return getPrefs().getBoolean(Preferences.SAMPLING_RUNNING, false);
}
public Boolean wasSamplingRunning() {
return getPrefs().getBoolean(Preferences.SAMPLING_RUNNING, false);
}
public boolean isSensorSelected() {
return !getPrefs().getString(Preferences.SENSOR_ADDRESS, "").equals("");
}
public String getSensor() {
String sensor = "No Sensor selected";
if (isSensorSelected()) {
sensor = getPrefs().getString(Preferences.SENSOR_NAME, "") + " ("
+ getPrefs().getString(Preferences.SENSOR_ADDRESS, "")
+ ")";
}
return sensor;
}
public void onEventMainThread(MovementAcceleration movement) {
this.sensorData.movementAcceleration = movement.getValue();
EventBus.getDefault().post(this.sensorData);
}
public void onEventMainThread(MetLevel metLevel) {
this.sensorData.metLevel = metLevel;
EventBus.getDefault().post(this.sensorData);
}
public void onEventMainThread(StepCount stepCount) {
this.sensorData.stepCount = stepCount;
EventBus.getDefault().post(this.sensorData);
}
public CurrentSensorData getCurrentSensorData() {
return this.sensorData;
}
public void onEventMainThread(BatteryLevel batteryLevel) {
this.batteryLevel = batteryLevel;
}
public BatteryLevel getCurrentBatteryLevel() {
return this.batteryLevel;
}
public void shutDown() {
Log.d(TAG, "Shutting down...");
SamplingService.stop(this, true);
}
}