Skip to content

Commit

Permalink
Utilize map in order to determine what bookmarks we have
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonycr committed Aug 22, 2014
1 parent bdb3fe2 commit d908513
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions src/acr/browser/lightning/BookmarkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -26,9 +28,12 @@ public class BookmarkManager {
private static final String FOLDER = "folder";
private static final String ORDER = "order";
private static final String FILE_BOOKMARKS = "bookmarks.dat";
private static SortedMap<String, Integer> mBookmarkMap = new TreeMap<String, Integer>(
String.CASE_INSENSITIVE_ORDER);

public BookmarkManager(Context context) {
mContext = context;
mBookmarkMap = getBookmarkUrls();
}

/**
Expand All @@ -39,9 +44,7 @@ public BookmarkManager(Context context) {
public void addBookmark(HistoryItem item) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);

List<String> bookmarkUrls = getBookmarkUrls();

if (bookmarkUrls.contains(item.getUrl())) {
if (mBookmarkMap.containsKey(item.getUrl())) {
return;
}
try {
Expand All @@ -54,6 +57,7 @@ public void addBookmark(HistoryItem item) {
bookmarkWriter.write(object.toString());
bookmarkWriter.newLine();
bookmarkWriter.close();
mBookmarkMap.put(item.getUrl(), 1);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
Expand All @@ -71,13 +75,16 @@ public void addBookmarkList(List<HistoryItem> list) {
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
for (HistoryItem item : list) {
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();
if (!mBookmarkMap.containsKey(item.getUrl())) {
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();
mBookmarkMap.put(item.getUrl(), 1);
}
}
bookmarkWriter.close();
} catch (IOException e) {
Expand All @@ -94,6 +101,7 @@ public void addBookmarkList(List<HistoryItem> list) {
*/
public void deleteBookmark(String url) {
List<HistoryItem> list = new ArrayList<HistoryItem>();
mBookmarkMap.remove(url);
list = getBookmarks();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
Expand Down Expand Up @@ -216,15 +224,15 @@ public List<HistoryItem> getBookmarksFromFolder(String folder) {
*
* @return
*/
private List<String> getBookmarkUrls() {
List<String> bookmarks = new ArrayList<String>();
private 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 {
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile));
String line;
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
bookmarks.add(object.getString(URL));
map.put(object.getString(URL), 1);
}
bookmarksReader.close();
} catch (FileNotFoundException e) {
Expand All @@ -234,7 +242,7 @@ private List<String> getBookmarkUrls() {
} catch (JSONException e) {
e.printStackTrace();
}
return bookmarks;
return map;
}

/**
Expand All @@ -244,19 +252,20 @@ private List<String> getBookmarkUrls() {
*/
public List<HistoryItem> getFolders() {
List<HistoryItem> folders = new ArrayList<HistoryItem>();
List<String> folderNameList = new ArrayList<String>();
SortedMap<String, Integer> folderMap = new TreeMap<String, Integer>(
String.CASE_INSENSITIVE_ORDER);
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedReader 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() && !folderNameList.contains(folderName)) {
if (!folderName.isEmpty() && !folderMap.containsKey(folderName)) {
HistoryItem item = new HistoryItem();
item.setTitle(folderName);
item.setUrl(Constants.FOLDER + folderName);
folderNameList.add(folderName);
folderMap.put(folderName, 1);
folders.add(item);
}
}
Expand Down Expand Up @@ -288,18 +297,15 @@ public void importBookmarksFromBrowser() {

String title, url;
int number = 0;
List<String> bookmarkUrls = getBookmarkUrls();
if (cursor.moveToFirst()) {
do {
title = cursor.getString(0);
url = cursor.getString(1);
if (title.isEmpty()) {
title = Utils.getDomainName(url);
}
if (!bookmarkUrls.contains(url)) {
number++;
bookmarkList.add(new HistoryItem(url, title));
}
number++;
bookmarkList.add(new HistoryItem(url, title));
} while (cursor.moveToNext());
}

Expand All @@ -324,22 +330,21 @@ public void importBookmarksFromBrowser() {
*/
public void importBookmarksFromFile(File dir, String file) {
File bookmarksImport = new File(dir, file);
List<HistoryItem> list = new ArrayList<HistoryItem>();
try {
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksImport));
String line;
List<String> bookmarkUrls = getBookmarkUrls();
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
if (!bookmarkUrls.contains(object.getString(URL))) {
HistoryItem item = new HistoryItem();
item.setTitle(object.getString(TITLE));
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
addBookmark(item);
}
HistoryItem item = new HistoryItem();
item.setTitle(object.getString(TITLE));
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
list.add(item);
}
bookmarksReader.close();
addBookmarkList(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Expand Down

0 comments on commit d908513

Please sign in to comment.