<commit_before>/**
 * Copyright (C) 2014 yvolk (<NAME>), http://yurivolkov.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.andstatus.app.data;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.WindowManager;

import org.andstatus.app.context.MyContextHolder;
import org.andstatus.app.util.MyLog;

public class AttachedImageDrawable {
    private final long downloadRowId;
    private final DownloadFile downloadFile;
    
    public static Drawable drawableFromCursor(Cursor cursor) {
        int columnIndex = cursor.getColumnIndex(MyDatabase.Download.IMAGE_ID);
        Long imageRowId = null;
        if (columnIndex >= 0) {
            imageRowId = cursor.getLong(columnIndex);
        }
        if (imageRowId == null || imageRowId == 0L) {
            return null;
        } else {
            return new AttachedImageDrawable(imageRowId, cursor.getString(cursor
                    .getColumnIndex(MyDatabase.Download.IMAGE_FILE_NAME))).getDrawable();
        }
    }
    
    public AttachedImageDrawable(long downloadRowIdIn, String filename) {
        downloadRowId = downloadRowIdIn;
        downloadFile = new DownloadFile(filename);
    }
    
    public Drawable getDrawable() {
        if (downloadFile.exists()) {
            String path = downloadFile.getFile().getAbsolutePath();
            return drawableFromPath(this, path);
        } 
        DownloadData.fromRowId(downloadRowId).requestDownload();
        return null;
    }

    public static final double MAX_ATTACHED_IMAGE_PART = 0.75;

    public static Drawable drawableFromPath(Object objTag, String path) {
        Bitmap bitmap = BitmapFactory
                .decodeFile(path, calculateScaling(objTag, getImageSize(path)));
        if (MyLog.isLoggable(MyLog.VERBOSE)) {
            MyLog.v(objTag, "Loaded bitmap " + bitmap.getWidth() + "x" + bitmap.getHeight()
                    + " '" + path + "'");
        }
        return new BitmapDrawable(MyContextHolder.get().context().getResources(), bitmap);
    }

    private static Point getImageSize(String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        return new Point(options.outWidth, options.outHeight);
    }

    private static BitmapFactory.Options calculateScaling(Object objTag,
            Point imageSize) {
        BitmapFactory.Options options2 = new BitmapFactory.Options();
        Point displaySize = getDisplaySize(MyContextHolder.get().context());
        int maxHeight = (int) (MAX_ATTACHED_IMAGE_PART * displaySize.y);
        if (imageSize.y > maxHeight || imageSize.x > displaySize.x) {
            options2.inSampleSize = (int) Math.floor(imageSize.y / maxHeight);
            int inSampleSize2 = (int) Math.floor(imageSize.x / displaySize.x);
            if (options2.inSampleSize < inSampleSize2) {
                options2.inSampleSize = inSampleSize2;<commit_msg>Fixed crash while loading invalid bitmap<commit_after>/**
 * Copyright (C) 2014 yvolk (<NAME>), http://yurivolkov.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.andstatus.app.data;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.WindowManager;

import org.andstatus.app.context.MyContextHolder;
import org.andstatus.app.util.MyLog;

public class AttachedImageDrawable {
    private final long downloadRowId;
    private final DownloadFile downloadFile;
    
    public static Drawable drawableFromCursor(Cursor cursor) {
        int columnIndex = cursor.getColumnIndex(MyDatabase.Download.IMAGE_ID);
        Long imageRowId = null;
        if (columnIndex >= 0) {
            imageRowId = cursor.getLong(columnIndex);
        }
        if (imageRowId == null || imageRowId == 0L) {
            return null;
        } else {
            return new AttachedImageDrawable(imageRowId, cursor.getString(cursor
                    .getColumnIndex(MyDatabase.Download.IMAGE_FILE_NAME))).getDrawable();
        }
    }
    
    public AttachedImageDrawable(long downloadRowIdIn, String filename) {
        downloadRowId = downloadRowIdIn;
        downloadFile = new DownloadFile(filename);
    }
    
    public Drawable getDrawable() {
        if (downloadFile.exists()) {
            String path = downloadFile.getFile().getAbsolutePath();
            return drawableFromPath(this, path);
        } 
        DownloadData.fromRowId(downloadRowId).requestDownload();
        return null;
    }

    public static final double MAX_ATTACHED_IMAGE_PART = 0.75;

    public static Drawable drawableFromPath(Object objTag, String path) {
        Bitmap bitmap = BitmapFactory
                .decodeFile(path, calculateScaling(objTag, getImageSize(path)));
        if (MyLog.isLoggable(MyLog.VERBOSE)) {
            MyLog.v(objTag, (bitmap == null ? "Failed to load bitmap" : "Loaded bitmap " + bitmap.getWidth() + "x" + bitmap.getHeight())
                    + " '" + path + "'");
        }
        if (bitmap == null) {
            return null;
        }
        return new BitmapDrawable(MyContextHolder.get().context().getResources(), bitmap);
    }

    private static Point getImageSize(String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        return new Point(options.outWidth, options.outHeight);
    }

    private static BitmapFactory.Options calculateScaling(Object objTag,
            Point imageSize) {
        BitmapFactory.Options options2 = new BitmapFactory.Options();
        Point displaySize = getDisplaySize(MyContextHolder.get().context());
        int maxHeight = (int) (MAX_ATTACHED_IMAGE_PART * displaySize.y);
        if (imageSize.y > maxHeight || imageSize.x > displaySize.x) {
            options2.inSampleSize = (int) Math.floor(imageSize.y / maxHeight);
            int inSampleSize2 = (int) Math.floor(imageSize.x / displaySize.x);
            if (options2.inSampleSize < inSampleSize2) {
                options2.inSampleSize = inSampleSize2;
{'fraction_non_alphanumeric': 0.09476995674400314, 'fraction_numerical': 0.005243151133831433, 'mean_word_length': 3.0263852242744065, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '528391', 'n_tokens_mistral': 2071, 'n_tokens_neox': 2025, 'n_words': 535}