<commit_before>/*
 * Copyright (C) 2009 Teleca Poland Sp. z o.o. <<EMAIL>>
 *
 * 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 com.teleca.jamendo.widget;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.concurrent.RejectedExecutionException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.StatFs;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.util.ImageCache;

/**
 * ImageView extended class allowing easy downloading
 * of remote images
 * 
 * @author <NAME>
 */
public class RemoteImageView extends ImageView{
	
	/**
	 * Maximum number of unsuccesful tries of downloading an image
	 */
	private static int MAX_FAILURES = 3;

	public RemoteImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public RemoteImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public RemoteImageView(Context context) {
		super(context);
		init();
	}

	/**
	 * Sharable code between constructors
	 */
	private void init(){
		mTimeDiff = DAYS_OF_CACHE*24*60*60*1000L;	
	}
	
	/**
	 * Remote image location
	 */
	private String mUrl;
	
	/**
	 * Currently successfully grabbed url
	 */
	private String mCurrentlyGrabbedUrl;
	
	/**
	 * Remote image download failure counter
	 */
	private int mFailure;

	/**
	 * Position of the image in the mListView
	 */
	private int mPosition;

	/**
	 * ListView containg this image
	 */
	private ListView mListView;
	
	/**
	 * Default image shown while loading or on url not found
	 */
	private Integer mDefaultImage;

	private long mTimeDiff;
	
	private final static String ALBUMS= "albums";
	private final static String ALBUMS_CONV= "bgc";
	private final static String RADIOS= "radios";
	private final static String RADIOS_CONV= "sbf";
	private final static String COVERS= "covers";
	private final static String COVERS_CONV= "dpw";
	private final static String ALBUM_COVER_MARKER= "1.500";
	private final static String JAMENDO_DIR= "Android/data/com.teleca.jamendo";
	private final static int MB = 1048576;
	// in mb, after exceded cache size the 40%(remove factor) of oldest album
	// covers are removed
	private final static int CACHE_SIZE= 150;
	//every DAYS_OF_CACHE the radio and album thumbnails jpegs are deleted
	private final static int DAYS_OF_CACHE= 45;
	//minimum free space on sd card to enable cache
	private final static int FREE_SD_SPACE_NEEDED_TO_CACHE= 10;
	
	

	/**
	 * Loads image from remote location
	 * 
	 * @param url eg. http://random.com/abz.jpg
	 */
	public void setImageUrl(String url){
		
		if(mUrl != null && mUrl.equals(url)){
			mFailure++;
			if(mFailure > MAX_FAILURES){
				Log.e(JamendoApplication.TAG, "Failed to download "+url+", falling back to default image");
				loadDefaultImage();
				return;
			}
		} else {
			mUrl = url;
			mFailure = 0;
		}

	
		if (url.contains(ALBUMS) || url.contains(RADIOS)) {			
			String fileName = convertUrlToFileName(url);
			String dir = getDirectory(fileName);
			String pathFileName = dir + "/" + fileName;						
			Bitmap tbmp = BitmapFactory.decodeFile(pathFileName);
			if (tbmp == null) {
				Log.d(JamendoApplication.TAG, "Image is not present, try to download");
				try{
					new DownloadTask().execute(url);
				} catch (RejectedExecutionException e) {
					// do nothing, just don't crash
				}
			} else {
				Log.i(JamendoApplication.TAG, "Loading album cover from file");
				this.setImageBitmap(tbmp);
				updateFileTime(dir,fileName );				
			}
			removeAlbumCoversCache(dir, fileName);
			removeRadioCoversCache(dir, fileName);<commit_msg>Fix for bug: If you open album of the same artist Like Search->BrokenKites-> Flight school After 4th song album art is missing<commit_after>/*
 * Copyright (C) 2009 Teleca Poland Sp. z o.o. <<EMAIL>>
 *
 * 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 com.teleca.jamendo.widget;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.RejectedExecutionException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.StatFs;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.util.ImageCache;

/**
 * ImageView extended class allowing easy downloading
 * of remote images
 * 
 * @author <NAME>
 */
public class RemoteImageView extends ImageView{
	
	/**
	 * Maximum number of unsuccesful tries of downloading an image
	 */
	private static int MAX_FAILURES = 3;

	public RemoteImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public RemoteImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public RemoteImageView(Context context) {
		super(context);
		init();
	}

	/**
	 * Sharable code between constructors
	 */
	private void init(){
		mTimeDiff = DAYS_OF_CACHE*24*60*60*1000L;	
	}
	
	/**
	 * Remote image location
	 */
	private String mUrl;
	
	/**
	 * Currently successfully grabbed url
	 */
	private String mCurrentlyGrabbedUrl;
	
	/**
	 * Remote image download failure counter
	 */
	private int mFailure;

	/**
	 * Position of the image in the mListView
	 */
	private int mPosition;

	/**
	 * ListView containg this image
	 */
	private ListView mListView;
	
	/**
	 * Default image shown while loading or on url not found
	 */
	private Integer mDefaultImage;

	private long mTimeDiff;
	
	private final static String ALBUMS= "albums";
	private final static String ALBUMS_CONV= "bgc";
	private final static String RADIOS= "radios";
	private final static String RADIOS_CONV= "sbf";
	private final static String COVERS= "covers";
	private final static String COVERS_CONV= "dpw";
	private final static String ALBUM_COVER_MARKER= "1.500";
	private final static String JAMENDO_DIR= "Android/data/com.teleca.jamendo";
	private final static int MB = 1048576;
	// in mb, after exceded cache size the 40%(remove factor) of oldest album
	// covers are removed
	private final static int CACHE_SIZE= 150;
	//every DAYS_OF_CACHE the radio and album thumbnails jpegs are deleted
	private final static int DAYS_OF_CACHE= 45;
	//minimum free space on sd card to enable cache
	private final static int FREE_SD_SPACE_NEEDED_TO_CACHE= 10;
	
	

	/**
	 * Loads image from remote location
	 * 
	 * @param url eg. http://random.com/abz.jpg
	 */
	public void setImageUrl(String url){
		
		
		if (mUrl != null && mUrl.equals(url) && (mCurrentlyGrabbedUrl == null ||
				(mCurrentlyGrabbedUrl != null && !mCurrentlyGrabbedUrl.equals(url)))) {
			mFailure++;			
			if(mFailure > MAX_FAILURES){
				Log.e(JamendoApplication.TAG, "Failed to download "+url+", falling back to default image");
				loadDefaultImage();
				return;
			}
		} else {
			mUrl = url;
			mFailure = 0;
		}

	
		if (url.contains(ALBUMS) || url.contains(RADIOS)) {			
			String fileName = convertUrlToFileName(url);
			String dir = getDirectory(fileName);
			String pathFileName = dir + "/" + fileName;						
			Bitmap tbmp = BitmapFactory.decodeFile(pathFileName);
			if (tbmp == null) {
				Log.d(JamendoApplication.TAG, "Image is not present, try to download");
				try{
					new DownloadTask().execute(url);
				} catch (RejectedExecutionException e) {
					// do nothing, just don't crash
				}
			} else {
				Log.i(JamendoApplication.TAG, "Loading album cover from file");
				this.setImageBitmap(tbmp);
				updateFileTime(dir,fileName );				
			}
			removeAlbumCoversCache(dir, fileName);
			removeRadioCoversCache(dir, fileName);
{'fraction_non_alphanumeric': 0.09817375699356065, 'fraction_numerical': 0.008550617544600443, 'mean_word_length': 3.962807752750131, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7385605', 'n_tokens_mistral': 3183, 'n_tokens_neox': 2797, 'n_words': 920}