From d26a8766fa4d497bd1aa1bc9c0d0975372e0f0c2 Mon Sep 17 00:00:00 2001 From: Marvin Arnold Date: Thu, 27 Aug 2015 03:05:14 -0500 Subject: [PATCH] Move stingray readings to base service --- .../api/app/AppBaseStringrayActivity.java | 22 +++++ .../api/app/AppStringrayAPIClientService.java | 10 ++ .../api/app/MainActivity.java | 94 ++++++++++--------- stingray-api-client/build.gradle | 2 +- .../api/clientandroid/RecurringRequest.java | 6 +- .../StingrayAPIClientService.java | 48 +++++++--- .../activities/BaseStingrayActivity.java | 8 +- .../clientandroid/requesters/Requester.java | 8 +- stingray-api-client/stingray-api-client.iml | 2 +- 9 files changed, 132 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/org/stingraymappingproject/api/app/AppBaseStringrayActivity.java create mode 100644 app/src/main/java/org/stingraymappingproject/api/app/AppStringrayAPIClientService.java diff --git a/app/src/main/java/org/stingraymappingproject/api/app/AppBaseStringrayActivity.java b/app/src/main/java/org/stingraymappingproject/api/app/AppBaseStringrayActivity.java new file mode 100644 index 0000000..c8c5091 --- /dev/null +++ b/app/src/main/java/org/stingraymappingproject/api/app/AppBaseStringrayActivity.java @@ -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); + } + } +} diff --git a/app/src/main/java/org/stingraymappingproject/api/app/AppStringrayAPIClientService.java b/app/src/main/java/org/stingraymappingproject/api/app/AppStringrayAPIClientService.java new file mode 100644 index 0000000..3d01418 --- /dev/null +++ b/app/src/main/java/org/stingraymappingproject/api/app/AppStringrayAPIClientService.java @@ -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 +} diff --git a/app/src/main/java/org/stingraymappingproject/api/app/MainActivity.java b/app/src/main/java/org/stingraymappingproject/api/app/MainActivity.java index d953a6d..5ea5da1 100644 --- a/app/src/main/java/org/stingraymappingproject/api/app/MainActivity.java +++ b/app/src/main/java/org/stingraymappingproject/api/app/MainActivity.java @@ -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 mStingrayReadings; - @Override protected void scheduleRequesters() { scheduleNearbyRequester(); @@ -42,15 +37,15 @@ public void scheduleFactoidsRequester() { @Override public void onResponse(Factoid[] response) { List 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); @@ -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 @@ -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); + } } } @@ -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 @@ -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; diff --git a/stingray-api-client/build.gradle b/stingray-api-client/build.gradle index 4309647..8e7cc60 100644 --- a/stingray-api-client/build.gradle +++ b/stingray-api-client/build.gradle @@ -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' diff --git a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/RecurringRequest.java b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/RecurringRequest.java index cef6b25..38df964 100644 --- a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/RecurringRequest.java +++ b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/RecurringRequest.java @@ -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; @@ -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() { diff --git a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/StingrayAPIClientService.java b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/StingrayAPIClientService.java index dc6b4ac..4e22537 100644 --- a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/StingrayAPIClientService.java +++ b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/StingrayAPIClientService.java @@ -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; @@ -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 mRecurringRequests; private RequestQueue mRequestQueue; @@ -47,6 +51,26 @@ public RequestQueue getRequestQueue() { private ScheduledExecutorService mRequestScheduler; public boolean isInitialized = false; + protected List mFactoids; + + public List getStingrayReadings() { + return mStingrayReadings; + } + + public List getFactoids() { + return mFactoids; + } + + public void setStingrayReadings(StingrayReading[] stingrayReadings) { + this.mStingrayReadings = Arrays.asList(stingrayReadings); + } + + public void addStingrayReading(StingrayReading stingrayReading) { + this.mStingrayReadings.add(stingrayReading); + } + + protected List mStingrayReadings; + public class ClientServiceBinder extends Binder { public StingrayAPIClientService getService() { return StingrayAPIClientService.this; @@ -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 && @@ -88,12 +112,12 @@ public void onDestroy() { } public void queueOfflineRequests() { - Log.d(TAG, "queueOfflineRequests"); +// Log.d(TAG, "queueOfflineRequests"); if(isInitialized && isOnline()) { ArrayList t = (ArrayList) mOfflineRequests.clone(); mOfflineRequests.clear(); for(GsonRequest offlineRequest : t) { - Log.d(TAG, "Syncing offlineRequests"); +// Log.d(TAG, "Syncing offlineRequests"); mRequestQueue.add(offlineRequest); } } @@ -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(); + this.mContext = getApplicationContext(); + this.mRequestScheduler = Executors.newScheduledThreadPool(1); + this.mRecurringRequests = new ArrayList<>(); + this.mRequestQueue = Volley.newRequestQueue(mContext); + this.mOfflineRequests = new ArrayList(); this.mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); this.isInitialized = true; + this.mFactoids = new ArrayList<>(); + this.mStingrayReadings = new ArrayList<>(); } public void scheduleRecurringRequests() { diff --git a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/activities/BaseStingrayActivity.java b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/activities/BaseStingrayActivity.java index 13f3bc0..53ab6e5 100644 --- a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/activities/BaseStingrayActivity.java +++ b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/activities/BaseStingrayActivity.java @@ -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; @@ -38,6 +39,11 @@ public void onResume() { startStingrayClientService(); } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + @Override public void onStop() { super.onStop(); @@ -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"); diff --git a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/requesters/Requester.java b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/requesters/Requester.java index 11f4d13..81cc67c 100644 --- a/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/requesters/Requester.java +++ b/stingray-api-client/src/main/java/org/stingraymappingproject/api/clientandroid/requesters/Requester.java @@ -1,7 +1,5 @@ package org.stingraymappingproject.api.clientandroid.requesters; -import android.util.Log; - import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; @@ -37,16 +35,16 @@ public void run() { protected GsonRequest getRequest(String endpointPath, int requestMethod, Class responseClass) { if(requestMethod == Request.Method.GET) { - Log.d(TAG, "getRequest:Request.Method.GET"); +// Log.d(TAG, "getRequest:Request.Method.GET"); return new GsonRequest(requestMethod, getRequestUrlWParams(endpointPath, getRequestParams()), getRequestParams(), this, this, responseClass); } - Log.d(TAG, "getRequest:Request.Method.POST"); +// Log.d(TAG, "getRequest:Request.Method.POST"); return new GsonRequest(requestMethod, getRequestUrlForEndpoint(endpointPath), getRequestParams(), this, this, responseClass); } @Override public void onErrorResponse(VolleyError error) { - Log.d(TAG, "onErrorResponse"); +// Log.d(TAG, "onErrorResponse"); } } \ No newline at end of file diff --git a/stingray-api-client/stingray-api-client.iml b/stingray-api-client/stingray-api-client.iml index 853c2fb..d5a5181 100644 --- a/stingray-api-client/stingray-api-client.iml +++ b/stingray-api-client/stingray-api-client.iml @@ -1,5 +1,5 @@ - +