Browse Source

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

master
Anthony Restaino 10 years ago
parent
commit
9ff1614a0f
  1. 32
      src/acr/browser/lightning/BrowserActivity.java
  2. 17
      src/acr/browser/lightning/LightningView.java

32
src/acr/browser/lightning/BrowserActivity.java

@ -1768,28 +1768,18 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl @@ -1768,28 +1768,18 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
}
class BookmarkViewHolder {
TextView txtTitle;
ImageView favicon;
}
}
private void getImage(ImageView image, HistoryItem web) {
try {
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();
}
new DownloadImageTask(image, web).execute(web.getUrl());
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
HistoryItem mWeb;
public DownloadImageTask(ImageView bmImage, HistoryItem web) {
@ -1798,17 +1788,24 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl @@ -1798,17 +1788,24 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
String url = urls[0];
Bitmap mIcon = null;
// 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");
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
if (!image.exists()) {
try {
// if not, download it...
URL url = new URL(urldisplay);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
URL urlDownload = new URL(urldisplay);
HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream in = connection.getInputStream();
@ -1835,8 +1832,9 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl @@ -1835,8 +1832,9 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
if (mIcon == null) {
try {
// if not, download it...
URL url = new URL("https://www.google.com/s2/favicons?domain_url=" + urldisplay);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
URL urlDownload = new URL("https://www.google.com/s2/favicons?domain_url="
+ url);
HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream in = connection.getInputStream();

17
src/acr/browser/lightning/LightningView.java

@ -524,6 +524,22 @@ public class LightningView { @@ -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")
@SuppressLint("NewApi")
public synchronized void find(String text) {
@ -943,6 +959,7 @@ public class LightningView { @@ -943,6 +959,7 @@ public class LightningView {
public void onReceivedIcon(WebView view, Bitmap icon) {
mTitle.setFavicon(icon);
mBrowserController.update();
cacheFavicon(icon);
}
@Override

Loading…
Cancel
Save