|
|
@ -14,6 +14,7 @@ import com.anthonycr.bonsai.SingleSubscriber; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URL; |
|
|
@ -34,11 +35,12 @@ public class ImageDownloader { |
|
|
|
|
|
|
|
|
|
|
|
@Inject Application mApp; |
|
|
|
@Inject Application mApp; |
|
|
|
|
|
|
|
|
|
|
|
@NonNull private Bitmap mDefaultBitmap; |
|
|
|
@NonNull private final Bitmap mDefaultBitmap; |
|
|
|
|
|
|
|
@NonNull private final BitmapFactory.Options mLoaderOptions = new BitmapFactory.Options(); |
|
|
|
|
|
|
|
|
|
|
|
public ImageDownloader(@NonNull Bitmap defaultBitmap) { |
|
|
|
public ImageDownloader(@NonNull Bitmap defaultBitmap) { |
|
|
|
mDefaultBitmap = defaultBitmap; |
|
|
|
|
|
|
|
BrowserApp.getAppComponent().inject(this); |
|
|
|
BrowserApp.getAppComponent().inject(this); |
|
|
|
|
|
|
|
mDefaultBitmap = defaultBitmap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -54,7 +56,7 @@ public class ImageDownloader { |
|
|
|
return Single.create(new SingleAction<Bitmap>() { |
|
|
|
return Single.create(new SingleAction<Bitmap>() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onSubscribe(@NonNull SingleSubscriber<Bitmap> subscriber) { |
|
|
|
public void onSubscribe(@NonNull SingleSubscriber<Bitmap> subscriber) { |
|
|
|
Bitmap favicon = retrieveBitmap(mApp, mDefaultBitmap, url); |
|
|
|
Bitmap favicon = retrieveFaviconForUrl(url); |
|
|
|
|
|
|
|
|
|
|
|
Bitmap paddedFavicon = Utils.padFavicon(favicon); |
|
|
|
Bitmap paddedFavicon = Utils.padFavicon(favicon); |
|
|
|
|
|
|
|
|
|
|
@ -65,69 +67,68 @@ public class ImageDownloader { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
private static Bitmap retrieveBitmap(@NonNull Application app, |
|
|
|
private Bitmap retrieveFaviconForUrl(@Nullable String url) { |
|
|
|
@NonNull Bitmap defaultBitmap, |
|
|
|
|
|
|
|
@Nullable String url) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// unique path for each url that is bookmarked.
|
|
|
|
// unique path for each url that is bookmarked.
|
|
|
|
if (url == null) { |
|
|
|
if (url == null) { |
|
|
|
return defaultBitmap; |
|
|
|
return mDefaultBitmap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Bitmap icon = null; |
|
|
|
Bitmap icon; |
|
|
|
File cache = app.getCacheDir(); |
|
|
|
File cache = mApp.getCacheDir(); |
|
|
|
final Uri uri = Uri.parse(url); |
|
|
|
Uri uri = Uri.parse(url); |
|
|
|
|
|
|
|
|
|
|
|
if (uri.getHost() == null || uri.getScheme() == null || Constants.FILE.startsWith(uri.getScheme())) { |
|
|
|
if (uri.getHost() == null || uri.getScheme() == null || Constants.FILE.startsWith(uri.getScheme())) { |
|
|
|
return defaultBitmap; |
|
|
|
return mDefaultBitmap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final String hash = String.valueOf(uri.getHost().hashCode()); |
|
|
|
String hash = String.valueOf(uri.getHost().hashCode()); |
|
|
|
final File image = new File(cache, hash + ".png"); |
|
|
|
File image = new File(cache, hash + ".png"); |
|
|
|
final String urlDisplay = uri.getScheme() + "://" + uri.getHost() + "/favicon.ico"; |
|
|
|
String urlDisplay = uri.getScheme() + "://" + uri.getHost() + "/favicon.ico"; |
|
|
|
|
|
|
|
|
|
|
|
// checks to see if the image exists
|
|
|
|
if (image.exists()) { |
|
|
|
if (!image.exists()) { |
|
|
|
// If image exists, pull it from the cache
|
|
|
|
FileOutputStream fos = null; |
|
|
|
icon = BitmapFactory.decodeFile(image.getPath()); |
|
|
|
InputStream in = null; |
|
|
|
} else { |
|
|
|
try { |
|
|
|
// Otherwise, load it from network
|
|
|
|
// if not, download it...
|
|
|
|
icon = retrieveBitmapFromUrl(urlDisplay); |
|
|
|
final URL urlDownload = new URL(urlDisplay); |
|
|
|
} |
|
|
|
final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection(); |
|
|
|
|
|
|
|
connection.setDoInput(true); |
|
|
|
|
|
|
|
connection.setConnectTimeout(1000); |
|
|
|
|
|
|
|
connection.setReadTimeout(1000); |
|
|
|
|
|
|
|
connection.connect(); |
|
|
|
|
|
|
|
in = connection.getInputStream(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (in != null) { |
|
|
|
if (icon == null) { |
|
|
|
icon = BitmapFactory.decodeStream(in); |
|
|
|
String googleFaviconUrl = "https://www.google.com/s2/favicons?domain_url=" + uri.toString(); |
|
|
|
|
|
|
|
icon = retrieveBitmapFromUrl(googleFaviconUrl); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (icon == null) { |
|
|
|
|
|
|
|
return mDefaultBitmap; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
cacheBitmap(image, icon); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return icon; |
|
|
|
} |
|
|
|
} |
|
|
|
// ...and cache it
|
|
|
|
|
|
|
|
if (icon != null) { |
|
|
|
|
|
|
|
fos = new FileOutputStream(image); |
|
|
|
|
|
|
|
icon.compress(Bitmap.CompressFormat.PNG, 100, fos); |
|
|
|
|
|
|
|
fos.flush(); |
|
|
|
|
|
|
|
Log.d(Constants.TAG, "Downloaded: " + urlDisplay); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception ignored) { |
|
|
|
private void cacheBitmap(@NonNull File cacheFile, @NonNull Bitmap imageToCache) { |
|
|
|
Log.d(TAG, "Could not download: " + urlDisplay); |
|
|
|
FileOutputStream fos = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fos = new FileOutputStream(cacheFile); |
|
|
|
|
|
|
|
imageToCache.compress(Bitmap.CompressFormat.PNG, 100, fos); |
|
|
|
|
|
|
|
fos.flush(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
Log.e(TAG, "Could not cache icon"); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
Utils.close(in); |
|
|
|
|
|
|
|
Utils.close(fos); |
|
|
|
Utils.close(fos); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
// if it exists, retrieve it from the cache
|
|
|
|
|
|
|
|
icon = BitmapFactory.decodeFile(image.getPath()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (icon == null) { |
|
|
|
@Nullable |
|
|
|
|
|
|
|
private Bitmap retrieveBitmapFromUrl(@NonNull String url) { |
|
|
|
InputStream in = null; |
|
|
|
InputStream in = null; |
|
|
|
FileOutputStream fos = null; |
|
|
|
Bitmap icon = null; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
// if not, download it...
|
|
|
|
final URL urlDownload = new URL(url); |
|
|
|
final URL urlDownload = new URL("https://www.google.com/s2/favicons?domain_url=" + uri.toString()); |
|
|
|
|
|
|
|
final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection(); |
|
|
|
final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection(); |
|
|
|
connection.setDoInput(true); |
|
|
|
connection.setDoInput(true); |
|
|
|
connection.setConnectTimeout(1000); |
|
|
|
connection.setConnectTimeout(1000); |
|
|
@ -136,27 +137,16 @@ public class ImageDownloader { |
|
|
|
in = connection.getInputStream(); |
|
|
|
in = connection.getInputStream(); |
|
|
|
|
|
|
|
|
|
|
|
if (in != null) { |
|
|
|
if (in != null) { |
|
|
|
icon = BitmapFactory.decodeStream(in); |
|
|
|
icon = BitmapFactory.decodeStream(in, null, mLoaderOptions); |
|
|
|
} |
|
|
|
} |
|
|
|
// ...and cache it
|
|
|
|
} catch (Exception ignored) { |
|
|
|
if (icon != null) { |
|
|
|
Log.d(TAG, "Could not download icon from: " + url); |
|
|
|
fos = new FileOutputStream(image); |
|
|
|
|
|
|
|
icon.compress(Bitmap.CompressFormat.PNG, 100, fos); |
|
|
|
|
|
|
|
fos.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
Log.d(TAG, "Could not download Google favicon"); |
|
|
|
|
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
Utils.close(in); |
|
|
|
Utils.close(in); |
|
|
|
Utils.close(fos); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (icon == null) { |
|
|
|
|
|
|
|
return defaultBitmap; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return icon; |
|
|
|
return icon; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|