// Path: mapsforge-samples-android/src/main/java/org/mapsforge/samples/android/Utils.java
// public final class Utils {
//     /**
//      * Compatibility method.
//      *
//      * @param a the current activity
//      */
//     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
//     public static void enableHome(Activity a) {
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//             // Show the Up button in the action bar.
//             a.getActionBar().setDisplayHomeAsUpEnabled(true);
//         }
//     }
// 
//     /**
//      * Compatibility method.
//      *
//      * @param view       the view to set the background on
//      * @param background the background
//      */
//     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
//     @SuppressWarnings("deprecation")
//     public static void setBackground(View view, Drawable background) {
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//             view.setBackground(background);
//         } else {
//             view.setBackgroundDrawable(background);
//         }
//     }
// 
//     static Marker createMarker(Context c, int resourceIdentifier,
//                                LatLong latLong) {
//         Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(c.getResources(), resourceIdentifier));
//         return new Marker(latLong, bitmap, 0, -bitmap.getHeight() / 2);
//     }
// 
//     static Paint createPaint(int color, int strokeWidth, Style style) {
//         Paint paint = AndroidGraphicFactory.INSTANCE.createPaint();
//         paint.setColor(color);
//         paint.setStrokeWidth(strokeWidth);
//         paint.setStyle(style);
//         return paint;
//     }
// 
//     static Marker createTappableMarker(final Context c, int resourceIdentifier,
//                                        LatLong latLong) {
//         Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(c.getResources(), resourceIdentifier));
//         bitmap.incrementRefCount();
//         return new Marker(latLong, bitmap, 0, -bitmap.getHeight() / 2) {
//             @Override
//             public boolean onTap(LatLong geoPoint, Point viewPosition,
//                                  Point tapPoint) {
//                 if (contains(viewPosition, tapPoint)) {
//                     Toast.makeText(c,
//                             "The Marker was tapped " + geoPoint.toString(),
//                             Toast.LENGTH_SHORT).show();
//                     return true;
//                 }
//                 return false;
//             }
//         };
//     }
// 
//     @SuppressWarnings("deprecation")
//     public static Bitmap viewToBitmap(Context c, View view) {
//         view.measure(MeasureSpec.getSize(view.getMeasuredWidth()),
//                 MeasureSpec.getSize(view.getMeasuredHeight()));
//         view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//         view.setDrawingCacheEnabled(true);
//         Drawable drawable = new BitmapDrawable(c.getResources(),
//                 android.graphics.Bitmap.createBitmap(view.getDrawingCache()));
//         view.setDrawingCacheEnabled(false);
//         return AndroidGraphicFactory.convertToBitmap(drawable);
//     }
// 
//     private Utils() {
//         throw new IllegalStateException();
//     }
// 
// }


import java.util.Map;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.model.DisplayModel;
import org.mapsforge.samples.android.R;
import org.mapsforge.samples.android.Utils;
import java.util.HashMap;
/*
 * Copyright 2009 Huan Erdao
 * Copyright 2014 Martin Vennekamp
 * Copyright 2015 mapsforge.org
 *
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.mapsforge.samples.android.cluster;




/**
 * Utility Class to handle MarkerBitmap
 * it handles grid offset to display on the map with offset
 */
public class MarkerBitmap {

    private static Map<String, Bitmap> captionViews = new HashMap<String, Bitmap>();
    private static Context context;
    /**
     * bitmap object for normal state icon
     */
    protected final Bitmap iconBmpNormal;
    /**
     * bitmap object for select state icon
     */
    protected final Bitmap iconBmpSelect;
    /**
     * Paint object for drawing icon
     */
    protected final Paint paint;
    /**
     * offset grid of icon in Point.
     * if you are using symmetric icon image, it should be half size of width&height.
     * adjust this parameter to offset the axis of the image.
     */
    protected Point iconOffset;
    /**
     * maximum item size for the marker.
     */
    protected int itemSizeMax;
    /**
     * text size for icon
     */
    protected float textSize;

    /**
     * NOTE: src_nrm & src_sel must be same bitmap size.
     *
     * @param src_nrm  source Bitmap object for normal state
     * @param src_sel  source Bitmap object for select state
     * @param grid     grid point to be offset
     * @param textSize text size for icon
     * @param maxSize  icon size threshold
     */
    public MarkerBitmap(Context context, Bitmap src_nrm, Bitmap src_sel,
                        Point grid, float textSize, int maxSize, Paint paint) {
        MarkerBitmap.context = context;
        iconBmpNormal = src_nrm;
        iconBmpSelect = src_sel;
        iconOffset = grid;
        this.textSize = textSize * DisplayModel.getDeviceScaleFactor();
        itemSizeMax = maxSize;
        this.paint = paint;
        this.paint.setTextSize(getTextSize());
    }

