130 lines
4.4 KiB
Java
130 lines
4.4 KiB
Java
package acr.browser.lightning.utils;
|
|
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.net.Uri;
|
|
import android.os.AsyncTask;
|
|
import android.support.annotation.NonNull;
|
|
import android.util.Log;
|
|
import android.widget.ImageView;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.InputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
import acr.browser.lightning.app.BrowserApp;
|
|
import acr.browser.lightning.constant.Constants;
|
|
import acr.browser.lightning.database.HistoryItem;
|
|
|
|
/**
|
|
* Created by Stefano Pacifici on 25/08/15.
|
|
*/
|
|
public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
|
|
|
|
private final ImageView bmImage;
|
|
private final HistoryItem mWeb;
|
|
private final File mCacheDir;
|
|
private final String mUrl;
|
|
private final Bitmap mDefaultBitmap;
|
|
|
|
public DownloadImageTask(@NonNull ImageView bmImage, @NonNull HistoryItem web,
|
|
@NonNull Bitmap defaultBitmap) {
|
|
this.bmImage = bmImage;
|
|
this.mWeb = web;
|
|
this.mCacheDir = BrowserApp.getAppContext().getCacheDir();
|
|
this.mUrl = web.getUrl();
|
|
this.mDefaultBitmap = defaultBitmap;
|
|
}
|
|
|
|
protected Bitmap doInBackground(Void... params) {
|
|
Bitmap mIcon = null;
|
|
// unique path for each url that is bookmarked.
|
|
if (mUrl == null) {
|
|
return mDefaultBitmap;
|
|
}
|
|
final Uri uri = Uri.parse(mUrl);
|
|
if (uri.getHost() == null || uri.getScheme() == null) {
|
|
return mDefaultBitmap;
|
|
}
|
|
final String hash = String.valueOf(uri.getHost().hashCode());
|
|
final File image = new File(mCacheDir, hash + ".png");
|
|
final String urlDisplay = uri.getScheme() + "://" + uri.getHost() + "/favicon.ico";
|
|
// checks to see if the image exists
|
|
if (!image.exists()) {
|
|
FileOutputStream fos = null;
|
|
InputStream in = null;
|
|
try {
|
|
// if not, download it...
|
|
final URL urlDownload = new URL(urlDisplay);
|
|
final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
|
|
connection.setDoInput(true);
|
|
connection.connect();
|
|
in = connection.getInputStream();
|
|
|
|
if (in != null) {
|
|
mIcon = BitmapFactory.decodeStream(in);
|
|
}
|
|
// ...and cache it
|
|
if (mIcon != null) {
|
|
fos = new FileOutputStream(image);
|
|
mIcon.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
|
fos.flush();
|
|
Log.d(Constants.TAG, "Downloaded: " + urlDisplay);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
Utils.close(in);
|
|
Utils.close(fos);
|
|
}
|
|
} else {
|
|
// if it exists, retrieve it from the cache
|
|
mIcon = BitmapFactory.decodeFile(image.getPath());
|
|
}
|
|
if (mIcon == null) {
|
|
InputStream in = null;
|
|
FileOutputStream fos = null;
|
|
try {
|
|
// if not, download it...
|
|
final URL urlDownload = new URL("https://www.google.com/s2/favicons?domain_url="
|
|
+ uri.toString());
|
|
final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
|
|
connection.setDoInput(true);
|
|
connection.connect();
|
|
in = connection.getInputStream();
|
|
|
|
if (in != null) {
|
|
mIcon = BitmapFactory.decodeStream(in);
|
|
}
|
|
// ...and cache it
|
|
if (mIcon != null) {
|
|
fos = new FileOutputStream(image);
|
|
mIcon.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
|
fos.flush();
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
Utils.close(in);
|
|
Utils.close(fos);
|
|
}
|
|
}
|
|
if (mIcon == null) {
|
|
return mDefaultBitmap;
|
|
} else {
|
|
return mIcon;
|
|
}
|
|
}
|
|
|
|
protected void onPostExecute(Bitmap result) {
|
|
final Bitmap fav = Utils.padFavicon(result);
|
|
bmImage.setImageBitmap(fav);
|
|
mWeb.setBitmap(fav);
|
|
// notifyBookmarkDataSetChanged();
|
|
}
|
|
}
|