<commit_before>package org.ccci.gto.android.common.picasso.transformation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;

import com.squareup.picasso.Transformation;

public class MaskTransformation implements Transformation {
    private static final Paint PAINT_MASK = new Paint();

    static {
        PAINT_MASK.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    }

    @NonNull
    private final Context mContext;
    @DrawableRes
    private final int mMask;

    public MaskTransformation(@NonNull final Context context, @DrawableRes final int mask) {
        mContext = context;
        mMask = mask;
    }

    @Override
    public String key() {
        return "MaskTransformation(mask=" + mContext.getResources().getResourceEntryName(mMask) + ")";
    }

    @Override
    public Bitmap transform(@NonNull final Bitmap source) {
        final int w = source.getWidth();
        final int h = source.getHeight();

        final Drawable mask = ContextCompat.getDrawable(mContext, mMask);
        if (mask == null) {
            throw new IllegalArgumentException("Unable to load mask");
        }

        final Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(result);
        mask.setBounds(0, 0, w, h);
        mask.draw(canvas);
        canvas.drawBitmap(source, 0, 0, PAINT_MASK);

        source.recycle();

        return result;
    }
}<commit_msg>prevent leaking a non-application context<commit_after>package org.ccci.gto.android.common.picasso.transformation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;

import com.squareup.picasso.Transformation;

import java.lang.ref.WeakReference;

public class MaskTransformation implements Transformation {
    private static final Paint PAINT_MASK = new Paint();

    static {
        PAINT_MASK.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    }

    @NonNull
    private final Context mApplicationContext;
    @NonNull
    private final WeakReference<Context> mContext;
    @DrawableRes
    private final int mMask;

    public MaskTransformation(@NonNull final Context context, @DrawableRes final int mask) {
        mApplicationContext = context.getApplicationContext();
        mContext = new WeakReference<>(context);
        mMask = mask;
    }

    @NonNull
    private Context getContext() {
        Context context = mContext.get();
        if (context == null) {
            context = mApplicationContext;
        }
        return context;
    }

    @Override
    public String key() {
        return "MaskTransformation(mask=" + getContext().getResources().getResourceEntryName(mMask) + ")";
    }

    @Override
    public Bitmap transform(@NonNull final Bitmap source) {
        final int w = source.getWidth();
        final int h = source.getHeight();

        final Drawable mask = ContextCompat.getDrawable(getContext(), mMask);
        if (mask == null) {
            throw new IllegalArgumentException("Unable to load mask");
        }

        final Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(result);
        mask.setBounds(0, 0, w, h);
        mask.draw(canvas);
        canvas.drawBitmap(source, 0, 0, PAINT_MASK);

        source.recycle();

        return result;
    }
}
{'fraction_non_alphanumeric': 0.09508360369353631, 'fraction_numerical': 0.004492138757174944, 'mean_word_length': 3.323624595469256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4829514', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1103, 'n_words': 270}