Using observables instead of executors
This commit is contained in:
parent
ee078c1495
commit
b8bbd8f6e0
@ -102,11 +102,6 @@ public class BrowserApp extends Application {
|
||||
return mIOThread;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Executor getTaskThread() {
|
||||
return mTaskThread;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this is a release build.
|
||||
*
|
||||
|
@ -83,7 +83,7 @@ public class BookmarkLocalSync {
|
||||
Uri uri = Uri.parse(contentUri);
|
||||
try {
|
||||
cursor = mContext.getContentResolver().query(uri,
|
||||
new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null);
|
||||
new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
@ -145,16 +145,23 @@ public class BookmarkLocalSync {
|
||||
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();
|
||||
Cursor stock = getStockCursor();
|
||||
Utils.close(stock);
|
||||
return chrome != null || dev != null || beta != null || stock != null;
|
||||
@NonNull
|
||||
public Single<Boolean> isBrowserImportSupported() {
|
||||
return Single.create(new SingleAction<Boolean>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull SingleSubscriber<Boolean> subscriber) {
|
||||
Cursor chrome = getChromeCursor();
|
||||
Utils.close(chrome);
|
||||
Cursor dev = getChromeDevCursor();
|
||||
Utils.close(dev);
|
||||
Cursor beta = getChromeBetaCursor();
|
||||
Cursor stock = getStockCursor();
|
||||
Utils.close(stock);
|
||||
|
||||
subscriber.onItem(chrome != null || dev != null || beta != null || stock != null);
|
||||
subscriber.onComplete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@ -164,20 +164,18 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
||||
importPref.setOnPreferenceClickListener(this);
|
||||
deletePref.setOnPreferenceClickListener(this);
|
||||
|
||||
BrowserApp.getTaskThread().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final boolean isBrowserImportSupported = getSync().isBrowserImportSupported();
|
||||
Schedulers.main().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Preference importStock = findPreference(SETTINGS_IMPORT_BROWSER);
|
||||
importStock.setEnabled(isBrowserImportSupported);
|
||||
importStock.setOnPreferenceClickListener(BookmarkSettingsFragment.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
getSync().isBrowserImportSupported()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(Schedulers.main())
|
||||
.subscribe(new SingleOnSubscribe<Boolean>() {
|
||||
@Override
|
||||
public void onItem(@Nullable Boolean supported) {
|
||||
Preconditions.checkNonNull(supported);
|
||||
Preference importStock = findPreference(SETTINGS_IMPORT_BROWSER);
|
||||
importStock.setEnabled(supported);
|
||||
importStock.setOnPreferenceClickListener(BookmarkSettingsFragment.this);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,12 @@ import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import com.anthonycr.bonsai.Completable;
|
||||
import com.anthonycr.bonsai.CompletableAction;
|
||||
import com.anthonycr.bonsai.CompletableOnSubscribe;
|
||||
import com.anthonycr.bonsai.CompletableSubscriber;
|
||||
import com.anthonycr.bonsai.Schedulers;
|
||||
|
||||
import acr.browser.lightning.R;
|
||||
import acr.browser.lightning.app.BrowserApp;
|
||||
import acr.browser.lightning.dialog.BrowserDialog;
|
||||
@ -40,7 +46,6 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
|
||||
private static final String SETTINGS_IDENTIFYINGHEADERS = "remove_identifying_headers";
|
||||
|
||||
private Activity mActivity;
|
||||
private Handler mMessageHandler;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@ -103,29 +108,6 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
|
||||
|
||||
cb3cookies.setEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
|
||||
|
||||
mMessageHandler = new MessageHandler(mActivity);
|
||||
}
|
||||
|
||||
private static class MessageHandler extends Handler {
|
||||
|
||||
final Activity mHandlerContext;
|
||||
|
||||
public MessageHandler(Activity context) {
|
||||
this.mHandlerContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(@NonNull Message msg) {
|
||||
switch (msg.what) {
|
||||
case 1:
|
||||
Utils.showSnackbar(mHandlerContext, R.string.message_clear_history);
|
||||
break;
|
||||
case 2:
|
||||
Utils.showSnackbar(mHandlerContext, R.string.message_cookies_cleared);
|
||||
break;
|
||||
}
|
||||
super.handleMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -152,19 +134,22 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
|
||||
builder.setTitle(getResources().getString(R.string.title_clear_history));
|
||||
Dialog dialog = builder.setMessage(getResources().getString(R.string.dialog_history))
|
||||
.setPositiveButton(getResources().getString(R.string.action_yes),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
BrowserApp.getIOThread().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
clearHistory();
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.setNegativeButton(getResources().getString(R.string.action_no), null).show();
|
||||
.setPositiveButton(getResources().getString(R.string.action_yes),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
clearHistory()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(Schedulers.main())
|
||||
.subscribe(new CompletableOnSubscribe() {
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Utils.showSnackbar(getActivity(), R.string.message_clear_history);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.setNegativeButton(getResources().getString(R.string.action_no), null).show();
|
||||
BrowserDialog.setDialogSize(mActivity, dialog);
|
||||
}
|
||||
|
||||
@ -172,19 +157,22 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
|
||||
builder.setTitle(getResources().getString(R.string.title_clear_cookies));
|
||||
builder.setMessage(getResources().getString(R.string.dialog_cookies))
|
||||
.setPositiveButton(getResources().getString(R.string.action_yes),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
BrowserApp.getTaskThread().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
clearCookies();
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.setNegativeButton(getResources().getString(R.string.action_no), null).show();
|
||||
.setPositiveButton(getResources().getString(R.string.action_yes),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
clearCookies()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(Schedulers.main())
|
||||
.subscribe(new CompletableOnSubscribe() {
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Utils.showSnackbar(getActivity(), R.string.message_cookies_cleared);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.setNegativeButton(getResources().getString(R.string.action_no), null).show();
|
||||
}
|
||||
|
||||
private void clearCache() {
|
||||
@ -194,14 +182,34 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
|
||||
Utils.showSnackbar(mActivity, R.string.message_cache_cleared);
|
||||
}
|
||||
|
||||
private void clearHistory() {
|
||||
WebUtils.clearHistory(getActivity());
|
||||
mMessageHandler.sendEmptyMessage(1);
|
||||
@NonNull
|
||||
private Completable clearHistory() {
|
||||
return Completable.create(new CompletableAction() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
WebUtils.clearHistory(activity);
|
||||
subscriber.onComplete();
|
||||
}
|
||||
subscriber.onError(new RuntimeException("Activity was null in clearHistory"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void clearCookies() {
|
||||
WebUtils.clearCookies(getActivity());
|
||||
mMessageHandler.sendEmptyMessage(2);
|
||||
@NonNull
|
||||
private Completable clearCookies() {
|
||||
return Completable.create(new CompletableAction() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
WebUtils.clearCookies(activity);
|
||||
subscriber.onComplete();
|
||||
}
|
||||
subscriber.onError(new RuntimeException("Activity was null in clearCookies"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void clearWebStorage() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user