Skip to content

Commit

Permalink
Implement GeoShapeOsmMapActivity in terms of the MapFragment interfac…
Browse files Browse the repository at this point in the history
…e, and provide an OSM implementation of MapFragment.
  • Loading branch information
zestyping committed Aug 18, 2018
1 parent 9fb47e3 commit 76cd7f8
Show file tree
Hide file tree
Showing 9 changed files with 1,169 additions and 505 deletions.
3 changes: 3 additions & 0 deletions collect_app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ the specific language governing permissions and limitations under the License.
<activity
android:name=".activities.GeoPointOsmMapActivity"
android:configChanges="orientation" />
<activity
android:name=".activities.GeoShapeOldOsmMapActivity"
android:configChanges="orientation" />
<activity
android:name=".activities.GeoShapeOsmMapActivity"
android:configChanges="orientation" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class GeoShapeGoogleMapActivity extends CollectAbstractActivity {
private View zoomDialogView;
private Button zoomPointButton;
private Button zoomLocationButton;
private boolean foundFirstLocation;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -65,10 +64,11 @@ public void onCreate(Bundle savedInstanceState) {
}

requestWindowFeature(Window.FEATURE_NO_TITLE);
setTitle(getString(R.string.geoshape_title));
setContentView(R.layout.geoshape_layout);

// TODO(ping): Remove when we're ready to use this class.
((TextView) findViewById(R.id.top_text)).setText("new GeoShapeActivity");
((TextView) findViewById(R.id.top_text)).setText("new Google GeoShapeActivity");

createMapFragment().addTo(this, R.id.map_container, this::setupMap);
}
Expand Down Expand Up @@ -100,10 +100,9 @@ private void setupMap(MapFragment newMapFragment) {

map = newMapFragment;
map.setGpsLocationEnabled(true);
map.setGpsLocationListener(this::onLocationFix);
map.setLongPressListener(this::addVertex);

helper = new MapHelper(this, newMapFragment);
helper = new MapHelper(this, map);

gpsButton = findViewById(R.id.gps);
gpsButton.setOnClickListener(v -> showZoomDialog());
Expand All @@ -114,6 +113,9 @@ private void setupMap(MapFragment newMapFragment) {
ImageButton saveButton = findViewById(R.id.save);
saveButton.setOnClickListener(v -> finishWithResult());

ImageButton layersButton = findViewById(R.id.layers);
layersButton.setOnClickListener(v -> helper.showLayersDialog());

List<MapPoint> points = new ArrayList<>();
Intent intent = getIntent();
if (intent != null && intent.hasExtra(GeoShapeWidget.SHAPE_LOCATION)) {
Expand All @@ -123,14 +125,21 @@ private void setupMap(MapFragment newMapFragment) {
gpsButton.setEnabled(!points.isEmpty());
clearButton.setEnabled(!points.isEmpty());

ImageButton layersButton = findViewById(R.id.layers);
layersButton.setOnClickListener(v -> helper.showLayersDialog());
if (!points.isEmpty()) {
map.zoomToBoundingBox(points, 0.8);
} else {
map.runOnGpsLocationReady(this::onGpsLocationReady);
}

zoomDialogView = getLayoutInflater().inflate(R.layout.geo_zoom_dialog, null);

zoomLocationButton = zoomDialogView.findViewById(R.id.zoom_location);
zoomLocationButton.setOnClickListener(v -> {
map.setCenter(map.getGpsLocation());
MapPoint location = map.getGpsLocation();
if (location != null) {
map.setCenter(location);
map.setZoom(15);
}
zoomDialog.dismiss();
});

Expand All @@ -142,14 +151,42 @@ private void setupMap(MapFragment newMapFragment) {

// If there is a last know location go there
if (hasWindowFocus() && map.getGpsLocation() != null) {
foundFirstLocation = true;
// foundFirstLocation = true;
gpsButton.setEnabled(true);
showZoomDialog();
}

helper.setBasemap();
}

private void onGpsLocationReady(MapFragment map) {
gpsButton.setEnabled(true);
if (hasWindowFocus()) {
showZoomDialog();
}
}

private void addVertex(MapPoint point) {
map.appendPointToShape(shapeId, point);
clearButton.setEnabled(true);
}

private void clear() {
map.clearFeatures();
shapeId = map.addDraggableShape(new ArrayList<>());
clearButton.setEnabled(false);
}

private void showClearDialog() {
if (!map.getPointsOfShape(shapeId).isEmpty()) {
new AlertDialog.Builder(this)
.setMessage(R.string.geo_clear_warning)
.setPositiveButton(R.string.clear, (dialog, id) -> clear())
.setNegativeButton(R.string.cancel, null)
.show();
}
}

private void finishWithResult() {
List<MapPoint> points = map.getPointsOfShape(shapeId);
if (points.size() < 3) {
Expand Down Expand Up @@ -209,36 +246,6 @@ private String formatPoints(List<MapPoint> points) {
return result;
}

private void onLocationFix(MapPoint point) {
gpsButton.setEnabled(true);
if (hasWindowFocus() && !foundFirstLocation) {
foundFirstLocation = true;
showZoomDialog();
}
}

private void addVertex(MapPoint point) {
map.appendPointToShape(shapeId, point);
clearButton.setEnabled(true);
}

private void clear() {
map.clearFeatures();
shapeId = map.addDraggableShape(new ArrayList<>());
map.setLongPressListener(this::addVertex);
clearButton.setEnabled(false);
}

private void showClearDialog() {
if (!map.getPointsOfShape(shapeId).isEmpty()) {
new AlertDialog.Builder(this)
.setMessage(getString(R.string.geo_clear_warning))
.setPositiveButton(getString(R.string.clear), (dialog, id) -> clear())
.setNegativeButton(R.string.cancel, null)
.show();
}
}

public void showZoomDialog() {
if (zoomDialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Expand All @@ -259,28 +266,25 @@ public void onCancel(DialogInterface dialog) {
zoomDialog = builder.create();
}

if (zoomLocationButton != null) {
if (map.getGpsLocation() != null) {
zoomLocationButton.setEnabled(true);
zoomLocationButton.setBackgroundColor(Color.parseColor("#50cccccc"));
zoomLocationButton.setTextColor(themeUtils.getPrimaryTextColor());
} else {
zoomLocationButton.setEnabled(false);
zoomLocationButton.setBackgroundColor(Color.parseColor("#50e2e2e2"));
zoomLocationButton.setTextColor(Color.parseColor("#FF979797"));
}

if (!map.getPointsOfShape(shapeId).isEmpty()) {
zoomPointButton.setEnabled(true);
zoomPointButton.setBackgroundColor(Color.parseColor("#50cccccc"));
zoomPointButton.setTextColor(themeUtils.getPrimaryTextColor());
} else {
zoomPointButton.setEnabled(false);
zoomPointButton.setBackgroundColor(Color.parseColor("#50e2e2e2"));
zoomPointButton.setTextColor(Color.parseColor("#FF979797"));
}
if (map.getGpsLocation() != null) {
zoomLocationButton.setEnabled(true);
zoomLocationButton.setBackgroundColor(Color.parseColor("#50cccccc"));
zoomLocationButton.setTextColor(themeUtils.getPrimaryTextColor());
} else {
zoomLocationButton.setEnabled(false);
zoomLocationButton.setBackgroundColor(Color.parseColor("#50e2e2e2"));
zoomLocationButton.setTextColor(Color.parseColor("#FF979797"));
}

if (!map.getPointsOfShape(shapeId).isEmpty()) {
zoomPointButton.setEnabled(true);
zoomPointButton.setBackgroundColor(Color.parseColor("#50cccccc"));
zoomPointButton.setTextColor(themeUtils.getPrimaryTextColor());
} else {
zoomPointButton.setEnabled(false);
zoomPointButton.setBackgroundColor(Color.parseColor("#50e2e2e2"));
zoomPointButton.setTextColor(Color.parseColor("#FF979797"));
}
zoomDialog.show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.geoshape_layout);

// TODO(ping): Remove when we're ready to use the new class.
((TextView) findViewById(R.id.top_text)).setText("old GeoShapeActivity");
((TextView) findViewById(R.id.top_text)).setText("old Google GeoShapeActivity");

SupportMapFragment mapFragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
Expand Down
Loading

0 comments on commit 76cd7f8

Please sign in to comment.