Browse Source

Moving bookmarks storage from flat file to database

master
anthony restaino 8 years ago
parent
commit
f148283053
  1. 30
      app/src/main/java/acr/browser/lightning/activity/BookmarkUiModel.java
  2. 66
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  3. 4
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  4. 2
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  5. 9
      app/src/main/java/acr/browser/lightning/app/AppModule.java
  6. 21
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  7. 117
      app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java
  8. 2
      app/src/main/java/acr/browser/lightning/constant/HistoryPage.java
  9. 14
      app/src/main/java/acr/browser/lightning/database/HistoryItem.java
  10. 375
      app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkDatabase.java
  11. 166
      app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkExporter.java
  12. 3
      app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkLocalSync.java
  13. 12
      app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkManager.java
  14. 60
      app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkModel.java
  15. 3
      app/src/main/java/acr/browser/lightning/database/history/HistoryDatabase.java
  16. 3
      app/src/main/java/acr/browser/lightning/database/history/HistoryModel.java
  17. 120
      app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java
  18. 62
      app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java
  19. 103
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java
  20. 25
      app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java
  21. 2
      app/src/main/java/acr/browser/lightning/utils/WebUtils.java
  22. 6
      app/src/main/java/acr/browser/lightning/view/LightningView.java

30
app/src/main/java/acr/browser/lightning/activity/BookmarkUiModel.java

@ -0,0 +1,30 @@
package acr.browser.lightning.activity;
import android.support.annotation.Nullable;
import acr.browser.lightning.browser.BookmarksView;
/**
* The UI model representing the current folder shown
* by the {@link BookmarksView}.
* <p>
* Created by anthonycr on 5/7/17.
*/
public class BookmarkUiModel {
@Nullable private String mCurrentFolder;
public void setCurrentFolder(@Nullable String folder) {
mCurrentFolder = folder;
}
public boolean isRootFolder() {
return mCurrentFolder == null;
}
@Nullable
public String getCurrentFolder() {
return mCurrentFolder;
}
}

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

@ -99,9 +99,9 @@ import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.controller.UIController; import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel; import acr.browser.lightning.database.bookmark.BookmarkModel;
import acr.browser.lightning.database.history.HistoryModel;
import acr.browser.lightning.dialog.BrowserDialog; import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.fragment.BookmarksFragment; import acr.browser.lightning.fragment.BookmarksFragment;
@ -181,7 +181,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private String mCameraPhotoPath; private String mCameraPhotoPath;
// The singleton BookmarkManager // The singleton BookmarkManager
@Inject BookmarkManager mBookmarkManager; @Inject BookmarkModel mBookmarkManager;
@Inject LightningDialogBuilder mBookmarksDialogBuilder; @Inject LightningDialogBuilder mBookmarksDialogBuilder;
@ -827,23 +827,37 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// By using a manager, adds a bookmark and notifies third parties about that // By using a manager, adds a bookmark and notifies third parties about that
private void addBookmark(final String title, final String url) { private void addBookmark(final String title, final String url) {
final HistoryItem item = !mBookmarkManager.isBookmark(url)
? new HistoryItem(url, title) final HistoryItem item = new HistoryItem(url, title);
: null; mBookmarkManager.addBookmarkIfNotExists(item)
if (item != null && mBookmarkManager.addBookmark(item)) { .subscribeOn(Schedulers.io())
mSuggestionsAdapter.refreshBookmarks(); .observeOn(Schedulers.main())
mBookmarksView.handleUpdatedUrl(url); .subscribe(new SingleOnSubscribe<Boolean>() {
} @Override
public void onItem(@Nullable Boolean item) {
if (Boolean.TRUE.equals(item)) {
mSuggestionsAdapter.refreshBookmarks();
mBookmarksView.handleUpdatedUrl(url);
}
}
});
} }
private void deleteBookmark(final String title, final String url) { private void deleteBookmark(final String title, final String url) {
final HistoryItem item = mBookmarkManager.isBookmark(url) final HistoryItem item = new HistoryItem(url, title);
? new HistoryItem(url, title)
: null; mBookmarkManager.deleteBookmark(item)
if (item != null && mBookmarkManager.deleteBookmark(item)) { .subscribeOn(Schedulers.io())
mSuggestionsAdapter.refreshBookmarks(); .observeOn(Schedulers.main())
mBookmarksView.handleUpdatedUrl(url); .subscribe(new SingleOnSubscribe<Boolean>() {
} @Override
public void onItem(@Nullable Boolean item) {
if (Boolean.TRUE.equals(item)) {
mSuggestionsAdapter.refreshBookmarks();
mBookmarksView.handleUpdatedUrl(url);
}
}
});
} }
private void putToolbarInRoot() { private void putToolbarInRoot() {
@ -1098,11 +1112,19 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
if (!UrlUtils.isSpecialUrl(url)) { if (!UrlUtils.isSpecialUrl(url)) {
if (!mBookmarkManager.isBookmark(url)) { mBookmarkManager.isBookmark(url)
addBookmark(title, url); .subscribeOn(Schedulers.io())
} else { .observeOn(Schedulers.main())
deleteBookmark(title, url); .subscribe(new SingleOnSubscribe<Boolean>() {
} @Override
public void onItem(@Nullable Boolean item) {
if (Boolean.TRUE.equals(item)) {
addBookmark(title, url);
} else {
deleteBookmark(title, url);
}
}
});
} }
} }

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

