|
|
@ -50,15 +50,15 @@ public class BookmarkManager { |
|
|
|
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 final String DEFAULT_BOOKMARK_TITLE; |
|
|
|
@NonNull private final String DEFAULT_BOOKMARK_TITLE; |
|
|
|
|
|
|
|
|
|
|
|
private Map<String, HistoryItem> mBookmarksMap; |
|
|
|
private Map<String, HistoryItem> mBookmarksMap; |
|
|
|
private String mCurrentFolder = ""; |
|
|
|
@NonNull private String mCurrentFolder = ""; |
|
|
|
private final ExecutorService mExecutor; |
|
|
|
@NonNull private final ExecutorService mExecutor; |
|
|
|
private final File mFilesDir; |
|
|
|
private final File mFilesDir; |
|
|
|
|
|
|
|
|
|
|
|
@Inject |
|
|
|
@Inject |
|
|
|
public BookmarkManager(Context context) { |
|
|
|
public BookmarkManager(@NonNull Context context) { |
|
|
|
mExecutor = Executors.newSingleThreadExecutor(); |
|
|
|
mExecutor = Executors.newSingleThreadExecutor(); |
|
|
|
mFilesDir = context.getFilesDir(); |
|
|
|
mFilesDir = context.getFilesDir(); |
|
|
|
DEFAULT_BOOKMARK_TITLE = context.getString(R.string.untitled); |
|
|
|
DEFAULT_BOOKMARK_TITLE = context.getString(R.string.untitled); |
|
|
@ -163,7 +163,7 @@ public class BookmarkManager { |
|
|
|
bookmarkWriter.newLine(); |
|
|
|
bookmarkWriter.newLine(); |
|
|
|
} |
|
|
|
} |
|
|
|
success = true; |
|
|
|
success = true; |
|
|
|
} catch (IOException | JSONException e) { |
|
|
|
} catch (@NonNull IOException | JSONException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
Utils.close(bookmarkWriter); |
|
|
|
Utils.close(bookmarkWriter); |
|
|
@ -196,7 +196,7 @@ public class BookmarkManager { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public synchronized boolean addBookmark(@NonNull HistoryItem item) { |
|
|
|
public synchronized boolean addBookmark(@NonNull HistoryItem item) { |
|
|
|
final String url = item.getUrl(); |
|
|
|
final String url = item.getUrl(); |
|
|
|
if (url == null || mBookmarksMap.containsKey(url)) { |
|
|
|
if (mBookmarksMap.containsKey(url)) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
mBookmarksMap.put(url, item); |
|
|
|
mBookmarksMap.put(url, item); |
|
|
@ -209,13 +209,13 @@ public class BookmarkManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param list the list of HistoryItems to add to bookmarks |
|
|
|
* @param list the list of HistoryItems to add to bookmarks |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public synchronized void addBookmarkList(List<HistoryItem> list) { |
|
|
|
public synchronized void addBookmarkList(@Nullable List<HistoryItem> list) { |
|
|
|
if (list == null || list.isEmpty()) { |
|
|
|
if (list == null || list.isEmpty()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
for (HistoryItem item : list) { |
|
|
|
for (HistoryItem item : list) { |
|
|
|
final String url = item.getUrl(); |
|
|
|
final String url = item.getUrl(); |
|
|
|
if (url != null && !mBookmarksMap.containsKey(url)) { |
|
|
|
if (!mBookmarksMap.containsKey(url)) { |
|
|
|
mBookmarksMap.put(url, item); |
|
|
|
mBookmarksMap.put(url, item); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -228,7 +228,7 @@ public class BookmarkManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param deleteItem the bookmark item to delete |
|
|
|
* @param deleteItem the bookmark item to delete |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public synchronized boolean deleteBookmark(HistoryItem deleteItem) { |
|
|
|
public synchronized boolean deleteBookmark(@Nullable HistoryItem deleteItem) { |
|
|
|
if (deleteItem == null || deleteItem.isFolder()) { |
|
|
|
if (deleteItem == null || deleteItem.isFolder()) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -288,7 +288,7 @@ public class BookmarkManager { |
|
|
|
* @param oldItem This is the old item that you wish to edit |
|
|
|
* @param oldItem This is the old item that you wish to edit |
|
|
|
* @param newItem This is the new item that will overwrite the old item |
|
|
|
* @param newItem This is the new item that will overwrite the old item |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public synchronized void editBookmark(HistoryItem oldItem, HistoryItem newItem) { |
|
|
|
public synchronized void editBookmark(@Nullable HistoryItem oldItem, @Nullable HistoryItem newItem) { |
|
|
|
if (oldItem == null || newItem == null || oldItem.isFolder()) { |
|
|
|
if (oldItem == null || newItem == null || oldItem.isFolder()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -313,7 +313,7 @@ public class BookmarkManager { |
|
|
|
* This method exports the stored bookmarks to a text file in the device's |
|
|
|
* This method exports the stored bookmarks to a text file in the device's |
|
|
|
* external download directory |
|
|
|
* external download directory |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public synchronized void exportBookmarks(Activity activity) { |
|
|
|
public synchronized void exportBookmarks(@NonNull Activity activity) { |
|
|
|
List<HistoryItem> bookmarkList = getAllBookmarks(true); |
|
|
|
List<HistoryItem> bookmarkList = getAllBookmarks(true); |
|
|
|
File bookmarksExport = new File( |
|
|
|
File bookmarksExport = new File( |
|
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), |
|
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), |
|
|
@ -341,7 +341,7 @@ public class BookmarkManager { |
|
|
|
} |
|
|
|
} |
|
|
|
Utils.showSnackbar(activity, activity.getString(R.string.bookmark_export_path) |
|
|
|
Utils.showSnackbar(activity, activity.getString(R.string.bookmark_export_path) |
|
|
|
+ ' ' + bookmarksExport.getPath()); |
|
|
|
+ ' ' + bookmarksExport.getPath()); |
|
|
|
} catch (IOException | JSONException e) { |
|
|
|
} catch (@NonNull IOException | JSONException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
Utils.close(bookmarkWriter); |
|
|
|
Utils.close(bookmarkWriter); |
|
|
@ -357,6 +357,7 @@ public class BookmarkManager { |
|
|
|
* @param sort force to sort the returned bookmarkList |
|
|
|
* @param sort force to sort the returned bookmarkList |
|
|
|
* @return returns a list of bookmarks that can be sorted |
|
|
|
* @return returns a list of bookmarks that can be sorted |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@NonNull |
|
|
|
public synchronized List<HistoryItem> getAllBookmarks(boolean sort) { |
|
|
|
public synchronized List<HistoryItem> getAllBookmarks(boolean sort) { |
|
|
|
final List<HistoryItem> bookmarks = new ArrayList<>(mBookmarksMap.values()); |
|
|
|
final List<HistoryItem> bookmarks = new ArrayList<>(mBookmarksMap.values()); |
|
|
|
if (sort) { |
|
|
|
if (sort) { |
|
|
@ -374,7 +375,8 @@ public class BookmarkManager { |
|
|
|
* @param folder the name of the folder to retrieve bookmarks from |
|
|
|
* @param folder the name of the folder to retrieve bookmarks from |
|
|
|
* @return a list of bookmarks found in that folder |
|
|
|
* @return a list of bookmarks found in that folder |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public synchronized List<HistoryItem> getBookmarksFromFolder(String folder, boolean sort) { |
|
|
|
@NonNull |
|
|
|
|
|
|
|
public synchronized List<HistoryItem> getBookmarksFromFolder(@Nullable String folder, boolean sort) { |
|
|
|
List<HistoryItem> bookmarks = new ArrayList<>(); |
|
|
|
List<HistoryItem> bookmarks = new ArrayList<>(); |
|
|
|
if (folder == null || folder.isEmpty()) { |
|
|
|
if (folder == null || folder.isEmpty()) { |
|
|
|
bookmarks.addAll(getFolders(sort)); |
|
|
|
bookmarks.addAll(getFolders(sort)); |
|
|
@ -405,6 +407,7 @@ public class BookmarkManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return the current folder |
|
|
|
* @return the current folder |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public String getCurrentFolder() { |
|
|
|
public String getCurrentFolder() { |
|
|
|
return mCurrentFolder; |
|
|
|
return mCurrentFolder; |
|
|
|
} |
|
|
|
} |
|
|
@ -416,11 +419,12 @@ public class BookmarkManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return a list of all folders |
|
|
|
* @return a list of all folders |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@NonNull |
|
|
|
private synchronized List<HistoryItem> getFolders(boolean sort) { |
|
|
|
private synchronized List<HistoryItem> getFolders(boolean sort) { |
|
|
|
final HashMap<String, HistoryItem> folders = new HashMap<>(); |
|
|
|
final HashMap<String, HistoryItem> folders = new HashMap<>(); |
|
|
|
for (HistoryItem item : mBookmarksMap.values()) { |
|
|
|
for (HistoryItem item : mBookmarksMap.values()) { |
|
|
|
final String folderName = item.getFolder(); |
|
|
|
final String folderName = item.getFolder(); |
|
|
|
if (folderName != null && !folderName.isEmpty() && !folders.containsKey(folderName)) { |
|
|
|
if (!folderName.isEmpty() && !folders.containsKey(folderName)) { |
|
|
|
final HistoryItem folder = new HistoryItem(); |
|
|
|
final HistoryItem folder = new HistoryItem(); |
|
|
|
folder.setIsFolder(true); |
|
|
|
folder.setIsFolder(true); |
|
|
|
folder.setTitle(folderName); |
|
|
|
folder.setTitle(folderName); |
|
|
@ -442,11 +446,12 @@ public class BookmarkManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return a list of folder title strings |
|
|
|
* @return a list of folder title strings |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@NonNull |
|
|
|
public synchronized List<String> getFolderTitles() { |
|
|
|
public synchronized List<String> getFolderTitles() { |
|
|
|
final Set<String> folders = new HashSet<>(); |
|
|
|
final Set<String> folders = new HashSet<>(); |
|
|
|
for (HistoryItem item : mBookmarksMap.values()) { |
|
|
|
for (HistoryItem item : mBookmarksMap.values()) { |
|
|
|
final String folderName = item.getFolder(); |
|
|
|
final String folderName = item.getFolder(); |
|
|
|
if (folderName != null && !folderName.isEmpty()) { |
|
|
|
if (!folderName.isEmpty()) { |
|
|
|
folders.add(folderName); |
|
|
|
folders.add(folderName); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -459,7 +464,7 @@ public class BookmarkManager { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param file the file to attempt to import bookmarks from |
|
|
|
* @param file the file to attempt to import bookmarks from |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public synchronized void importBookmarksFromFile(File file, Activity activity) { |
|
|
|
public synchronized void importBookmarksFromFile(@Nullable File file, @NonNull Activity activity) { |
|
|
|
if (file == null) { |
|
|
|
if (file == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -483,7 +488,7 @@ public class BookmarkManager { |
|
|
|
addBookmarkList(list); |
|
|
|
addBookmarkList(list); |
|
|
|
String message = activity.getResources().getString(R.string.message_import); |
|
|
|
String message = activity.getResources().getString(R.string.message_import); |
|
|
|
Utils.showSnackbar(activity, number + " " + message); |
|
|
|
Utils.showSnackbar(activity, number + " " + message); |
|
|
|
} catch (IOException | JSONException e) { |
|
|
|
} catch (@NonNull IOException | JSONException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
Utils.createInformativeDialog(activity, R.string.title_error, R.string.import_bookmark_error); |
|
|
|
Utils.createInformativeDialog(activity, R.string.title_error, R.string.import_bookmark_error); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
@ -496,8 +501,8 @@ public class BookmarkManager { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static class SortIgnoreCase implements Comparator<HistoryItem> { |
|
|
|
private static class SortIgnoreCase implements Comparator<HistoryItem> { |
|
|
|
|
|
|
|
|
|
|
|
public int compare(HistoryItem o1, HistoryItem o2) { |
|
|
|
public int compare(@Nullable HistoryItem o1, @Nullable HistoryItem o2) { |
|
|
|
if (o1 == null || o2 == null || o1.getTitle() == null || o2.getTitle() == null) { |
|
|
|
if (o1 == null || o2 == null) { |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
if (o1.isFolder() == o2.isFolder()) { |
|
|
|
if (o1.isFolder() == o2.isFolder()) { |
|
|
|