use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.
the class BookmarkSettingsFragment method createDialog.
private void createDialog() {
if (mActivity == null) {
return;
}
final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
final String title = getString(R.string.title_chooser);
builder.setTitle(title + ": " + Environment.getExternalStorageDirectory());
if (mFileList == null) {
Dialog dialog = builder.show();
BrowserDialog.setDialogSize(mActivity, dialog);
return;
}
builder.setItems(mFileNameList, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mFileList[which].isDirectory()) {
builder.setTitle(title + ": " + mFileList[which]);
loadFileList(mFileList[which]);
builder.setItems(mFileNameList, this);
Dialog dialog1 = builder.show();
BrowserDialog.setDialogSize(mActivity, dialog1);
} else {
BookmarkExporter.importBookmarksFromFile(mFileList[which]).subscribeOn(Schedulers.io()).subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable final List<HistoryItem> importList) {
Preconditions.checkNonNull(importList);
mBookmarkManager.addBookmarkList(importList).observeOn(Schedulers.main()).subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
Activity activity = getActivity();
if (activity != null) {
String message = activity.getResources().getString(R.string.message_import);
Utils.showSnackbar(activity, importList.size() + " " + message);
}
}
});
}
@Override
public void onError(@NonNull Throwable throwable) {
Log.e(TAG, "onError: importing bookmarks", throwable);
Utils.createInformativeDialog(getActivity(), R.string.title_error, R.string.import_bookmark_error);
}
});
}
}
});
Dialog dialog = builder.show();
BrowserDialog.setDialogSize(mActivity, dialog);
}
use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.
the class BookmarkSettingsFragment method onPreferenceClick.
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
switch(preference.getKey()) {
case SETTINGS_EXPORT:
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(getActivity(), REQUIRED_PERMISSIONS, new PermissionsResultAction() {
@Override
public void onGranted() {
mBookmarkManager.getAllBookmarks().subscribeOn(Schedulers.io()).subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@Override
public void onItem(@Nullable List<HistoryItem> item) {
Preconditions.checkNonNull(item);
final File exportFile = BookmarkExporter.createNewExportFile();
BookmarkExporter.exportBookmarksToFile(item, exportFile).subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new CompletableOnSubscribe() {
@Override
public void onComplete() {
Activity activity = getActivity();
if (activity != null) {
Utils.showSnackbar(activity, activity.getString(R.string.bookmark_export_path) + ' ' + exportFile.getPath());
}
}
});
}
});
}
@Override
public void onDenied(String permission) {
//TODO Show message
}
});
return true;
case SETTINGS_IMPORT:
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(getActivity(), REQUIRED_PERMISSIONS, new PermissionsResultAction() {
@Override
public void onGranted() {
loadFileList(null);
createDialog();
}
@Override
public void onDenied(String permission) {
//TODO Show message
}
});
return true;
case SETTINGS_IMPORT_BROWSER:
getSync().getSupportedBrowsers().subscribeOn(Schedulers.worker()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<List<Source>>() {
@Override
public void onItem(@Nullable List<Source> item) {
Activity activity = getActivity();
if (item == null || activity == null) {
return;
}
List<String> titles = buildTitleList(activity, item);
showChooserDialog(activity, titles);
}
});
return true;
case SETTINGS_DELETE_BOOKMARKS:
showDeleteBookmarksDialog();
return true;
default:
return false;
}
}
use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.
the class BrowserActivity method addBookmark.
// By using a manager, adds a bookmark and notifies third parties about that
private void addBookmark(final String title, final String url) {
final HistoryItem item = new HistoryItem(url, title);
mBookmarkManager.addBookmarkIfNotExists(item).subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<Boolean>() {
@Override
public void onItem(@Nullable Boolean item) {
if (Boolean.TRUE.equals(item)) {
mSuggestionsAdapter.refreshBookmarks();
mBookmarksView.handleUpdatedUrl(url);
}
}
});
}
use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.
the class BrowserActivity method onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final LightningView currentView = mTabsManager.getCurrentTab();
final String currentUrl = currentView != null ? currentView.getUrl() : null;
// Handle action buttons
switch(item.getItemId()) {
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(getBookmarkDrawer())) {
mDrawerLayout.closeDrawer(getBookmarkDrawer());
}
return true;
case R.id.action_back:
if (currentView != null && currentView.canGoBack()) {
currentView.goBack();
}
return true;
case R.id.action_forward:
if (currentView != null && currentView.canGoForward()) {
currentView.goForward();
}
return true;
case R.id.action_add_to_homescreen:
if (currentView != null) {
HistoryItem shortcut = new HistoryItem(currentView.getUrl(), currentView.getTitle());
shortcut.setBitmap(currentView.getFavicon());
Utils.createShortcut(this, shortcut);
}
return true;
case R.id.action_new_tab:
newTab(null, true);
return true;
case R.id.action_incognito:
startActivity(new Intent(this, IncognitoActivity.class));
overridePendingTransition(R.anim.slide_up_in, R.anim.fade_out_scale);
return true;
case R.id.action_share:
new IntentUtils(this).shareUrl(currentUrl, currentView != null ? currentView.getTitle() : null);
return true;
case R.id.action_bookmarks:
openBookmarks();
return true;
case R.id.action_copy:
if (currentUrl != null && !UrlUtils.isSpecialUrl(currentUrl)) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", currentUrl);
clipboard.setPrimaryClip(clip);
Utils.showSnackbar(this, R.string.message_link_copied);
}
return true;
case R.id.action_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.action_history:
openHistory();
return true;
case R.id.action_downloads:
openDownloads();
return true;
case R.id.action_add_bookmark:
if (currentUrl != null && !UrlUtils.isSpecialUrl(currentUrl)) {
addBookmark(currentView.getTitle(), currentUrl);
}
return true;
case R.id.action_find:
findInPage();
return true;
case R.id.action_reading_mode:
if (currentUrl != null) {
Intent read = new Intent(this, ReadingActivity.class);
read.putExtra(Constants.LOAD_READING_URL, currentUrl);
startActivity(read);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
use of acr.browser.lightning.database.HistoryItem in project Lightning-Browser by anthonycr.
the class BookmarkExporter method importBookmarksFromAssets.
/**
* Retrieves all the default bookmarks stored
* in the raw file within assets.
*
* @param context the context necessary to open assets.
* @return a non null list of the bookmarks stored in assets.
*/
@NonNull
public static List<HistoryItem> importBookmarksFromAssets(@NonNull Context context) {
List<HistoryItem> bookmarks = new ArrayList<>();
BufferedReader bookmarksReader = null;
InputStream inputStream = null;
try {
inputStream = context.getResources().openRawResource(R.raw.default_bookmarks);
//noinspection IOResourceOpenedButNotSafelyClosed
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(KEY_TITLE));
final String url = object.getString(KEY_URL);
item.setUrl(url);
item.setFolder(object.getString(KEY_FOLDER));
item.setPosition(object.getInt(KEY_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);
Utils.close(inputStream);
}
return bookmarks;
}
Aggregations