@ -35,7 +35,6 @@ import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.dialog.BrowserDialog; import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.FileUtils; import acr.browser.lightning.utils.FileUtils;
@ -51,7 +50,7 @@ import acr.browser.lightning.view.LightningView;
public class TabsManager { public class TabsManager {
private static final String TAG = "TabsManager"; private static final String TAG = "TabsManager";
private static final String BUNDLE_KEY = "WEBVIEW_"; private static final String BUNDLE_KEY = "WEBVIEW_";
private static final String URL_KEY = "URL_KEY"; private static final String URL_KEY = "URL_KEY";
private static final String BUNDLE_STORAGE = "SAVED_TABS.parcel"; private static final String BUNDLE_STORAGE = "SAVED_TABS.parcel";
@ -64,7 +63,6 @@ public class TabsManager {
private final List<Runnable> mPostInitializationWorkList = new ArrayList<>(); private final List<Runnable> mPostInitializationWorkList = new ArrayList<>();
@Inject PreferenceManager mPreferenceManager; @Inject PreferenceManager mPreferenceManager;
@Inject BookmarkManager mBookmarkManager;
@Inject Application mApp; @Inject Application mApp;
public TabsManager() { public TabsManager() {

2
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -11,7 +11,7 @@ import acr.browser.lightning.browser.BrowserPresenter;
import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.database.HistoryDatabase; import acr.browser.lightning.database.history.HistoryDatabase;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener; import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.fragment.BookmarkSettingsFragment; import acr.browser.lightning.fragment.BookmarkSettingsFragment;

9
app/src/main/java/acr/browser/lightning/app/AppModule.java

@ -8,6 +8,8 @@ import net.i2p.android.ui.I2PAndroidHelper;
import javax.inject.Singleton; import javax.inject.Singleton;
import acr.browser.lightning.database.bookmark.BookmarkDatabase;
import acr.browser.lightning.database.bookmark.BookmarkModel;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
@ -29,6 +31,13 @@ public class AppModule {
return mApp.getApplicationContext(); return mApp.getApplicationContext();
} }
@NonNull
@Provides
@Singleton
public BookmarkModel provideBookmarkMode() {
return new BookmarkDatabase(mApp);
}
@NonNull @NonNull
@Provides @Provides
@Singleton @Singleton

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

@ -12,14 +12,19 @@ import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.webkit.WebView; import android.webkit.WebView;
import com.anthonycr.bonsai.Schedulers;
import com.squareup.leakcanary.LeakCanary; import com.squareup.leakcanary.LeakCanary;
import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import javax.inject.Inject; import javax.inject.Inject;
import acr.browser.lightning.BuildConfig; import acr.browser.lightning.BuildConfig;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.bookmark.BookmarkManager;
import acr.browser.lightning.database.bookmark.BookmarkModel;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.FileUtils; import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.MemoryLeakUtils; import acr.browser.lightning.utils.MemoryLeakUtils;
@ -33,6 +38,8 @@ public class BrowserApp extends Application {
private static final Executor mIOThread = Executors.newSingleThreadExecutor(); private static final Executor mIOThread = Executors.newSingleThreadExecutor();
@Inject PreferenceManager mPreferenceManager; @Inject PreferenceManager mPreferenceManager;
@Inject BookmarkManager mOldBookmarkManager;
@Inject BookmarkModel mBookmarkModel;
@Override @Override
protected void attachBaseContext(Context base) { protected void attachBaseContext(Context base) {
@ -74,6 +81,20 @@ public class BrowserApp extends Application {
sAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build(); sAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
sAppComponent.inject(this); sAppComponent.inject(this);
Schedulers.worker().execute(new Runnable() {
@Override
public void run() {
List<HistoryItem> oldBookmarks = mOldBookmarkManager.getAllBookmarks(true);
mOldBookmarkManager.deleteAllBookmarks();
if (!oldBookmarks.isEmpty()) {
mBookmarkModel.addBookmarkList(oldBookmarks).subscribeOn(Schedulers.io()).subscribe();
} else {
// TODO: 5/7/17 Import bookmarks from assets if not empty
}
}
});
if (mPreferenceManager.getUseLeakCanary() && !isRelease()) { if (mPreferenceManager.getUseLeakCanary() && !isRelease()) {
LeakCanary.install(this); LeakCanary.install(this);
} }

117
app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java

@ -11,6 +11,7 @@ import android.support.annotation.Nullable;
import com.anthonycr.bonsai.Single; import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction; import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleOnSubscribe;
import com.anthonycr.bonsai.SingleSubscriber; import com.anthonycr.bonsai.SingleSubscriber;
import java.io.File; import java.io.File;
@ -24,8 +25,9 @@ import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.bookmark.BookmarkModel;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
@ -37,25 +39,25 @@ public final class BookmarkPage {
public static final String FILENAME = "bookmarks.html"; public static final String FILENAME = "bookmarks.html";
private static final String HEADING_1 = "<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>\n" + private static final String HEADING_1 = "<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>\n" +
"<head>\n" + "<head>\n" +
"<meta content=en-us http-equiv=Content-Language />\n" + "<meta content=en-us http-equiv=Content-Language />\n" +
"<meta content='text/html; charset=utf-8' http-equiv=Content-Type />\n" + "<meta content='text/html; charset=utf-8' http-equiv=Content-Type />\n" +
"<meta name=viewport content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'>\n" + "<meta name=viewport content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'>\n" +
"<title>"; "<title>";
private static final String HEADING_2 = "</title>\n" + private static final String HEADING_2 = "</title>\n" +
"</head>\n" + "</head>\n" +
"<style>body{background:#f5f5f5;max-width:100%;min-height:100%}#content{width:100%;max-width:800px;margin:0 auto;text-align:center}.box{vertical-align:middle;text-align:center;position:relative;display:inline-block;height:45px;width:150px;margin:10px;background-color:#fff;box-shadow:0 3px 6px rgba(0,0,0,0.25);font-family:Arial;color:#444;font-size:12px;-moz-border-radius:2px;-webkit-border-radius:2px;border-radius:2px}.box-content{height:25px;width:100%;vertical-align:middle;text-align:center;display:table-cell}p.ellipses{" + "<style>body{background:#f5f5f5;max-width:100%;min-height:100%}#content{width:100%;max-width:800px;margin:0 auto;text-align:center}.box{vertical-align:middle;text-align:center;position:relative;display:inline-block;height:45px;width:150px;margin:10px;background-color:#fff;box-shadow:0 3px 6px rgba(0,0,0,0.25);font-family:Arial;color:#444;font-size:12px;-moz-border-radius:2px;-webkit-border-radius:2px;border-radius:2px}.box-content{height:25px;width:100%;vertical-align:middle;text-align:center;display:table-cell}p.ellipses{" +
"width:130px;font-size: small;font-family: Arial, Helvetica, 'sans-serif';white-space:nowrap;overflow:hidden;text-align:left;vertical-align:middle;margin:auto;text-overflow:ellipsis;-o-text-overflow:ellipsis;-ms-text-overflow:ellipsis}.box a{width:100%;height:100%;position:absolute;left:0;top:0}img{vertical-align:middle;margin-right:10px;width:20px;height:20px;}.margin{margin:10px}</style>\n" + "width:130px;font-size: small;font-family: Arial, Helvetica, 'sans-serif';white-space:nowrap;overflow:hidden;text-align:left;vertical-align:middle;margin:auto;text-overflow:ellipsis;-o-text-overflow:ellipsis;-ms-text-overflow:ellipsis}.box a{width:100%;height:100%;position:absolute;left:0;top:0}img{vertical-align:middle;margin-right:10px;width:20px;height:20px;}.margin{margin:10px}</style>\n" +
"<body><div id=content>"; "<body><div id=content>";
private static final String PART1 = "<div class=box><a href='"; private static final String PART1 = "<div class=box><a href='";
private static final String PART2 = "'></a>\n" + private static final String PART2 = "'></a>\n" +
"<div class=margin>\n" + "<div class=margin>\n" +
"<div class=box-content>\n" + "<div class=box-content>\n" +
"<p class=ellipses>\n" + "<p class=ellipses>\n" +
"<img src='"; "<img src='";
private static final String PART3 = "https://www.google.com/s2/favicons?domain="; private static final String PART3 = "https://www.google.com/s2/favicons?domain=";
@ -71,7 +73,7 @@ public final class BookmarkPage {
private File mCacheDir; private File mCacheDir;
@Inject Application mApp; @Inject Application mApp;
@Inject BookmarkManager mManager; @Inject BookmarkModel mManager;
@NonNull private final Bitmap mFolderIcon; @NonNull private final Bitmap mFolderIcon;
@NonNull private final String mTitle; @NonNull private final String mTitle;
@ -115,45 +117,52 @@ public final class BookmarkPage {
} }
private void buildBookmarkPage(@Nullable final String folder) { private void buildBookmarkPage(@Nullable final String folder) {
final List<HistoryItem> list = mManager.getBookmarksCopyFromFolder(folder, true); mManager.getBookmarksFromFolder(folder)
final File bookmarkWebPage; .subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
if (folder == null || folder.isEmpty()) { @Override
bookmarkWebPage = new File(mFilesDir, FILENAME); public void onItem(@Nullable List<HistoryItem> list) {
} else { Preconditions.checkNonNull(list);
bookmarkWebPage = new File(mFilesDir, folder + '-' + FILENAME);
} final File bookmarkWebPage;
final StringBuilder bookmarkBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2); if (folder == null || folder.isEmpty()) {
bookmarkWebPage = new File(mFilesDir, FILENAME);
final String folderIconPath = Constants.FILE + mCacheDir + '/' + FOLDER_ICON; } else {
for (int n = 0, size = list.size(); n < size; n++) { bookmarkWebPage = new File(mFilesDir, folder + '-' + FILENAME);
final HistoryItem item = list.get(n); }
bookmarkBuilder.append(PART1); final StringBuilder bookmarkBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
if (item.isFolder()) {
final File folderPage = new File(mFilesDir, item.getTitle() + '-' + FILENAME); final String folderIconPath = Constants.FILE + mCacheDir + '/' + FOLDER_ICON;
bookmarkBuilder.append(Constants.FILE).append(folderPage); for (int n = 0, size = list.size(); n < size; n++) {
bookmarkBuilder.append(PART2); final HistoryItem item = list.get(n);
bookmarkBuilder.append(folderIconPath); bookmarkBuilder.append(PART1);
buildBookmarkPage(item.getTitle()); if (item.isFolder()) {
} else { final File folderPage = new File(mFilesDir, item.getTitle() + '-' + FILENAME);
bookmarkBuilder.append(item.getUrl()); bookmarkBuilder.append(Constants.FILE).append(folderPage);
bookmarkBuilder.append(PART2).append(PART3); bookmarkBuilder.append(PART2);
bookmarkBuilder.append(item.getUrl()); bookmarkBuilder.append(folderIconPath);
} buildBookmarkPage(item.getTitle());
bookmarkBuilder.append(PART4); } else {
bookmarkBuilder.append(item.getTitle()); bookmarkBuilder.append(item.getUrl());
bookmarkBuilder.append(PART5); bookmarkBuilder.append(PART2).append(PART3);
} bookmarkBuilder.append(item.getUrl());
bookmarkBuilder.append(END); }
FileWriter bookWriter = null; bookmarkBuilder.append(PART4);
try { bookmarkBuilder.append(item.getTitle());
//noinspection IOResourceOpenedButNotSafelyClosed bookmarkBuilder.append(PART5);
bookWriter = new FileWriter(bookmarkWebPage, false); }
bookWriter.write(bookmarkBuilder.toString()); bookmarkBuilder.append(END);
} catch (IOException e) { FileWriter bookWriter = null;
e.printStackTrace(); try {
} finally { //noinspection IOResourceOpenedButNotSafelyClosed
Utils.close(bookWriter); bookWriter = new FileWriter(bookmarkWebPage, false);
} bookWriter.write(bookmarkBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
Utils.close(bookWriter);
}
}
});
} }
} }

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

@ -27,7 +27,7 @@ import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel; import acr.browser.lightning.database.history.HistoryModel;
import acr.browser.lightning.utils.Preconditions; import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;

14
app/src/main/java/acr/browser/lightning/database/HistoryItem.java

@ -25,7 +25,7 @@ public class HistoryItem implements Comparable<HistoryItem> {
private Bitmap mBitmap = null; private Bitmap mBitmap = null;
private int mImageId = 0; private int mImageId = 0;
private int mOrder = 0; private int mPosition = 0;
private boolean mIsFolder = false; private boolean mIsFolder = false;
public HistoryItem() {} public HistoryItem() {}
@ -63,12 +63,12 @@ public class HistoryItem implements Comparable<HistoryItem> {
mFolder = (folder == null) ? "" : folder; mFolder = (folder == null) ? "" : folder;
} }
public void setOrder(int order) { public void setPosition(int order) {
mOrder = order; mPosition = order;
} }
public int getOrder() { public int getPosition() {
return mOrder; return mPosition;
} }
@NonNull @NonNull
@ -132,8 +132,8 @@ public class HistoryItem implements Comparable<HistoryItem> {
HistoryItem that = (HistoryItem) object; HistoryItem that = (HistoryItem) object;
return mImageId == that.mImageId && return mImageId == that.mImageId &&
this.mTitle.equals(that.mTitle) && this.mUrl.equals(that.mUrl) && this.mTitle.equals(that.mTitle) && this.mUrl.equals(that.mUrl) &&
this.mFolder.equals(that.mFolder); this.mFolder.equals(that.mFolder);
} }
@Override @Override

375
app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkDatabase.java

@ -0,0 +1,375 @@
package acr.browser.lightning.database.bookmark;
import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
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.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import acr.browser.lightning.R;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.database.HistoryItem;
/**
* The disk backed bookmark database.
* <p>
* Created by anthonycr on 5/6/17.
*/
@Singleton
public class BookmarkDatabase extends SQLiteOpenHelper implements BookmarkModel {
private static final String TAG = "BookmarkDatabase";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "bookmarkManager";
// HistoryItems table name
private static final String TABLE_BOOKMARK = "bookmark";
// HistoryItems Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_URL = "url";
private static final String KEY_TITLE = "title";
private static final String KEY_FOLDER = "folder";
private static final String KEY_POSITION = "position";
@NonNull private final String DEFAULT_BOOKMARK_TITLE;
@Nullable private SQLiteDatabase mDatabase;
@Inject
public BookmarkDatabase(@NonNull Application application) {
super(application, DATABASE_NAME, null, DATABASE_VERSION);
DEFAULT_BOOKMARK_TITLE = application.getString(R.string.untitled);
}
@NonNull
private SQLiteDatabase lazyDatabase() {
if (mDatabase == null) {
mDatabase = getWritableDatabase();
}
return mDatabase;
}
// Creating Tables
@Override
public void onCreate(@NonNull SQLiteDatabase db) {
String CREATE_BOOKMARK_TABLE = "CREATE TABLE " +
DatabaseUtils.sqlEscapeString(TABLE_BOOKMARK) + '(' +
DatabaseUtils.sqlEscapeString(KEY_ID) + " INTEGER PRIMARY KEY," +
DatabaseUtils.sqlEscapeString(KEY_URL) + " TEXT," +
DatabaseUtils.sqlEscapeString(KEY_TITLE) + " TEXT," +
DatabaseUtils.sqlEscapeString(KEY_FOLDER) + " TEXT," +
DatabaseUtils.sqlEscapeString(KEY_POSITION) + " INTEGER" + ')';
db.execSQL(CREATE_BOOKMARK_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if it exists
db.execSQL("DROP TABLE IF EXISTS " + DatabaseUtils.sqlEscapeString(TABLE_BOOKMARK));
// Create tables again
onCreate(db);
}
@NonNull
private static ContentValues bindBookmarkToContentValues(@NonNull HistoryItem historyItem) {
ContentValues contentValues = new ContentValues(4);
contentValues.put(KEY_TITLE, historyItem.getTitle());
contentValues.put(KEY_URL, historyItem.getUrl());
contentValues.put(KEY_FOLDER, historyItem.getFolder());
contentValues.put(KEY_POSITION, historyItem.getPosition());
return contentValues;
}
@NonNull
private static HistoryItem bindCursorToHistoryItem(@NonNull Cursor cursor) {
HistoryItem bookmark = new HistoryItem();
bookmark.setImageId(R.drawable.ic_bookmark);
bookmark.setUrl(cursor.getString(cursor.getColumnIndex(KEY_URL)));
bookmark.setTitle(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
bookmark.setFolder(cursor.getString(cursor.getColumnIndex(KEY_FOLDER)));
bookmark.setPosition(cursor.getInt(cursor.getColumnIndex(KEY_POSITION)));
return bookmark;
}
@NonNull
private static List<HistoryItem> bindCursorToHistoryItemList(@NonNull Cursor cursor) {
List<HistoryItem> bookmarks = new ArrayList<>();
while (cursor.moveToNext()) {
bookmarks.add(bindCursorToHistoryItem(cursor));
}
cursor.close();
return bookmarks;
}
@NonNull
@Override
public Single<HistoryItem> findBookmarkForUrl(@NonNull final String url) {
return Single.create(new SingleAction<HistoryItem>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<HistoryItem> subscriber) {
Cursor cursor = lazyDatabase().query(TABLE_BOOKMARK, null, KEY_URL + "=?", new String[]{url}, null, null, null, "1");
if (cursor.moveToFirst()) {
subscriber.onItem(bindCursorToHistoryItem(cursor));
}
cursor.close();
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Single<Boolean> isBookmark(@NonNull final String url) {
return Single.create(new SingleAction<Boolean>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<Boolean> subscriber) {
Cursor cursor = lazyDatabase().query(TABLE_BOOKMARK, null, KEY_URL + "=?", new String[]{url}, null, null, null, "1");
subscriber.onItem(cursor.moveToFirst());
cursor.close();
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Single<Boolean> addBookmarkIfNotExists(@NonNull final HistoryItem item) {
return Single.create(new SingleAction<Boolean>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<Boolean> subscriber) {
Cursor cursor = lazyDatabase().query(TABLE_BOOKMARK, null, KEY_URL + "=?", new String[]{item.getUrl()}, null, null, null, "1");
if (cursor.moveToFirst()) {
cursor.close();
subscriber.onItem(false);
subscriber.onComplete();
return;
}
cursor.close();
long id = lazyDatabase().insert(TABLE_BOOKMARK, null, bindBookmarkToContentValues(item));
subscriber.onItem(id != -1);
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Completable addBookmarkList(@NonNull final List<HistoryItem> bookmarkItems) {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
lazyDatabase().beginTransaction();
for (HistoryItem item : bookmarkItems) {
addBookmarkIfNotExists(item).subscribe();
}
lazyDatabase().setTransactionSuccessful();
lazyDatabase().endTransaction();
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Single<Boolean> deleteBookmark(@NonNull final HistoryItem bookmark) {
return Single.create(new SingleAction<Boolean>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<Boolean> subscriber) {
int rows = lazyDatabase().delete(TABLE_BOOKMARK, KEY_URL + "=?", new String[]{bookmark.getUrl()});
subscriber.onItem(rows > 0);
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Completable renameFolder(@NonNull final String oldName, @NonNull final String newName) {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
ContentValues contentValues = new ContentValues(1);
contentValues.put(KEY_FOLDER, newName);
lazyDatabase().update(TABLE_BOOKMARK, contentValues, KEY_FOLDER + "=?", new String[]{oldName});
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Completable deleteFolder(@NonNull final String folderToDelete) {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
renameFolder(folderToDelete, "").subscribe();
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Completable deleteAllBookmarks() {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
lazyDatabase().delete(TABLE_BOOKMARK, null, null);
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Completable editBookmark(@NonNull final HistoryItem oldBookmark, @NonNull final HistoryItem newBookmark) {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
if (newBookmark.getTitle().isEmpty()) {
newBookmark.setTitle(DEFAULT_BOOKMARK_TITLE);
}
ContentValues contentValues = bindBookmarkToContentValues(newBookmark);
lazyDatabase().update(TABLE_BOOKMARK, contentValues, KEY_URL + "=?", new String[]{oldBookmark.getUrl()});
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Single<List<HistoryItem>> getAllBookmarks() {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
Cursor cursor = lazyDatabase().query(TABLE_BOOKMARK, null, null, null, null, null, null);
subscriber.onItem(bindCursorToHistoryItemList(cursor));
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Single<List<HistoryItem>> getBookmarksFromFolder(@Nullable final String folder) {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
String finalFolder = folder != null ? folder : "";
Cursor cursor = lazyDatabase().query(TABLE_BOOKMARK, null, KEY_FOLDER + "=?", new String[]{finalFolder}, null, null, null);
subscriber.onItem(bindCursorToHistoryItemList(cursor));
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Single<List<HistoryItem>> getFolders() {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
Cursor cursor = lazyDatabase().query(true, TABLE_BOOKMARK, new String[]{KEY_FOLDER}, null, null, null, null, null, null);
List<HistoryItem> folders = new ArrayList<>();
while (cursor.moveToNext()) {
String folderName = cursor.getString(cursor.getColumnIndex(KEY_FOLDER));
if (TextUtils.isEmpty(folderName)) {
continue;
}
final HistoryItem folder = new HistoryItem();
folder.setIsFolder(true);
folder.setTitle(folderName);
folder.setImageId(R.drawable.ic_folder);
folder.setUrl(Constants.FOLDER + folderName);
folders.add(folder);
}
cursor.close();
subscriber.onItem(folders);
subscriber.onComplete();
}
});
}
@NonNull
@Override
public Single<List<String>> getFolderNames() {
return Single.create(new SingleAction<List<String>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<String>> subscriber) {
Cursor cursor = lazyDatabase().query(true, TABLE_BOOKMARK, new String[]{KEY_FOLDER}, null, null, null, null, null, null);
List<String> folders = new ArrayList<>();
while (cursor.moveToNext()) {
String folderName = cursor.getString(cursor.getColumnIndex(KEY_FOLDER));
if (TextUtils.isEmpty(folderName)) {
continue;
}
folders.add(folderName);
}
cursor.close();
subscriber.onItem(folders);
subscriber.onComplete();
}
});
}
}

166
app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkExporter.java

@ -0,0 +1,166 @@
package acr.browser.lightning.database.bookmark;
import android.content.Context;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.WorkerThread;
import android.util.Log;
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 org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import acr.browser.lightning.R;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils;
/**
* The class responsible for importing and exporting
* bookmarks in the JSON format.
* <p>
* Created by anthonycr on 5/7/17.
*/
public class BookmarkExporter {
private static final String TAG = "BookmarkExporter";
private static final String KEY_URL = "url";
private static final String KEY_TITLE = "title";
private static final String KEY_FOLDER = "folder";
private static final String KEY_ORDER = "order";
@NonNull
public static List<HistoryItem> importBookmarksFromAssets(@NonNull Context context) {
List<HistoryItem> bookmarks = new ArrayList<>();
BufferedReader bookmarksReader = null;
InputStream inputStream = null;
try {
inputStream = context.getResources().openRawResource(R.raw.default_bookmarks);
//noinspection IOResourceOpenedButNotSafelyClosed
bookmarksReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bookmarksReader.readLine()) != null) {
try {
JSONObject object = new JSONObject(line);
HistoryItem item = new HistoryItem();
item.setTitle(object.getString(KEY_TITLE));
final String url = object.getString(KEY_URL);
item.setUrl(url);
item.setFolder(object.getString(KEY_FOLDER));
item.setPosition(object.getInt(KEY_ORDER));
item.setImageId(R.drawable.ic_bookmark);
bookmarks.add(item);
} catch (JSONException e) {
Log.e(TAG, "Can't parse line " + line, e);
}
}
} catch (IOException e) {
Log.e(TAG, "Error reading the bookmarks file", e);
} finally {
Utils.close(bookmarksReader);
Utils.close(inputStream);
}
return bookmarks;
}
@NonNull
public static Completable exportBookmarksToFile(@NonNull final List<HistoryItem> bookmarkList,
@NonNull final File file) {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull final CompletableSubscriber subscriber) {
Preconditions.checkNonNull(bookmarkList);
BufferedWriter bookmarkWriter = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
bookmarkWriter = new BufferedWriter(new FileWriter(file, false));
JSONObject object = new JSONObject();
for (HistoryItem item : bookmarkList) {
object.put(KEY_TITLE, item.getTitle());
object.put(KEY_URL, item.getUrl());
object.put(KEY_FOLDER, item.getFolder());
object.put(KEY_ORDER, item.getPosition());
bookmarkWriter.write(object.toString());
bookmarkWriter.newLine();
}
subscriber.onComplete();
} catch (@NonNull IOException | JSONException e) {
subscriber.onError(e);
} finally {
Utils.close(bookmarkWriter);
}
}
});
}
@NonNull
public static Single<List<HistoryItem>> importBookmarksFromFile(@NonNull final File file) {
return Single.create(new SingleAction<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
BufferedReader bookmarksReader = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
bookmarksReader = new BufferedReader(new FileReader(file));
String line;
List<HistoryItem> bookmarks = new ArrayList<>();
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
HistoryItem item = new HistoryItem();
item.setTitle(object.getString(KEY_TITLE));
item.setUrl(object.getString(KEY_URL));
item.setFolder(object.getString(KEY_FOLDER));
item.setPosition(object.getInt(KEY_ORDER));
bookmarks.add(item);
}
subscriber.onItem(bookmarks);
subscriber.onComplete();
} catch (IOException | JSONException e) {
subscriber.onError(e);
} finally {
Utils.close(bookmarksReader);
}
}
});
}
@WorkerThread
@NonNull
public static File createNewExportFile() {
File bookmarksExport = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"BookmarksExport.txt");
int counter = 0;
while (bookmarksExport.exists()) {
counter++;
bookmarksExport = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"BookmarksExport-" + counter + ".txt");
}
return bookmarksExport;
}
}

3
app/src/main/java/acr/browser/lightning/database/BookmarkLocalSync.java → app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkLocalSync.java

@ -1,4 +1,4 @@
package acr.browser.lightning.database; package acr.browser.lightning.database.bookmark;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -15,6 +15,7 @@ import com.anthonycr.bonsai.SingleSubscriber;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
public class BookmarkLocalSync { public class BookmarkLocalSync {

12
app/src/main/java/acr/browser/lightning/database/BookmarkManager.java → app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkManager.java

@ -1,4 +1,4 @@
package acr.browser.lightning.database; package acr.browser.lightning.database.bookmark;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@ -37,8 +37,10 @@ import javax.inject.Singleton;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
@Deprecated
@Singleton @Singleton
public class BookmarkManager { public class BookmarkManager {
@ -102,7 +104,7 @@ public class BookmarkManager {
final String url = object.getString(URL); final String url = object.getString(URL);
item.setUrl(url); item.setUrl(url);
item.setFolder(object.getString(FOLDER)); item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER)); item.setPosition(object.getInt(ORDER));
item.setImageId(R.drawable.ic_bookmark); item.setImageId(R.drawable.ic_bookmark);
bookmarks.put(url, item); bookmarks.put(url, item);
} catch (JSONException e) { } catch (JSONException e) {
@ -147,7 +149,7 @@ public class BookmarkManager {
object.put(TITLE, item.getTitle()); object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl()); object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder()); object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder()); object.put(ORDER, item.getPosition());
bookmarkWriter.write(object.toString()); bookmarkWriter.write(object.toString());
bookmarkWriter.newLine(); bookmarkWriter.newLine();
} }
@ -339,7 +341,7 @@ public class BookmarkManager {
object.put(TITLE, item.getTitle()); object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl()); object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder()); object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder()); object.put(ORDER, item.getPosition());
bookmarkWriter.write(object.toString()); bookmarkWriter.write(object.toString());
bookmarkWriter.newLine(); bookmarkWriter.newLine();
} }
@ -515,7 +517,7 @@ public class BookmarkManager {
item.setTitle(object.getString(TITLE)); item.setTitle(object.getString(TITLE));
item.setUrl(object.getString(URL)); item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER)); item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER)); item.setPosition(object.getInt(ORDER));
list.add(item); list.add(item);
number++; number++;
} }

