-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for fused location provider
- Loading branch information
Showing
9 changed files
with
550 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
app/android/src/uk/co/lutraconsulting/MMAndroidPosition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package uk.co.lutraconsulting; | ||
|
||
import android.Manifest; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.location.GnssStatus; | ||
import android.location.Location; | ||
import android.location.LocationListener; | ||
import android.location.LocationManager; | ||
import android.os.Looper; | ||
import android.os.Handler; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.core.app.ActivityCompat; | ||
|
||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.GoogleApiAvailability; | ||
import com.google.android.gms.location.FusedLocationProviderClient; | ||
import com.google.android.gms.location.LocationCallback; | ||
import com.google.android.gms.location.LocationRequest; | ||
import com.google.android.gms.location.LocationResult; | ||
import com.google.android.gms.location.LocationServices; | ||
import com.google.android.gms.location.Priority; | ||
|
||
|
||
|
||
public class MMAndroidPosition { | ||
|
||
static public abstract class Callback { | ||
public void onPositionChanged(@NonNull Location location, GnssStatus gnssStatus) { | ||
} | ||
} | ||
|
||
private static native void jniOnPositionUpdated(int instanceId, Location location, GnssStatus gnssStatus); | ||
|
||
static public MMAndroidPosition createWithJniCallback(Context context, boolean useFused, int instanceId) { | ||
Log.i("CPP", "[java] createWithJniCallback"); | ||
|
||
MMAndroidPosition.Callback callback = new MMAndroidPosition.Callback() { | ||
@Override | ||
public void onPositionChanged(@NonNull Location location, GnssStatus gnssStatus) { | ||
jniOnPositionUpdated(instanceId, location, gnssStatus); | ||
} | ||
}; | ||
|
||
return new MMAndroidPosition(context, callback, useFused); | ||
} | ||
|
||
private final Context mContext; | ||
private final LocationManager mLocationManager; | ||
private final boolean mUseFused; | ||
private FusedLocationProviderClient mFusedLocationClient = null; | ||
private final LocationCallback mLocationCallback; | ||
private final LocationListener mLocationManagerCallback; | ||
private final GnssStatus.Callback mGnssStatusCallback; | ||
private final MMAndroidPosition.Callback mClientCallback; | ||
private boolean mFusedAvailable = false; | ||
private boolean mGpsProviderAvailable = false; | ||
private boolean mIsStarted = false; | ||
private String mErrorMessage; | ||
private GnssStatus mLastGnssStatus; | ||
|
||
public MMAndroidPosition(Context context, MMAndroidPosition.Callback clientCallback, boolean useFused) { | ||
mContext = context; | ||
mClientCallback = clientCallback; | ||
mUseFused = useFused; | ||
|
||
Log.i("CPP", "[java] constructor!"); | ||
|
||
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | ||
|
||
if (mUseFused) { | ||
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); | ||
mFusedAvailable = googleApiAvailability.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS; | ||
Log.i("CPP", "[java] fused available: " + mFusedAvailable); | ||
if (mFusedAvailable) { | ||
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context); | ||
} | ||
} else { | ||
mGpsProviderAvailable = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); | ||
Log.i("CPP", "[java] gps provider available: " + mGpsProviderAvailable); | ||
} | ||
|
||
mLocationCallback = new LocationCallback() { | ||
@Override | ||
public void onLocationResult(@NonNull LocationResult locationResult) { | ||
for (Location location : locationResult.getLocations()) { | ||
Log.i("CPP", "[java] FLP " + location.getLatitude() + " " + location.getLongitude()); | ||
|
||
// call the native function! | ||
mClientCallback.onPositionChanged(location, mLastGnssStatus); | ||
} | ||
} | ||
}; | ||
|
||
mGnssStatusCallback = new GnssStatus.Callback() { | ||
@Override | ||
public void onSatelliteStatusChanged(@NonNull GnssStatus status) { | ||
//Log.v("LOC", "GNSS Status: " + status.getSatelliteCount() + " satellites."); | ||
|
||
// store the satellite info | ||
mLastGnssStatus = status; | ||
} | ||
}; | ||
|
||
mLocationManagerCallback = new LocationListener() { | ||
@Override | ||
public void onLocationChanged(@NonNull Location location) { | ||
Log.i("CPP", "[java] GPS " + location.getLatitude() + " " + location.getLongitude()); | ||
|
||
mClientCallback.onPositionChanged(location, mLastGnssStatus); | ||
} | ||
}; | ||
|
||
Log.i("CPP", "[java] constructor end"); | ||
|
||
} | ||
|
||
public String errorMessage() { | ||
return mErrorMessage; | ||
} | ||
|
||
public boolean start() { | ||
Log.i("CPP", "[java] start()"); | ||
|
||
if (mIsStarted) | ||
return false; | ||
|
||
Log.e("CPP", "[java] here 0"); | ||
|
||
if (mUseFused && !mFusedAvailable) { | ||
mErrorMessage = "FUSED_NOT_AVAILABLE"; | ||
Log.e("CPP", "[java] FUSED_NOT_AVAILABLE"); | ||
return false; | ||
} | ||
|
||
if (!mUseFused && !mGpsProviderAvailable) { | ||
mErrorMessage = "GPS_NOT_AVAILABLE"; | ||
Log.e("CPP", "[java] GPS_NOT_AVAILABLE"); | ||
return false; | ||
} | ||
|
||
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && | ||
ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | ||
mErrorMessage = "MISSING_PERMISSIONS"; | ||
Log.e("CPP", "[java] MISSING_PERMISSIONS"); | ||
return false; | ||
} | ||
|
||
Log.e("CPP", "[java] here 1"); | ||
|
||
if (mUseFused) { | ||
LocationRequest locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000).build(); | ||
|
||
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.getMainLooper()); | ||
} | ||
else { | ||
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 0.F, mLocationManagerCallback, Looper.getMainLooper()); | ||
Log.e("CPP", "[java] here 2"); | ||
} | ||
|
||
mLocationManager.registerGnssStatusCallback(mGnssStatusCallback, new Handler(Looper.getMainLooper())); | ||
|
||
Log.i("CPP", "[java] started!"); | ||
|
||
mIsStarted = true; | ||
return true; | ||
} | ||
|
||
public boolean stop() { | ||
Log.i("CPP", "[java] stop()"); | ||
|
||
if (!mIsStarted) | ||
return false; | ||
|
||
if (mUseFused) { | ||
mFusedLocationClient.removeLocationUpdates(mLocationCallback); | ||
} else { | ||
mLocationManager.removeUpdates(mLocationManagerCallback); | ||
} | ||
|
||
mLocationManager.unregisterGnssStatusCallback(mGnssStatusCallback); | ||
|
||
Log.i("CPP", "[java] stopped!"); | ||
|
||
mIsStarted = false; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.