Browse Source

Moving to asynchronous consumption of the HistoryDatabase

master
anthony restaino 8 years ago
parent
commit
bed8163399
  1. 2
      app/build.gradle
  2. 48
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  3. 25
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  4. 26
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  5. 67
      app/src/main/java/acr/browser/lightning/constant/HistoryPage.java
  6. 70
      app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java
  7. 84
      app/src/main/java/acr/browser/lightning/database/HistoryModel.java
  8. 18
      app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java
  9. 7
      app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java
  10. 17
      app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java
  11. 10
      app/src/main/java/acr/browser/lightning/utils/WebUtils.java
  12. 4
      build.gradle

2
app/build.gradle

@ -99,7 +99,7 @@ dependencies { @@ -99,7 +99,7 @@ dependencies {
compile "info.guardianproject.netcipher:netcipher:$netcipherVersion"
compile "info.guardianproject.netcipher:netcipher-webkit:$netcipherVersion"
compile 'com.anthonycr.bonsai:bonsai:1.0'
compile 'com.anthonycr.bonsai:bonsai:1.1.0'
compile 'com.anthonycr.progress:animated-progress:1.0'

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

@ -13,7 +13,6 @@ import android.content.DialogInterface; @@ -13,7 +13,6 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteException;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
@ -81,7 +80,9 @@ import android.widget.TextView.OnEditorActionListener; @@ -81,7 +80,9 @@ import android.widget.TextView.OnEditorActionListener;
import android.widget.VideoView;
import com.anthonycr.bonsai.Completable;
import com.anthonycr.bonsai.CompletableOnSubscribe;
import com.anthonycr.bonsai.Schedulers;
import com.anthonycr.bonsai.SingleOnSubscribe;
import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.progress.AnimatedProgressBar;
import com.squareup.otto.Bus;
@ -105,8 +106,8 @@ import acr.browser.lightning.constant.Constants; @@ -105,8 +106,8 @@ import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel;
import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.fragment.BookmarksFragment;
@ -115,6 +116,7 @@ import acr.browser.lightning.interpolator.BezierDecelerateInterpolator; @@ -115,6 +116,7 @@ import acr.browser.lightning.interpolator.BezierDecelerateInterpolator;
import acr.browser.lightning.receiver.NetworkReceiver;
import acr.browser.lightning.search.SuggestionsAdapter;
import acr.browser.lightning.utils.DrawableUtils;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.UrlUtils;
@ -197,8 +199,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -197,8 +199,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private TabsManager mTabsManager;
@Inject HistoryDatabase mHistoryDatabase;
// Image
private Bitmap mWebpageBitmap;
private final ColorDrawable mBackground = new ColorDrawable();
@ -1139,7 +1139,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1139,7 +1139,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
Log.d(TAG, "Cache Cleared");
}
if (mPreferences.getClearHistoryExitEnabled() && !isIncognito()) {
WebUtils.clearHistory(this, mHistoryDatabase);
WebUtils.clearHistory(this);
Log.d(TAG, "History Cleared");
}
if (mPreferences.getClearCookiesExitEnabled() && !isIncognito()) {
@ -1280,11 +1280,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1280,11 +1280,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mPresenter.shutdown();
if (mHistoryDatabase != null) {
mHistoryDatabase.close();
mHistoryDatabase = null;
}
super.onDestroy();
}
@ -1481,18 +1476,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1481,18 +1476,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
if (UrlUtils.isSpecialUrl(url)) {
return;
}
BrowserApp.getIOThread().execute(new Runnable() {
HistoryModel.visitHistoryItem(url, title)
.subscribeOn(Schedulers.io())
.subscribe(new CompletableOnSubscribe() {
@Override
public void run() {
try {
mHistoryDatabase.visitHistoryItem(url, title);
} catch (IllegalStateException e) {
Log.e(TAG, "IllegalStateException in updateHistory", e);
} catch (NullPointerException e) {
Log.e(TAG, "NullPointerException in updateHistory", e);
} catch (SQLiteException e) {
Log.e(TAG, "SQLiteException in updateHistory", e);
}
public void onError(@NonNull Throwable throwable) {
Log.e(TAG, "Exception while updating history", throwable);
}
});
}
@ -1543,7 +1533,19 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1543,7 +1533,19 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* function that opens the HTML history page in the browser
*/
private void openHistory() {
new HistoryPage(mTabsManager.getCurrentTab(), getApplication(), mHistoryDatabase).load();
HistoryPage.getHistoryPage()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
LightningView view = mTabsManager.getCurrentTab();
if (view != null) {
view.loadUrl(item);
}
}
});
}
private View getBookmarkDrawer() {
@ -2190,7 +2192,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2190,7 +2192,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Subscribe
public void loadHistory(final BrowserEvents.OpenHistoryInCurrentTab event) {
new HistoryPage(mTabsManager.getCurrentTab(), getApplication(), mHistoryDatabase).load();
openHistory();
}
/**

25
app/src/main/java/acr/browser/lightning/activity/TabsManager.java

@ -17,10 +17,7 @@ import android.webkit.WebView; @@ -17,10 +17,7 @@ import android.webkit.WebView;
import com.anthonycr.bonsai.Completable;
import com.anthonycr.bonsai.CompletableAction;
import com.anthonycr.bonsai.CompletableSubscriber;
import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleOnSubscribe;
import com.anthonycr.bonsai.SingleSubscriber;
import com.anthonycr.bonsai.Stream;
import com.anthonycr.bonsai.StreamAction;
import com.anthonycr.bonsai.StreamOnSubscribe;
@ -39,13 +36,13 @@ import acr.browser.lightning.constant.Constants; @@ -39,13 +36,13 @@ import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.preference.PreferenceManager;
import com.anthonycr.bonsai.Schedulers;
import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.UrlUtils;
import acr.browser.lightning.view.LightningView;
@ -70,7 +67,6 @@ public class TabsManager { @@ -70,7 +67,6 @@ public class TabsManager {
@Inject PreferenceManager mPreferenceManager;
@Inject BookmarkManager mBookmarkManager;
@Inject HistoryDatabase mHistoryManager;
@Inject Bus mEventBus;
@Inject Application mApp;
@ -158,11 +154,13 @@ public class TabsManager { @@ -158,11 +154,13 @@ public class TabsManager {
private void restoreLostTabs(@Nullable final String url, @NonNull final Activity activity,
@NonNull final CompletableSubscriber subscriber) {
restoreState().subscribeOn(Schedulers.io())
.observeOn(Schedulers.main()).subscribe(new StreamOnSubscribe<Bundle>() {
restoreState()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new StreamOnSubscribe<Bundle>() {
@Override
public void onNext(@Nullable Bundle item) {
LightningView tab = newTab(activity, "", false);
final LightningView tab = newTab(activity, "", false);
String url = item.getString(URL_KEY);
if (url != null && tab.getWebView() != null) {
if (UrlUtils.isBookmarkUrl(url)) {
@ -170,7 +168,16 @@ public class TabsManager { @@ -170,7 +168,16 @@ public class TabsManager {
} else if (UrlUtils.isStartPageUrl(url)) {
new StartPage(tab, mApp).load();
} else if (UrlUtils.isHistoryUrl(url)) {
new HistoryPage(tab, mApp, mHistoryManager).load();
HistoryPage.getHistoryPage()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
tab.loadUrl(item);
}
});
}
} else if (tab.getWebView() != null) {
tab.getWebView().restoreState(item);

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

@ -8,6 +8,7 @@ import android.content.Context; @@ -8,6 +8,7 @@ import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.webkit.WebView;
@ -23,18 +24,26 @@ import acr.browser.lightning.BuildConfig; @@ -23,18 +24,26 @@ import acr.browser.lightning.BuildConfig;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.MemoryLeakUtils;
import acr.browser.lightning.utils.Preconditions;
public class BrowserApp extends Application {
private static final String TAG = BrowserApp.class.getSimpleName();
private static AppComponent mAppComponent;
@Nullable private static Application sApplication;
@Nullable private static AppComponent sAppComponent;
private static final Executor mIOThread = Executors.newSingleThreadExecutor();
private static final Executor mTaskThread = Executors.newCachedThreadPool();
@Inject Bus mBus;
@Inject PreferenceManager mPreferenceManager;
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
sApplication = this;
}
@Override
public void onCreate() {
super.onCreate();
@ -67,8 +76,8 @@ public class BrowserApp extends Application { @@ -67,8 +76,8 @@ public class BrowserApp extends Application {
}
});
mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
mAppComponent.inject(this);
sAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
sAppComponent.inject(this);
if (mPreferenceManager.getUseLeakCanary() && !isRelease()) {
LeakCanary.install(this);
@ -86,13 +95,21 @@ public class BrowserApp extends Application { @@ -86,13 +95,21 @@ 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();
}
@NonNull
public static AppComponent getAppComponent() {
return mAppComponent;
Preconditions.checkNonNull(sAppComponent);
return sAppComponent;
}
@NonNull
@ -105,6 +122,7 @@ public class BrowserApp extends Application { @@ -105,6 +122,7 @@ public class BrowserApp extends Application {
return mTaskThread;
}
@NonNull
public static Bus getBus(@NonNull Context context) {
return get(context).mBus;
}

67
app/src/main/java/acr/browser/lightning/constant/HistoryPage.java

@ -4,26 +4,29 @@ @@ -4,26 +4,29 @@
package acr.browser.lightning.constant;
import android.app.Application;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleOnSubscribe;
import com.anthonycr.bonsai.SingleSubscriber;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.List;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.view.LightningView;
public class HistoryPage extends AsyncTask<Void, Void, Void> {
public class HistoryPage {
private static final String TAG = HistoryPage.class.getSimpleName();
@ -43,41 +46,23 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> { @@ -43,41 +46,23 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> {
private static final String END = "</div></body></html>";
@NonNull private final WeakReference<LightningView> mTabReference;
@NonNull private final Application mApp;
@NonNull private final String mTitle;
private final HistoryDatabase mHistoryDatabase;
@Nullable private String mHistoryUrl = null;
public HistoryPage(LightningView tab, @NonNull Application app, HistoryDatabase database) {
mTabReference = new WeakReference<>(tab);
mApp = app;
mTitle = app.getString(R.string.action_history);
mHistoryDatabase = database;
}
private HistoryPage() {}
@Nullable
@NonNull
public static Single<String> getHistoryPage() {
return Single.create(new SingleAction<String>() {
@Override
protected Void doInBackground(Void... params) {
mHistoryUrl = getHistoryPage();
return null;
}
public void onSubscribe(@NonNull final SingleSubscriber<String> subscriber) {
final String title = BrowserApp.getApplication().getString(R.string.action_history);
final StringBuilder historyBuilder = new StringBuilder(HEADING_1 + title + HEADING_2);
HistoryModel.lastHundredVisitedHistoryItems()
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LightningView tab = mTabReference.get();
if (tab != null && mHistoryUrl != null) {
tab.loadUrl(mHistoryUrl);
}
}
public void onItem(@Nullable List<HistoryItem> item) {
@NonNull
private String getHistoryPage() {
StringBuilder historyBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
List<HistoryItem> historyList = mHistoryDatabase.getLastHundredItems();
Iterator<HistoryItem> it = historyList.iterator();
Preconditions.checkNonNull(item);
Iterator<HistoryItem> it = item.iterator();
HistoryItem helper;
while (it.hasNext()) {
helper = it.next();
@ -91,7 +76,7 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> { @@ -91,7 +76,7 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> {
}
historyBuilder.append(END);
File historyWebPage = new File(mApp.getFilesDir(), FILENAME);
File historyWebPage = new File(BrowserApp.getApplication().getFilesDir(), FILENAME);
FileWriter historyWriter = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
@ -102,11 +87,13 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> { @@ -102,11 +87,13 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> {
} finally {
Utils.close(historyWriter);
}
return Constants.FILE + historyWebPage;
}
public void load() {
executeOnExecutor(BrowserApp.getIOThread());
subscriber.onItem(Constants.FILE + historyWebPage);
subscriber.onComplete();
}
});
}
});
}
/**

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

@ -4,7 +4,6 @@ @@ -4,7 +4,6 @@
package acr.browser.lightning.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
@ -16,15 +15,11 @@ import android.support.annotation.WorkerThread; @@ -16,15 +15,11 @@ import android.support.annotation.WorkerThread;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
@SuppressWarnings("unused")
@WorkerThread
@Singleton
public class HistoryDatabase extends SQLiteOpenHelper {
class HistoryDatabase extends SQLiteOpenHelper {
// All Static variables
// Database Version
@ -42,15 +37,24 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -42,15 +37,24 @@ public class HistoryDatabase extends SQLiteOpenHelper {
private static final String KEY_TITLE = "title";
private static final String KEY_TIME_VISITED = "time";
@Nullable
private SQLiteDatabase mDatabase;
@Nullable private SQLiteDatabase mDatabase;
@Nullable private static HistoryDatabase sInstance;
@Inject
public HistoryDatabase(@NonNull Context context) {
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
private HistoryDatabase() {
super(BrowserApp.getApplication(), DATABASE_NAME, null, DATABASE_VERSION);
mDatabase = HistoryDatabase.this.getWritableDatabase();
}
@NonNull
public synchronized static HistoryDatabase getInstance() {
if (sInstance == null) {
sInstance = new HistoryDatabase();
}
return sInstance;
}
// Creating Tables
@Override
public void onCreate(@NonNull SQLiteDatabase db) {
@ -78,6 +82,16 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -78,6 +82,16 @@ public class HistoryDatabase extends SQLiteOpenHelper {
super.close();
}
@NonNull
private static HistoryItem fromCursor(@NonNull Cursor cursor) {
HistoryItem historyItem = new HistoryItem();
historyItem.setUrl(cursor.getString(1));
historyItem.setTitle(cursor.getString(2));
historyItem.setImageId(R.drawable.ic_history);
return historyItem;
}
@WorkerThread
@NonNull
private SQLiteDatabase openIfNecessary() {
@ -88,7 +102,7 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -88,7 +102,7 @@ public class HistoryDatabase extends SQLiteOpenHelper {
}
@WorkerThread
public synchronized void deleteHistory() {
synchronized void deleteHistory() {
mDatabase = openIfNecessary();
mDatabase.delete(TABLE_HISTORY, null, null);
mDatabase.close();
@ -96,13 +110,13 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -96,13 +110,13 @@ public class HistoryDatabase extends SQLiteOpenHelper {
}
@WorkerThread
public synchronized void deleteHistoryItem(@NonNull String url) {
synchronized void deleteHistoryItem(@NonNull String url) {
mDatabase = openIfNecessary();
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url});
}
@WorkerThread
public synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) {
synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) {
mDatabase = openIfNecessary();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, title == null ? "" : title);
@ -148,7 +162,7 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -148,7 +162,7 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@WorkerThread
@NonNull
public synchronized List<HistoryItem> findItemsContaining(@Nullable String search) {
synchronized List<HistoryItem> findItemsContaining(@Nullable String search) {
mDatabase = openIfNecessary();
List<HistoryItem> itemList = new ArrayList<>(5);
if (search == null) {
@ -161,11 +175,7 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -161,11 +175,7 @@ public class HistoryDatabase extends SQLiteOpenHelper {
new String[]{search, search}, null, null, KEY_TIME_VISITED + " DESC", "5");
while (cursor.moveToNext()) {
HistoryItem item = new HistoryItem();
item.setUrl(cursor.getString(1));
item.setTitle(cursor.getString(2));
item.setImageId(R.drawable.ic_history);
itemList.add(item);
itemList.add(fromCursor(cursor));
}
cursor.close();
@ -175,17 +185,13 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -175,17 +185,13 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@WorkerThread
@NonNull
public synchronized List<HistoryItem> getLastHundredItems() {
synchronized List<HistoryItem> getLastHundredItems() {
mDatabase = openIfNecessary();
List<HistoryItem> itemList = new ArrayList<>(100);
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC", "100");
while (cursor.moveToNext()) {
HistoryItem item = new HistoryItem();
item.setUrl(cursor.getString(1));
item.setTitle(cursor.getString(2));
item.setImageId(R.drawable.ic_history);
itemList.add(item);
itemList.add(fromCursor(cursor));
}
cursor.close();
@ -195,18 +201,14 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -195,18 +201,14 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@WorkerThread
@NonNull
public synchronized List<HistoryItem> getAllHistoryItems() {
synchronized List<HistoryItem> getAllHistoryItems() {
mDatabase = openIfNecessary();
List<HistoryItem> itemList = new ArrayList<>();
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, null, null, null, null, KEY_TIME_VISITED + " DESC");
while (cursor.moveToNext()) {
HistoryItem item = new HistoryItem();
item.setUrl(cursor.getString(1));
item.setTitle(cursor.getString(2));
item.setImageId(R.drawable.ic_history);
itemList.add(item);
itemList.add(fromCursor(cursor));
}
cursor.close();
@ -215,7 +217,7 @@ public class HistoryDatabase extends SQLiteOpenHelper { @@ -215,7 +217,7 @@ public class HistoryDatabase extends SQLiteOpenHelper {
}
@WorkerThread
public synchronized long getHistoryItemsCount() {
synchronized long getHistoryItemsCount() {
return DatabaseUtils.queryNumEntries(mDatabase, TABLE_HISTORY);
}
}

84
app/src/main/java/acr/browser/lightning/database/HistoryModel.java

@ -0,0 +1,84 @@ @@ -0,0 +1,84 @@
package acr.browser.lightning.database;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.anthonycr.bonsai.Completable;
import com.anthonycr.bonsai.CompletableAction;
import com.anthonycr.bonsai.CompletableSubscriber;
import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleSubscriber;
import java.util.List;
/**
* A model class providing reactive bindings
* with the underlying history database.
*/
public final class HistoryModel {
private HistoryModel() {}
@NonNull
public static Completable deleteHistory() {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
HistoryDatabase.getInstance().deleteHistory();
subscriber.onComplete();
}
});
}
@NonNull
public static Completable deleteHistoryItem(@NonNull final String url) {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
HistoryDatabase.getInstance().deleteHistoryItem(url);
subscriber.onComplete();
}
});
}
@NonNull
public static Completable visitHistoryItem(@NonNull final String url, @Nullable final String title) {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
HistoryDatabase.getInstance().visitHistoryItem(url, title);
subscriber.onComplete();
}
});
}
@NonNull
public static Single<List<HistoryItem>> findHistoryItemsContaining(@NonNull final String query) {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
List<HistoryItem> result = HistoryDatabase.getInstance().findItemsContaining(query);
subscriber.onItem(result);
subscriber.onComplete();
}
});
}
@NonNull
public static Single<List<HistoryItem>> lastHundredVisitedHistoryItems() {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
List<HistoryItem> result = HistoryDatabase.getInstance().getLastHundredItems();
subscriber.onItem(result);
subscriber.onComplete();
}
});
}
}

