Skip to content

Commit

Permalink
Merge pull request #169 from shankari/fix_location_services_toggle
Browse files Browse the repository at this point in the history
Fix location services tracking toggle on android
  • Loading branch information
shankari authored Mar 4, 2019
2 parents ee16139 + 77d9aa0 commit 2dce243
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 72 deletions.
4 changes: 3 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="edu.berkeley.eecs.emission.cordova.datacollection"
version="1.1.0">
version="1.2.0">

<name>DataCollection</name>
<description>Background data collection FTW! This is the part that I really
Expand Down Expand Up @@ -72,6 +72,8 @@
<action android:name="local.transition.stopped_moving"></action>
<action android:name="local.transition.stop_tracking"></action>
<action android:name="local.transition.start_tracking"></action>
<action android:name="local.transition.tracking_error"></action>
<action android:name="android.location.MODE_CHANGED"></action>
</intent-filter>
</receiver>
<service
Expand Down
56 changes: 55 additions & 1 deletion src/android/DataCollectionPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
import org.json.JSONObject;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import edu.berkeley.eecs.emission.*;
import edu.berkeley.eecs.emission.cordova.tracker.location.TripDiaryStateMachineService;
import edu.berkeley.eecs.emission.cordova.tracker.wrapper.ConsentConfig;
import edu.berkeley.eecs.emission.cordova.tracker.wrapper.LocationTrackingConfig;
import edu.berkeley.eecs.emission.cordova.tracker.location.TripDiaryStateMachineReceiver;
Expand All @@ -31,7 +36,8 @@
import edu.berkeley.eecs.emission.cordova.usercache.BuiltinUserCache;

public class DataCollectionPlugin extends CordovaPlugin {
public static String TAG = "DataCollectionPlugin";
public static final String TAG = "DataCollectionPlugin";
public static final int ENABLE_LOCATION_CODE = 362253;

@Override
public void pluginInitialize() {
Expand Down Expand Up @@ -135,4 +141,52 @@ private static Map<String, String> getTransitionMap(Context ctxt) {
retVal.put("START_TRACKING", ctxt.getString(R.string.transition_start_tracking));
return retVal;
}

@Override
public void onNewIntent(Intent intent) {
Log.d(cordova.getActivity(), TAG, "onNewIntent(" + intent.getDataString() + ")");
Log.d(cordova.getActivity(), TAG, "Found extras " + intent.getExtras());
PendingIntent piFromIntent = intent.getParcelableExtra(NotificationHelper.RESOLUTION_PENDING_INTENT_KEY);
if (piFromIntent != null) {
try {
// cordova.setActivityResultCallback(this);
cordova.getActivity().startIntentSenderForResult(piFromIntent.getIntentSender(), ENABLE_LOCATION_CODE, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
NotificationHelper.createNotification(cordova.getActivity(), Constants.TRACKING_ERROR_ID, "Unable to resolve issue");
}
}
}

/*
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(cordova.getActivity(), TAG, "received onActivityResult("+requestCode+","+
resultCode+","+data.getDataString()+")");
switch (requestCode) {
case ENABLE_LOCATION_CODE:
Activity mAct = cordova.getActivity();
Log.d(mAct, TAG, requestCode + " is our code, handling callback");
cordova.setActivityResultCallback(null);
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
Log.i(cordova.getActivity(), TAG, "All changes successfully made, reinitializing");
NotificationHelper.cancelNotification(mAct, Constants.TRACKING_ERROR_ID);
TripDiaryStateMachineService.restartFSMIfStartState(mAct);
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
Log.e(cordova.getActivity(), TAG, "User chose not to change settings, dunno what to do");
break;
default:
break;
}
break;
default:
Log.d(cordova.getActivity(), TAG, "Got unsupported request code "+requestCode+ " , ignoring...");
}
}
*/

}
3 changes: 1 addition & 2 deletions src/android/location/GeofenceExitIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ protected void onHandleIntent(Intent intent) {
// https://github.com/e-mission/e-mission-data-collection/issues/128#issuecomment-250304943
if (parsedEvent.hasError()) {
Log.i(this, TAG, "Found error "+parsedEvent.getErrorCode()+
"moving to start state");
sendBroadcast(new Intent(getString(R.string.transition_tracking_error)));
"will be handled by MODE_CHANGE transition");
} else {
Log.i(this, TAG, "Got event with transition = "+parsedEvent.getGeofenceTransition()+
" but hasError = false, ignoring");
Expand Down
6 changes: 5 additions & 1 deletion src/android/location/TripDiaryStateMachineReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.preference.PreferenceManager;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;

import org.apache.cordova.ConfigXmlParser;
import org.json.JSONException;
Expand All @@ -21,6 +23,7 @@

import edu.berkeley.eecs.emission.BuildConfig;
import edu.berkeley.eecs.emission.R;

import edu.berkeley.eecs.emission.cordova.tracker.ConfigManager;
import edu.berkeley.eecs.emission.cordova.tracker.sensors.BatteryUtils;
import edu.berkeley.eecs.emission.cordova.tracker.wrapper.Battery;
Expand Down Expand Up @@ -76,7 +79,8 @@ public void onReceive(Context context, Intent intent) {
context.getString(R.string.transition_stopped_moving),
context.getString(R.string.transition_stop_tracking),
context.getString(R.string.transition_start_tracking),
context.getString(R.string.transition_tracking_error)
context.getString(R.string.transition_tracking_error),
LocationManager.MODE_CHANGED_ACTION
}));

if (!validTransitions.contains(intent.getAction())) {
Expand Down
Loading

0 comments on commit 2dce243

Please sign in to comment.