    @SuppressWarnings("deprecation")
    public static Bitmap getBitmapFromTitle(String title, Paint paint) {
        if (!captionViews.containsKey(title)) {
            TextView bubbleView = new TextView(context);
// Path: mapsforge-samples-android/src/main/java/org/mapsforge/samples/android/Utils.java
// public final class Utils {
//     /**
//      * Compatibility method.
//      *
//      * @param a the current activity
//      */
//     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
//     public static void enableHome(Activity a) {
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//             // Show the Up button in the action bar.
//             a.getActionBar().setDisplayHomeAsUpEnabled(true);
//         }
//     }
// 
//     /**
//      * Compatibility method.
//      *
//      * @param view       the view to set the background on
//      * @param background the background
//      */
//     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
//     @SuppressWarnings("deprecation")
//     public static void setBackground(View view, Drawable background) {
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//             view.setBackground(background);
//         } else {
//             view.setBackgroundDrawable(background);
//         }
//     }
// 
//     static Marker createMarker(Context c, int resourceIdentifier,
//                                LatLong latLong) {
//         Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(c.getResources(), resourceIdentifier));
//         return new Marker(latLong, bitmap, 0, -bitmap.getHeight() / 2);
//     }
// 
//     static Paint createPaint(int color, int strokeWidth, Style style) {
//         Paint paint = AndroidGraphicFactory.INSTANCE.createPaint();
//         paint.setColor(color);
//         paint.setStrokeWidth(strokeWidth);
//         paint.setStyle(style);
//         return paint;
//     }
// 
//     static Marker createTappableMarker(final Context c, int resourceIdentifier,
//                                        LatLong latLong) {
//         Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(c.getResources(), resourceIdentifier));
//         bitmap.incrementRefCount();
//         return new Marker(latLong, bitmap, 0, -bitmap.getHeight() / 2) {
//             @Override
//             public boolean onTap(LatLong geoPoint, Point viewPosition,
//                                  Point tapPoint) {
//                 if (contains(viewPosition, tapPoint)) {
//                     Toast.makeText(c,
//                             "The Marker was tapped " + geoPoint.toString(),
//                             Toast.LENGTH_SHORT).show();
//                     return true;
//                 }
//                 return false;
//             }
//         };
//     }
// 
//     @SuppressWarnings("deprecation")
//     public static Bitmap viewToBitmap(Context c, View view) {
//         view.measure(MeasureSpec.getSize(view.getMeasuredWidth()),
//                 MeasureSpec.getSize(view.getMeasuredHeight()));
//         view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//         view.setDrawingCacheEnabled(true);
//         Drawable drawable = new BitmapDrawable(c.getResources(),
//                 android.graphics.Bitmap.createBitmap(view.getDrawingCache()));
//         view.setDrawingCacheEnabled(false);
//         return AndroidGraphicFactory.convertToBitmap(drawable);
//     }
// 
//     private Utils() {
//         throw new IllegalStateException();
//     }
// 
// }


// Path: mapsforge-samples-android/src/main/java/org/mapsforge/samples/android/cluster/MarkerBitmap.java
import java.util.Map;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.model.DisplayModel;
import org.mapsforge.samples.android.R;
import org.mapsforge.samples.android.Utils;
import java.util.HashMap;

/*
 * Copyright 2009 Huan Erdao
 * Copyright 2014 Martin Vennekamp
 * Copyright 2015 mapsforge.org
 *
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.mapsforge.samples.android.cluster;




/**
 * Utility Class to handle MarkerBitmap
 * it handles grid offset to display on the map with offset
 */
public class MarkerBitmap {

    private static Map<String, Bitmap> captionViews = new HashMap<String, Bitmap>();
    private static Context context;
    /**
     * bitmap object for normal state icon
     */
    protected final Bitmap iconBmpNormal;
    /**
     * bitmap object for select state icon
     */
    protected final Bitmap iconBmpSelect;
    /**
     * Paint object for drawing icon
     */
    protected final Paint paint;
    /**
     * offset grid of icon in Point.
     * if you are using symmetric icon image, it should be half size of width&height.
     * adjust this parameter to offset the axis of the image.
     */
    protected Point iconOffset;
    /**
     * maximum item size for the marker.
     */
    protected int itemSizeMax;
    /**
     * text size for icon
     */
    protected float textSize;

    /**
     * NOTE: src_nrm & src_sel must be same bitmap size.
     *
     * @param src_nrm  source Bitmap object for normal state
     * @param src_sel  source Bitmap object for select state
     * @param grid     grid point to be offset
     * @param textSize text size for icon
     * @param maxSize  icon size threshold
     */
    public MarkerBitmap(Context context, Bitmap src_nrm, Bitmap src_sel,
                        Point grid, float textSize, int maxSize, Paint paint) {
        MarkerBitmap.context = context;
        iconBmpNormal = src_nrm;
        iconBmpSelect = src_sel;
        iconOffset = grid;
        this.textSize = textSize * DisplayModel.getDeviceScaleFactor();
        itemSizeMax = maxSize;
        this.paint = paint;
        this.paint.setTextSize(getTextSize());
    }

    @SuppressWarnings("deprecation")
    public static Bitmap getBitmapFromTitle(String title, Paint paint) {
        if (!captionViews.containsKey(title)) {
            TextView bubbleView = new TextView(context);
            Utils.setBackground(bubbleView, context.getResources().getDrawable(R.drawable.caption_background));