From bb50e09c088990901259d0125c9af44eab2de24b Mon Sep 17 00:00:00 2001 From: "Chris J. Karr" Date: Sun, 4 Dec 2016 23:35:20 -0600 Subject: [PATCH] Added functionality and infrastructure for customizing data generators --- .../generators/Generators.java | 27 +++++++++++++++++ .../generators/device/Location.java | 30 ++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/com/audacious_software/passive_data_kit/generators/Generators.java b/src/com/audacious_software/passive_data_kit/generators/Generators.java index fb9e412..c48087b 100755 --- a/src/com/audacious_software/passive_data_kit/generators/Generators.java +++ b/src/com/audacious_software/passive_data_kit/generators/Generators.java @@ -240,6 +240,33 @@ public void broadcastLatestDataPoints() { } } + public Generator getGenerator(String className) { + Log.e("BB", "GENERATOR FIND START"); + for (String name : this.mActiveGenerators) { + Log.e("BB", "GENERATOR NAME: " + name); + } + Log.e("BB", "GENERATOR FIND END"); + + if (this.mActiveGenerators.contains(className)) { + try { + Class probeClass = (Class) Class.forName(className); + + Method getInstance = probeClass.getDeclaredMethod("getInstance", Context.class); + return (Generator) getInstance.invoke(null, this.mContext); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + return null; + } + private static class GeneratorsHolder { public static Generators instance = new Generators(); } diff --git a/src/com/audacious_software/passive_data_kit/generators/device/Location.java b/src/com/audacious_software/passive_data_kit/generators/device/Location.java index 814c5ad..5b3abf4 100755 --- a/src/com/audacious_software/passive_data_kit/generators/device/Location.java +++ b/src/com/audacious_software/passive_data_kit/generators/device/Location.java @@ -73,8 +73,11 @@ public class Location extends Generator implements GoogleApiClient.ConnectionCal private static Location sInstance = null; private GoogleApiClient mGoogleApiClient = null; private android.location.Location mLastLocation = null; + private long mUpdateInterval = 60000; public static Location getInstance(Context context) { + Log.e("BB", "SHARED LOCATION INSTANCE: " + Location.sInstance); + if (Location.sInstance == null) { Location.sInstance = new Location(context.getApplicationContext()); } @@ -114,7 +117,6 @@ else if (Location.useGoogleLocationServices(me.mContext)) me.mGoogleApiClient = builder.build(); me.mGoogleApiClient.connect(); } - } else { @@ -130,6 +132,13 @@ else if (Location.useGoogleLocationServices(me.mContext)) Generators.getInstance(this.mContext).registerCustomViewClass(Location.GENERATOR_IDENTIFIER, Location.class); } + private void stopGenerator() { + if (this.mGoogleApiClient != null) { + this.mGoogleApiClient.disconnect(); + this.mGoogleApiClient = null; + } + } + public static boolean useGoogleLocationServices(Context context) { SharedPreferences prefs = Generators.getInstance(context).getSharedPreferences(context); @@ -206,13 +215,15 @@ public void run() { return actions; } - @Override public void onConnected(Bundle bundle) { final LocationRequest request = new LocationRequest(); request.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); - request.setInterval(60000); + Log.e("BB", "USING INTERVAL: " + this.mUpdateInterval); + + request.setFastestInterval(this.mUpdateInterval); + request.setInterval(this.mUpdateInterval); if (this.mGoogleApiClient != null && this.mGoogleApiClient.isConnected()) { if (ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { @@ -223,6 +234,8 @@ public void onConnected(Bundle bundle) { @Override public void onConnectionSuspended(int i) { + Log.e("BB", "DISCONNECTING"); + if (this.mGoogleApiClient != null && this.mGoogleApiClient.isConnected()) LocationServices.FusedLocationApi.removeLocationUpdates(this.mGoogleApiClient, this); } @@ -239,6 +252,8 @@ public void onLocationChanged(android.location.Location location) { long now = System.currentTimeMillis(); + Log.e("BB", "LOCATION UPDATE"); + Bundle bundle = new Bundle(); bundle.putDouble(Location.LATITUDE_KEY, location.getLatitude()); @@ -380,7 +395,7 @@ public android.location.Location getLastKnownLocation() { android.location.Location last = null; - if (ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && + if (ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Log.e("FC", "LOCATION PERMISSIONS GRANTED..."); @@ -402,4 +417,11 @@ public android.location.Location getLastKnownLocation() { public static void broadcastLatestDataPoint(Context context) { Generators.getInstance(context).transmitData(Location.GENERATOR_IDENTIFIER, new Bundle()); } + + public void setUpdateInterval(long interval) { + this.mUpdateInterval = interval; + + this.stopGenerator(); + this.startGenerator(); + } }