Search in sources :

Example 1 with EmptyLocationException

use of org.eyeseetea.malariacare.domain.exception.EmptyLocationException in project pictureapp by EyeSeeTea.

the class PushClient method prepareMetadata.

/**
 * Adds metadata info to json object
 *
 * @return JSONObject with progra, orgunit, eventdate and so on...
 */
private JSONObject prepareMetadata() throws EmptyLocationException, JSONException, ApiCallException {
    Log.d(TAG, "prepareMetadata for survey: " + survey.getId_survey());
    JSONObject object = new JSONObject();
    object.put(TAG_PROGRAM, survey.getProgram().getUid());
    object.put(TAG_ORG_UNIT, ServerAPIController.getOrgUnitUID());
    object.put(TAG_EVENTDATE, android.text.format.DateFormat.format("yyyy-MM-dd", survey.getEventDate()));
    object.put(TAG_STATUS, COMPLETED);
    object.put(TAG_STOREDBY, survey.getUser().getName());
    // TODO: put it in the object.
    Location lastLocation = LocationMemory.get(survey.getId_survey());
    // If there is no location (location is required) -> exception
    if (lastLocation == null) {
        throw new EmptyLocationException(activity.getString(R.string.dialog_error_push_no_location));
    }
    object.put(TAG_COORDINATE, prepareCoordinates(lastLocation));
    Log.d(TAG, "prepareMetadata: " + object.toString());
    return object;
}
Also used : JSONObject(org.json.JSONObject) EmptyLocationException(org.eyeseetea.malariacare.domain.exception.EmptyLocationException) Location(android.location.Location)

Example 2 with EmptyLocationException

use of org.eyeseetea.malariacare.domain.exception.EmptyLocationException 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 = null;
    try {
        locationManager = (LocationManager) LocationMemory.getContext().getSystemService(Context.LOCATION_SERVICE);
    } catch (NullPointerException e) {
        new EmptyLocationException(e);
    }
    if (locationManager == null) {
        saveDefaultLocationIfIsNotAvailable(activity, locationListener);
        return;
    }
    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) {
            saveDefaultLocationIfIsNotAvailable(activity, locationListener);
            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 {
            saveDefaultLocationIfIsNotAvailable(activity, locationListener);
        }
    }
}
Also used : LocationManager(android.location.LocationManager) EmptyLocationException(org.eyeseetea.malariacare.domain.exception.EmptyLocationException) SurveyLocationListener(org.eyeseetea.malariacare.layout.listeners.SurveyLocationListener) Location(android.location.Location)

Example 3 with EmptyLocationException

use of org.eyeseetea.malariacare.domain.exception.EmptyLocationException in project pictureapp by EyeSeeTea.

the class PushClient method push.

public PushResult push() {
    try {
        JSONObject data = prepareMetadata();
        data = prepareDataElements(data);
        PushResult result = new PushResult(pushData(data));
        if (result.isSuccessful()) {
            updateSurveyState();
        }
        return result;
    } catch (EmptyLocationException ex) {
        new ApiCallException(ex);
        return new PushResult(ex);
    } catch (JSONException ex) {
        new ApiCallException(ex);
        return new PushResult(ex);
    } catch (ApiCallException ex) {
        return new PushResult(ex);
    }
}
Also used : JSONObject(org.json.JSONObject) EmptyLocationException(org.eyeseetea.malariacare.domain.exception.EmptyLocationException) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException)

Aggregations

EmptyLocationException (org.eyeseetea.malariacare.domain.exception.EmptyLocationException)3 Location (android.location.Location)2 JSONObject (org.json.JSONObject)2 LocationManager (android.location.LocationManager)1 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)1 SurveyLocationListener (org.eyeseetea.malariacare.layout.listeners.SurveyLocationListener)1 JSONException (org.json.JSONException)1