<commit_before>package org.osmdroid.views.overlay;

import org.osmdroid.ResourceProxy;
import org.osmdroid.views.MapView;
import org.osmdroid.views.safecanvas.ISafeCanvas;
import org.osmdroid.views.safecanvas.SafeTranslatedCanvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Build;

/**
 * An overlay class that uses the safe drawing canvas to draw itself and can be zoomed in to high
 * levels without drawing issues.
 * 
 * @see {@link ISafeCanvas}
 */
public abstract class SafeDrawOverlay extends Overlay {

	private static final SafeTranslatedCanvas sSafeCanvas = new SafeTranslatedCanvas();
	private static final Matrix sMatrix = new Matrix();
	private static final float[] sMatrixValues = new float[9];
	private boolean mUseSafeCanvas = true;

	protected abstract void drawSafe(final ISafeCanvas c, final MapView osmv, final boolean shadow);

	public SafeDrawOverlay(Context ctx) {
		super(ctx);
	}

	public SafeDrawOverlay(ResourceProxy pResourceProxy) {
		super(pResourceProxy);
	}

	protected void draw(final Canvas c, final MapView osmv, final boolean shadow) {

		sSafeCanvas.setCanvas(c);

		if (this.isUsingSafeCanvas()) {

			// Find the screen offset
			Rect screenRect = osmv.getProjection().getScreenRect();
			sSafeCanvas.xOffset = -screenRect.left;
			sSafeCanvas.yOffset = -screenRect.top;

			// Save the canvas state
			c.save();

			// Get the matrix values
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				sMatrix.set(osmv.getMatrix());
			} else {
				c.getMatrix(sMatrix);
			}
			sMatrix.getValues(sMatrixValues);

			// If we're rotating, then reverse the rotation
			// This gets us proper MSCALE values in the matrix.
			final double angrad = Math.atan2(sMatrixValues[Matrix.MSKEW_Y],
					sMatrixValues[Matrix.MSCALE_X]);
			sMatrix.preRotate((float) -Math.toDegrees(angrad), screenRect.centerX(),
					screenRect.centerY());

			// Get the new matrix values to find the scaling factor
			sMatrix.getValues(sMatrixValues);

			// Shift the canvas to remove scroll, while accounting for scaling values
			c.translate(screenRect.left * sMatrixValues[Matrix.MSCALE_X], screenRect.top
					* sMatrixValues[Matrix.MSCALE_Y]);

		} else {
			sSafeCanvas.xOffset = 0;
			sSafeCanvas.yOffset = 0;
		}
		this.drawSafe(sSafeCanvas, osmv, shadow);

		if (this.isUsingSafeCanvas()) {
			c.restore();
		}
	}

	public boolean isUsingSafeCanvas() {
		return mUseSafeCanvas;
	}

	public void setUseSafeCanvas(boolean useSafeCanvas) {
		mUseSafeCanvas = useSafeCanvas;
	}
}<commit_msg>Fixes issues with pre-honeycomb drawing. Update issue 413<commit_after>package org.osmdroid.views.overlay;

import org.osmdroid.ResourceProxy;
import org.osmdroid.views.MapView;
import org.osmdroid.views.safecanvas.ISafeCanvas;
import org.osmdroid.views.safecanvas.SafeTranslatedCanvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Build;

/**
 * An overlay class that uses the safe drawing canvas to draw itself and can be zoomed in to high
 * levels without drawing issues.
 * 
 * @see {@link ISafeCanvas}
 */
public abstract class SafeDrawOverlay extends Overlay {

	private static final SafeTranslatedCanvas sSafeCanvas = new SafeTranslatedCanvas();
	private static final Matrix sMatrix = new Matrix();
	private static final float[] sMatrixValues = new float[9];
	private boolean mUseSafeCanvas = true;

	protected abstract void drawSafe(final ISafeCanvas c, final MapView osmv, final boolean shadow);

	public SafeDrawOverlay(Context ctx) {
		super(ctx);
	}

	public SafeDrawOverlay(ResourceProxy pResourceProxy) {
		super(pResourceProxy);
	}

	protected void draw(final Canvas c, final MapView osmv, final boolean shadow) {

		sSafeCanvas.setCanvas(c);

		if (this.isUsingSafeCanvas()) {

			// Find the screen offset
			Rect screenRect = osmv.getProjection().getScreenRect();
			sSafeCanvas.xOffset = -screenRect.left;
			sSafeCanvas.yOffset = -screenRect.top;

			// Save the canvas state
			c.save();

			// Get the matrix values
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				sMatrix.set(osmv.getMatrix());
			} else {
				c.getMatrix(sMatrix);
			}
			sMatrix.getValues(sMatrixValues);

			// If we're rotating, then reverse the rotation
			// This gets us proper MSCALE values in the matrix.
			final double angrad = Math.atan2(sMatrixValues[Matrix.MSKEW_Y],
					sMatrixValues[Matrix.MSCALE_X]);
			sMatrix.preRotate((float) -Math.toDegrees(angrad), screenRect.centerX(),
					screenRect.centerY());

			// Get the new matrix values to find the scaling factor
			sMatrix.getValues(sMatrixValues);

			// Shift the canvas to remove scroll, while accounting for scaling values
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				c.translate(screenRect.left * sMatrixValues[Matrix.MSCALE_X], screenRect.top
						* sMatrixValues[Matrix.MSCALE_Y]);
			} else {
				c.getMatrix(sMatrix);
				sMatrix.postTranslate(screenRect.left * sMatrixValues[Matrix.MSCALE_X],
						screenRect.top * sMatrixValues[Matrix.MSCALE_Y]);
				c.setMatrix(sMatrix);
			}

		} else {
			sSafeCanvas.xOffset = 0;
			sSafeCanvas.yOffset = 0;
		}
		this.drawSafe(sSafeCanvas, osmv, shadow);

		if (this.isUsingSafeCanvas()) {
			c.restore();
		}
	}

	public boolean isUsingSafeCanvas() {
		return mUseSafeCanvas;
	}

	public void setUseSafeCanvas(boolean useSafeCanvas) {
		mUseSafeCanvas = useSafeCanvas;
	}
}
{'fraction_non_alphanumeric': 0.09992797983435361, 'fraction_numerical': 0.0019805545552754773, 'mean_word_length': 4.2754036087369425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6101546', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1702, 'n_words': 439}