Browse Source

Async loading of homepage, delegate IOThread responsibility to BrowserApp class

master
Anthony Restaino 9 years ago
parent
commit
7318a818c4
  1. 4
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 8
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  3. 6
      app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java
  4. 4
      app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java
  5. 40
      app/src/main/java/acr/browser/lightning/view/LightningView.java

4
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -1342,7 +1342,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
if (UrlUtils.isSpecialUrl(url)) { if (UrlUtils.isSpecialUrl(url)) {
return; return;
} }
BrowserApp.getHistoryDatabase().getIOThread().execute(new Runnable() { BrowserApp.getIOThread().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1406,7 +1406,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
private void openHistory() { private void openHistory() {
// use a thread so that history retrieval doesn't block the UI // use a thread so that history retrieval doesn't block the UI
BrowserApp.getHistoryDatabase().getIOThread().execute(new Runnable() { BrowserApp.getIOThread().execute(new Runnable() {
@Override @Override
public void run() { public void run() {

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

@ -6,6 +6,9 @@ import android.content.Context;
import com.squareup.leakcanary.LeakCanary; import com.squareup.leakcanary.LeakCanary;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.database.HistoryDatabase; import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
@ -14,6 +17,7 @@ public class BrowserApp extends Application {
private static BrowserApp sInstance; private static BrowserApp sInstance;
private static AppComponent appComponent; private static AppComponent appComponent;
private static Executor mIOThread = Executors.newSingleThreadExecutor();
@Override @Override
public void onCreate() { public void onCreate() {
@ -39,6 +43,10 @@ public class BrowserApp extends Application {
return appComponent.getHistoryDatabase(); return appComponent.getHistoryDatabase();
} }
public static Executor getIOThread() {
return mIOThread;
}
public static PreferenceManager getPreferenceManager() { public static PreferenceManager getPreferenceManager() {
return appComponent.getPreferenceManager(); return appComponent.getPreferenceManager();
} }

6
app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java

@ -42,8 +42,6 @@ public class HistoryDatabase extends SQLiteOpenHelper {
private SQLiteDatabase mDatabase; private SQLiteDatabase mDatabase;
private Executor mIOThread = Executors.newSingleThreadExecutor();
@Inject @Inject
public HistoryDatabase(Context context) { public HistoryDatabase(Context context) {
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION); super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
@ -78,10 +76,6 @@ public class HistoryDatabase extends SQLiteOpenHelper {
return mDatabase == null || !mDatabase.isOpen(); return mDatabase == null || !mDatabase.isOpen();
} }
public Executor getIOThread() {
return mIOThread;
}
@Override @Override
public synchronized void close() { public synchronized void close() {
if (mDatabase != null) { if (mDatabase != null) {

4
app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java

@ -149,9 +149,7 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface arg0, int arg1) { public void onClick(DialogInterface arg0, int arg1) {
BrowserApp.getHistoryDatabase() BrowserApp.getIOThread().execute(new Runnable() {
.getIOThread()
.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
clearHistory(); clearHistory();

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

@ -147,13 +147,32 @@ public class LightningView {
if (mWebView == null) { if (mWebView == null) {
return; return;
} }
if (mHomepage.startsWith("about:home")) { switch (mHomepage) {
mWebView.loadUrl(StartPage.getHomepage(mActivity), mRequestHeaders); case "about:home":
} else if (mHomepage.startsWith("about:bookmarks")) { loadStartpage();
break;
case "about:bookmarks":
loadBookmarkpage(); loadBookmarkpage();
} else { break;
default:
mWebView.loadUrl(mHomepage, mRequestHeaders); mWebView.loadUrl(mHomepage, mRequestHeaders);
break;
}
}
private void loadStartpage() {
BrowserApp.getIOThread().execute(new Runnable() {
@Override
public void run() {
final String homepage = StartPage.getHomepage(mActivity);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadUrl(homepage, mRequestHeaders);
} }
});
}
});
} }
/** /**
@ -162,6 +181,9 @@ public class LightningView {
public void loadBookmarkpage() { public void loadBookmarkpage() {
if (mWebView == null) if (mWebView == null)
return; return;
BrowserApp.getIOThread().execute(new Runnable() {
@Override
public void run() {
Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false); Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false);
FileOutputStream outputStream = null; FileOutputStream outputStream = null;
File image = new File(mActivity.getCacheDir(), "folder.png"); File image = new File(mActivity.getCacheDir(), "folder.png");
@ -174,11 +196,17 @@ public class LightningView {
} finally { } finally {
Utils.close(outputStream); Utils.close(outputStream);
} }
File bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME); final File bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME);
BrowserApp.getBookmarkPage().buildBookmarkPage(null); BrowserApp.getBookmarkPage().buildBookmarkPage(null);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadUrl(Constants.FILE + bookmarkWebPage, mRequestHeaders); mWebView.loadUrl(Constants.FILE + bookmarkWebPage, mRequestHeaders);
}
});
}
});
} }
/** /**

Loading…
Cancel
Save