diff --git a/app/src/main/java/acr/browser/lightning/app/AppComponent.java b/app/src/main/java/acr/browser/lightning/app/AppComponent.java index b265c9d..1254739 100644 --- a/app/src/main/java/acr/browser/lightning/app/AppComponent.java +++ b/app/src/main/java/acr/browser/lightning/app/AppComponent.java @@ -25,6 +25,7 @@ import acr.browser.lightning.fragment.TabsFragment; import acr.browser.lightning.search.SuggestionsAdapter; import acr.browser.lightning.utils.AdBlock; import acr.browser.lightning.utils.ProxyUtils; +import acr.browser.lightning.view.LightningChromeClient; import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.LightningWebClient; import dagger.Component; @@ -77,6 +78,8 @@ public interface AppComponent { void inject(SuggestionsAdapter suggestionsAdapter); - @NonNull HistoryDatabase historyDatabase(); + void inject(LightningChromeClient chromeClient); + + HistoryDatabase historyDatabase(); } diff --git a/app/src/main/java/acr/browser/lightning/app/BrowserApp.java b/app/src/main/java/acr/browser/lightning/app/BrowserApp.java index cf30a59..8866e5f 100644 --- a/app/src/main/java/acr/browser/lightning/app/BrowserApp.java +++ b/app/src/main/java/acr/browser/lightning/app/BrowserApp.java @@ -49,13 +49,13 @@ public class BrowserApp extends Application { super.onCreate(); if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() - .detectAll() - .penaltyLog() - .build()); + .detectAll() + .penaltyLog() + .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() - .detectAll() - .penaltyLog() - .build()); + .detectAll() + .penaltyLog() + .build()); } final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); @@ -95,12 +95,6 @@ public class BrowserApp extends Application { }); } - @NonNull - public static Application getApplication() { - Preconditions.checkNonNull(sApplication); - return sApplication; - } - @NonNull public static BrowserApp get(@NonNull Context context) { return (BrowserApp) context.getApplicationContext(); diff --git a/app/src/main/java/acr/browser/lightning/favicon/FaviconModel.java b/app/src/main/java/acr/browser/lightning/favicon/FaviconModel.java index 3b51104..350e6b9 100644 --- a/app/src/main/java/acr/browser/lightning/favicon/FaviconModel.java +++ b/app/src/main/java/acr/browser/lightning/favicon/FaviconModel.java @@ -17,6 +17,9 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import javax.inject.Inject; +import javax.inject.Singleton; + import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.utils.Utils; @@ -24,14 +27,18 @@ import acr.browser.lightning.utils.Utils; * Reactive model that can fetch favicons * from URLs and also cache them. */ +@Singleton public class FaviconModel { private static final String TAG = "FaviconModel"; - private final ImageFetcher mImageFetcher; + @NonNull private final ImageFetcher mImageFetcher; + @NonNull private final Application mApplication; - public FaviconModel() { + @Inject + public FaviconModel(@NonNull Application application) { mImageFetcher = new ImageFetcher(); + mApplication = application; } @NonNull @@ -63,9 +70,7 @@ public class FaviconModel { return; } - Application app = BrowserApp.getApplication(); - - File faviconCacheFile = createFaviconCacheFile(app, uri); + File faviconCacheFile = createFaviconCacheFile(mApplication, uri); Bitmap favicon = null; @@ -117,13 +122,11 @@ public class FaviconModel { return; } - Application app = BrowserApp.getApplication(); - Log.d(TAG, "Caching icon for " + uri.getHost()); FileOutputStream fos = null; try { - File image = createFaviconCacheFile(app, uri); + File image = createFaviconCacheFile(mApplication, uri); fos = new FileOutputStream(image); favicon.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); diff --git a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java index 3968405..fbb75f4 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java @@ -88,7 +88,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @Inject PreferenceManager mPreferenceManager; - private FaviconModel mFaviconModel; + @Inject FaviconModel mFaviconModel; private TabsManager mTabsManager; @@ -148,8 +148,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, mFolderBitmap = ThemeUtils.getThemedBitmap(context, R.drawable.ic_folder, darkTheme); mIconColor = darkTheme ? ThemeUtils.getIconDarkThemeColor(context) : ThemeUtils.getIconLightThemeColor(context); - - mFaviconModel = new FaviconModel(); } private TabsManager getTabsManager() { @@ -269,8 +267,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, mFolderBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, darkTheme); mIconColor = darkTheme ? ThemeUtils.getIconDarkThemeColor(activity) : ThemeUtils.getIconLightThemeColor(activity); - - mFaviconModel = new FaviconModel(); } private void updateBookmarkIndicator(final String url) { diff --git a/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java b/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java index 1ab5694..2aab716 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java @@ -22,13 +22,16 @@ import com.anthonycr.bonsai.Schedulers; import com.anthonycr.grant.PermissionsManager; import com.anthonycr.grant.PermissionsResultAction; +import javax.inject.Inject; + import acr.browser.lightning.R; +import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.controller.UIController; import acr.browser.lightning.dialog.BrowserDialog; import acr.browser.lightning.favicon.FaviconModel; import acr.browser.lightning.utils.Preconditions; -class LightningChromeClient extends WebChromeClient { +public class LightningChromeClient extends WebChromeClient { private static final String TAG = LightningChromeClient.class.getSimpleName(); @@ -37,15 +40,15 @@ class LightningChromeClient extends WebChromeClient { @NonNull private final Activity mActivity; @NonNull private final LightningView mLightningView; @NonNull private final UIController mUIController; - @NonNull private final FaviconModel mFaviconModel; + @Inject FaviconModel mFaviconModel; LightningChromeClient(@NonNull Activity activity, @NonNull LightningView lightningView) { Preconditions.checkNonNull(activity); Preconditions.checkNonNull(lightningView); + BrowserApp.getAppComponent().inject(this); mActivity = activity; mUIController = (UIController) activity; mLightningView = lightningView; - mFaviconModel = new FaviconModel(); } @Override