Search in sources :

Example 1 with SurveyLocationListener

use of org.eyeseetea.malariacare.layout.listeners.SurveyLocationListener in project pictureapp by EyeSeeTea.

the class ADashboardActivityStrategy method prepareLocationListener.

public void prepareLocationListener(Activity activity, Survey survey) {
    SurveyLocationListener locationListener = new SurveyLocationListener(survey.getId_survey());
    LocationManager locationManager = (LocationManager) LocationMemory.getContext().getSystemService(Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Log.d(TAG, "requestLocationUpdates via NETWORK");
        if (ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.d(TAG, "requestLocationUpdates via GPS");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    } else {
        Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (lastLocation != null) {
            Log.d(TAG, "location not available via GPS|NETWORK, last know: " + lastLocation);
            locationListener.saveLocation(lastLocation);
        } else {
            String defaultLatitude = activity.getString(R.string.GPS_LATITUDE_DEFAULT);
            String defaultLongitude = activity.getString(R.string.GPS_LONGITUDE_DEFAULT);
            Location defaultLocation = new Location(activity.getString(R.string.GPS_PROVIDER_DEFAULT));
            defaultLocation.setLatitude(Double.parseDouble(defaultLatitude));
            defaultLocation.setLongitude(Double.parseDouble(defaultLongitude));
            Log.d(TAG, "location not available via GPS|NETWORK, default: " + defaultLocation);
            locationListener.saveLocation(defaultLocation);
        }
    }
}
Also used : LocationManager(android.location.LocationManager) SurveyLocationListener(org.eyeseetea.malariacare.layout.listeners.SurveyLocationListener) Location(android.location.Location)

Aggregations

Location (android.location.Location)1 LocationManager (android.location.LocationManager)1 SurveyLocationListener (org.eyeseetea.malariacare.layout.listeners.SurveyLocationListener)1