<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.mozstumbler.client.mapview.tiles;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import org.mozilla.osmdroid.DefaultResourceProxyImpl;
import org.mozilla.osmdroid.tileprovider.BetterTileProvider;
import org.mozilla.osmdroid.tileprovider.MapTile;
import org.mozilla.osmdroid.tileprovider.ReusableBitmapDrawable;
import org.mozilla.osmdroid.util.TileLooper;
import org.mozilla.osmdroid.views.Projection;
import org.mozilla.osmdroid.views.overlay.TilesOverlay;

import java.util.HashSet;
import java.util.Set;

public abstract class AbstractMapOverlay extends TilesOverlay {
    // We want the map to zoom to level 20, even if tiles have less zoom available
    public static final int MAX_ZOOM_LEVEL_OF_MAP = 20;
    private static int sMinZoomLevelOfMapDisplaySizeBased;

    public static final int TILE_PIXEL_SIZE = 256;
    // Use png32 which is a 32-color indexed image, the tiles are ~30% smaller
    public static String FILE_TYPE_SUFFIX_PNG = ".png32";
    private final Rect mTileRect = new Rect();
    private final Point mTilePoint = new Point();
    private final Point mTilePointMercator = new Point();
    private final Set<MapTile> mDrawnSet = new HashSet<MapTile>();
    private Projection mProjection;

    // TODO make this a single value configurable in deveoper settings
    private static final int SMALL_SCREEN_MIN_ZOOM = 11;
    private static final int LARGE_SCREEN_MIN_ZOOM = 13;


    public enum LowResType {
        HIGHER_ZOOM, LOWER_ZOOM
    }

    public AbstractMapOverlay(final Context context) {
        super(new BetterTileProvider(context), new DefaultResourceProxyImpl(context));
    }

    public static void setDisplayBasedMinimumZoomLevel(Context c) {
        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);

        sMinZoomLevelOfMapDisplaySizeBased = (outMetrics.widthPixels < 500)? SMALL_SCREEN_MIN_ZOOM : LARGE_SCREEN_MIN_ZOOM;
    }

    public static int getDisplaySizeBasedMinZoomLevel() {
        return sMinZoomLevelOfMapDisplaySizeBased;
    }

    // Though the tile provider can only provide up to 13, this overlay will display higher.
    @Override
    public int getMaximumZoomLevel() {
        return MAX_ZOOM_LEVEL_OF_MAP;
    }

    @Override
    public void drawTiles(final Canvas c, final Projection projection, final int zoomLevel,
                          final int tileSizePx, final Rect viewPort) {
        if (zoomLevel <= mTileProvider.getMaximumZoomLevel()) {
            super.drawTiles(c, projection, zoomLevel, tileSizePx, viewPort);
        } else {
            mProjection = projection;
            mDrawnSet.clear();
            mCoverageTileLooper.loop(c, zoomLevel, tileSizePx, viewPort);
        }
    }

    protected final TileLooper mCoverageTileLooper = new TileLooper() {
        @Override
        public void initialiseLoop(int pZoomLevel, int pTileSizePx) {<commit_msg>Add addition zoom for medium screen<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.mozstumbler.client.mapview.tiles;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import org.mozilla.osmdroid.DefaultResourceProxyImpl;
import org.mozilla.osmdroid.tileprovider.BetterTileProvider;
import org.mozilla.osmdroid.tileprovider.MapTile;
import org.mozilla.osmdroid.tileprovider.ReusableBitmapDrawable;
import org.mozilla.osmdroid.util.TileLooper;
import org.mozilla.osmdroid.views.Projection;
import org.mozilla.osmdroid.views.overlay.TilesOverlay;

import java.util.HashSet;
import java.util.Set;

public abstract class AbstractMapOverlay extends TilesOverlay {
    // We want the map to zoom to level 20, even if tiles have less zoom available
    public static final int MAX_ZOOM_LEVEL_OF_MAP = 20;
    private static int sMinZoomLevelOfMapDisplaySizeBased;

    public static final int TILE_PIXEL_SIZE = 256;
    // Use png32 which is a 32-color indexed image, the tiles are ~30% smaller
    public static String FILE_TYPE_SUFFIX_PNG = ".png32";
    private final Rect mTileRect = new Rect();
    private final Point mTilePoint = new Point();
    private final Point mTilePointMercator = new Point();
    private final Set<MapTile> mDrawnSet = new HashSet<MapTile>();
    private Projection mProjection;

    // TODO make this a single value configurable in developer settings
    private static final int SMALL_SCREEN_MIN_ZOOM = 11;
    private static final int MEDIUM_SCREEN_MIN_ZOOM = 12;
    private static final int LARGE_SCREEN_MIN_ZOOM = 13;


    public enum LowResType {
        HIGHER_ZOOM, LOWER_ZOOM
    }

    public AbstractMapOverlay(final Context context) {
        super(new BetterTileProvider(context), new DefaultResourceProxyImpl(context));
    }

    public static void setDisplayBasedMinimumZoomLevel(Context c) {
        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);

        if (outMetrics.widthPixels > 700) {
            sMinZoomLevelOfMapDisplaySizeBased = LARGE_SCREEN_MIN_ZOOM;
        } else if (outMetrics.widthPixels < 500) {
            sMinZoomLevelOfMapDisplaySizeBased = SMALL_SCREEN_MIN_ZOOM;
        } else {
            sMinZoomLevelOfMapDisplaySizeBased = MEDIUM_SCREEN_MIN_ZOOM;
        }
    }

    public static int getDisplaySizeBasedMinZoomLevel() {
        return sMinZoomLevelOfMapDisplaySizeBased;
    }

    // Though the tile provider can only provide up to 13, this overlay will display higher.
    @Override
    public int getMaximumZoomLevel() {
        return MAX_ZOOM_LEVEL_OF_MAP;
    }

    @Override
    public void drawTiles(final Canvas c, final Projection projection, final int zoomLevel,
                          final int tileSizePx, final Rect viewPort) {
        if (zoomLevel <= mTileProvider.getMaximumZoomLevel()) {
            super.drawTiles(c, projection, zoomLevel, tileSizePx, viewPort);
        } else {
            mProjection = projection;
            mDrawnSet.clear();
            mCoverageTileLooper.loop(c, zoomLevel, tileSizePx, viewPort);
        }
    }

    protected final TileLooper mCoverageTileLooper = new TileLooper() {
        @Override
        public void initialiseLoop(int pZoomLevel, int pTileSizePx) {
{'fraction_non_alphanumeric': 0.07226615762203437, 'fraction_numerical': 0.008317425688573766, 'mean_word_length': 3.6394686907020875, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 8, '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': '5741949', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2090, 'n_words': 594}