Cache favicons when they are downloaded by the WebView for use by bookmarks

This commit is contained in:
Anthony Restaino 2015-03-26 18:46:14 -04:00
parent 58ca7fa303
commit 9ff1614a0f
2 changed files with 32 additions and 17 deletions

View File

@ -1768,28 +1768,18 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
} }
class BookmarkViewHolder { class BookmarkViewHolder {
TextView txtTitle; TextView txtTitle;
ImageView favicon; ImageView favicon;
} }
} }
private void getImage(ImageView image, HistoryItem web) { private void getImage(ImageView image, HistoryItem web) {
try { new DownloadImageTask(image, web).execute(web.getUrl());
new DownloadImageTask(image, web).execute(Utils.getProtocol(web.getUrl())
+ getDomainName(web.getUrl()) + "/favicon.ico");
} catch (URISyntaxException e) {
new DownloadImageTask(image, web)
.execute("https://www.google.com/s2/favicons?domain_url=" + web.getUrl());
e.printStackTrace();
}
} }
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage; ImageView bmImage;
HistoryItem mWeb; HistoryItem mWeb;
public DownloadImageTask(ImageView bmImage, HistoryItem web) { public DownloadImageTask(ImageView bmImage, HistoryItem web) {
@ -1798,17 +1788,24 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
} }
protected Bitmap doInBackground(String... urls) { protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0]; String url = urls[0];
Bitmap mIcon = null; Bitmap mIcon = null;
// unique path for each url that is bookmarked. // unique path for each url that is bookmarked.
String hash = String.valueOf(urldisplay.hashCode()); String hash = String.valueOf(Utils.getDomainName(url).hashCode());
File image = new File(mContext.getCacheDir(), hash + ".png"); File image = new File(mContext.getCacheDir(), hash + ".png");
String urldisplay;
try {
urldisplay = Utils.getProtocol(url) + getDomainName(url) + "/favicon.ico";
} catch (URISyntaxException e) {
e.printStackTrace();
urldisplay = "https://www.google.com/s2/favicons?domain_url=" + url;
}
// checks to see if the image exists // checks to see if the image exists
if (!image.exists()) { if (!image.exists()) {
try { try {
// if not, download it... // if not, download it...
URL url = new URL(urldisplay); URL urlDownload = new URL(urldisplay);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
connection.setDoInput(true); connection.setDoInput(true);
connection.connect(); connection.connect();
InputStream in = connection.getInputStream(); InputStream in = connection.getInputStream();
@ -1835,8 +1832,9 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
if (mIcon == null) { if (mIcon == null) {
try { try {
// if not, download it... // if not, download it...
URL url = new URL("https://www.google.com/s2/favicons?domain_url=" + urldisplay); URL urlDownload = new URL("https://www.google.com/s2/favicons?domain_url="
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + url);
HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
connection.setDoInput(true); connection.setDoInput(true);
connection.connect(); connection.connect();
InputStream in = connection.getInputStream(); InputStream in = connection.getInputStream();

View File

@ -524,6 +524,22 @@ public class LightningView {
} }
} }
private void cacheFavicon(Bitmap icon) {
String hash = String.valueOf(Utils.getDomainName(getUrl()).hashCode());
Log.d(Constants.TAG, "Caching icon for " + Utils.getDomainName(getUrl()));
File image = new File(mActivity.getCacheDir(), hash + ".png");
try {
FileOutputStream fos = new FileOutputStream(image);
icon.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@SuppressLint("NewApi") @SuppressLint("NewApi")
public synchronized void find(String text) { public synchronized void find(String text) {
@ -943,6 +959,7 @@ public class LightningView {
public void onReceivedIcon(WebView view, Bitmap icon) { public void onReceivedIcon(WebView view, Bitmap icon) {
mTitle.setFavicon(icon); mTitle.setFavicon(icon);
mBrowserController.update(); mBrowserController.update();
cacheFavicon(icon);
} }
@Override @Override