Skip to content

Commit

Permalink
Abstracting relevant functions to abstract parent class. getodk#507
Browse files Browse the repository at this point in the history
  • Loading branch information
MukundAnanthu committed Jun 2, 2017
1 parent 7dd65cb commit 0c45fcc
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 202 deletions.
Expand Up @@ -20,10 +20,8 @@
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
Expand All @@ -44,9 +42,6 @@
import org.odk.collect.android.utilities.InfoLogger;
import org.odk.collect.android.utilities.ToastUtils;
import org.odk.collect.android.widgets.GeoPointWidget;
import java.text.DecimalFormat;
import java.util.List;

import timber.log.Timber;

/**
Expand All @@ -56,8 +51,8 @@
* @author guisalmon@gmail.com
* @author jonnordling@gmail.com
*/
public class GeoPointGoogleMapActivity extends GeoPointMapActivity implements LocationListener,
OnMarkerDragListener, OnMapLongClickListener {
public class GeoPointGoogleMapActivity extends GeoPointMapActivity implements OnMarkerDragListener,
OnMapLongClickListener {

private GoogleMap map;
private MarkerOptions markerOptions;
Expand Down Expand Up @@ -91,19 +86,6 @@ public void onMapReady(GoogleMap googleMap) {
});
}

@Override
protected void onStart() {
super.onStart();
Collect.getInstance().getActivityLogger().logOnStart(this);
}

@Override
protected void onStop() {
Collect.getInstance().getActivityLogger().logOnStop(this);
super.onStop();
}


private void returnLocation() {
Intent i = new Intent();
if (setClear || (readOnly && latLng == null)) {
Expand All @@ -129,19 +111,6 @@ private void returnLocation() {
finish();
}


private String truncateFloat(float f) {
return new DecimalFormat("#.##").format(f);
}

@Override
protected void onPause() {
super.onPause();
if (locationManager != null) {
locationManager.removeUpdates(this);
}
}

private void setupMap(GoogleMap googleMap) {
map = googleMap;
if (map == null) {
Expand Down Expand Up @@ -308,36 +277,7 @@ public void onClick(View v) {
upMyLocationOverlayLayers();
}

private void upMyLocationOverlayLayers() {
// make sure we have a good location provider before continuing
locationCountNum = 0;
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
if (provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER)) {
gpsOn = true;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationCountFoundLimit = 0;
} else if (provider.equalsIgnoreCase(LocationManager.NETWORK_PROVIDER)) {
// Only if GPS Provider is not available use network location. bug (well know
// android bug) http://stackoverflow
// .com/questions/6719207/locationmanager-returns-old-cached-wifi-location-with
// -current-timestamp
networkOn = true;
locationCountFoundLimit =
1; // increase count due to network location bug (well know android bug)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
}
}
//showLocationButton.setClickable(marker != null);
if (!gpsOn && !networkOn) {
showGPSDisabledAlertToUser();
} else {
overlayMyLocationLayers();
}
}

private void overlayMyLocationLayers() {
protected void overlayMyLocationLayers() {
if (draggable & !readOnly) {
map.setOnMarkerDragListener(this);
map.setOnMapLongClickListener(this);
Expand Down Expand Up @@ -386,21 +326,6 @@ public void onLocationChanged(Location location) {

}


@Override
public void onProviderDisabled(String provider) {
}


@Override
public void onProviderEnabled(String provider) {
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onMarkerDrag(Marker arg0) {

Expand Down Expand Up @@ -438,14 +363,14 @@ public void onMapLongClick(LatLng latLng) {
captureLocation = true;
}

private void zoomToLocation() {
protected void zoomToLocation() {
LatLng here = new LatLng(location.getLatitude(), location.getLongitude());
if (location != null) {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(here, 16));
}
}

private void zoomToPoint() {
protected void zoomToPoint() {
if (latLng != null) {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 16));
}
Expand Down Expand Up @@ -497,24 +422,5 @@ public void onCancel(DialogInterface dialog) {

}

private void showGPSDisabledAlertToUser() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(getString(R.string.gps_enable_message))
.setCancelable(false)
.setPositiveButton(getString(R.string.enable_gps),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivityForResult(
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
});
alertDialogBuilder.setNegativeButton(getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

}
Expand Up @@ -12,26 +12,32 @@
* the License.
*/




package org.odk.collect.android.activities;

import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.widget.Button;
import android.widget.TextView;
import android.app.AlertDialog;
import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.spatial.MapHelper;
import android.view.View;
import java.text.DecimalFormat;
import java.util.List;

/**
* Abstracts the functionalities present in GeoPointGoogleMapActivity and GeoPointOsmMapActivity.
* @author mukund.code@gmail.com (Mukund Ananthu)
*/

public abstract class GeoPointMapActivity extends FragmentActivity {
public abstract class GeoPointMapActivity extends FragmentActivity implements LocationListener {

protected static final String LOCATION_COUNT = "locationCount";
protected TextView locationStatus;
Expand All @@ -58,4 +64,102 @@ public abstract class GeoPointMapActivity extends FragmentActivity {
protected Boolean intentDraggable = false;
protected Boolean locationFromIntent = false;

@Override
protected void onPause() {
super.onPause();
if (locationManager != null) {
locationManager.removeUpdates(this);
}
}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
protected void onStart() {
super.onStart();
Collect.getInstance().getActivityLogger().logOnStart(this);
}

@Override
protected void onStop() {
Collect.getInstance().getActivityLogger().logOnStop(this);
super.onStop();
}

protected String truncateFloat(float f) {
return new DecimalFormat("#.##").format(f);
}

protected void upMyLocationOverlayLayers() {
// make sure we have a good location provider before continuing
locationCountNum = 0;
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
if (provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER)) {
gpsOn = true;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationCountFoundLimit = 0;
} else if (provider.equalsIgnoreCase(LocationManager.NETWORK_PROVIDER)) {
// Only if GPS Provider is not available use network location. bug (well know
// android bug) http://stackoverflow
// .com/questions/6719207/locationmanager-returns-old-cached-wifi-location-with
// -current-timestamp
networkOn = true;
locationCountFoundLimit =
1; // increase count due to network location bug (well know android bug)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
}
}
//showLocationButton.setClickable(marker != null);
if (!gpsOn && !networkOn) {
showGPSDisabledAlertToUser();
} else {
overlayMyLocationLayers();
}
}

protected abstract void overlayMyLocationLayers();

protected void showGPSDisabledAlertToUser() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(getString(R.string.gps_enable_message))
.setCancelable(false)
.setPositiveButton(getString(R.string.enable_gps),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivityForResult(
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
});
alertDialogBuilder.setNegativeButton(getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

abstract public void onLocationChanged(Location location);

abstract protected void zoomToLocation();

abstract protected void zoomToPoint();

abstract public void showZoomDialog();

}

0 comments on commit 0c45fcc

Please sign in to comment.