Skip to content

Commit

Permalink
Added functionality and infrastructure for customizing data generators
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Dec 5, 2016
1 parent 791502d commit bb50e09
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Expand Up @@ -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<Generator> probeClass = (Class<Generator>) 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();
}
Expand Down
Expand Up @@ -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());
}
Expand Down Expand Up @@ -114,7 +117,6 @@ else if (Location.useGoogleLocationServices(me.mContext))
me.mGoogleApiClient = builder.build();
me.mGoogleApiClient.connect();
}

}
else
{
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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());
Expand Down Expand Up @@ -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...");
Expand All @@ -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();
}
}

0 comments on commit bb50e09

Please sign in to comment.