From f00bb778513f590752d556ff4b067b3febe2b236 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sun, 10 Jan 2016 14:45:03 -0500 Subject: [PATCH] Start using a single thread executor for any database access to eliminate unnecessary thread creation --- .../lightning/activity/BrowserActivity.java | 19 +++++++++---------- .../lightning/database/HistoryDatabase.java | 8 ++++++++ .../fragment/PrivacySettingsFragment.java | 17 ++++++++++------- .../browser/lightning/view/LightningView.java | 15 --------------- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 06cc35a..efcc473 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -173,7 +173,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private boolean mShowTabsInDrawer; private int mOriginalOrientation, mBackgroundColor, mIdGenerator, mIconColor, mCurrentUiColor = Color.BLACK; - private String mSearchText, mUntitledTitle, mHomepage, mCameraPhotoPath; + private String mSearchText; + private String mUntitledTitle; + private String mCameraPhotoPath; // The singleton BookmarkManager @Inject @@ -258,8 +260,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mWebpageBitmap = ThemeUtils.getThemedBitmap(this, R.drawable.ic_webpage, mDarkTheme); - mHomepage = mPreferences.getHomepage(); - final TabsFragment tabsFragment = new TabsFragment(); final int containerId = mShowTabsInDrawer ? R.id.left_drawer : R.id.tabs_toolbar_container; final Bundle tabsFragmentArguments = new Bundle(); @@ -1339,7 +1339,10 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } void addItemToHistory(@Nullable final String title, @NonNull final String url) { - Runnable update = new Runnable() { + if (UrlUtils.isSpecialUrl(url)) { + return; + } + BrowserApp.getAppComponent().getHistoryDatabase().getIOThread().execute(new Runnable() { @Override public void run() { try { @@ -1352,10 +1355,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements Log.e(Constants.TAG, "SQLiteException in updateHistory", e); } } - }; - if (!UrlUtils.isSpecialUrl(url)) { - new Thread(update).start(); - } + }); } /** @@ -1406,7 +1406,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements */ private void openHistory() { // use a thread so that history retrieval doesn't block the UI - Thread history = new Thread(new Runnable() { + BrowserApp.getAppComponent().getHistoryDatabase().getIOThread().execute(new Runnable() { @Override public void run() { @@ -1415,7 +1415,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } }); - history.run(); } /** diff --git a/app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java b/app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java index bc1895d..ce8323b 100644 --- a/app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java +++ b/app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java @@ -13,6 +13,8 @@ import android.support.annotation.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import javax.inject.Inject; import javax.inject.Singleton; @@ -40,6 +42,8 @@ public class HistoryDatabase extends SQLiteOpenHelper { private SQLiteDatabase mDatabase; + private Executor mIOThread = Executors.newSingleThreadExecutor(); + @Inject public HistoryDatabase(Context context) { super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION); @@ -74,6 +78,10 @@ public class HistoryDatabase extends SQLiteOpenHelper { return mDatabase == null || !mDatabase.isOpen(); } + public Executor getIOThread() { + return mIOThread; + } + @Override public synchronized void close() { if (mDatabase != null) { diff --git a/app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java index 4b76aa9..9a3aef1 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java @@ -15,6 +15,7 @@ import android.support.v7.app.AlertDialog; import android.webkit.WebView; import acr.browser.lightning.R; +import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.WebUtils; import acr.browser.lightning.view.LightningView; @@ -148,13 +149,15 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { - Thread clear = new Thread(new Runnable() { - @Override - public void run() { - clearHistory(); - } - }); - clear.start(); + BrowserApp.getAppComponent() + .getHistoryDatabase() + .getIOThread() + .execute(new Runnable() { + @Override + public void run() { + clearHistory(); + } + }); } }) .setNegativeButton(getResources().getString(R.string.action_no), null).show(); diff --git a/app/src/main/java/acr/browser/lightning/view/LightningView.java b/app/src/main/java/acr/browser/lightning/view/LightningView.java index ff80b8b..c5fb309 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -12,7 +12,6 @@ import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; -import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Message; @@ -546,20 +545,6 @@ public class LightningView { } } - /** - * Naive caching of the favicon according to the domain name of the URL - * - * @param icon the icon to cache - */ - private void cacheFavicon(final Bitmap icon) { - if (icon == null) return; - final Uri uri = Uri.parse(getUrl()); - if (uri.getHost() == null) { - return; - } - new Thread(new IconCacheTask(uri, icon)).start(); - } - @SuppressLint("NewApi") public synchronized void find(String text) { if (mWebView != null) {