Browse Source

BookmarkPage restored and proper dependency injection

master
Stefano Pacifici 9 years ago
parent
commit
23e97306dd
  1. 6
      app/build.gradle
  2. 2
      app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java
  3. 2
      app/src/main/AndroidManifest.xml
  4. 84
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  5. 6
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  6. 2
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  7. 18
      app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java
  8. 38
      app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java
  9. 5
      app/src/main/java/acr/browser/lightning/constant/Constants.java
  10. 2
      app/src/main/java/acr/browser/lightning/constant/HistoryPage.java
  11. 2
      app/src/main/java/acr/browser/lightning/constant/StartPage.java
  12. 420
      app/src/main/java/acr/browser/lightning/database/BookmarkManager.java
  13. 196
      app/src/main/java/acr/browser/lightning/dialog/BookmarksDialogBuilder.java
  14. 8
      app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java
  15. 226
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java
  16. 7
      app/src/main/java/acr/browser/lightning/object/SearchAdapter.java
  17. 2
      app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java
  18. 2
      app/src/main/java/acr/browser/lightning/utils/DownloadImageTask.java
  19. 17
      app/src/main/java/acr/browser/lightning/utils/Utils.java
  20. 2
      app/src/main/java/acr/browser/lightning/view/LightningView.java
  21. 1
      build.gradle

6
app/build.gradle

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
@ -49,9 +50,14 @@ dependencies { @@ -49,9 +50,14 @@ dependencies {
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'org.jsoup:jsoup:1.8.1'
compile 'com.squareup:otto:1.3.8'
compile 'com.google.dagger:dagger:2.0.1'
apt 'com.google.dagger:dagger-compiler:2.0.1'
// Only Lightning Plus needs the proxy libraries
lightningPlusCompile 'net.i2p.android:client:0.7'
lightningPlusCompile(project(':libnetcipher'))
provided 'javax.annotation:jsr250-api:1.0'
// git submodule foreach git reset --hard
// git submodule update --remote
}

2
app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java

@ -10,7 +10,7 @@ import android.util.Log; @@ -10,7 +10,7 @@ import android.util.Log;
import net.i2p.android.ui.I2PAndroidHelper;
import acr.browser.lightning.R;
import acr.browser.lightning.activity.BrowserApp;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
import info.guardianproject.netcipher.proxy.OrbotHelper;

2
app/src/main/AndroidManifest.xml

@ -20,7 +20,7 @@ @@ -20,7 +20,7 @@
android:required="false" />
<application
android:name=".activity.BrowserApp"
android:name=".app.BrowserApp"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"

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

@ -89,21 +89,22 @@ import android.widget.TextView; @@ -89,21 +89,22 @@ import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.VideoView;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.bus.BusProvider;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
@ -111,6 +112,7 @@ import acr.browser.lightning.controller.BrowserController; @@ -111,6 +112,7 @@ import acr.browser.lightning.controller.BrowserController;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.dialog.BookmarksDialogBuilder;
import acr.browser.lightning.object.ClickHandler;
import acr.browser.lightning.object.DrawerArrowDrawable;
import acr.browser.lightning.object.SearchAdapter;
@ -172,9 +174,22 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -172,9 +174,22 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// Storage
private HistoryDatabase mHistoryDatabase;
private BookmarkManager mBookmarkManager;
private PreferenceManager mPreferences;
// The singleton BookmarkManager
@Inject
BookmarkManager bookmarkManager;
// Event bus
@Inject
Bus eventBus;
@Inject
BookmarkPage bookmarkPage;
@Inject
BookmarksDialogBuilder bookmarksDialogBuilder;
// Image
private Bitmap mDefaultVideoPoster, mWebpageBitmap;
private final ColorDrawable mBackground = new ColorDrawable();
@ -206,6 +221,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -206,6 +221,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BrowserApp.getAppComponent().inject(this);
initialize();
}
@ -764,7 +780,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -764,7 +780,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
return true;
case R.id.action_add_bookmark:
if (mCurrentView != null && !mCurrentView.getUrl().startsWith(Constants.FILE)) {
BusProvider.getInstance()
eventBus
.post(new BrowserEvents.AddBookmark(mCurrentView.getTitle(),
mCurrentView.getUrl()));
}
@ -943,7 +959,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -943,7 +959,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}, 150);
// Should update the bookmark status in BookmarksFragment
BusProvider.getInstance()
eventBus
.post(new BrowserEvents.CurrentPageUrl(mCurrentView.getUrl()));
// new Handler().postDelayed(new Runnable() {
@ -988,7 +1004,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -988,7 +1004,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}
mCurrentView.loadUrl(url);
BusProvider.getInstance().post(new BrowserEvents.CurrentPageUrl(url));
eventBus.post(new BrowserEvents.CurrentPageUrl(url));
}
@Override
@ -1163,7 +1179,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1163,7 +1179,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) {
mDrawerLayout.closeDrawer(mDrawerLeft);
} else if (mDrawerLayout.isDrawerOpen(mDrawerRight)) {
BusProvider.getInstance()
eventBus
.post(new BrowserEvents.UserPressedBack());
} else {
if (mCurrentView != null) {
@ -1203,7 +1219,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1203,7 +1219,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
overridePendingTransition(R.anim.fade_in_scale, R.anim.slide_down_out);
}
BusProvider.getInstance().unregister(busEventListener);
eventBus.unregister(busEventListener);
}
void saveOpenTabs() {
@ -1268,7 +1284,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1268,7 +1284,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
filter.addAction(NETWORK_BROADCAST_ACTION);
registerReceiver(mNetworkReceiver, filter);
BusProvider.getInstance().register(busEventListener);
eventBus.register(busEventListener);
}
/**
@ -1429,6 +1445,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1429,6 +1445,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}
/**
* TODO Can this method been removed?
* Animates the color of the toolbar from one color to another. Optionally animates
* the color of the tab background, for use when the tabs are displayed on the top
* of the screen.
@ -1483,21 +1500,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1483,21 +1500,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
});
}
private static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
if (domain == null) {
return url;
}
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
@Override
public void updateUrl(String url, boolean shortUrl) {
if (url == null || mSearch == null || mSearch.hasFocus()) {
return;
}
BusProvider.getInstance()
eventBus
.post(new BrowserEvents.CurrentPageUrl(url));
if (shortUrl && !url.startsWith(Constants.FILE)) {
switch (mPreferences.getUrlBoxContentChoice()) {
@ -1664,9 +1672,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1664,9 +1672,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} finally {
Utils.close(outputStream);
}
File bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME);
File bookmarkWebPage = new File(mActivity.getFilesDir(), Constants.BOOKMARKS_FILENAME);
BookmarkPage.buildBookmarkPage(this, null, mBookmarkManager.getBookmarksFromFolder(null, true));
bookmarkPage.buildBookmarkPage(null, bookmarkManager.getBookmarksFromFolder(null, true));
view.loadUrl(Constants.FILE + bookmarkWebPage);
}
@ -2099,14 +2107,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2099,14 +2107,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
final String newUrl = result.getExtra();
longPressHistoryLink(newUrl);
}
} else if (currentUrl.endsWith(BookmarkPage.FILENAME)) {
/* TODO Wtf is this?
} else if (currentUrl.endsWith(Constants.BOOKMARKS_FILENAME)) {
if (url != null) {
longPressBookmarkLink(url);
bookmarksDialogBuilder.showLongPressedDialogForUrl(this, url);
} else if (result != null && result.getExtra() != null) {
final String newUrl = result.getExtra();
longPressBookmarkLink(newUrl);
} */
bookmarksDialogBuilder.showLongPressedDialogForUrl(this, newUrl);
}
}
} else {
if (url != null) {
@ -2405,7 +2412,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2405,7 +2412,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/
@Subscribe
public void bookmarkCurrentPage(final BookmarkEvents.WantToBookmarkCurrentPage event) {
BusProvider.getInstance()
eventBus
.post(new BrowserEvents
.AddBookmark(mCurrentView.getTitle(), mCurrentView.getUrl()));
}
@ -2421,6 +2428,21 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2421,6 +2428,21 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSearchAdapter.refreshBookmarks();
}
/**
* This is received when the user edit a bookmark
*
* @param event
*/
@Subscribe
public void bookmarkChanged(final BookmarkEvents.BookmarkChanged event) {
if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE)
&& mCurrentView.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) {
openBookmarkPage(mWebView);
}
eventBus
.post(new BrowserEvents.CurrentPageUrl(mCurrentView.getUrl()));
}
/**
* Notify the browser that a bookmark was deleted
*
@ -2428,7 +2450,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2428,7 +2450,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/
@Subscribe
public void bookmarkDeleted(final BookmarkEvents.Deleted event) {
BusProvider.getInstance()
if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE)
&& mCurrentView.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) {
openBookmarkPage(mWebView);
}
eventBus
.post(new BrowserEvents.CurrentPageUrl(mCurrentView.getUrl()));
}

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

@ -3,6 +3,8 @@ package acr.browser.lightning.app; @@ -3,6 +3,8 @@ package acr.browser.lightning.app;
import javax.inject.Singleton;
import acr.browser.lightning.activity.BrowserActivity;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.dialog.BookmarksDialogBuilder;
import acr.browser.lightning.fragment.BookmarkSettingsFragment;
import acr.browser.lightning.fragment.BookmarksFragment;
import acr.browser.lightning.object.SearchAdapter;
@ -22,4 +24,8 @@ public interface AppComponent { @@ -22,4 +24,8 @@ public interface AppComponent {
void inject(BookmarkSettingsFragment fragment);
void inject(SearchAdapter adapter);
void inject(BookmarksDialogBuilder builder);
void inject(BookmarkPage bookmarkPage);
}

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

@ -3,8 +3,6 @@ package acr.browser.lightning.app; @@ -3,8 +3,6 @@ package acr.browser.lightning.app;
import android.app.Application;
import android.content.Context;
import acr.browser.lightning.activity.DaggerAppComponent;
public class BrowserApp extends Application {
private static Context context;

18
app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java

@ -65,8 +65,8 @@ public final class BookmarkEvents { @@ -65,8 +65,8 @@ public final class BookmarkEvents {
* The {@link acr.browser.lightning.fragment.BookmarksFragment} want to know the url (and title)
* of the currently shown web page.
*/
public static class WantInfoAboutCurrentPage {
}
// public static class WantInfoAboutCurrentPage {
// }
/**
* Sended by the {@link acr.browser.lightning.fragment.BookmarksFragment} when it wants to close
@ -75,4 +75,18 @@ public final class BookmarkEvents { @@ -75,4 +75,18 @@ public final class BookmarkEvents {
*/
public static class CloseBookmarks {
}
/**
* Sended when a bookmark is edited
*/
public static class BookmarkChanged {
public final HistoryItem oldBookmark;
public final HistoryItem newBookmark;
public BookmarkChanged(final HistoryItem oldItem, final HistoryItem newItem) {
oldBookmark = oldItem;
newBookmark = newItem;
}
}
}

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