60
app/src/main/java/acr/browser/lightning/database/bookmark/BookmarkModel.java

@ -0,0 +1,60 @@
package acr.browser.lightning.database.bookmark;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.anthonycr.bonsai.Completable;
import com.anthonycr.bonsai.Single;
import java.io.File;
import java.util.List;
import acr.browser.lightning.database.HistoryItem;
/**
* The interface that should be used to
* communicate with the bookmark database.
* <p>
* Created by anthonycr on 5/6/17.
*/
public interface BookmarkModel {
@NonNull
Single<HistoryItem> findBookmarkForUrl(@NonNull String url);
@NonNull
Single<Boolean> isBookmark(@NonNull String url);
@NonNull
Single<Boolean> addBookmarkIfNotExists(@NonNull HistoryItem item);
@NonNull
Completable addBookmarkList(@NonNull List<HistoryItem> bookmarkItems);
@NonNull
Single<Boolean> deleteBookmark(@NonNull HistoryItem bookmark);
@NonNull
Completable renameFolder(@NonNull String oldName, @NonNull String newName);
@NonNull
Completable deleteFolder(@NonNull String folderToDelete);
@NonNull
Completable deleteAllBookmarks();
@NonNull
Completable editBookmark(@NonNull HistoryItem oldBookmark, @NonNull HistoryItem newBookmark);
@NonNull
Single<List<HistoryItem>> getAllBookmarks();
@NonNull
Single<List<HistoryItem>> getBookmarksFromFolder(@Nullable String folder);
@NonNull
Single<List<HistoryItem>> getFolders();
@NonNull
Single<List<String>> getFolderNames();
}

