Skip to content

Commit

Permalink
Utilize new BookmarkManager class for bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonycr committed Aug 22, 2014
1 parent d908513 commit df51c67
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 211 deletions.
31 changes: 2 additions & 29 deletions src/acr/browser/lightning/AdvancedSettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
Expand Down Expand Up @@ -778,33 +777,7 @@ public void onClick(DialogInterface dialog, int which) {
}

public void importFromStockBrowser() {
if (mSystemBrowser) {
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
// use 0 for history, 1 for bookmarks
String sel = Browser.BookmarkColumns.BOOKMARK + " = 1";
Cursor mCur;
mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);

String title, url;
int number = 0;
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (!mCur.isAfterLast()) {
number++;
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
if (title.length() < 1) {
title = Utils.getDomainName(url);
}
Utils.addBookmark(mContext, title, url);
mCur.moveToNext();
}
}
Utils.showToast(mContext,
number + " " + getResources().getString(R.string.message_import));
} else {
Utils.createInformativeDialog(mContext, getResources().getString(R.string.title_error),
getResources().getString(R.string.dialog_import_error));
}
BookmarkManager manager = new BookmarkManager(this);
manager.importBookmarksFromBrowser();
}
}
31 changes: 19 additions & 12 deletions src/acr/browser/lightning/BookmarkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public BookmarkManager(Context context) {
*
* @param item
*/
public void addBookmark(HistoryItem item) {
public synchronized boolean addBookmark(HistoryItem item) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);

if (mBookmarkMap.containsKey(item.getUrl())) {
return;
return false;
}
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
Expand All @@ -63,14 +63,15 @@ public void addBookmark(HistoryItem item) {
} catch (JSONException e) {
e.printStackTrace();
}
return true;
}

/**
* This method adds the list of HistoryItems to permanent bookmark storage
*
* @param list
*/
public void addBookmarkList(List<HistoryItem> list) {
public synchronized void addBookmarkList(List<HistoryItem> list) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
Expand Down Expand Up @@ -99,11 +100,12 @@ public void addBookmarkList(List<HistoryItem> list) {
*
* @param url
*/
public void deleteBookmark(String url) {
public synchronized boolean deleteBookmark(String url) {
List<HistoryItem> list = new ArrayList<HistoryItem>();
mBookmarkMap.remove(url);
list = getBookmarks();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
boolean bookmarkDeleted = false;
try {
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(bookmarksFile, false));
for (HistoryItem item : list) {
Expand All @@ -115,6 +117,8 @@ public void deleteBookmark(String url) {
object.put(ORDER, item.getOrder());
fileWriter.write(object.toString());
fileWriter.newLine();
} else {
bookmarkDeleted = true;
}
}
fileWriter.close();
Expand All @@ -123,13 +127,14 @@ public void deleteBookmark(String url) {
} catch (JSONException e) {
e.printStackTrace();
}
return bookmarkDeleted;
}

/**
* This method exports the stored bookmarks to a text file in the device's
* external download directory
*/
public void exportBookmarks() {
public synchronized void exportBookmarks() {
List<HistoryItem> bookmarkList = getBookmarks();
File bookmarksExport = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
Expand Down Expand Up @@ -159,7 +164,7 @@ public void exportBookmarks() {
*
* @return
*/
public List<HistoryItem> getBookmarks() {
public synchronized List<HistoryItem> getBookmarks() {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
Expand All @@ -172,6 +177,7 @@ public List<HistoryItem> getBookmarks() {
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
item.setImageId(R.drawable.ic_bookmark);
bookmarks.add(item);
}
bookmarksReader.close();
Expand All @@ -191,7 +197,7 @@ public List<HistoryItem> getBookmarks() {
* @param folder
* @return
*/
public List<HistoryItem> getBookmarksFromFolder(String folder) {
public synchronized List<HistoryItem> getBookmarksFromFolder(String folder) {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
Expand All @@ -205,6 +211,7 @@ public List<HistoryItem> getBookmarksFromFolder(String folder) {
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
item.setImageId(R.drawable.ic_bookmark);
bookmarks.add(item);
}
}
Expand All @@ -224,7 +231,7 @@ public List<HistoryItem> getBookmarksFromFolder(String folder) {
*
* @return
*/
private SortedMap<String, Integer> getBookmarkUrls() {
private synchronized SortedMap<String, Integer> getBookmarkUrls() {
SortedMap<String, Integer> map = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
Expand All @@ -250,7 +257,7 @@ private SortedMap<String, Integer> getBookmarkUrls() {
*
* @return
*/
public List<HistoryItem> getFolders() {
public synchronized List<HistoryItem> getFolders() {
List<HistoryItem> folders = new ArrayList<HistoryItem>();
SortedMap<String, Integer> folderMap = new TreeMap<String, Integer>(
String.CASE_INSENSITIVE_ORDER);
Expand Down Expand Up @@ -284,7 +291,7 @@ public List<HistoryItem> getFolders() {
* This method imports all bookmarks that are included in the device's
* permanent bookmark storage
*/
public void importBookmarksFromBrowser() {
public synchronized void importBookmarksFromBrowser() {
if (mContext.getSharedPreferences(PreferenceConstants.PREFERENCES, 0).getBoolean(
PreferenceConstants.SYSTEM_BROWSER_PRESENT, false)) {

Expand Down Expand Up @@ -328,7 +335,7 @@ public void importBookmarksFromBrowser() {
* @param dir
* @param file
*/
public void importBookmarksFromFile(File dir, String file) {
public synchronized void importBookmarksFromFile(File dir, String file) {
File bookmarksImport = new File(dir, file);
List<HistoryItem> list = new ArrayList<HistoryItem>();
try {
Expand Down Expand Up @@ -361,7 +368,7 @@ public void importBookmarksFromFile(File dir, String file) {
*
* @param list
*/
public void overwriteBookmarks(List<HistoryItem> list) {
public synchronized void overwriteBookmarks(List<HistoryItem> list) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, false));
Expand Down
Loading

3 comments on commit df51c67

@chongbo2013
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUG:in the 2g net to look the online video,the app ofen ant No response.

@chongbo2013
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java.lang.NullPointerException
at android.webkit.HTML5VideoViewProxy$VideoPlayer.exitFullScreenVideo(HTML5VideoViewProxy.java:202)
at android.webkit.HTML5VideoViewProxy.onKey(HTML5VideoViewProxy.java:942)
at android.view.View.dispatchKeyEvent(View.java:7239)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1947)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1420)
at android.app.Activity.dispatchKeyEvent(Activity.java:2443)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1874)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:4175)
at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:4118)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3191)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

@anthonycr
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the Issues tracker to report any bugs instead of commenting on commit messages. First, as I've said before, 2G is just too slow to load most webpages. That bug you posted does look legitimate, but if you aren't on the latest version of the app that is posted on this Github repo then it isn't very helpful as I've changed video functionality quite a while ago. Furthermore, that crash is in the WebView itself which I don't control so it might not be fixable.

Please sign in to comment.