<commit_before>package fr.free.nrw.commons.upload;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.graphics.BitmapCompat;
import android.view.View;
import android.widget.FrameLayout;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Zoom {

    private View thumbView;
    private InputStream input;
    private Uri imageUri;
    private ContentResolver contentResolver;
    private FrameLayout flContainer;


    Zoom(View thumbView, FrameLayout flContainer, InputStream input, Uri imageUri, ContentResolver contentResolver) {
        this.thumbView = thumbView;
        this.input = input;
        this.imageUri = imageUri;
        this.contentResolver = contentResolver;
        this.flContainer = flContainer;
    }

    Bitmap createScaledImage() {

        Bitmap scaled = null;
        BitmapRegionDecoder decoder = null;
        try {
            decoder = BitmapRegionDecoder.newInstance(input, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
        try {
            //Compress the Image
            System.gc();
            Runtime rt = Runtime.getRuntime();
            long maxMemory = rt.freeMemory();
            bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
            int bitmapByteCount = BitmapCompat.getAllocationByteCount(bitmap);
            long height = bitmap.getHeight();
            long width = bitmap.getWidth();
            long calHeight = (long) ((height * maxMemory) / (bitmapByteCount * 1.1));
            long calWidth = (long) ((width * maxMemory) / (bitmapByteCount * 1.1));
            scaled = Bitmap.createScaledBitmap(bitmap, (int) Math.min(width, calWidth), (int) Math.min(height, calHeight), true);
        } catch (IOException e) {
        } catch (NullPointerException e) {
            scaled = bitmap;
        }
        return scaled;
    }

    float adjustStartEndBounds(Rect startBounds, Rect finalBounds, Point globalOffset) {
        // Calculate the starting and ending bounds for the zoomed-in image.
        // The start bounds are the global visible rectangle of the thumbnail,
        // and the final bounds are the global visible rectangle of the container
        // view. Also set the container view's offset as the origin for the
        // bounds, since that's the origin for the positioning animation
        // properties (X, Y).
        thumbView.getGlobalVisibleRect(startBounds);
        flContainer.getGlobalVisibleRect(finalBounds, globalOffset);
        startBounds.offset(-globalOffset.x, -globalOffset.y);
        finalBounds.offset(-globalOffset.x, -globalOffset.y);

        // Adjust the start bounds to be the same aspect ratio as the final
        // bounds using the "center crop" technique. This prevents undesirable
        // stretching during the animation. Also calculate the start scaling
        // factor (the end scaling factor is always 1.0).<commit_msg>Catch exceptions in Zoom.java<commit_after>package fr.free.nrw.commons.upload;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.graphics.BitmapCompat;
import android.view.View;
import android.widget.FrameLayout;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import timber.log.Timber;

public class Zoom {

    private View thumbView;
    private InputStream input;
    private Uri imageUri;
    private ContentResolver contentResolver;
    private FrameLayout flContainer;

    Zoom(View thumbView, FrameLayout flContainer, InputStream input, Uri imageUri, ContentResolver contentResolver) {
        this.thumbView = thumbView;
        this.input = input;
        this.imageUri = imageUri;
        this.contentResolver = contentResolver;
        this.flContainer = flContainer;
    }

    Bitmap createScaledImage() {

        Bitmap scaled = null;
        BitmapRegionDecoder decoder = null;
        Bitmap bitmap = null;

        try {
            decoder = BitmapRegionDecoder.newInstance(input, false);
            bitmap = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
        } catch (IOException e) {
            Timber.e(e);
        } catch (NullPointerException e) {
            Timber.e(e);
        }
        try {
            //Compress the Image
            System.gc();
            Runtime rt = Runtime.getRuntime();
            long maxMemory = rt.freeMemory();
            bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
            int bitmapByteCount = BitmapCompat.getAllocationByteCount(bitmap);
            long height = bitmap.getHeight();
            long width = bitmap.getWidth();
            long calHeight = (long) ((height * maxMemory) / (bitmapByteCount * 1.1));
            long calWidth = (long) ((width * maxMemory) / (bitmapByteCount * 1.1));
            scaled = Bitmap.createScaledBitmap(bitmap, (int) Math.min(width, calWidth), (int) Math.min(height, calHeight), true);
        } catch (IOException e) {
            Timber.e(e);
        } catch (NullPointerException e) {
            Timber.e(e);
            scaled = bitmap;
        }
        return scaled;
    }

    float adjustStartEndBounds(Rect startBounds, Rect finalBounds, Point globalOffset) {
        // Calculate the starting and ending bounds for the zoomed-in image.
        // The start bounds are the global visible rectangle of the thumbnail,
        // and the final bounds are the global visible rectangle of the container
        // view. Also set the container view's offset as the origin for the
        // bounds, since that's the origin for the positioning animation
        // properties (X, Y).
        thumbView.getGlobalVisibleRect(startBounds);
        flContainer.getGlobalVisibleRect(finalBounds, globalOffset);
        startBounds.offset(-globalOffset.x, -globalOffset.y);
        finalBounds.offset(-globalOffset.x, -globalOffset.y);

        // Adjust the start bounds to be the same aspect ratio as the final
        // bounds using the "center crop" technique. This prevents undesirable
        // stretching during the animation. Also calculate the start scaling
        // factor (the end scaling factor is always 1.0).
{'fraction_non_alphanumeric': 0.0880597014925373, 'fraction_numerical': 0.004477611940298508, 'mean_word_length': 2.714523281596452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4622866', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1681, 'n_words': 524}