Skip to content

Commit

Permalink
Move stingray readings to base service
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinmarnold committed Aug 27, 2015
1 parent 1e65c38 commit d26a876
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.stingraymappingproject.api.app;

import android.content.Context;
import android.content.Intent;

import org.stingraymappingproject.api.clientandroid.activities.BaseStingrayActivity;

/**
* Created by Marvin Arnold on 27/08/15.
*/
public abstract class AppBaseStringrayActivity extends BaseStingrayActivity {
@Override
public void startStingrayClientService() {
if (!mBoundToStingrayAPIService) {
// Bind to LocalService
Intent intent = new Intent(AppBaseStringrayActivity.this, AppStringrayAPIClientService.class);
//Start Service before binding to keep it resident when activity is destroyed
startService(intent);
bindService(intent, mStingrayAPIServiceConnection, Context.BIND_AUTO_CREATE);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.stingraymappingproject.api.app;

import org.stingraymappingproject.api.clientandroid.StingrayAPIClientService;

/**
* Created by Marvin Arnold on 27/08/15.
*/
public class AppStringrayAPIClientService extends StingrayAPIClientService {
// receive callbacks
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;
import org.stingraymappingproject.api.clientandroid.RecurringRequest;
import org.stingraymappingproject.api.clientandroid.activities.BaseStingrayActivity;
import org.stingraymappingproject.api.clientandroid.models.Factoid;
import org.stingraymappingproject.api.clientandroid.models.StingrayReading;
import org.stingraymappingproject.api.clientandroid.requesters.FactoidsRequester;
import org.stingraymappingproject.api.clientandroid.requesters.NearbyRequester;
import org.stingraymappingproject.api.clientandroid.requesters.PostStingrayReadingRequester;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MainActivity extends BaseStingrayActivity {
public class MainActivity extends AppBaseStringrayActivity {
private final String TAG = "MainActivity";
private static final int FREQUENCY_VALUE = 6;
private static final TimeUnit FREQUENCY_UNIT = TimeUnit.SECONDS;

protected List<StingrayReading> mStingrayReadings;

@Override
protected void scheduleRequesters() {
scheduleNearbyRequester();
Expand All @@ -42,15 +37,15 @@ public void scheduleFactoidsRequester() {
@Override
public void onResponse(Factoid[] response) {
List<Factoid> factoids = Arrays.asList(response);
Log.d(TAG, "onResponse");
// Log.d(TAG, "onResponse");
for(Factoid f : factoids) {
Log.d(TAG, "Factoid: " + f.getFact());
// Log.d(TAG, "Factoid: " + f.getFact());
}
}

@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse");
// Log.d(TAG, "onErrorResponse");
}
};
RecurringRequest recurringRequest = new RecurringRequest(FREQUENCY_VALUE, FREQUENCY_UNIT, factoidsRequester);
Expand All @@ -77,10 +72,8 @@ protected String getRequestParams() {
e.printStackTrace();
}


Log.d(TAG, "scheduleNearbyRequester: " + timeAndSpaceField.toString());
return timeAndSpaceField.toString();

// Log.d(TAG, "scheduleNearbyRequester: " + timeAndSpaceField.toString());
return timeAndSpaceField.toString();
}

@Override
Expand All @@ -91,10 +84,11 @@ public void onErrorResponse(VolleyError error) {
@Override
public void onResponse(StingrayReading[] response) {

Log.d(TAG, "scheduleNearbyRequester:onResponse");
// Log.d(TAG, "scheduleNearbyRequester:onResponse");
if(response.length > 0) {
mStingrayReadings = new ArrayList<>();
mStingrayReadings = Arrays.asList(response);
if(mBoundToStingrayAPIService) {
mStingrayAPIService.setStingrayReadings(response);
}
}
}

Expand All @@ -104,41 +98,16 @@ public void onResponse(StingrayReading[] response) {
}

private void schedulePostStingrayReadingRequester() {
mStingrayReadings = new ArrayList<>();
Log.d(TAG, "schedulePostStingrayReadingRequester");
// Log.d(TAG, "schedulePostStingrayReadingRequester");
PostStingrayReadingRequester postStingrayReadingRequester = new PostStingrayReadingRequester(mStingrayAPIService) {
@Override
protected String getRequestParams() {
JSONObject attributeFields = new JSONObject();
JSONObject stingrayJSON = new JSONObject();

// Attributes
int _threat_level = 20;
Date _observed_at = new Date();
double _lat = 17.214;
double _long = 9.32;
String _version = getVersion();
StingrayReading stingrayReading = new StingrayReading(_threat_level, _observed_at, _lat, _long, null, _version);
mStingrayReadings.add(stingrayReading);

try {
//:lat,:long,:since)
attributeFields.put("threat_level", _threat_level);
attributeFields.put("lat", _lat);
attributeFields.put("long", _long);
attributeFields.put("observed_at", _observed_at);
attributeFields.put("unique_token", stingrayReading.getUniqueToken());
attributeFields.put("version", _version);
stingrayJSON.put("stingray_reading", attributeFields);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

Log.d(TAG, "schedulePostStingrayReadingRequester: " + stingrayJSON);
return stingrayJSON.toString();
return getReqParamsAndAddNewReading(_threat_level, _lat, _long);
}

@Override
Expand All @@ -148,14 +117,49 @@ public void onErrorResponse(VolleyError error) {

@Override
public void onResponse(StingrayReading response) {
Log.d(TAG, "schedulePostStingrayReadingRequester:onResponse");
mStingrayReadings.add(response);
// Log.d(TAG, "schedulePostStingrayReadingRequester:onResponse");
if(mBoundToStingrayAPIService) {
mStingrayAPIService.addStingrayReading(response);
}
}
};
RecurringRequest recurringRequest = new RecurringRequest(FREQUENCY_VALUE, FREQUENCY_UNIT, postStingrayReadingRequester);
mStingrayAPIService.addRecurringRequest(recurringRequest);
}

private String getReqParamsAndAddNewReading(int _threat_level, double _lat, double _long) {
JSONObject attributeFields = new JSONObject();
JSONObject stingrayJSON = new JSONObject();

Date _observed_at = new Date();
String _version = getVersion();
StingrayReading stingrayReading = new StingrayReading(_threat_level, _observed_at, _lat, _long, null, _version);


if(mBoundToStingrayAPIService) {
mStingrayAPIService.addStingrayReading(stingrayReading);
}

try {
//:lat,:long,:since)
attributeFields.put("threat_level", _threat_level);
attributeFields.put("lat", _lat);
attributeFields.put("long", _long);
attributeFields.put("observed_at", _observed_at);
attributeFields.put("unique_token", stingrayReading.getUniqueToken());
attributeFields.put("version", _version);
stingrayJSON.put("stingray_reading", attributeFields);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

// Log.d(TAG, "schedulePostStingrayReadingRequester: " + stingrayJSON);
return stingrayJSON.toString();
}

public String getVersion() {
PackageManager pm = getPackageManager();
PackageInfo pInfo = null;
Expand Down
2 changes: 1 addition & 1 deletion stingray-api-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/marvinmarnold/StingrayAPIClient' // Homepage URL of the library
gitUrl = 'https://github.com/marvinmarnold/StingrayAPIClient.git' // Git repository URL

libraryVersion = '0.0.533'
libraryVersion = '0.0.539'

developerId = 'marvinmarnold'
developerName = 'Marvin Arnold'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.stingraymappingproject.api.clientandroid;

import android.util.Log;

import org.stingraymappingproject.api.clientandroid.requesters.Requester;

import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -26,12 +24,12 @@ public RecurringRequest(int frequencyValue, TimeUnit frequencyUnit, Requester ru
}

public void schedule(ScheduledExecutorService scheduler) {
Log.d(TAG, "start scheduleUploader");
// Log.d(TAG, "start scheduleUploader");
if (isScheduled) return;

mRequester = scheduler.scheduleAtFixedRate(mRunnable, 0, mFrequencyValue, mFrequencyUnit);
isScheduled = true;
Log.d(TAG, "end scheduleUploader");
// Log.d(TAG, "end scheduleUploader");
}

public void cancel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import android.net.NetworkInfo;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import org.stingraymappingproject.api.clientandroid.models.Factoid;
import org.stingraymappingproject.api.clientandroid.models.StingrayReading;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -37,8 +40,9 @@ public RequestQueue getRequestQueue() {
}
public void setApiBaseUrl(String apiBaseUrl) { this.mApiBaseUrl = apiBaseUrl; }


// Should end with '/'
private String mApiBaseUrl = "http://api.stingraymappingproject.org/";
private String mApiBaseUrl = "https://stingray-mapping-server.herokuapp.com/";
private List<RecurringRequest> mRecurringRequests;

private RequestQueue mRequestQueue;
Expand All @@ -47,6 +51,26 @@ public RequestQueue getRequestQueue() {
private ScheduledExecutorService mRequestScheduler;
public boolean isInitialized = false;

protected List<Factoid> mFactoids;

public List<StingrayReading> getStingrayReadings() {
return mStingrayReadings;
}

public List<Factoid> getFactoids() {
return mFactoids;
}

public void setStingrayReadings(StingrayReading[] stingrayReadings) {
this.mStingrayReadings = Arrays.asList(stingrayReadings);
}

public void addStingrayReading(StingrayReading stingrayReading) {
this.mStingrayReadings.add(stingrayReading);
}

protected List<StingrayReading> mStingrayReadings;

public class ClientServiceBinder extends Binder {
public StingrayAPIClientService getService() {
return StingrayAPIClientService.this;
Expand All @@ -55,13 +79,13 @@ public StingrayAPIClientService getService() {

@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "#onBind");
// Log.d(TAG, "#onBind");
return mBinder;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
// Log.d(TAG, "onStartCommand");
init();
if (intent != null &&
intent.getAction() != null &&
Expand All @@ -88,12 +112,12 @@ public void onDestroy() {
}

public void queueOfflineRequests() {
Log.d(TAG, "queueOfflineRequests");
// Log.d(TAG, "queueOfflineRequests");
if(isInitialized && isOnline()) {
ArrayList<GsonRequest> t = (ArrayList<GsonRequest>) mOfflineRequests.clone();
mOfflineRequests.clear();
for(GsonRequest offlineRequest : t) {
Log.d(TAG, "Syncing offlineRequests");
// Log.d(TAG, "Syncing offlineRequests");
mRequestQueue.add(offlineRequest);
}
}
Expand All @@ -107,13 +131,15 @@ public boolean isOnline() {

public void init() {
if(isInitialized) return;
mContext = getApplicationContext();
mRequestScheduler = Executors.newScheduledThreadPool(1);
mRecurringRequests = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(mContext);
mOfflineRequests = new ArrayList<GsonRequest>();
this.mContext = getApplicationContext();
this.mRequestScheduler = Executors.newScheduledThreadPool(1);
this.mRecurringRequests = new ArrayList<>();
this.mRequestQueue = Volley.newRequestQueue(mContext);
this.mOfflineRequests = new ArrayList<GsonRequest>();
this.mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
this.isInitialized = true;
this.mFactoids = new ArrayList<>();
this.mStingrayReadings = new ArrayList<>();
}

public void scheduleRecurringRequests() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
Expand Down Expand Up @@ -38,6 +39,11 @@ public void onResume() {
startStingrayClientService();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onStop() {
super.onStop();
Expand All @@ -60,7 +66,7 @@ public void startStingrayClientService() {
}
}

private final ServiceConnection mStingrayAPIServiceConnection = new ServiceConnection() {
protected final ServiceConnection mStingrayAPIServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected");
Expand Down
Loading

0 comments on commit d26a876

Please sign in to comment.