3
app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java → app/src/main/java/acr/browser/lightning/database/history/HistoryDatabase.java

@ -1,7 +1,7 @@
/* /*
* Copyright 2014 A.C.R. Development * Copyright 2014 A.C.R. Development
*/ */
package acr.browser.lightning.database; package acr.browser.lightning.database.history;
import android.app.Application; import android.app.Application;
import android.content.ContentValues; import android.content.ContentValues;
@ -20,6 +20,7 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.database.HistoryItem;
@Singleton @Singleton
@WorkerThread @WorkerThread

3
app/src/main/java/acr/browser/lightning/database/HistoryModel.java → app/src/main/java/acr/browser/lightning/database/history/HistoryModel.java

@ -1,4 +1,4 @@
package acr.browser.lightning.database; package acr.browser.lightning.database.history;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -13,6 +13,7 @@ import com.anthonycr.bonsai.SingleSubscriber;
import java.util.List; import java.util.List;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryItem;
/** /**
* A model class providing reactive bindings * A model class providing reactive bindings

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

@ -5,6 +5,7 @@ import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
@ -14,6 +15,7 @@ import android.widget.EditText;
import com.anthonycr.bonsai.CompletableOnSubscribe; import com.anthonycr.bonsai.CompletableOnSubscribe;
import com.anthonycr.bonsai.Schedulers; import com.anthonycr.bonsai.Schedulers;
import com.anthonycr.bonsai.SingleOnSubscribe;
import java.util.List; import java.util.List;
@ -25,10 +27,11 @@ import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.controller.UIController; import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel; import acr.browser.lightning.database.bookmark.BookmarkModel;
import acr.browser.lightning.database.history.HistoryModel;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
/** /**
@ -44,7 +47,7 @@ public class LightningDialogBuilder {
INCOGNITO INCOGNITO
} }
@Inject BookmarkManager mBookmarkManager; @Inject BookmarkModel mBookmarkManager;
@Inject PreferenceManager mPreferenceManager; @Inject PreferenceManager mPreferenceManager;
@Inject @Inject
@ -60,7 +63,7 @@ public class LightningDialogBuilder {
* @param url the long pressed url * @param url the long pressed url
*/ */
public void showLongPressedDialogForBookmarkUrl(@NonNull final Activity activity, public void showLongPressedDialogForBookmarkUrl(@NonNull final Activity activity,
@NonNull UIController uiController, @NonNull final UIController uiController,
@NonNull final String url) { @NonNull final String url) {
final HistoryItem item; final HistoryItem item;
if (url.startsWith(Constants.FILE) && url.endsWith(BookmarkPage.FILENAME)) { if (url.startsWith(Constants.FILE) && url.endsWith(BookmarkPage.FILENAME)) {
@ -73,15 +76,19 @@ public class LightningDialogBuilder {
item.setTitle(folderTitle); item.setTitle(folderTitle);
item.setImageId(R.drawable.ic_folder); item.setImageId(R.drawable.ic_folder);
item.setUrl(Constants.FOLDER + folderTitle); item.setUrl(Constants.FOLDER + folderTitle);
showBookmarkFolderLongPressedDialog(activity, uiController, item);
} else { } else {
item = mBookmarkManager.findBookmarkForUrl(url); mBookmarkManager.findBookmarkForUrl(url)
} .subscribeOn(Schedulers.io())
if (item != null) { .observeOn(Schedulers.main())
if (item.isFolder()) { .subscribe(new SingleOnSubscribe<HistoryItem>() {
showBookmarkFolderLongPressedDialog(activity, uiController, item); @Override
} else { public void onItem(@Nullable HistoryItem historyItem) {
showLongPressedDialogForBookmarkUrl(activity, uiController, item); if (historyItem != null) {
} showLongPressedDialogForBookmarkUrl(activity, uiController, historyItem);
}
}
});
} }
} }
@ -116,9 +123,18 @@ public class LightningDialogBuilder {
new BrowserDialog.Item(R.string.dialog_remove_bookmark) { new BrowserDialog.Item(R.string.dialog_remove_bookmark) {
@Override @Override
public void onClick() { public void onClick() {
if (mBookmarkManager.deleteBookmark(item)) { mBookmarkManager.deleteBookmark(item)
uiController.handleBookmarkDeleted(item); .subscribeOn(Schedulers.io())
} .observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<Boolean>() {
@Override
public void onItem(@Nullable Boolean success) {
Preconditions.checkNonNull(success);
if (success) {
uiController.handleBookmarkDeleted(item);
}
}
});
} }
}, },
new BrowserDialog.Item(R.string.dialog_edit_bookmark) { new BrowserDialog.Item(R.string.dialog_edit_bookmark) {
@ -143,28 +159,44 @@ public class LightningDialogBuilder {
(AutoCompleteTextView) dialogLayout.findViewById(R.id.bookmark_folder); (AutoCompleteTextView) dialogLayout.findViewById(R.id.bookmark_folder);
getFolder.setHint(R.string.folder); getFolder.setHint(R.string.folder);
getFolder.setText(item.getFolder()); getFolder.setText(item.getFolder());
final List<String> folders = mBookmarkManager.getFolderTitles();
final ArrayAdapter<String> suggestionsAdapter = new ArrayAdapter<>(activity,
android.R.layout.simple_dropdown_item_1line, folders);
getFolder.setThreshold(1);
getFolder.setAdapter(suggestionsAdapter);
editBookmarkDialog.setView(dialogLayout);
editBookmarkDialog.setPositiveButton(activity.getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
mBookmarkManager.getFolderNames()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<List<String>>() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onItem(@Nullable List<String> folders) {
HistoryItem editedItem = new HistoryItem(); Preconditions.checkNonNull(folders);
editedItem.setTitle(getTitle.getText().toString()); final ArrayAdapter<String> suggestionsAdapter = new ArrayAdapter<>(activity,
editedItem.setUrl(getUrl.getText().toString()); android.R.layout.simple_dropdown_item_1line, folders);
editedItem.setUrl(getUrl.getText().toString()); getFolder.setThreshold(1);
editedItem.setFolder(getFolder.getText().toString()); getFolder.setAdapter(suggestionsAdapter);
mBookmarkManager.editBookmark(item, editedItem); editBookmarkDialog.setView(dialogLayout);
uiController.handleBookmarksChange(); editBookmarkDialog.setPositiveButton(activity.getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
HistoryItem editedItem = new HistoryItem();
editedItem.setTitle(getTitle.getText().toString());
editedItem.setUrl(getUrl.getText().toString());
editedItem.setUrl(getUrl.getText().toString());
editedItem.setFolder(getFolder.getText().toString());
mBookmarkManager.editBookmark(item, editedItem)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
uiController.handleBookmarksChange();
}
});
}
});
Dialog dialog = editBookmarkDialog.show();
BrowserDialog.setDialogSize(activity, dialog);
} }
}); });
Dialog dialog = editBookmarkDialog.show();
BrowserDialog.setDialogSize(activity, dialog);
} }
public void showBookmarkFolderLongPressedDialog(@NonNull final Activity activity, public void showBookmarkFolderLongPressedDialog(@NonNull final Activity activity,
@ -181,8 +213,15 @@ public class LightningDialogBuilder {
new BrowserDialog.Item(R.string.dialog_remove_folder) { new BrowserDialog.Item(R.string.dialog_remove_folder) {
@Override @Override
public void onClick() { public void onClick() {
mBookmarkManager.deleteFolder(item.getTitle()); mBookmarkManager.deleteFolder(item.getTitle())
uiController.handleBookmarkDeleted(item); .subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
uiController.handleBookmarkDeleted(item);
}
});
} }
}); });
} }
@ -202,8 +241,15 @@ public class LightningDialogBuilder {
editedItem.setUrl(Constants.FOLDER + text); editedItem.setUrl(Constants.FOLDER + text);
editedItem.setFolder(item.getFolder()); editedItem.setFolder(item.getFolder());
editedItem.setIsFolder(true); editedItem.setIsFolder(true);
mBookmarkManager.renameFolder(oldTitle, text); mBookmarkManager.renameFolder(oldTitle, text)
uiController.handleBookmarksChange(); .subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
uiController.handleBookmarksChange();
}
});
} }
} }
}); });

62
app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java

@ -21,6 +21,7 @@ import android.support.v7.app.AlertDialog;
import android.util.Log; import android.util.Log;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import com.anthonycr.bonsai.CompletableOnSubscribe;
import com.anthonycr.bonsai.SingleOnSubscribe; import com.anthonycr.bonsai.SingleOnSubscribe;
import com.anthonycr.grant.PermissionsManager; import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction; import com.anthonycr.grant.PermissionsResultAction;
@ -36,13 +37,14 @@ import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.BookmarkLocalSync; import acr.browser.lightning.database.bookmark.BookmarkExporter;
import acr.browser.lightning.database.BookmarkLocalSync.Source; import acr.browser.lightning.database.bookmark.BookmarkLocalSync;
import acr.browser.lightning.database.BookmarkManager; import acr.browser.lightning.database.bookmark.BookmarkLocalSync.Source;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import com.anthonycr.bonsai.Schedulers; import com.anthonycr.bonsai.Schedulers;
import acr.browser.lightning.database.bookmark.BookmarkModel;
import acr.browser.lightning.dialog.BrowserDialog; import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.utils.Preconditions; import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
@ -58,7 +60,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
@Nullable private Activity mActivity; @Nullable private Activity mActivity;
@Inject BookmarkManager mBookmarkManager; @Inject BookmarkModel mBookmarkManager;
private File[] mFileList; private File[] mFileList;
private String[] mFileNameList; private String[] mFileNameList;
@Nullable private BookmarkLocalSync mSync; @Nullable private BookmarkLocalSync mSync;
@ -187,7 +189,28 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
new PermissionsResultAction() { new PermissionsResultAction() {
@Override @Override
public void onGranted() { public void onGranted() {
mBookmarkManager.exportBookmarks(getActivity()); mBookmarkManager.getAllBookmarks()
.subscribeOn(Schedulers.io())
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable List<HistoryItem> item) {
Preconditions.checkNonNull(item);
final File exportFile = BookmarkExporter.createNewExportFile();
BookmarkExporter.exportBookmarksToFile(item, exportFile)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
Activity activity = getActivity();
if (activity != null) {
Utils.showSnackbar(activity, activity.getString(R.string.bookmark_export_path)
+ ' ' + exportFile.getPath());
}
}
});
}
});
} }
@Override @Override
@ -245,7 +268,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
mBookmarkManager.deleteAllBookmarks(); mBookmarkManager.deleteAllBookmarks().subscribeOn(Schedulers.io()).subscribe();
} }
}); });
Dialog dialog = builder.show(); Dialog dialog = builder.show();
@ -408,7 +431,32 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
Dialog dialog1 = builder.show(); Dialog dialog1 = builder.show();
BrowserDialog.setDialogSize(mActivity, dialog1); BrowserDialog.setDialogSize(mActivity, dialog1);
} else { } else {
mBookmarkManager.importBookmarksFromFile(mFileList[which], getActivity()); BookmarkExporter.importBookmarksFromFile(mFileList[which])
.subscribeOn(Schedulers.io())
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable final List<HistoryItem> importList) {
Preconditions.checkNonNull(importList);
mBookmarkManager.addBookmarkList(importList)
.observeOn(Schedulers.main())
.subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
Activity activity = getActivity();
if (activity != null) {
String message = activity.getResources().getString(R.string.message_import);
Utils.showSnackbar(activity, importList.size() + " " + message);
}
}
});
}
@Override
public void onError(@NonNull Throwable throwable) {
Log.e(TAG, "onError: importing bookmarks", throwable);
Utils.createInformativeDialog(getActivity(), R.string.title_error, R.string.import_bookmark_error);
}
});
} }
} }

103
app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

@ -35,6 +35,7 @@ import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.activity.BookmarkUiModel;
import acr.browser.lightning.activity.ReadingActivity; import acr.browser.lightning.activity.ReadingActivity;
import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.activity.TabsManager;
import acr.browser.lightning.animation.AnimationUtils; import acr.browser.lightning.animation.AnimationUtils;
@ -42,8 +43,8 @@ import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.browser.BookmarksView; import acr.browser.lightning.browser.BookmarksView;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.controller.UIController; import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.bookmark.BookmarkModel;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.favicon.FaviconModel; import acr.browser.lightning.favicon.FaviconModel;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
@ -71,7 +72,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
public final static String INCOGNITO_MODE = TAG + ".INCOGNITO_MODE"; public final static String INCOGNITO_MODE = TAG + ".INCOGNITO_MODE";
// Managers // Managers
@Inject BookmarkManager mBookmarkManager; @Inject BookmarkModel mBookmarkManager;
// Dialog builder // Dialog builder
@Inject LightningDialogBuilder mBookmarksDialogBuilder; @Inject LightningDialogBuilder mBookmarksDialogBuilder;
@ -109,16 +110,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Nullable @Nullable
private Subscription mBookmarksSubscription; private Subscription mBookmarksSubscription;
private static Single<List<HistoryItem>> initBookmarks(@NonNull final BookmarkManager bookmarkManager) { @NonNull
return Single.create(new SingleAction<List<HistoryItem>>() { private final BookmarkUiModel mUiModel = new BookmarkUiModel();
@Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
subscriber.onItem(bookmarkManager.getBookmarksFromFolder(null, true));
subscriber.onComplete();
}
});
}
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -150,7 +143,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
final HistoryItem item = mBookmarks.get(position); final HistoryItem item = mBookmarks.get(position);
if (item.isFolder()) { if (item.isFolder()) {
mScrollIndex = ((LinearLayoutManager) mBookmarksListView.getLayoutManager()).findFirstVisibleItemPosition(); mScrollIndex = ((LinearLayoutManager) mBookmarksListView.getLayoutManager()).findFirstVisibleItemPosition();
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true), true); setBookmarksShown(item.getTitle(), true);
} else { } else {
mUiController.bookmarkItemClicked(item); mUiController.bookmarkItemClicked(item);
} }
@ -170,7 +163,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (mBookmarkAdapter != null) { if (mBookmarkAdapter != null) {
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); setBookmarksShown(null, false);
} }
} }
@ -184,9 +177,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
backView.setOnClickListener(new View.OnClickListener() { backView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (mBookmarkManager == null) return; if (!mUiModel.isRootFolder()) {
if (!mBookmarkManager.isRootFolder()) { setBookmarksShown(null, true);
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
mBookmarksListView.getLayoutManager().scrollToPosition(mScrollIndex); mBookmarksListView.getLayoutManager().scrollToPosition(mScrollIndex);
} }
} }
@ -201,17 +193,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
mBookmarksListView.setLayoutManager(new LinearLayoutManager(getContext())); mBookmarksListView.setLayoutManager(new LinearLayoutManager(getContext()));
mBookmarksListView.setAdapter(mBookmarkAdapter); mBookmarksListView.setAdapter(mBookmarkAdapter);
mBookmarksSubscription = initBookmarks(mBookmarkManager) setBookmarksShown(null, true);
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable List<HistoryItem> item) {
mBookmarksSubscription = null;
Preconditions.checkNonNull(item);
setBookmarkDataSet(item, true);
}
});
return view; return view;
} }
@ -251,31 +233,70 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
} }
private void updateBookmarkIndicator(final String url) { private void updateBookmarkIndicator(final String url) {
if (!mBookmarkManager.isBookmark(url)) { mBookmarkManager.isBookmark(url)
mBookmarkImage.setImageResource(R.drawable.ic_action_star); .subscribeOn(Schedulers.io())
mBookmarkImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); .observeOn(Schedulers.main())
} else { .subscribe(new SingleOnSubscribe<Boolean>() {
mBookmarkImage.setImageResource(R.drawable.ic_bookmark); @Override
mBookmarkImage.setColorFilter(ThemeUtils.getAccentColor(getContext()), PorterDuff.Mode.SRC_IN); public void onItem(@Nullable Boolean item) {
} Preconditions.checkNonNull(item);
if (!item) {
mBookmarkImage.setImageResource(R.drawable.ic_action_star);
mBookmarkImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN);
} else {
mBookmarkImage.setImageResource(R.drawable.ic_bookmark);
mBookmarkImage.setColorFilter(ThemeUtils.getAccentColor(getContext()), PorterDuff.Mode.SRC_IN);
}
}
});
} }
@Override @Override
public void handleBookmarkDeleted(@NonNull HistoryItem item) { public void handleBookmarkDeleted(@NonNull HistoryItem item) {
mBookmarks.remove(item); mBookmarks.remove(item);
if (item.isFolder()) { if (item.isFolder()) {
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); setBookmarksShown(null, false);
} else { } else {
mBookmarkAdapter.notifyDataSetChanged(); mBookmarkAdapter.notifyDataSetChanged();
} }
} }
private void setBookmarksShown(@Nullable final String folder, final boolean animate) {
mBookmarksSubscription = mBookmarkManager.getBookmarksFromFolder(folder)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable final List<HistoryItem> item) {
mBookmarksSubscription = null;
Preconditions.checkNonNull(item);
mUiModel.setCurrentFolder(folder);
if (folder == null) {
mBookmarkManager.getFolders()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable List<HistoryItem> folders) {
Preconditions.checkNonNull(folders);
item.addAll(folders);
setBookmarkDataSet(item, animate);
}
});
} else {
setBookmarkDataSet(item, animate);
}
}
});
}
private void setBookmarkDataSet(@NonNull List<HistoryItem> items, boolean animate) { private void setBookmarkDataSet(@NonNull List<HistoryItem> items, boolean animate) {
mBookmarks.clear(); mBookmarks.clear();
mBookmarks.addAll(items); mBookmarks.addAll(items);
mBookmarkAdapter.notifyDataSetChanged(); mBookmarkAdapter.notifyDataSetChanged();
final int resource; final int resource;
if (mBookmarkManager.isRootFolder()) { if (mUiModel.isRootFolder()) {
resource = R.drawable.ic_action_star; resource = R.drawable.ic_action_star;
} else { } else {
resource = R.drawable.ic_action_back; resource = R.drawable.ic_action_back;
@ -339,10 +360,10 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Override @Override
public void navigateBack() { public void navigateBack() {
if (mBookmarkManager.isRootFolder()) { if (mUiModel.isRootFolder()) {
mUiController.closeBookmarksDrawer(); mUiController.closeBookmarksDrawer();
} else { } else {
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true); setBookmarksShown(null, true);
mBookmarksListView.getLayoutManager().scrollToPosition(mScrollIndex); mBookmarksListView.getLayoutManager().scrollToPosition(mScrollIndex);
} }
} }
@ -350,8 +371,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Override @Override
public void handleUpdatedUrl(@NonNull String url) { public void handleUpdatedUrl(@NonNull String url) {
updateBookmarkIndicator(url); updateBookmarkIndicator(url);
String folder = mBookmarkManager.getCurrentFolder(); String folder = mUiModel.getCurrentFolder();
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false); setBookmarksShown(folder, false);
} }
static class BookmarkViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { static class BookmarkViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

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

@ -38,9 +38,9 @@ import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.database.HistoryModel; import acr.browser.lightning.database.bookmark.BookmarkModel;
import acr.browser.lightning.database.history.HistoryModel;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Preconditions; import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
@ -65,7 +65,7 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable {
private final Comparator<HistoryItem> mFilterComparator = new SuggestionsComparator(); private final Comparator<HistoryItem> mFilterComparator = new SuggestionsComparator();
@Inject BookmarkManager mBookmarkManager; @Inject BookmarkModel mBookmarkManager;
@Inject PreferenceManager mPreferenceManager; @Inject PreferenceManager mPreferenceManager;
@Inject Application mApplication; @Inject Application mApplication;
@ -102,15 +102,16 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable {
} }
public void refreshBookmarks() { public void refreshBookmarks() {
Completable.create(new CompletableAction() { mBookmarkManager.getAllBookmarks()
@Override .subscribeOn(Schedulers.io())
public void onSubscribe(@NonNull CompletableSubscriber subscriber) { .subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
mAllBookmarks.clear(); @Override
mAllBookmarks.addAll(mBookmarkManager.getAllBookmarks(true)); public void onItem(@Nullable List<HistoryItem> item) {
Preconditions.checkNonNull(item);
subscriber.onComplete(); mAllBookmarks.clear();
} mAllBookmarks.addAll(item);
}).subscribeOn(Schedulers.io()).subscribe(); }
});
} }
@Override @Override

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

@ -13,7 +13,7 @@ import android.webkit.WebViewDatabase;
import com.anthonycr.bonsai.Schedulers; import com.anthonycr.bonsai.Schedulers;
import acr.browser.lightning.database.HistoryModel; import acr.browser.lightning.database.history.HistoryModel;
/** /**
* Copyright 8/4/2015 Anthony Restaino * Copyright 8/4/2015 Anthony Restaino

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

@ -32,6 +32,7 @@ import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.PluginState; import android.webkit.WebSettings.PluginState;
import android.webkit.WebView; import android.webkit.WebView;
import com.anthonycr.bonsai.Schedulers;
import com.anthonycr.bonsai.Single; import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction; import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleOnSubscribe; import com.anthonycr.bonsai.SingleOnSubscribe;
@ -49,13 +50,9 @@ import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.controller.UIController; import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener; import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import com.anthonycr.bonsai.Schedulers;
import acr.browser.lightning.utils.Preconditions; import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.UrlUtils; import acr.browser.lightning.utils.UrlUtils;
@ -110,7 +107,6 @@ public class LightningView {
@Inject PreferenceManager mPreferences; @Inject PreferenceManager mPreferences;
@Inject LightningDialogBuilder mBookmarksDialogBuilder; @Inject LightningDialogBuilder mBookmarksDialogBuilder;
@Inject ProxyUtils mProxyUtils; @Inject ProxyUtils mProxyUtils;
@Inject BookmarkManager mBookmarkManager;
public LightningView(@NonNull Activity activity, @Nullable String url, boolean isIncognito) { public LightningView(@NonNull Activity activity, @Nullable String url, boolean isIncognito) {
BrowserApp.getAppComponent().inject(this); BrowserApp.getAppComponent().inject(this);

Loading…
Cancel
Save