Created
September 26, 2017 18:33
-
-
Save hi-manshu/2f0e1813d4da090853b78039486dbc0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SleepAccelerometerService extends Service implements SensorEventListener { | |
private SensorManager sensorMan; | |
public Sensor accelerometer; | |
public Context context; | |
private float[] mGravity; | |
private float mAccel; | |
private float mAccelCurrent; | |
private float mAccelLast; | |
String nowAsISO; | |
DateFormat df; | |
String df1; | |
long initDate; | |
Date currentDate; | |
long hours; | |
long seconds, minutes; | |
public int counter = 0; | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
sensorMan = (SensorManager) getSystemService(SENSOR_SERVICE); | |
accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
mAccel = 0.00f; | |
startTimer(); | |
mAccelCurrent = SensorManager.GRAVITY_EARTH; | |
sensorMan.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); | |
mAccelLast = SensorManager.GRAVITY_EARTH; | |
SharedPreference.init(getApplicationContext()); | |
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta"); | |
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); | |
df.setTimeZone(tz); | |
return START_STICKY; | |
} | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { | |
mGravity = event.values.clone(); | |
// Shake detection | |
float x = mGravity[0]; | |
float y = mGravity[1]; | |
float z = mGravity[2]; | |
mAccelLast = mAccelCurrent; | |
mAccelCurrent = (float) Math.sqrt(x * x + y * y + z * z); | |
float delta = mAccelCurrent - mAccelLast; | |
mAccel = mAccel * 0.9f + delta; | |
// Make this higher or lower according to how much | |
// motion you want to detect | |
if (mAccel > 1.5) { | |
Log.d("mAccel", String.valueOf(mAccel)); | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); | |
currentDate = new Date(); | |
df1 = dateFormat.format(new Date()); | |
initDate = currentDate.getTime(); | |
//df1 is start value | |
try { | |
Log.d("InitTime", String.valueOf(SharedPreference.read(Constants.ISOTIME, null))); | |
Log.d("CurrentTime", String.valueOf(initDate)); | |
Log.d("FinalTime", String.valueOf(df1)); | |
Log.d("UserID", SharedPreference.read(Constants.USERID, null)); | |
Log.d("TOKENNEW", SharedPreference.read(Constants.AUTH_TOKEN, null)); | |
long diff = initDate - SharedPreference.read(Constants.TIMEINTI, -1L); | |
//df1 is end and read current is start | |
seconds = diff / 1000; | |
minutes = seconds / 60; | |
CustomLogClass.d("TimeGone", String.valueOf(seconds)); | |
hours = minutes / 60; | |
} catch (Exception e) { | |
} | |
if (CheckNetwork.isInternetAvailable(getApplicationContext())) { | |
if (seconds > 3) { | |
try { | |
Log.d("TOKENNEW", SharedPreference.read(Constants.TOKEN, null)); | |
Log.d("working", String.valueOf(seconds)); | |
//sending data to my activity | |
//API Call to be done | |
new SleepPush().execute(); | |
Toast.makeText(getApplicationContext(), "Send Data to backend", Toast.LENGTH_SHORT).show(); | |
} catch (Exception e) { | |
} | |
} else { | |
try { | |
// Toast.makeText(getApplicationContext(), "Less than 3 hours.", Toast.LENGTH_SHORT).show(); | |
SharedPreference.write(Constants.CURRENTTIMESLEEP, nowAsISO); //currentSleep | |
SharedPreference.write(Constants.TIMEINTI, currentDate.getTime()); //initDate | |
SharedPreference.write(Constants.ISOTIME, df1); | |
Log.d("ISO", SharedPreference.read(Constants.ISOTIME, null)); | |
Log.d("ISO", String.valueOf(df1)); | |
} catch (Exception e) { | |
} | |
} | |
} else { | |
Toast.makeText(getApplicationContext(), "No Internet.", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
} | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int accuracy) { | |
Log.d("Accuracy", String.valueOf(accuracy)); | |
} | |
private class SleepPush extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... params) { | |
try { | |
Log.d("TOKENNEW1", SharedPreference.read(Constants.TOKEN, null)); | |
Log.d("TOKENNEW2", String.valueOf(SharedPreference.read(Constants.ISOTIME, null))); | |
Log.d("TOKENNEW3", String.valueOf(df1)); | |
sendSleepDataToBackend("Token " + SharedPreference.read(Constants.AUTH_TOKEN, null), String.valueOf(SharedPreference.read(Constants.ISOTIME, null)), String.valueOf(df1), Integer.parseInt(SharedPreference.read(Constants.USERID, null))); | |
} catch (Exception e) { | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void result) { | |
//to be done when the work is executed. | |
try { | |
SharedPreference.write(Constants.CURRENTTIMESLEEP, nowAsISO); //currentSleep | |
SharedPreference.write(Constants.TIMEINTI, currentDate.getTime()); //initDate | |
SharedPreference.write(Constants.ISOTIME, df1); | |
} catch (Exception e) { | |
} | |
} | |
@Override | |
protected void onPreExecute() { | |
} | |
@Override | |
protected void onProgressUpdate(Void... text) { | |
} | |
} | |
private void sendSleepDataToBackend(String token, String startTime, String currentTime, int userid) { | |
try { | |
GeneralAPI generalAPI = APIClientPort.getClient().create(GeneralAPI.class); | |
Call<SleepLoggingData> call = generalAPI.sendSleepInBackend(token, startTime, currentTime,(userid)); | |
call.enqueue(new Callback<SleepLoggingData>() { | |
@Override | |
public void onResponse(Call<SleepLoggingData> call, Response<SleepLoggingData> response) { | |
SleepLoggingData result = response.body(); | |
if (response.isSuccessful()) { | |
Log.d("Logged", "Reason : Logged"); | |
} else { | |
Log.d("Logged"+ "Reason : ", response.message()); | |
} | |
} | |
@Override | |
public void onFailure(Call<SleepLoggingData> call, Throwable t) { | |
} | |
}); | |
} catch (Exception e) { | |
} | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Log.i("EXIT", "ondestroy!"); | |
Intent broadcastIntent = new Intent("com.a98fit.RestartSensor"); | |
sendBroadcast(broadcastIntent); | |
} | |
private Timer timer; | |
private TimerTask timerTask; | |
long oldTime = 0; | |
public void startTimer() { | |
//set a new Timer | |
timer = new Timer(); | |
//initialize the TimerTask's job | |
initializeTimerTask(); | |
//schedule the timer, to wake up every 1 second | |
timer.schedule(timerTask, 1000, 1000); // | |
} | |
public void initializeTimerTask() { | |
timerTask = new TimerTask() { | |
public void run() { | |
Log.i("TimerrSleep", "in timer ++++ " + (counter++)); | |
} | |
}; | |
} | |
@Override | |
public void onTaskRemoved(Intent rootIntent) { | |
super.onTaskRemoved(rootIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment