|
|
@ -9,6 +9,8 @@ import java.io.FileWriter; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.SortedMap; |
|
|
|
|
|
|
|
import java.util.TreeMap; |
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException; |
|
|
|
import org.json.JSONException; |
|
|
|
import org.json.JSONObject; |
|
|
|
import org.json.JSONObject; |
|
|
@ -26,9 +28,12 @@ public class BookmarkManager { |
|
|
|
private static final String FOLDER = "folder"; |
|
|
|
private static final String FOLDER = "folder"; |
|
|
|
private static final String ORDER = "order"; |
|
|
|
private static final String ORDER = "order"; |
|
|
|
private static final String FILE_BOOKMARKS = "bookmarks.dat"; |
|
|
|
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) { |
|
|
|
public BookmarkManager(Context context) { |
|
|
|
mContext = context; |
|
|
|
mContext = context; |
|
|
|
|
|
|
|
mBookmarkMap = getBookmarkUrls(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -39,9 +44,7 @@ public class BookmarkManager { |
|
|
|
public void addBookmark(HistoryItem item) { |
|
|
|
public void addBookmark(HistoryItem item) { |
|
|
|
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); |
|
|
|
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); |
|
|
|
|
|
|
|
|
|
|
|
List<String> bookmarkUrls = getBookmarkUrls(); |
|
|
|
if (mBookmarkMap.containsKey(item.getUrl())) { |
|
|
|
|
|
|
|
|
|
|
|
if (bookmarkUrls.contains(item.getUrl())) { |
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
try { |
|
|
@ -54,6 +57,7 @@ public class BookmarkManager { |
|
|
|
bookmarkWriter.write(object.toString()); |
|
|
|
bookmarkWriter.write(object.toString()); |
|
|
|
bookmarkWriter.newLine(); |
|
|
|
bookmarkWriter.newLine(); |
|
|
|
bookmarkWriter.close(); |
|
|
|
bookmarkWriter.close(); |
|
|
|
|
|
|
|
mBookmarkMap.put(item.getUrl(), 1); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} catch (JSONException e) { |
|
|
|
} catch (JSONException e) { |
|
|
@ -71,13 +75,16 @@ public class BookmarkManager { |
|
|
|
try { |
|
|
|
try { |
|
|
|
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true)); |
|
|
|
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true)); |
|
|
|
for (HistoryItem item : list) { |
|
|
|
for (HistoryItem item : list) { |
|
|
|
JSONObject object = new JSONObject(); |
|
|
|
if (!mBookmarkMap.containsKey(item.getUrl())) { |
|
|
|
object.put(TITLE, item.getTitle()); |
|
|
|
JSONObject object = new JSONObject(); |
|
|
|
object.put(URL, item.getUrl()); |
|
|
|
object.put(TITLE, item.getTitle()); |
|
|
|
object.put(FOLDER, item.getFolder()); |
|
|
|
object.put(URL, item.getUrl()); |
|
|
|
object.put(ORDER, item.getOrder()); |
|
|
|
object.put(FOLDER, item.getFolder()); |
|
|
|
bookmarkWriter.write(object.toString()); |
|
|
|
object.put(ORDER, item.getOrder()); |
|
|
|
bookmarkWriter.newLine(); |
|
|
|
bookmarkWriter.write(object.toString()); |
|
|
|
|
|
|
|
bookmarkWriter.newLine(); |
|
|
|
|
|
|
|
mBookmarkMap.put(item.getUrl(), 1); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
bookmarkWriter.close(); |
|
|
|
bookmarkWriter.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
@ -94,6 +101,7 @@ public class BookmarkManager { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void deleteBookmark(String url) { |
|
|
|
public void deleteBookmark(String url) { |
|
|
|
List<HistoryItem> list = new ArrayList<HistoryItem>(); |
|
|
|
List<HistoryItem> list = new ArrayList<HistoryItem>(); |
|
|
|
|
|
|
|
mBookmarkMap.remove(url); |
|
|
|
list = getBookmarks(); |
|
|
|
list = getBookmarks(); |
|
|
|
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); |
|
|
|
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); |
|
|
|
try { |
|
|
|
try { |
|
|
@ -216,15 +224,15 @@ public class BookmarkManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private List<String> getBookmarkUrls() { |
|
|
|
private SortedMap<String, Integer> getBookmarkUrls() { |
|
|
|
List<String> bookmarks = new ArrayList<String>(); |
|
|
|
SortedMap<String, Integer> map = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER); |
|
|
|
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); |
|
|
|
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); |
|
|
|
try { |
|
|
|
try { |
|
|
|
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile)); |
|
|
|
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile)); |
|
|
|
String line; |
|
|
|
String line; |
|
|
|
while ((line = bookmarksReader.readLine()) != null) { |
|
|
|
while ((line = bookmarksReader.readLine()) != null) { |
|
|
|
JSONObject object = new JSONObject(line); |
|
|
|
JSONObject object = new JSONObject(line); |
|
|
|
bookmarks.add(object.getString(URL)); |
|
|
|
map.put(object.getString(URL), 1); |
|
|
|
} |
|
|
|
} |
|
|
|
bookmarksReader.close(); |
|
|
|
bookmarksReader.close(); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
} catch (FileNotFoundException e) { |
|
|
@ -234,7 +242,7 @@ public class BookmarkManager { |
|
|
|
} catch (JSONException e) { |
|
|
|
} catch (JSONException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
return bookmarks; |
|
|
|
return map; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -244,7 +252,8 @@ public class BookmarkManager { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<HistoryItem> getFolders() { |
|
|
|
public List<HistoryItem> getFolders() { |
|
|
|
List<HistoryItem> folders = new ArrayList<HistoryItem>(); |
|
|
|
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); |
|
|
|
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); |
|
|
|
try { |
|
|
|
try { |
|
|
|
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile)); |
|
|
|
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile)); |
|
|
@ -252,11 +261,11 @@ public class BookmarkManager { |
|
|
|
while ((line = bookmarksReader.readLine()) != null) { |
|
|
|
while ((line = bookmarksReader.readLine()) != null) { |
|
|
|
JSONObject object = new JSONObject(line); |
|
|
|
JSONObject object = new JSONObject(line); |
|
|
|
String folderName = object.getString(FOLDER); |
|
|
|
String folderName = object.getString(FOLDER); |
|
|
|
if (!folderName.isEmpty() && !folderNameList.contains(folderName)) { |
|
|
|
if (!folderName.isEmpty() && !folderMap.containsKey(folderName)) { |
|
|
|
HistoryItem item = new HistoryItem(); |
|
|
|
HistoryItem item = new HistoryItem(); |
|
|
|
item.setTitle(folderName); |
|
|
|
item.setTitle(folderName); |
|
|
|
item.setUrl(Constants.FOLDER + folderName); |
|
|
|
item.setUrl(Constants.FOLDER + folderName); |
|
|
|
folderNameList.add(folderName); |
|
|
|
folderMap.put(folderName, 1); |
|
|
|
folders.add(item); |
|
|
|
folders.add(item); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -288,7 +297,6 @@ public class BookmarkManager { |
|
|
|
|
|
|
|
|
|
|
|
String title, url; |
|
|
|
String title, url; |
|
|
|
int number = 0; |
|
|
|
int number = 0; |
|
|
|
List<String> bookmarkUrls = getBookmarkUrls(); |
|
|
|
|
|
|
|
if (cursor.moveToFirst()) { |
|
|
|
if (cursor.moveToFirst()) { |
|
|
|
do { |
|
|
|
do { |
|
|
|
title = cursor.getString(0); |
|
|
|
title = cursor.getString(0); |
|
|
@ -296,10 +304,8 @@ public class BookmarkManager { |
|
|
|
if (title.isEmpty()) { |
|
|
|
if (title.isEmpty()) { |
|
|
|
title = Utils.getDomainName(url); |
|
|
|
title = Utils.getDomainName(url); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!bookmarkUrls.contains(url)) { |
|
|
|
number++; |
|
|
|
number++; |
|
|
|
bookmarkList.add(new HistoryItem(url, title)); |
|
|
|
bookmarkList.add(new HistoryItem(url, title)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} while (cursor.moveToNext()); |
|
|
|
} while (cursor.moveToNext()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -324,22 +330,21 @@ public class BookmarkManager { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void importBookmarksFromFile(File dir, String file) { |
|
|
|
public void importBookmarksFromFile(File dir, String file) { |
|
|
|
File bookmarksImport = new File(dir, file); |
|
|
|
File bookmarksImport = new File(dir, file); |
|
|
|
|
|
|
|
List<HistoryItem> list = new ArrayList<HistoryItem>(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksImport)); |
|
|
|
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksImport)); |
|
|
|
String line; |
|
|
|
String line; |
|
|
|
List<String> bookmarkUrls = getBookmarkUrls(); |
|
|
|
|
|
|
|
while ((line = bookmarksReader.readLine()) != null) { |
|
|
|
while ((line = bookmarksReader.readLine()) != null) { |
|
|
|
JSONObject object = new JSONObject(line); |
|
|
|
JSONObject object = new JSONObject(line); |
|
|
|
if (!bookmarkUrls.contains(object.getString(URL))) { |
|
|
|
HistoryItem item = new HistoryItem(); |
|
|
|
HistoryItem item = new HistoryItem(); |
|
|
|
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.setOrder(object.getInt(ORDER)); |
|
|
|
list.add(item); |
|
|
|
addBookmark(item); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
bookmarksReader.close(); |
|
|
|
bookmarksReader.close(); |
|
|
|
|
|
|
|
addBookmarkList(list); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|