Browse Source

Support bookmark importing from chrome variants and stock browser, lint fixes

master
Anthony Restaino 9 years ago
parent
commit
79d619f82b
  1. 2
      app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java
  2. 4
      app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java
  3. 2
      app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java
  4. 160
      app/src/main/java/acr/browser/lightning/database/BookmarkLocalSync.java
  5. 10
      app/src/main/java/acr/browser/lightning/database/BookmarkManager.java
  6. 194
      app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java
  7. 2
      app/src/main/java/acr/browser/lightning/object/SearchAdapter.java
  8. 2
      app/src/main/java/acr/browser/lightning/react/Observable.java
  9. 2
      app/src/main/java/acr/browser/lightning/view/LightningWebClient.java
  10. 5
      app/src/main/res/values/strings.xml
  11. 9
      app/src/main/res/xml/preference_bookmarks.xml

2
app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java

@ -21,7 +21,7 @@ import acr.browser.lightning.R; @@ -21,7 +21,7 @@ import acr.browser.lightning.R;
public class SettingsActivity extends ThemableSettingsActivity {
private static final List<String> mFragments = new ArrayList<>();
private static final List<String> mFragments = new ArrayList<>(6);
@Override
protected void onCreate(Bundle savedInstanceState) {

4
app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java

@ -33,10 +33,10 @@ public class BrowserPresenter { @@ -33,10 +33,10 @@ public class BrowserPresenter {
@Inject PreferenceManager mPreferences;
@Inject Bus mEventBus;
@NonNull private BrowserView mView;
@NonNull private final BrowserView mView;
@Nullable private LightningView mCurrentTab;
private boolean mIsIncognito;
private final boolean mIsIncognito;
private boolean mIsNewIntent;
public BrowserPresenter(@NonNull BrowserView view, boolean isIncognito) {

2
app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java

@ -63,7 +63,7 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> { @@ -63,7 +63,7 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
private File mFilesDir;
private File mCacheDir;
private Application mApp;
private final Application mApp;
private final BookmarkManager mManager;
@NonNull private final WeakReference<LightningView> mTabReference;
private final Bitmap mFolderIcon;

160
app/src/main/java/acr/browser/lightning/database/BookmarkLocalSync.java

@ -11,6 +11,9 @@ import android.util.Log; @@ -11,6 +11,9 @@ import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import acr.browser.lightning.react.Action;
import acr.browser.lightning.react.Observable;
import acr.browser.lightning.react.Subscriber;
import acr.browser.lightning.utils.Utils;
public class BookmarkLocalSync {
@ -28,18 +31,20 @@ public class BookmarkLocalSync { @@ -28,18 +31,20 @@ public class BookmarkLocalSync {
@NonNull private final Context mContext;
public enum Source {
STOCK,
CHROME_STABLE,
CHROME_BETA,
CHROME_DEV
}
public BookmarkLocalSync(@NonNull Context context) {
mContext = context;
}
@NonNull
@WorkerThread
public List<HistoryItem> getBookmarksFromStockBrowser() {
public List<HistoryItem> getBookmarksFromContentUri(String contentUri) {
List<HistoryItem> list = new ArrayList<>();
if (!isStockSupported()) {
return list;
}
Cursor cursor = getStockCursor();
Cursor cursor = getBrowserCursor(contentUri);
try {
if (cursor != null) {
for (int n = 0; n < cursor.getColumnCount(); n++) {
@ -69,114 +74,109 @@ public class BookmarkLocalSync { @@ -69,114 +74,109 @@ public class BookmarkLocalSync {
return list;
}
@NonNull
@Nullable
@WorkerThread
public List<HistoryItem> getBookmarksFromChrome() {
List<HistoryItem> list = new ArrayList<>();
if (!isChromeSupported()) {
return list;
}
Cursor cursor = getChromeCursor();
private Cursor getBrowserCursor(String contentUri) {
Cursor cursor;
Uri uri = Uri.parse(contentUri);
try {
if (cursor != null) {
for (int n = 0; n < cursor.getColumnCount(); n++) {
Log.d(TAG, cursor.getColumnName(n));
}
cursor = mContext.getContentResolver().query(uri,
new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null);
} catch (IllegalArgumentException e) {
return null;
}
return cursor;
}
while (cursor.moveToNext()) {
if (cursor.getInt(2) == 1) {
String url = cursor.getString(0);
String title = cursor.getString(1);
if (url.isEmpty()) {
continue;
}
if (title == null || title.isEmpty()) {
title = Utils.getDomainName(url);
}
if (title != null) {
list.add(new HistoryItem(url, title));
}
}
@NonNull
public Observable<List<Source>> getSupportedBrowsers() {
return Observable.create(new Action<List<Source>>() {
@Override
public void onSubscribe(@NonNull Subscriber<List<Source>> subscriber) {
List<Source> sources = new ArrayList<>(1);
if (isBrowserSupported(STOCK_BOOKMARKS_CONTENT)) {
sources.add(Source.STOCK);
}
if (isBrowserSupported(CHROME_BOOKMARKS_CONTENT)) {
sources.add(Source.CHROME_STABLE);
}
if (isBrowserSupported(CHROME_BETA_BOOKMARKS_CONTENT)) {
sources.add(Source.CHROME_BETA);
}
if (isBrowserSupported(CHROME_DEV_BOOKMARKS_CONTENT)) {
sources.add(Source.CHROME_DEV);
}
subscriber.onNext(sources);
subscriber.onComplete();
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
private boolean isBrowserSupported(String contentUri) {
Cursor cursor = getBrowserCursor(contentUri);
boolean supported = cursor != null;
Utils.close(cursor);
return list;
return supported;
}
@NonNull
@WorkerThread
public List<HistoryItem> getBookmarksFromStockBrowser() {
return getBookmarksFromContentUri(STOCK_BOOKMARKS_CONTENT);
}
@NonNull
@WorkerThread
public boolean isStockSupported() {
Cursor cursor = getStockCursor();
Utils.close(cursor);
return cursor != null;
public List<HistoryItem> getBookmarksFromChrome() {
return getBookmarksFromContentUri(CHROME_BOOKMARKS_CONTENT);
}
@NonNull
@WorkerThread
public boolean isChromeSupported() {
Cursor cursor = getChromeCursor();
Utils.close(cursor);
public List<HistoryItem> getBookmarksFromChromeBeta() {
return getBookmarksFromContentUri(CHROME_BETA_BOOKMARKS_CONTENT);
}
@NonNull
@WorkerThread
public List<HistoryItem> getBookmarksFromChromeDev() {
return getBookmarksFromContentUri(CHROME_DEV_BOOKMARKS_CONTENT);
}
@WorkerThread
public boolean isBrowserImportSupported() {
Cursor chrome = getChromeCursor();
Utils.close(chrome);
Cursor dev = getChromeDevCursor();
Utils.close(dev);
Cursor beta = getChromeBetaCursor();
return cursor != null || dev != null || beta != null;
Cursor stock = getStockCursor();
Utils.close(stock);
return chrome != null || dev != null || beta != null || stock != null;
}
@Nullable
@WorkerThread
private Cursor getChromeBetaCursor() {
Cursor cursor;
Uri uri = Uri.parse(CHROME_BETA_BOOKMARKS_CONTENT);
try {
cursor = mContext.getContentResolver().query(uri,
new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null);
} catch (IllegalArgumentException e) {
return null;
}
return cursor;
return getBrowserCursor(CHROME_BETA_BOOKMARKS_CONTENT);
}
@Nullable
@WorkerThread
private Cursor getChromeDevCursor() {
Cursor cursor;
Uri uri = Uri.parse(CHROME_DEV_BOOKMARKS_CONTENT);
try {
cursor = mContext.getContentResolver().query(uri,
new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null);
} catch (IllegalArgumentException e) {
return null;
}
return cursor;
return getBrowserCursor(CHROME_DEV_BOOKMARKS_CONTENT);
}
@Nullable
@WorkerThread
private Cursor getChromeCursor() {
Cursor cursor;
Uri uri = Uri.parse(CHROME_BOOKMARKS_CONTENT);
try {
cursor = mContext.getContentResolver().query(uri,
new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null);
} catch (IllegalArgumentException e) {
return null;
}
return cursor;
return getBrowserCursor(CHROME_BOOKMARKS_CONTENT);
}
@Nullable
@WorkerThread
private Cursor getStockCursor() {
Cursor cursor;
Uri uri = Uri.parse(STOCK_BOOKMARKS_CONTENT);
try {
cursor = mContext.getContentResolver().query(uri,
new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null);
} catch (IllegalArgumentException e) {
return null;
}
return cursor;
return getBrowserCursor(STOCK_BOOKMARKS_CONTENT);
}
public void printAllColumns() {

10
app/src/main/java/acr/browser/lightning/database/BookmarkManager.java

@ -282,6 +282,16 @@ public class BookmarkManager { @@ -282,6 +282,16 @@ public class BookmarkManager {
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
}
/**
* This method deletes ALL bookmarks created
* by the user. Use this method carefully and
* do not use it without explicit user consent.
*/
public synchronized void deleteAllBookmarks() {
mBookmarksMap = new HashMap<>();
mExecutor.execute(new BookmarksWriter(new LinkedList<>(mBookmarksMap.values())));
}
/**
* This method edits a particular bookmark in the bookmark database
*

194
app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java

@ -6,6 +6,8 @@ package acr.browser.lightning.fragment; @@ -6,6 +6,8 @@ package acr.browser.lightning.fragment;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
@ -15,9 +17,15 @@ import android.preference.PreferenceFragment; @@ -15,9 +17,15 @@ import android.preference.PreferenceFragment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.ArrayAdapter;
import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
@ -26,13 +34,13 @@ import javax.inject.Inject; @@ -26,13 +34,13 @@ import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.database.BookmarkLocalSync;
import acr.browser.lightning.database.BookmarkLocalSync.Source;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem;
import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;
import acr.browser.lightning.react.OnSubscribe;
import acr.browser.lightning.react.Schedulers;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils;
@ -41,6 +49,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref @@ -41,6 +49,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
private static final String SETTINGS_EXPORT = "export_bookmark";
private static final String SETTINGS_IMPORT = "import_bookmark";
private static final String SETTINGS_IMPORT_BROWSER = "import_browser";
private static final String SETTINGS_DELETE_BOOKMARKS = "delete_bookmarks";
@Nullable private Activity mActivity;
@ -53,27 +62,41 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref @@ -53,27 +62,41 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private ImportBookmarksTask mImportTaskReference;
private static final File mPath = new File(Environment.getExternalStorageDirectory().toString());
private class ImportBookmarksTask extends AsyncTask<Void, Void, Integer> {
@NonNull private final WeakReference<Activity> mActivityReference;
private final Source mSource;
public ImportBookmarksTask(Activity activity) {
public ImportBookmarksTask(Activity activity, Source source) {
mActivityReference = new WeakReference<>(activity);
mSource = source;
}
@Override
protected Integer doInBackground(Void... params) {
List<HistoryItem> list = null;
if (getSync().isStockSupported()) {
list = getSync().getBookmarksFromStockBrowser();
} else if (getSync().isChromeSupported()) {
list = getSync().getBookmarksFromChrome();
List<HistoryItem> list;
Log.d(Constants.TAG, "Loading bookmarks from: " + mSource.name());
switch (mSource) {
case STOCK:
list = getSync().getBookmarksFromStockBrowser();
break;
case CHROME_STABLE:
list = getSync().getBookmarksFromChrome();
break;
case CHROME_BETA:
list = getSync().getBookmarksFromChromeBeta();
break;
case CHROME_DEV:
list = getSync().getBookmarksFromChromeDev();
break;
default:
list = new ArrayList<>(0);
break;
}
int count = 0;
if (list != null && !list.isEmpty()) {
if (!list.isEmpty()) {
mBookmarkManager.addBookmarkList(list);
count = list.size();
}
@ -124,30 +147,28 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref @@ -124,30 +147,28 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
public void onDestroy() {
super.onDestroy();
mActivity = null;
if (mImportTaskReference != null) {
mImportTaskReference.cancel(false);
}
}
private void initPrefs() {
Preference exportpref = findPreference(SETTINGS_EXPORT);
Preference importpref = findPreference(SETTINGS_IMPORT);
Preference exportPref = findPreference(SETTINGS_EXPORT);
Preference importPref = findPreference(SETTINGS_IMPORT);
Preference deletePref = findPreference(SETTINGS_DELETE_BOOKMARKS);
exportpref.setOnPreferenceClickListener(this);
importpref.setOnPreferenceClickListener(this);
exportPref.setOnPreferenceClickListener(this);
importPref.setOnPreferenceClickListener(this);
deletePref.setOnPreferenceClickListener(this);
BrowserApp.getTaskThread().execute(mInitializeImportPreference);
}
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
Preference importStock = findPreference(SETTINGS_IMPORT_BROWSER);
importStock.setEnabled(getSync().isBrowserImportSupported());
importStock.setOnPreferenceClickListener(BookmarkSettingsFragment.this);
}
});
private final Runnable mInitializeImportPreference = new Runnable() {
@Override
public void run() {
Preference importStock = findPreference(SETTINGS_IMPORT_BROWSER);
importStock.setEnabled(getSync().isStockSupported() || getSync().isChromeSupported());
importStock.setOnPreferenceClickListener(BookmarkSettingsFragment.this);
}
};
}
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
@ -182,14 +203,125 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref @@ -182,14 +203,125 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
});
return true;
case SETTINGS_IMPORT_BROWSER:
mImportTaskReference = new ImportBookmarksTask(getActivity());
mImportTaskReference.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
getSync().getSupportedBrowsers().subscribeOn(Schedulers.worker())
.observeOn(Schedulers.main()).subscribe(new OnSubscribe<List<Source>>() {
@Override
public void onNext(@Nullable List<Source> items) {
Activity activity = getActivity();
if (items == null || activity == null) {
return;
}
List<String> titles = buildTitleList(activity, items);
showChooserDialog(activity, titles);
}
});
return true;
case SETTINGS_DELETE_BOOKMARKS:
showDeleteBookmarksDialog();
return true;
default:
return false;
}
}
private void showDeleteBookmarksDialog() {
Activity activity = getActivity();
if (activity == null) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.action_delete);
builder.setMessage(R.string.action_delete_all_bookmarks);
builder.setNegativeButton(R.string.no, null);
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mBookmarkManager.deleteAllBookmarks();
}
});
builder.show();
}
@NonNull
private List<String> buildTitleList(@NonNull Activity activity, @NonNull List<Source> items) {
List<String> titles = new ArrayList<>();
String title;
for (Source source : items) {
switch (source) {
case STOCK:
titles.add(getString(R.string.stock_browser));
break;
case CHROME_STABLE:
title = getTitle(activity, "com.android.chrome");
if (title != null) {
titles.add(title);
}
break;
case CHROME_BETA:
title = getTitle(activity, "com.chrome.beta");
if (title != null) {
titles.add(title);
}
break;
case CHROME_DEV:
title = getTitle(activity, "com.chrome.beta");
if (title != null) {
titles.add(title);
}
break;
default:
break;
}
}
return titles;
}
private void showChooserDialog(final Activity activity, List<String> list) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(activity,
android.R.layout.simple_list_item_1);
for (String title : list) {
adapter.add(title);
}
builder.setTitle(R.string.supported_browsers_title);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String title = adapter.getItem(which);
Source source = null;
if (title.equals(getString(R.string.stock_browser))) {
source = Source.STOCK;
} else if (title.equals(getTitle(activity, "com.android.chrome"))) {
source = Source.CHROME_STABLE;
} else if (title.equals(getTitle(activity, "com.android.beta"))) {
source = Source.CHROME_BETA;
} else if (title.equals(getTitle(activity, "com.android.dev"))) {
source = Source.CHROME_DEV;
}
if (source != null) {
new ImportBookmarksTask(activity, source).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
});
builder.show();
}
@Nullable
private String getTitle(@NonNull Activity activity, @NonNull String packageName) {
PackageManager pm = activity.getPackageManager();
try {
ApplicationInfo info = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
CharSequence title = pm.getApplicationLabel(info);
if (title != null) {
return title.toString();
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
private void loadFileList(@Nullable File path) {
File file;
if (path != null) {

2
app/src/main/java/acr/browser/lightning/object/SearchAdapter.java

@ -308,7 +308,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @@ -308,7 +308,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
protected List<HistoryItem> doInBackground(String... arg0) {
mIsExecuting = true;
List<HistoryItem> filter = new ArrayList<>();
List<HistoryItem> filter = new ArrayList<>(5);
String query = arg0[0];
try {
query = SPACE_PATTERN.matcher(query).replaceAll("+");

2
app/src/main/java/acr/browser/lightning/react/Observable.java

@ -144,7 +144,7 @@ public class Observable<T> { @@ -144,7 +144,7 @@ public class Observable<T> {
private static class SubscriberImpl<T> implements Subscriber<T> {
@Nullable private OnSubscribe<T> mOnSubscribe;
@NonNull private Observable<T> mObservable;
@NonNull private final Observable<T> mObservable;
private boolean mOnCompleteExecuted = false;
public SubscriberImpl(@NonNull OnSubscribe<T> onSubscribe, @NonNull Observable<T> observable) {

2
app/src/main/java/acr/browser/lightning/view/LightningWebClient.java

@ -194,7 +194,7 @@ public class LightningWebClient extends WebViewClient { @@ -194,7 +194,7 @@ public class LightningWebClient extends WebViewClient {
@NonNull
private static List<Integer> getAllSslErrorMessageCodes(@NonNull SslError error) {
List<Integer> errorCodeMessageCodes = new ArrayList<>();
List<Integer> errorCodeMessageCodes = new ArrayList<>(1);
if (error.hasError(SslError.SSL_DATE_INVALID)) {
errorCodeMessageCodes.add(R.string.message_certificate_date_invalid);

5
app/src/main/res/values/strings.xml

@ -42,7 +42,7 @@ @@ -42,7 +42,7 @@
<string name="block">Block Images</string>
<string name="window">Allow sites to open new windows</string>
<string name="cookies">Enable Cookies</string>
<string name="importbookmarks">Import bookmarks from Browser</string>
<string name="importbookmarks">Import bookmarks from browser</string>
<string name="size">Text size</string>
<string name="recommended">Recommended</string>
<string name="search">Search Engine</string>
@ -50,6 +50,8 @@ @@ -50,6 +50,8 @@
<string name="wideViewPort">Use wide viewport</string>
<string name="overViewMode">Load pages in overview mode</string>
<string name="restore">Restore lost tabs on start</string>
<string name="supported_browsers_title">Supported Browsers</string>
<string name="stock_browser">Stock Browser</string>
<string name="stock_browser_unavailable">No stock browser detected</string>
<string name="stock_browser_available">Supported stock browser detected</string>
<string name="fullScreenOption">Hide status bar while browsing</string>
@ -230,4 +232,5 @@ @@ -230,4 +232,5 @@
<string name="remove_identifying_headers">Remove Identifying Headers</string>
<string name="action_add_to_homescreen">Add to Homescreen</string>
<string name="message_added_to_homescreen">Shortcut Added to Homescreen</string>
<string name="action_delete_all_bookmarks">Delete all bookmarks</string>
</resources>

9
app/src/main/res/xml/preference_bookmarks.xml

@ -4,12 +4,15 @@ @@ -4,12 +4,15 @@
<PreferenceCategory android:title="@string/bookmark_settings">
<Preference
android:key="export_bookmark"
android:title="@string/export_bookmarks" />
android:title="@string/export_bookmarks"/>
<Preference
android:key="import_bookmark"
android:title="@string/import_backup" />
android:title="@string/import_backup"/>
<Preference
android:key="import_browser"
android:title="@string/importbookmarks" />
android:title="@string/importbookmarks"/>
<Preference
android:key="delete_bookmarks"
android:title="@string/action_delete_all_bookmarks"/>
</PreferenceCategory>
</PreferenceScreen>
Loading…
Cancel
Save