Browse Source

Utilize IconCacheTask, add Application.get because maybe storing the application context is bad????

master
Anthony Restaino 9 years ago
parent
commit
a434c0af68
  1. 4
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  2. 13
      app/src/main/java/acr/browser/lightning/view/IconCacheTask.java
  3. 26
      app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java
  4. 6
      app/src/main/java/acr/browser/lightning/view/LightningView.java

4
app/src/main/java/acr/browser/lightning/app/BrowserApp.java

@ -33,6 +33,10 @@ public class BrowserApp extends Application { @@ -33,6 +33,10 @@ public class BrowserApp extends Application {
LeakCanary.install(this);
}
public static BrowserApp get(Context context) {
return (BrowserApp) context.getApplicationContext();
}
public static AppComponent getAppComponent() {
return appComponent;
}

13
app/src/main/java/acr/browser/lightning/view/IconCacheTask.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package acr.browser.lightning.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
@ -12,17 +13,15 @@ import acr.browser.lightning.app.BrowserApp; @@ -12,17 +13,15 @@ import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.utils.Utils;
/**
* @author Anthony C. Restaino
* @date 2015/09/29
*/
class IconCacheTask implements Runnable{
class IconCacheTask implements Runnable {
private final Uri uri;
private final Bitmap icon;
private final Context context;
public IconCacheTask(Uri uri, Bitmap icon) {
public IconCacheTask(final Uri uri, final Bitmap icon, final Context context) {
this.uri = uri;
this.icon = icon;
this.context = BrowserApp.get(context);
}
@Override
@ -31,7 +30,7 @@ class IconCacheTask implements Runnable{ @@ -31,7 +30,7 @@ class IconCacheTask implements Runnable{
Log.d(Constants.TAG, "Caching icon for " + uri.getHost());
FileOutputStream fos = null;
try {
File image = new File(BrowserApp.getContext().getCacheDir(), hash + ".png");
File image = new File(context.getCacheDir(), hash + ".png");
fos = new FileOutputStream(image);
icon.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();

26
app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java

@ -2,6 +2,7 @@ package acr.browser.lightning.view; @@ -2,6 +2,7 @@ package acr.browser.lightning.view;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
@ -28,8 +29,10 @@ import acr.browser.lightning.app.BrowserApp; @@ -28,8 +29,10 @@ import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.controller.UIController;
import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;
import acr.browser.lightning.utils.Utils;
/**
@ -63,7 +66,7 @@ class LightningChromeClient extends WebChromeClient { @@ -63,7 +66,7 @@ class LightningChromeClient extends WebChromeClient {
public void onReceivedIcon(WebView view, Bitmap icon) {
mLightningView.getTitleInfo().setFavicon(icon);
eventBus.post(new BrowserEvents.TabsChanged());
cacheFavicon(view.getUrl(), icon);
cacheFavicon(view.getUrl(), icon, mActivity);
}
/**
@ -71,30 +74,13 @@ class LightningChromeClient extends WebChromeClient { @@ -71,30 +74,13 @@ class LightningChromeClient extends WebChromeClient {
*
* @param icon the icon to cache
*/
private static void cacheFavicon(final String url, final Bitmap icon) {
private static void cacheFavicon(final String url, final Bitmap icon, final Context context) {
if (icon == null) return;
final Uri uri = Uri.parse(url);
if (uri.getHost() == null) {
return;
}
new Thread(new Runnable() {
@Override
public void run() {
String hash = String.valueOf(uri.getHost().hashCode());
Log.d(Constants.TAG, "Caching icon for " + uri.getHost());
FileOutputStream fos = null;
try {
File image = new File(BrowserApp.getContext().getCacheDir(), hash + ".png");
fos = new FileOutputStream(image);
icon.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
Utils.close(fos);
}
}
}).start();
BrowserApp.getIOThread().execute(new IconCacheTask(uri, icon, context));
}

6
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -402,11 +402,11 @@ public class LightningView { @@ -402,11 +402,11 @@ public class LightningView {
settings.setAllowUniversalAccessFromFileURLs(false);
}
settings.setAppCachePath(BrowserApp.getContext().getDir("appcache", 0).getPath());
settings.setGeolocationDatabasePath(BrowserApp.getContext().getDir("geolocation", 0).getPath());
settings.setAppCachePath(BrowserApp.get(mActivity).getDir("appcache", 0).getPath());
settings.setGeolocationDatabasePath(BrowserApp.get(mActivity).getDir("geolocation", 0).getPath());
if (API < Build.VERSION_CODES.KITKAT) {
//noinspection deprecation
settings.setDatabasePath(BrowserApp.getContext().getDir("databases", 0).getPath());
settings.setDatabasePath(BrowserApp.get(mActivity).getDir("databases", 0).getPath());
}
}

Loading…
Cancel
Save