package cgeo.geocaching.maps.google.v1;

import cgeo.geocaching.maps.CachesOverlay;
import cgeo.geocaching.maps.interfaces.ItemizedOverlayImpl;
import cgeo.geocaching.maps.interfaces.MapProjectionImpl;
import cgeo.geocaching.maps.interfaces.MapViewImpl;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;

import org.eclipse.jdt.annotation.NonNull;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Google specific implementation of the itemized cache overlay
 */
public class GoogleCacheOverlay extends ItemizedOverlay<GoogleCacheOverlayItem> implements ItemizedOverlayImpl {

    @NonNull
    private final CachesOverlay base;
    @NonNull
    private final Lock lock = new ReentrantLock();

    public GoogleCacheOverlay(final Context contextIn, final Drawable markerIn) {
        super(boundCenterBottom(markerIn));
        base = new CachesOverlay(this, contextIn);
    }

    @Override
    public CachesOverlay getBase() {
        return base;
    }

    @Override
    protected GoogleCacheOverlayItem createItem(final int i) {
        return (GoogleCacheOverlayItem) base.createItem(i);
    }

    @Override
    public int size() {
        return base.size();
    }

    @Override
    protected boolean onTap(final int arg0) {
        return base.onTap(arg0);
    }

    @Override
    public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
        base.draw(canvas, castMapViewImpl(mapView), shadow);
    }

    private static MapViewImpl castMapViewImpl(final MapView mapView) {
        assert mapView instanceof MapViewImpl;
        return (MapViewImpl) mapView;
    }

    @Override
    public void superPopulate() {
        populate();
    }

    @Override
    public Drawable superBoundCenterBottom(final Drawable marker) {
        return ItemizedOverlay.boundCenterBottom(marker);
    }

    @Override
    public void superSetLastFocusedItemIndex(final int i) {
        super.setLastFocusedIndex(i);
    }

    @Override
    public boolean superOnTap(final int index) {
        return super.onTap(index);
    }

    @Override
    public void superDraw(final Canvas canvas, final MapViewImpl mapView, final boolean shadow) {
        super.draw(canvas, (MapView) mapView, shadow);
    }

    @Override
    public void superDrawOverlayBitmap(final Canvas canvas, final Point drawPosition,
            final MapProjectionImpl projection, final byte drawZoomLevel) {
        // Nothing to do here...
    }

    @Override
    public void lock() {
        lock.lock();
    }

    @Override
    public void unlock() {
        lock.unlock();
    }

    @Override
    public MapViewImpl getMapViewImpl() {
        throw new UnsupportedOperationException();
    }

}

@@ -372,22 +372,23 @@
 tation.N
-onN
 ull
+able
 ;%0A%0Aimpor
@@ -797,36 +797,266 @@
 Impl %7B%0A%0A    
-@NonNull%0A   
+/**%0A     * The super constructor already invokes methods accessing this member before it is initialized. Therefore it can be%0A     * null, although it is assigned in the constructor. Don't trust static code analysis here.%0A     */%0A    @Nullable
  private fin
@@ -1082,21 +1082,8 @@
 se;%0A
-    @NonNull%0A
     
@@ -1462,32 +1462,64 @@
 (final int i) %7B%0A
+        if (base != null) %7B%0A    
         return (
@@ -1558,24 +1558,55 @@
 ateItem(i);%0A
+        %7D%0A        return null;%0A
     %7D%0A%0A    @
@@ -1650,126 +1650,250 @@
     
-return base.size();%0A    %7D%0A%0A    @Override%0A    protected boolean onTap(final int arg0) %7B%0A        return base.onTap(arg0)
+if (base != null) %7B%0A            return base.size();%0A        %7D%0A        return 0;%0A    %7D%0A%0A    @Override%0A    protected boolean onTap(final int arg0) %7B%0A        if (base != null) %7B%0A            return base.onTap(arg0);%0A        %7D%0A        return false
 ;%0A  
@@ -1992,32 +1992,64 @@
 olean shadow) %7B%0A
+        if (base != null) %7B%0A    
         base.dra
@@ -2085,32 +2085,42 @@
 View), shadow);%0A
+        %7D%0A
     %7D%0A%0A    priva

fix #4848: NPE when starting map