18
app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java

@ -12,6 +12,7 @@ import android.widget.ArrayAdapter; @@ -12,6 +12,7 @@ import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import com.anthonycr.bonsai.CompletableOnSubscribe;
import com.anthonycr.bonsai.Schedulers;
import com.squareup.otto.Bus;
@ -27,8 +28,8 @@ import acr.browser.lightning.bus.BrowserEvents; @@ -27,8 +28,8 @@ import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils;
@ -41,7 +42,6 @@ public class LightningDialogBuilder { @@ -41,7 +42,6 @@ public class LightningDialogBuilder {
@Inject BookmarkManager mBookmarkManager;
@Inject PreferenceManager mPreferenceManager;
@Inject HistoryDatabase mHistoryDatabase;
@Inject Bus mEventBus;
@Inject
@ -225,22 +225,18 @@ public class LightningDialogBuilder { @@ -225,22 +225,18 @@ public class LightningDialogBuilder {
new BrowserDialog.Item(R.string.dialog_remove_from_history) {
@Override
public void onClick() {
BrowserApp.getIOThread().execute(new Runnable() {
HistoryModel.deleteHistoryItem(url)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new CompletableOnSubscribe() {
@Override
public void run() {
mHistoryDatabase.deleteHistoryItem(url);
// openHistory();
Schedulers.main().execute(new Runnable() {
@Override
public void run() {
public void onComplete() {
mEventBus.post(new BrowserEvents.OpenHistoryInCurrentTab());
}
});
}
});
}
});
}
// TODO There should be a way in which we do not need an activity reference to dowload a file
public void showLongPressImageDialog(@NonNull final Activity activity, @NonNull final String url,

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

@ -16,11 +16,8 @@ import android.support.annotation.NonNull; @@ -16,11 +16,8 @@ import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.webkit.WebView;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.utils.WebUtils;
@ -45,8 +42,6 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme @@ -45,8 +42,6 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
private Activity mActivity;
private Handler mMessageHandler;
@Inject HistoryDatabase mHistoryDatabase;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -200,7 +195,7 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme @@ -200,7 +195,7 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
}
private void clearHistory() {
WebUtils.clearHistory(getActivity(), mHistoryDatabase);
WebUtils.clearHistory(getActivity());
mMessageHandler.sendEmptyMessage(1);
}

17
app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java

@ -40,8 +40,8 @@ import javax.inject.Inject; @@ -40,8 +40,8 @@ import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.ThemeUtils;
@ -65,7 +65,6 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable { @@ -65,7 +65,6 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable {
private final Comparator<HistoryItem> mFilterComparator = new SuggestionsComparator();
@Inject HistoryDatabase mDatabaseHandler;
@Inject BookmarkManager mBookmarkManager;
@Inject PreferenceManager mPreferenceManager;
@ -303,18 +302,6 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable { @@ -303,18 +302,6 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable {
}
}
@NonNull
private Single<List<HistoryItem>> getHistoryForQuery(@NonNull final String query) {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
List<HistoryItem> historyList = mDatabaseHandler.findItemsContaining(query);
subscriber.onItem(historyList);
subscriber.onComplete();
}
});
}
private boolean shouldRequestNetwork() {
return !mIsIncognito && mSuggestionChoice != PreferenceManager.Suggestion.SUGGESTION_NONE;
}
@ -358,7 +345,7 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable { @@ -358,7 +345,7 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable {
}
});
mSuggestionsAdapter.getHistoryForQuery(query)
HistoryModel.findHistoryItemsContaining(query)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {

10
app/src/main/java/acr/browser/lightning/utils/WebUtils.java

@ -11,7 +11,9 @@ import android.webkit.WebStorage; @@ -11,7 +11,9 @@ import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewDatabase;
import acr.browser.lightning.database.HistoryDatabase;
import com.anthonycr.bonsai.Schedulers;
import acr.browser.lightning.database.HistoryModel;
/**
* Copyright 8/4/2015 Anthony Restaino
@ -34,8 +36,10 @@ public class WebUtils { @@ -34,8 +36,10 @@ public class WebUtils {
WebStorage.getInstance().deleteAllData();
}
public static void clearHistory(@NonNull Context context, @NonNull HistoryDatabase historyDatabase) {
historyDatabase.deleteHistory();
public static void clearHistory(@NonNull Context context) {
HistoryModel.deleteHistory()
.subscribeOn(Schedulers.io())
.subscribe();
WebViewDatabase m = WebViewDatabase.getInstance(context);
m.clearFormData();
m.clearHttpAuthUsernamePassword();

4
build.gradle

@ -17,10 +17,6 @@ allprojects { @@ -17,10 +17,6 @@ allprojects {
}
ext {
// Necessary for Bonsai submodule
versionCode = 1
versionName = '1.0'
minSdkVersion = 14
targetSdkVersion = 25
buildToolsVersion = '25.0.2'

Loading…
Cancel
Save