@ -3,22 +3,22 @@ @@ -3,22 +3,22 @@
*/
package acr.browser.lightning.constant;
import android.app.Activity;
import android.content.Context;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.activity.BrowserApp;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.Utils;
public class BookmarkPage {
public static final String FILENAME = "bookmarks.html";
public final class BookmarkPage {
public static final String HEADING = "<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>\n" +
"<head>\n" +
@ -49,29 +49,41 @@ public class BookmarkPage { @@ -49,29 +49,41 @@ public class BookmarkPage {
public static final String END = "</div></body></html>";
public static void buildBookmarkPage(final Activity activity, final String folder, final List<HistoryItem> list) {
final BookmarkManager manager = BookmarkManager.getInstance(activity);
File bookmarkWebPage;
@Inject
BookmarkManager manager;
private final File FILES_DIR;
private final File CACHE_DIR;
@Inject
public BookmarkPage(Context context) {
BrowserApp.getAppComponent().inject(this);
FILES_DIR = context.getFilesDir();
CACHE_DIR = context.getCacheDir();
}
public void buildBookmarkPage(final String folder, final List<HistoryItem> list) {
final File bookmarkWebPage;
if (folder == null || folder.isEmpty()) {
bookmarkWebPage = new File(activity.getFilesDir(), BookmarkPage.FILENAME);
bookmarkWebPage = new File(FILES_DIR, Constants.BOOKMARKS_FILENAME);
} else {
bookmarkWebPage = new File(activity.getFilesDir(), folder + '-' + BookmarkPage.FILENAME);
bookmarkWebPage = new File(FILES_DIR, folder + '-' + Constants.BOOKMARKS_FILENAME);
}
final StringBuilder bookmarkBuilder = new StringBuilder(BookmarkPage.HEADING);
String folderIconPath = Constants.FILE + activity.getCacheDir() + "/folder.png";
final String folderIconPath = Constants.FILE + CACHE_DIR + "/folder.png";
for (int n = 0; n < list.size(); n++) {
final HistoryItem item = list.get(n);
bookmarkBuilder.append(BookmarkPage.PART1);
if (item.isFolder()) {
File folderPage = new File(activity.getFilesDir(), item.getTitle() + '-' + BookmarkPage.FILENAME);
final File folderPage = new File(FILES_DIR, item.getTitle() + '-' + Constants.BOOKMARKS_FILENAME);
bookmarkBuilder.append(Constants.FILE).append(folderPage);
bookmarkBuilder.append(BookmarkPage.PART2);
bookmarkBuilder.append(folderIconPath);
new Thread(new Runnable() {
@Override
public void run() {
buildBookmarkPage(activity, item.getTitle(), manager.getBookmarksFromFolder(item.getTitle(), true));
buildBookmarkPage(item.getTitle(), manager.getBookmarksFromFolder(item.getTitle(), true));
}
}).run();
} else {

5
app/src/main/java/acr/browser/lightning/constant/Constants.java

@ -47,6 +47,11 @@ public final class Constants { @@ -47,6 +47,11 @@ public final class Constants {
public static final int PROXY_I2P = 2;
public static final int PROXY_MANUAL = 3;
/**
* The bookmark page standard suffix
*/
public static final String BOOKMARKS_FILENAME = "bookmarks.html";
public static final String DEFAULT_ENCODING = "UTF-8";
public static final String[] TEXT_ENCODINGS = {"ISO-8859-1", "UTF-8", "GBK", "Big5", "ISO-2022-JP", "SHIFT_JS", "EUC-JP", "EUC-KR"};

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

@ -11,7 +11,7 @@ import java.util.List; @@ -11,7 +11,7 @@ import java.util.List;
import android.content.Context;
import acr.browser.lightning.activity.BrowserApp;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.R;
import acr.browser.lightning.database.HistoryDatabase;

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

@ -9,7 +9,7 @@ import java.io.File; @@ -9,7 +9,7 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import acr.browser.lightning.activity.BrowserApp;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.R;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils;

420
app/src/main/java/acr/browser/lightning/database/BookmarkManager.java

@ -2,10 +2,9 @@ package acr.browser.lightning.database; @@ -2,10 +2,9 @@ package acr.browser.lightning.database;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.provider.Browser;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import org.json.JSONException;
@ -23,78 +22,189 @@ import java.io.InputStreamReader; @@ -23,78 +22,189 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.inject.Singleton;
import acr.browser.lightning.R;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils;
@Singleton
public class BookmarkManager {
private static final String TAG = BookmarkManager.class.getSimpleName();
private final Context mContext;
private static final String TITLE = "title";
private static final String URL = "url";
private static final String FOLDER = "folder";
private static final String ORDER = "order";
private static final String FILE_BOOKMARKS = "bookmarks.dat";
private Set<String> mBookmarkSearchSet = new HashSet<>();
private final List<HistoryItem> mBookmarkList = new ArrayList<>();
private final String DEFAULT_BOOKMARK_TITLE;
private Map<String, HistoryItem> mBookmarksMap = new HashMap<>();
// private final List<HistoryItem> mBookmarkList = new ArrayList<>();
private String mCurrentFolder = "";
private static BookmarkManager mInstance;
private final ExecutorService mExecutor;
private boolean mReady = false;
private final File mFilesDir;
public BookmarkManager(Context context) {
mExecutor = Executors.newSingleThreadExecutor();
mFilesDir = context.getFilesDir();
DEFAULT_BOOKMARK_TITLE = context.getString(R.string.untitled);
mExecutor.execute(new BookmarkInitializer(context));
}
/**
*
* @return true if the BookmarkManager was initialized, false otherwise
*/
public boolean isReady() {
return mReady;
}
/**
* Look for bookmark using the url
* @param url the lookup url
* @return the bookmark as an {@link HistoryItem} or null
*/
@Nullable
public HistoryItem findBookmarkForUrl(final String url) {
return mBookmarksMap.get(url);
}
/**
* Initialize the Bookmaneger, it's a one-time operation and will be executed asynchronously.
* When done, mReady flag will been setted to true.
*/
private class BookmarkInitializer implements Runnable{
private final Context mContext;
public static BookmarkManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new BookmarkManager(context);
public BookmarkInitializer(Context context) {
mContext = context;
}
return mInstance;
@Override
public void run() {
synchronized (BookmarkManager.this) {
final Map<String, HistoryItem> bookmarks = new HashMap<>();
final File bookmarksFile = new File(mFilesDir, FILE_BOOKMARKS);
BufferedReader bookmarksReader = null;
try {
final InputStream inputStream;
if (bookmarksFile.exists() && bookmarksFile.isFile()) {
inputStream = new FileInputStream(bookmarksFile);
} else {
inputStream = mContext.getResources().openRawResource(R.raw.default_bookmarks);
}
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(TITLE));
final String url = object.getString(URL);
item.setUrl(url);
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
item.setImageId(R.drawable.ic_bookmark);
bookmarks.put(url, 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);
}
mBookmarksMap = bookmarks;
mReady = true;
}
}
}
private BookmarkManager(Context context) {
mContext = context;
mBookmarkList.clear();
mBookmarkList.addAll(getAllBookmarks(true));
mBookmarkSearchSet = getBookmarkUrls(mBookmarkList);
/**
* Dump all the given bookmarks to the bookmark file using a temporary file
*/
private class BookmarksWriter implements Runnable {
private final List<HistoryItem> mBookmarks;
public BookmarksWriter(List<HistoryItem> bookmarks) {
mBookmarks = bookmarks;
}
@Override
public void run() {
final File tempFile = new File(mFilesDir,
String.format("bm_%d.dat", System.currentTimeMillis()));
final File bookmarksFile = new File(mFilesDir, FILE_BOOKMARKS);
boolean success = false;
BufferedWriter bookmarkWriter = null;
try {
bookmarkWriter = new BufferedWriter(new FileWriter(tempFile, false));
JSONObject object = new JSONObject();
for (HistoryItem item : mBookmarks) {
object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder());
bookmarkWriter.write(object.toString());
bookmarkWriter.newLine();
}
success = true;
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
Utils.close(bookmarkWriter);
}
if (success) {
// Overwrite the bookmarks file by renaming the temp file
tempFile.renameTo(bookmarksFile);
}
}
}
@Override
protected void finalize() throws Throwable {
mExecutor.shutdownNow();
super.finalize();
}
public boolean isBookmark(String url) {
return mBookmarkSearchSet.contains(url);
return mBookmarksMap.containsKey(url);
}
/**
* This method adds the the HistoryItem item to permanent bookmark storage.
* It returns true if the operation was successful.
* This method adds the the HistoryItem item to permanent bookmark storage.<br>
* This operation is blocking if the manager is still not ready.
*
* @param item the item to add
* @param item the item to add
* @return It returns true if the operation was successful.
*/
public synchronized boolean addBookmark(HistoryItem item) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
if (item.getUrl() == null || mBookmarkSearchSet.contains(item.getUrl())) {
public synchronized boolean addBookmark(@NonNull HistoryItem item) {
final String url = item.getUrl();
if (url == null || mBookmarksMap.containsKey(url)) {
return false;
}
mBookmarkList.add(item);
BufferedWriter bookmarkWriter = null;
try {
bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
JSONObject object = new JSONObject();
object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder());
bookmarkWriter.write(object.toString());
bookmarkWriter.newLine();
mBookmarkSearchSet.add(item.getUrl());
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
Utils.close(bookmarkWriter);
}
mBookmarksMap.put(url, item);
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
return true;
}
@ -104,28 +214,16 @@ public class BookmarkManager { @@ -104,28 +214,16 @@ public class BookmarkManager {
* @param list the list of HistoryItems to add to bookmarks
*/
private synchronized void addBookmarkList(List<HistoryItem> list) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
BufferedWriter bookmarkWriter = null;
try {
bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
JSONObject object = new JSONObject();
for (HistoryItem item : list) {
if (item.getUrl() != null && !mBookmarkSearchSet.contains(item.getUrl())) {
object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder());
bookmarkWriter.write(object.toString());
bookmarkWriter.newLine();
mBookmarkSearchSet.add(item.getUrl());
mBookmarkList.add(item);
}
if (list == null || list.isEmpty()) {
return;
}
for (HistoryItem item : list) {
final String url = item.getUrl();
if (url != null && !mBookmarksMap.containsKey(url)) {
mBookmarksMap.put(url, item);
}
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
Utils.close(bookmarkWriter);
}
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
}
/**
@ -138,9 +236,8 @@ public class BookmarkManager { @@ -138,9 +236,8 @@ public class BookmarkManager {
if (deleteItem == null || deleteItem.isFolder()) {
return false;
}
mBookmarkSearchSet.remove(deleteItem.getUrl());
mBookmarkList.remove(deleteItem);
overwriteBookmarks(mBookmarkList);
mBookmarksMap.remove(deleteItem.getUrl());
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
return true;
}
@ -154,15 +251,15 @@ public class BookmarkManager { @@ -154,15 +251,15 @@ public class BookmarkManager {
if (newName.isEmpty()) {
return;
}
for (int n = 0; n < mBookmarkList.size(); n++) {
if (mBookmarkList.get(n).getFolder().equals(oldName)) {
mBookmarkList.get(n).setFolder(newName);
} else if (mBookmarkList.get(n).isFolder() && mBookmarkList.get(n).getTitle().equals(oldName)) {
mBookmarkList.get(n).setTitle(newName);
mBookmarkList.get(n).setUrl(Constants.FOLDER + newName);
for (HistoryItem item: mBookmarksMap.values()) {
if (item.getFolder().equals(oldName)) {
item.setFolder(newName);
} else if (item.isFolder() && item.getTitle().equals(oldName)) {
item.setTitle(newName);
item.setUrl(Constants.FOLDER + newName);
}
}
overwriteBookmarks(mBookmarkList);
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
}
/**
@ -171,16 +268,22 @@ public class BookmarkManager { @@ -171,16 +268,22 @@ public class BookmarkManager {
* @param name the name of the folder to be deleted
*/
public synchronized void deleteFolder(@NonNull String name) {
Iterator<HistoryItem> iterator = mBookmarkList.iterator();
while (iterator.hasNext()) {
HistoryItem item = iterator.next();
if (!item.isFolder() && item.getFolder().equals(name)) {
item.setFolder("");
} else if (item.getTitle().equals(name)) {
iterator.remove();
final Map<String, HistoryItem> bookmarks = new HashMap<>();
for (HistoryItem item: mBookmarksMap.values()) {
final String url = item.getUrl();
if (item.isFolder()) {
if (!item.getTitle().equals(name)) {
bookmarks.put(url, item);
}
} else {
if (item.getFolder().equals(name)) {
item.setFolder("");
}
bookmarks.put(url, item);
}
}
overwriteBookmarks(mBookmarkList);
mBookmarksMap = bookmarks;
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
}
/**
@ -193,21 +296,21 @@ public class BookmarkManager { @@ -193,21 +296,21 @@ public class BookmarkManager {
if (oldItem == null || newItem == null || oldItem.isFolder()) {
return;
}
mBookmarkList.remove(oldItem);
mBookmarkList.add(newItem);
if (!oldItem.getUrl().equals(newItem.getUrl())) {
// Update the BookmarkMap if the URL has been changed
mBookmarkSearchSet.remove(oldItem.getUrl());
mBookmarkSearchSet.add(newItem.getUrl());
}
if (newItem.getUrl().isEmpty()) {
deleteBookmark(oldItem);
return;
}
if (newItem.getTitle().isEmpty()) {
newItem.setTitle(mContext.getString(R.string.untitled));
newItem.setTitle(DEFAULT_BOOKMARK_TITLE);
}
final String oldUrl = oldItem.getUrl();
final String newUrl = newItem.getUrl();
if (!oldUrl.equals(newUrl)) {
// The url has been changed, remove the old one
mBookmarksMap.remove(oldUrl);
}
overwriteBookmarks(mBookmarkList);
mBookmarksMap.put(newUrl, newItem);
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
}
/**
@ -254,42 +357,12 @@ public class BookmarkManager { @@ -254,42 +357,12 @@ public class BookmarkManager {
* This is a disk-bound operation and should not be
* done very frequently.
*
* @param sort force to sort the returned bookmarkList
*
* @return returns a list of bookmarks that can be sorted
*/
public synchronized List<HistoryItem> getAllBookmarks(boolean sort) {
final List<HistoryItem> bookmarks = new ArrayList<>();
final File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
BufferedReader bookmarksReader = null;
try {
final InputStream inputStream;
if (bookmarksFile.exists() && bookmarksFile.isFile()) {
inputStream = new FileInputStream(bookmarksFile);
} else {
inputStream = mContext.getResources().openRawResource(R.raw.default_bookmarks);
}
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(TITLE));
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(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);
}
final List<HistoryItem> bookmarks = new ArrayList<>(mBookmarksMap.values());
if (sort) {
Collections.sort(bookmarks, new SortIgnoreCase());
}
@ -312,9 +385,9 @@ public class BookmarkManager { @@ -312,9 +385,9 @@ public class BookmarkManager {
folder = "";
}
mCurrentFolder = folder;
for (int n = 0; n < mBookmarkList.size(); n++) {
if (mBookmarkList.get(n).getFolder().equals(folder))
bookmarks.add(mBookmarkList.get(n));
for (HistoryItem item: mBookmarksMap.values()) {
if (item.getFolder().equals(folder))
bookmarks.add(item);
}
if (sort) {
Collections.sort(bookmarks, new SortIgnoreCase());
@ -338,9 +411,9 @@ public class BookmarkManager { @@ -338,9 +411,9 @@ public class BookmarkManager {
*/
private Set<String> getBookmarkUrls(List<HistoryItem> list) {
Set<String> set = new HashSet<>();
for (int n = 0; n < list.size(); n++) {
if (!mBookmarkList.get(n).isFolder())
set.add(mBookmarkList.get(n).getUrl());
for (HistoryItem item: mBookmarksMap.values()) {
if (!item.isFolder())
set.add(item.getUrl());
}
return set;
}
@ -353,34 +426,23 @@ public class BookmarkManager { @@ -353,34 +426,23 @@ public class BookmarkManager {
* @return a list of all folders
*/
public synchronized List<HistoryItem> getFolders(boolean sort) {
List<HistoryItem> folders = new ArrayList<>();
Set<String> folderMap = new HashSet<>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
BufferedReader bookmarksReader = null;
try {
bookmarksReader = new BufferedReader(new FileReader(bookmarksFile));
String line;
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
String folderName = object.getString(FOLDER);
if (!folderName.isEmpty() && !folderMap.contains(folderName)) {
HistoryItem item = new HistoryItem();
item.setTitle(folderName);
item.setUrl(Constants.FOLDER + folderName);
item.setIsFolder(true);
folderMap.add(folderName);
folders.add(item);
}
final HashMap<String, HistoryItem> folders = new HashMap<>();
for (HistoryItem item: mBookmarksMap.values()) {
final String folderName = item.getFolder();
if (folderName != null && !folderName.isEmpty() && !folders.containsKey(folderName)) {
final HistoryItem folder = new HistoryItem();
folder.setIsFolder(true);
folder.setTitle(folderName);
folder.setImageId(R.drawable.ic_folder);
folder.setUrl(Constants.FOLDER + folderName);
folders.put(folderName, folder);
}
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
Utils.close(bookmarksReader);
}
final List<HistoryItem> result = new ArrayList<>(folders.values());
if (sort) {
Collections.sort(folders, new SortIgnoreCase());
Collections.sort(result, new SortIgnoreCase());
}
return folders;
return result;
}
/**
@ -390,27 +452,14 @@ public class BookmarkManager { @@ -390,27 +452,14 @@ public class BookmarkManager {
* @return a list of folder title strings
*/
public synchronized List<String> getFolderTitles() {
List<String> folders = new ArrayList<>();
Set<String> folderMap = new HashSet<>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
BufferedReader bookmarksReader = null;
try {
bookmarksReader = new BufferedReader(new FileReader(bookmarksFile));
String line;
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
String folderName = object.getString(FOLDER);
if (!folderName.isEmpty() && !folderMap.contains(folderName)) {
folders.add(folderName);
folderMap.add(folderName);
}
final Set<String> folders = new HashSet<>();
for (HistoryItem item: mBookmarksMap.values()) {
final String folderName = item.getFolder();
if (folderName != null && !folderName.isEmpty()) {
folders.add(folderName);
}
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
Utils.close(bookmarksReader);
}
return folders;
return new ArrayList<>(folders);
}
/**
@ -450,37 +499,6 @@ public class BookmarkManager { @@ -450,37 +499,6 @@ public class BookmarkManager {
}
}
/**
* This method overwrites the entire bookmark file with the list of
* bookmarks. This is useful when an edit has been made to one or more
* bookmarks in the list
*
* @param list the list of bookmarks to overwrite the old ones with
*/
private synchronized void overwriteBookmarks(List<HistoryItem> list) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
BufferedWriter bookmarkWriter = null;
try {
bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, false));
JSONObject object = new JSONObject();
for (int n = 0; n < list.size(); n++) {
HistoryItem item = list.get(n);
if (!item.isFolder()) {
object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder());
bookmarkWriter.write(object.toString());
bookmarkWriter.newLine();
}
}
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
Utils.close(bookmarkWriter);
}
}
/**
* find the index of a bookmark in a list using only its URL
*

196
app/src/main/java/acr/browser/lightning/dialog/BookmarksDialogBuilder.java

@ -1,7 +1,201 @@ @@ -1,7 +1,201 @@
package acr.browser.lightning.dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.squareup.otto.Bus;
import java.util.List;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.Utils;
/**
* Created by Stefano Pacifici on 02/09/15.
* Created by Stefano Pacifici on 02/09/15, based on Anthony C. Restaino's code.
*/
public class BookmarksDialogBuilder {
@Inject
BookmarkManager bookmarkManager;
@Inject
Bus eventBus;
@Inject
public BookmarksDialogBuilder() {
BrowserApp.getAppComponent().inject(this);
}
/**
* Show the appropriated dialog for the long pressed link. It means that we try to understand
* if the link is relative to a bookmark or is just a folder.
* @param context used to show the dialog
* @param url the long pressed url
*/
public void showLongPressedDialogForUrl(final Context context, final String url) {
final HistoryItem item;
if (url.startsWith(Constants.FILE) && url.endsWith(Constants.BOOKMARKS_FILENAME)) {
// TODO this in so many ways is wrong, probably we need to redesign the folder mechanism
final Uri uri = Uri.parse(url);
final String filename = uri.getLastPathSegment();
final String folderTitle = filename.substring(0, filename.length() - Constants.BOOKMARKS_FILENAME.length() - 1);
item = new HistoryItem();
item.setIsFolder(true);
item.setTitle(folderTitle);
item.setImageId(R.drawable.ic_folder);
item.setUrl(Constants.FOLDER + folderTitle);
} else {
item = bookmarkManager.findBookmarkForUrl(url);
}
if (item != null) {
if (item.isFolder()) {
showBookmarkFolderLongPressedDialog(context, item);
} else {
showLongPressedDialogForUrl(context, item);
}
}
}
public void showLongPressedDialogForUrl(final Context context, final HistoryItem item) {
final DialogInterface.OnClickListener dialogClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BookmarkEvents.AsNewTab(item));
break;
case DialogInterface.BUTTON_NEGATIVE:
if (bookmarkManager.deleteBookmark(item)) {
eventBus.post(new BookmarkEvents.Deleted(item));
}
break;
case DialogInterface.BUTTON_NEUTRAL:
showEditBookmarkDialog(context, item);
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.action_bookmarks)
.setMessage(R.string.dialog_bookmark)
.setCancelable(true)
.setPositiveButton(R.string.action_new_tab, dialogClickListener)
.setNegativeButton(R.string.action_delete, dialogClickListener)
.setNeutralButton(R.string.action_edit, dialogClickListener)
.show();
}
public void showEditBookmarkDialog(final Context context, final HistoryItem item) {
final AlertDialog.Builder editBookmarkDialog = new AlertDialog.Builder(context);
editBookmarkDialog.setTitle(R.string.title_edit_bookmark);
final View dialogLayout = View.inflate(context, R.layout.dialog_edit_bookmark, null);
final EditText getTitle = (EditText) dialogLayout.findViewById(R.id.bookmark_title);
getTitle.setText(item.getTitle());
final EditText getUrl = (EditText) dialogLayout.findViewById(R.id.bookmark_url);
getUrl.setText(item.getUrl());
final AutoCompleteTextView getFolder =
(AutoCompleteTextView) dialogLayout.findViewById(R.id.bookmark_folder);
getFolder.setHint(R.string.folder);
getFolder.setText(item.getFolder());
final List<String> folders = bookmarkManager.getFolderTitles();
final ArrayAdapter<String> suggestionsAdapter = new ArrayAdapter<>(context,
android.R.layout.simple_dropdown_item_1line, folders);
getFolder.setThreshold(1);
getFolder.setAdapter(suggestionsAdapter);
editBookmarkDialog.setView(dialogLayout);
editBookmarkDialog.setPositiveButton(context.getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
HistoryItem editedItem = new HistoryItem();
String currentFolder = item.getFolder();
editedItem.setTitle(getTitle.getText().toString());
editedItem.setUrl(getUrl.getText().toString());
editedItem.setUrl(getUrl.getText().toString());
editedItem.setFolder(getFolder.getText().toString());
bookmarkManager.editBookmark(item, editedItem);
eventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
}
});
editBookmarkDialog.show();
}
public void showBookmarkFolderLongPressedDialog(final Context context, final HistoryItem item) {
assert item.isFolder();
final DialogInterface.OnClickListener dialogClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
showRenameFolderDialog(context, item);
break;
case DialogInterface.BUTTON_NEGATIVE:
bookmarkManager.deleteFolder(item.getTitle());
// setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
eventBus.post(new BookmarkEvents.Deleted(item));
break;
}
}
};
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.action_folder)
.setMessage(R.string.dialog_folder)
.setCancelable(true)
.setPositiveButton(R.string.action_rename, dialogClickListener)
.setNegativeButton(R.string.action_delete, dialogClickListener)
.show();
}
public void showRenameFolderDialog(final Context context, final HistoryItem item) {
assert item.isFolder();
final AlertDialog.Builder editFolderDialog = new AlertDialog.Builder(context);
editFolderDialog.setTitle(R.string.title_rename_folder);
final EditText getTitle = new EditText(context);
getTitle.setHint(R.string.hint_title);
getTitle.setText(item.getTitle());
getTitle.setSingleLine();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
int padding = Utils.dpToPx(10);
layout.setPadding(padding, padding, padding, padding);
layout.addView(getTitle);
editFolderDialog.setView(layout);
editFolderDialog.setPositiveButton(context.getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String oldTitle = item.getTitle();
final String newTitle = getTitle.getText().toString();
final HistoryItem editedItem = new HistoryItem();
editedItem.setTitle(newTitle);
editedItem.setUrl(Constants.FOLDER + newTitle);
editedItem.setFolder(item.getFolder());
editedItem.setIsFolder(true);
bookmarkManager.renameFolder(oldTitle, newTitle);
eventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
}
});
editFolderDialog.show();
}
}

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

@ -17,7 +17,10 @@ import java.io.File; @@ -17,7 +17,10 @@ import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
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.utils.PermissionsManager;
@ -27,7 +30,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref @@ -27,7 +30,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
private static final String SETTINGS_IMPORT = "import_bookmark";
private Activity mActivity;
private BookmarkManager mBookmarkManager;
@Inject BookmarkManager mBookmarkManager;
private File[] mFileList;
private String[] mFileNameList;
private PermissionsManager mPermissionsManager;
@ -40,13 +43,12 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref @@ -40,13 +43,12 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BrowserApp.getAppComponent().inject(this);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preference_bookmarks);
mActivity = getActivity();
mBookmarkManager = BookmarkManager.getInstance(mActivity.getApplicationContext());
initPrefs();
mPermissionsManager = PermissionsManager.getInstance();

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

@ -1,8 +1,6 @@ @@ -1,8 +1,6 @@
package acr.browser.lightning.fragment;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.os.Bundle;
@ -22,11 +20,8 @@ import android.widget.AdapterView; @@ -22,11 +20,8 @@ import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
@ -37,19 +32,19 @@ import java.util.ArrayList; @@ -37,19 +32,19 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.activity.BrowserActivity;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.bus.BusProvider;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.dialog.BookmarksDialogBuilder;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.DownloadImageTask;
import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.Utils;
import static android.support.v7.app.AlertDialog.Builder;
/**
* Created by Stefano Pacifici on 25/08/15. Based on Anthony C. Restaino's code.
@ -57,7 +52,16 @@ import static android.support.v7.app.AlertDialog.Builder; @@ -57,7 +52,16 @@ import static android.support.v7.app.AlertDialog.Builder;
public class BookmarksFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener {
// Managers
private BookmarkManager mBookmarkManager;
@Inject
BookmarkManager mBookmarkManager;
// Event bus
@Inject
Bus eventBus;
// Dialog builder
@Inject
BookmarksDialogBuilder bookmarksDialogBuilder;
// Adapter
private BookmarkViewAdapter mBookmarkAdapter;
@ -80,13 +84,18 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -80,13 +84,18 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Override
public void run() {
final Context context = getContext();
mBookmarkManager = BookmarkManager.getInstance(context.getApplicationContext());
mBookmarkAdapter = new BookmarkViewAdapter(context, mBookmarks);
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
mBookmarksListView.setAdapter(mBookmarkAdapter);
}
};
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BrowserApp.getAppComponent().inject(this);
}
// Handle bookmark click
private final OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
@ -96,7 +105,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -96,7 +105,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true),
true);
} else {
BusProvider.getInstance().post(new BookmarkEvents.Clicked(item));
eventBus.post(new BookmarkEvents.Clicked(item));
}
}
};
@ -154,13 +163,13 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -154,13 +163,13 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Override
public void onStart() {
super.onStart();
BusProvider.getInstance().register(this);
eventBus.register(this);
}
@Override
public void onStop() {
super.onStop();
BusProvider.getInstance().unregister(this);
eventBus.unregister(this);
}
@Subscribe
@ -170,7 +179,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -170,7 +179,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
mBookmarks.add(item);
Collections.sort(mBookmarks, new BookmarkManager.SortIgnoreCase());
mBookmarkAdapter.notifyDataSetChanged();
BusProvider.getInstance()
eventBus
.post(new BookmarkEvents.Added(item));
updateBookmarkIndicator(event.url);
}
@ -181,6 +190,16 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -181,6 +190,16 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
updateBookmarkIndicator(event.url);
}
@Subscribe
public void bookmarkChanged(BookmarkEvents.BookmarkChanged event) {
final int size = mBookmarks.size();
mBookmarks.remove(event.oldBookmark);
assert mBookmarks.size() < size;
mBookmarks.add(event.newBookmark);
mBookmarkAdapter.notifyDataSetChanged();
Collections.sort(mBookmarks, new BookmarkManager.SortIgnoreCase());
}
private void updateBookmarkIndicator(final String url) {
if (!mBookmarkManager.isBookmark(url)) {
mBookmarkImage.setImageResource(R.drawable.ic_action_star);
@ -194,13 +213,22 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -194,13 +213,22 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Subscribe
public void userPressedBack(final BrowserEvents.UserPressedBack event) {
if (mBookmarkManager.isRootFolder()) {
BusProvider.getInstance()
eventBus
.post(new BookmarkEvents.CloseBookmarks());
} else {
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
}
}
@Subscribe
public void bookmarkDeleted(final BookmarkEvents.Deleted event) {
final HistoryItem item = event.item;
final int size = mBookmarks.size();
mBookmarks.remove(event);
assert mBookmarks.size() < size;
mBookmarkAdapter.notifyDataSetChanged();
}
private void setBookmarkDataSet(List<HistoryItem> items, boolean animate) {
mBookmarks.clear();
mBookmarks.addAll(items);
@ -261,177 +289,17 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -261,177 +289,17 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
private void handleLongPress(final HistoryItem item, final int position) {
if (item.isFolder()) {
longPressFolder(item, position);
return;
bookmarksDialogBuilder.showBookmarkFolderLongPressedDialog(getContext(), item);
} else {
final Bus bus = BusProvider.getInstance();
final DialogInterface.OnClickListener dialogClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
bus.post(new BookmarkEvents.AsNewTab(item));
break;
case DialogInterface.BUTTON_NEGATIVE:
if (mBookmarkManager.deleteBookmark(item)) {
mBookmarks.remove(position);
mBookmarkAdapter.notifyDataSetChanged();
bus.post(new BookmarkEvents.Deleted(item));
}
break;
case DialogInterface.BUTTON_NEUTRAL:
editBookmark(item, position);
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(R.string.action_bookmarks)
.setMessage(R.string.dialog_bookmark)
.setCancelable(true)
.setPositiveButton(R.string.action_new_tab, dialogClickListener)
.setNegativeButton(R.string.action_delete, dialogClickListener)
.setNeutralButton(R.string.action_edit, dialogClickListener)
.show();
bookmarksDialogBuilder.showLongPressedDialogForUrl(getContext(), item);
}
}
private void longPressFolder(final HistoryItem item, final int position) {
final DialogInterface.OnClickListener dialogClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
renameFolder(item, position);
break;
case DialogInterface.BUTTON_NEGATIVE:
mBookmarkManager.deleteFolder(item.getTitle());
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
BusProvider.getInstance().post(new BookmarkEvents.Deleted(item));
/* TODO Restore Bookmarkpage
if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE)
&& mCurrentView.getUrl().endsWith(BookmarkPage.FILENAME)) {
openBookmarkPage(mWebView);
}*/
break;
}
}
};
Builder builder = new Builder(getContext());
builder.setTitle(R.string.action_folder)
.setMessage(R.string.dialog_folder)
.setCancelable(true)
.setPositiveButton(R.string.action_rename, dialogClickListener)
.setNegativeButton(R.string.action_delete, dialogClickListener)
.show();
}
/**
* Takes in the id of which bookmark was selected and shows a dialog that
* allows the user to rename and change the url of the bookmark
*
* @param item the bookmark
* @param position the position inside the adapter
*/
private synchronized void editBookmark(final HistoryItem item, final int position) {
final Builder editBookmarkDialog = new Builder(getContext());
editBookmarkDialog.setTitle(R.string.title_edit_bookmark);
final View dialogLayout = View.inflate(getContext(), R.layout.dialog_edit_bookmark, null);
final EditText getTitle = (EditText) dialogLayout.findViewById(R.id.bookmark_title);
getTitle.setText(item.getTitle());
final EditText getUrl = (EditText) dialogLayout.findViewById(R.id.bookmark_url);
getUrl.setText(item.getUrl());
final AutoCompleteTextView getFolder =
(AutoCompleteTextView) dialogLayout.findViewById(R.id.bookmark_folder);
getFolder.setHint(R.string.folder);
getFolder.setText(item.getFolder());
final List<String> folders = mBookmarkManager.getFolderTitles();
final ArrayAdapter<String> suggestionsAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_dropdown_item_1line, folders);
getFolder.setThreshold(1);
getFolder.setAdapter(suggestionsAdapter);
editBookmarkDialog.setView(dialogLayout);
editBookmarkDialog.setPositiveButton(getResources().getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
HistoryItem editedItem = new HistoryItem();
String currentFolder = item.getFolder();
editedItem.setTitle(getTitle.getText().toString());
editedItem.setUrl(getUrl.getText().toString());
editedItem.setUrl(getUrl.getText().toString());
editedItem.setFolder(getFolder.getText().toString());
mBookmarkManager.editBookmark(item, editedItem);
List<HistoryItem> list = mBookmarkManager.getBookmarksFromFolder(currentFolder, true);
if (list.isEmpty()) {
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
} else {
setBookmarkDataSet(list, false);
}
BusProvider.getInstance()
.post(new BookmarkEvents.WantInfoAboutCurrentPage());
/* TODO Restore BookmarkPage
if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE)
&& mCurrentView.getUrl().endsWith(BookmarkPage.FILENAME)) {
openBookmarkPage(mWebView);
}*/
}
});
editBookmarkDialog.show();
}
/**
* Show a dialog to rename a folder
*
* @param id the position of the HistoryItem (folder) in the bookmark list
*/
private synchronized void renameFolder(final HistoryItem item, final int id) {
final Context context = getContext();
final Builder editFolderDialog = new Builder(context);
editFolderDialog.setTitle(R.string.title_rename_folder);
final EditText getTitle = new EditText(context);
getTitle.setHint(R.string.hint_title);
getTitle.setText(item.getTitle());
getTitle.setSingleLine();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
int padding = Utils.dpToPx(10);
layout.setPadding(padding, padding, padding, padding);
layout.addView(getTitle);
editFolderDialog.setView(layout);
editFolderDialog.setPositiveButton(getResources().getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String oldTitle = item.getTitle();
String newTitle = getTitle.getText().toString();
mBookmarkManager.renameFolder(oldTitle, newTitle);
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
/* TODO Restore Bookmarkpage
if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE)
&& mCurrentView.getUrl().endsWith(BookmarkPage.FILENAME)) {
openBookmarkPage(mWebView);
}*/
}
});
editFolderDialog.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.action_add_bookmark:
BusProvider.getInstance().post(new BookmarkEvents.WantToBookmarkCurrentPage());
eventBus.post(new BookmarkEvents.WantToBookmarkCurrentPage());
break;
default:
break;

7
app/src/main/java/acr/browser/lightning/object/SearchAdapter.java

@ -36,7 +36,10 @@ import java.util.Iterator; @@ -36,7 +36,10 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
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;
@ -58,7 +61,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @@ -58,7 +61,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private boolean mIsExecuting = false;
private final boolean mDarkTheme;
private final boolean mIncognito;
private final BookmarkManager mBookmarkManager;
@Inject BookmarkManager mBookmarkManager;
private static final String CACHE_FILE_TYPE = ".sgg";
private static final String ENCODING = "ISO-8859-1";
private static final long INTERVAL_DAY = 86400000;
@ -71,8 +74,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @@ -71,8 +74,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private final Drawable mBookmarkDrawable;
public SearchAdapter(Context context, boolean dark, boolean incognito) {
BrowserApp.getAppComponent().inject(this);
mDatabaseHandler = HistoryDatabase.getInstance(context.getApplicationContext());
mBookmarkManager = BookmarkManager.getInstance(context.getApplicationContext());
mAllBookmarks.addAll(mBookmarkManager.getAllBookmarks(true));
mUseGoogle = PreferenceManager.getInstance().getGoogleSearchSuggestionsEnabled();
mContext = context;

2
app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java

@ -3,7 +3,7 @@ package acr.browser.lightning.preference; @@ -3,7 +3,7 @@ package acr.browser.lightning.preference;
import android.content.SharedPreferences;
import android.os.Environment;
import acr.browser.lightning.activity.BrowserApp;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
public class PreferenceManager {

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

@ -46,7 +46,7 @@ public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> { @@ -46,7 +46,7 @@ public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
// unique path for each url that is bookmarked.
final Uri uri = Uri.parse(mUrl);
final String hash = "" + Utils.hash(uri.getHost());
final String hash = "" + uri.getHost().hashCode();
final File image = new File(mCacheDir, hash + ".png");
final Uri urldisplay = Uri.fromParts(uri.getScheme(), uri.getHost(), "favicon.ico");
// checks to see if the image exists

17
app/src/main/java/acr/browser/lightning/utils/Utils.java

@ -311,21 +311,4 @@ public final class Utils { @@ -311,21 +311,4 @@ public final class Utils {
canvas.drawPath(wallpath, paint);
}
/**
* Calculate the SHA-1 hash of the given string
* @param str the string to hash
* @return the long representation of the hash
*/
public static long hash(final String str) {
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-1");
final byte[] bytes = digest.digest(str.getBytes());
final BigInteger intRepr = new BigInteger(bytes);
return intRepr.longValue();
} catch (NoSuchAlgorithmException e) {
Log.e(Constants.TAG, "The device has not SHA-1 support", e);
return 0;
}
}
}

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

@ -56,8 +56,6 @@ import java.io.IOException; @@ -56,8 +56,6 @@ import java.io.IOException;
import java.net.URISyntaxException;
import acr.browser.lightning.R;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.bus.BusProvider;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.controller.BrowserController;

1
build.gradle

@ -5,6 +5,7 @@ buildscript { @@ -5,6 +5,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
}
}

Loading…
Cancel
Save