Async loading of homepage, delegate IOThread responsibility to BrowserApp class
This commit is contained in:
parent
e06d530528
commit
7318a818c4
@ -1342,7 +1342,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
|
||||
if (UrlUtils.isSpecialUrl(url)) {
|
||||
return;
|
||||
}
|
||||
BrowserApp.getHistoryDatabase().getIOThread().execute(new Runnable() {
|
||||
BrowserApp.getIOThread().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
@ -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
|
||||
BrowserApp.getHistoryDatabase().getIOThread().execute(new Runnable() {
|
||||
BrowserApp.getIOThread().execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -6,6 +6,9 @@ import android.content.Context;
|
||||
import com.squareup.leakcanary.LeakCanary;
|
||||
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.database.HistoryDatabase;
|
||||
import acr.browser.lightning.preference.PreferenceManager;
|
||||
@ -14,6 +17,7 @@ public class BrowserApp extends Application {
|
||||
|
||||
private static BrowserApp sInstance;
|
||||
private static AppComponent appComponent;
|
||||
private static Executor mIOThread = Executors.newSingleThreadExecutor();
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
@ -39,6 +43,10 @@ public class BrowserApp extends Application {
|
||||
return appComponent.getHistoryDatabase();
|
||||
}
|
||||
|
||||
public static Executor getIOThread() {
|
||||
return mIOThread;
|
||||
}
|
||||
|
||||
public static PreferenceManager getPreferenceManager() {
|
||||
return appComponent.getPreferenceManager();
|
||||
}
|
||||
|
@ -42,8 +42,6 @@ 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);
|
||||
@ -78,10 +76,6 @@ public class HistoryDatabase extends SQLiteOpenHelper {
|
||||
return mDatabase == null || !mDatabase.isOpen();
|
||||
}
|
||||
|
||||
public Executor getIOThread() {
|
||||
return mIOThread;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (mDatabase != null) {
|
||||
|
@ -149,14 +149,12 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
BrowserApp.getHistoryDatabase()
|
||||
.getIOThread()
|
||||
.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
clearHistory();
|
||||
}
|
||||
});
|
||||
BrowserApp.getIOThread().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
clearHistory();
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.setNegativeButton(getResources().getString(R.string.action_no), null).show();
|
||||
|
@ -147,38 +147,66 @@ public class LightningView {
|
||||
if (mWebView == null) {
|
||||
return;
|
||||
}
|
||||
if (mHomepage.startsWith("about:home")) {
|
||||
mWebView.loadUrl(StartPage.getHomepage(mActivity), mRequestHeaders);
|
||||
} else if (mHomepage.startsWith("about:bookmarks")) {
|
||||
loadBookmarkpage();
|
||||
} else {
|
||||
mWebView.loadUrl(mHomepage, mRequestHeaders);
|
||||
switch (mHomepage) {
|
||||
case "about:home":
|
||||
loadStartpage();
|
||||
break;
|
||||
case "about:bookmarks":
|
||||
loadBookmarkpage();
|
||||
break;
|
||||
default:
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the HTML bookmarks page in this view
|
||||
*/
|
||||
public void loadBookmarkpage() {
|
||||
if (mWebView == null)
|
||||
return;
|
||||
Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false);
|
||||
FileOutputStream outputStream = null;
|
||||
File image = new File(mActivity.getCacheDir(), "folder.png");
|
||||
try {
|
||||
outputStream = new FileOutputStream(image);
|
||||
folderIcon.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
|
||||
folderIcon.recycle();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
Utils.close(outputStream);
|
||||
}
|
||||
File bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME);
|
||||
|
||||
BrowserApp.getBookmarkPage().buildBookmarkPage(null);
|
||||
mWebView.loadUrl(Constants.FILE + bookmarkWebPage, mRequestHeaders);
|
||||
BrowserApp.getIOThread().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false);
|
||||
FileOutputStream outputStream = null;
|
||||
File image = new File(mActivity.getCacheDir(), "folder.png");
|
||||
try {
|
||||
outputStream = new FileOutputStream(image);
|
||||
folderIcon.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
|
||||
folderIcon.recycle();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
Utils.close(outputStream);
|
||||
}
|
||||
final File bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME);
|
||||
|
||||
BrowserApp.getBookmarkPage().buildBookmarkPage(null);
|
||||
mActivity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mWebView.loadUrl(Constants.FILE + bookmarkWebPage, mRequestHeaders);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user