Browse Source

Inject Bus, HistoryDatabase, and PreferenceManager rather than using BrowserApp to access instances

master
Anthony Restaino 8 years ago
parent
commit
cb52aa0065
  1. 9
      app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java
  2. 6
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  3. 10
      app/src/main/java/acr/browser/lightning/activity/ThemableSettingsActivity.java
  4. 15
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  5. 16
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  6. 11
      app/src/main/java/acr/browser/lightning/constant/HistoryPage.java
  7. 10
      app/src/main/java/acr/browser/lightning/constant/StartPage.java
  8. 61
      app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java
  9. 14
      app/src/main/java/acr/browser/lightning/download/DownloadHandler.java
  10. 2
      app/src/main/java/acr/browser/lightning/download/FetchUrlMimeType.java
  11. 10
      app/src/main/java/acr/browser/lightning/download/LightningDownloadListener.java
  12. 16
      app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java
  13. 13
      app/src/main/java/acr/browser/lightning/utils/AdBlock.java
  14. 6
      app/src/main/java/acr/browser/lightning/utils/Utils.java
  15. 5
      app/src/main/java/acr/browser/lightning/utils/WebUtils.java
  16. 2
      app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java
  17. 2
      app/src/main/java/acr/browser/lightning/view/LightningWebClient.java

9
app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java

@ -5,6 +5,8 @@ import android.content.DialogInterface; @@ -5,6 +5,8 @@ import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import com.squareup.otto.Bus;
import net.i2p.android.ui.I2PAndroidHelper;
import javax.inject.Inject;
@ -26,6 +28,7 @@ public class ProxyUtils { @@ -26,6 +28,7 @@ public class ProxyUtils {
@Inject PreferenceManager mPreferences;
@Inject I2PAndroidHelper mI2PHelper;
@Inject Bus mBus;
@Inject
public ProxyUtils() {
@ -140,12 +143,10 @@ public class ProxyUtils { @@ -140,12 +143,10 @@ public class ProxyUtils {
public boolean isProxyReady() {
if (mPreferences.getProxyChoice() == Constants.PROXY_I2P) {
if (!mI2PHelper.isI2PAndroidRunning()) {
BrowserApp.getBus()
.post(new BrowserEvents.ShowSnackBarMessage(R.string.i2p_not_running));
mBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.i2p_not_running));
return false;
} else if (!mI2PHelper.areTunnelsActive()) {
BrowserApp.getBus()
.post(new BrowserEvents.ShowSnackBarMessage(R.string.i2p_tunnels_not_ready));
mBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.i2p_tunnels_not_ready));
return false;
}
}

6
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -1066,7 +1066,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1066,7 +1066,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
Log.d(Constants.TAG, "Cache Cleared");
}
if (mPreferences.getClearHistoryExitEnabled() && !isIncognito()) {
WebUtils.clearHistory(this);
WebUtils.clearHistory(this, mHistoryDatabase);
Log.d(Constants.TAG, "History Cleared");
}
if (mPreferences.getClearCookiesExitEnabled() && !isIncognito()) {
@ -1400,7 +1400,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1400,7 +1400,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* function that opens the HTML history page in the browser
*/
private void openHistory() {
new HistoryPage(mTabsManager.getCurrentTab(), getApplication()).load();
new HistoryPage(mTabsManager.getCurrentTab(), getApplication(), mHistoryDatabase).load();
}
/**
@ -1989,7 +1989,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1989,7 +1989,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Subscribe
public void loadHistory(final BrowserEvents.OpenHistoryInCurrentTab event) {
new HistoryPage(mTabsManager.getCurrentTab(), getApplication()).load();
new HistoryPage(mTabsManager.getCurrentTab(), getApplication(), mHistoryDatabase).load();
}
/**

10
app/src/main/java/acr/browser/lightning/activity/ThemableSettingsActivity.java

@ -3,17 +3,23 @@ package acr.browser.lightning.activity; @@ -3,17 +3,23 @@ package acr.browser.lightning.activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.ThemeUtils;
public abstract class ThemableSettingsActivity extends AppCompatPreferenceActivity {
private int mTheme;
@Inject PreferenceManager mPreferenceManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
mTheme = BrowserApp.getPreferenceManager().getUseTheme();
BrowserApp.getAppComponent().inject(this);
mTheme = mPreferenceManager.getUseTheme();
// set the theme
if (mTheme == 0) {
@ -32,7 +38,7 @@ public abstract class ThemableSettingsActivity extends AppCompatPreferenceActivi @@ -32,7 +38,7 @@ public abstract class ThemableSettingsActivity extends AppCompatPreferenceActivi
@Override
protected void onResume() {
super.onResume();
if (BrowserApp.getPreferenceManager().getUseTheme() != mTheme) {
if (mPreferenceManager.getUseTheme() != mTheme) {
restart();
}
}

15
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -5,13 +5,18 @@ import javax.inject.Singleton; @@ -5,13 +5,18 @@ import javax.inject.Singleton;
import acr.browser.lightning.activity.BrowserActivity;
import acr.browser.lightning.activity.ReadingActivity;
import acr.browser.lightning.activity.ThemableBrowserActivity;
import acr.browser.lightning.activity.ThemableSettingsActivity;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.fragment.BookmarkSettingsFragment;
import acr.browser.lightning.fragment.BookmarksFragment;
import acr.browser.lightning.fragment.LightningPreferenceFragment;
import acr.browser.lightning.fragment.PrivacySettingsFragment;
import acr.browser.lightning.fragment.TabsFragment;
import acr.browser.lightning.object.SearchAdapter;
import acr.browser.lightning.utils.AdBlock;
import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.view.LightningView;
import acr.browser.lightning.view.LightningWebClient;
@ -47,4 +52,14 @@ public interface AppComponent { @@ -47,4 +52,14 @@ public interface AppComponent {
void inject(LightningWebClient webClient);
void inject(ThemableSettingsActivity activity);
void inject(AdBlock adBlock);
void inject(LightningDownloadListener listener);
void inject(PrivacySettingsFragment fragment);
void inject(StartPage startPage);
}

16
app/src/main/java/acr/browser/lightning/app/BrowserApp.java

@ -20,9 +20,7 @@ public class BrowserApp extends Application { @@ -20,9 +20,7 @@ public class BrowserApp extends Application {
private static AppComponent appComponent;
private static final Executor mIOThread = Executors.newSingleThreadExecutor();
@Inject static HistoryDatabase historyDatabase;
@Inject static Bus bus;
@Inject static PreferenceManager preferenceManager;
@Inject Bus bus;
@Override
public void onCreate() {
@ -40,20 +38,12 @@ public class BrowserApp extends Application { @@ -40,20 +38,12 @@ public class BrowserApp extends Application {
return appComponent;
}
public static HistoryDatabase getHistoryDatabase() {
return historyDatabase;
}
public static Executor getIOThread() {
return mIOThread;
}
public static PreferenceManager getPreferenceManager() {
return preferenceManager;
}
public static Bus getBus() {
return bus;
public static Bus getBus(Context context) {
return get(context).bus;
}
}

11
app/src/main/java/acr/browser/lightning/constant/HistoryPage.java

@ -42,13 +42,15 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> { @@ -42,13 +42,15 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> {
private final WeakReference<LightningView> mTabReference;
private final File mFilesDir;
private final String mTitle;
private final HistoryDatabase mHistoryDatabase;
private String mHistoryUrl = null;
public HistoryPage(LightningView tab, Application app) {
public HistoryPage(LightningView tab, Application app, HistoryDatabase database) {
mTabReference = new WeakReference<>(tab);
mFilesDir = app.getFilesDir();
mTitle = app.getString(R.string.action_history);
mHistoryDatabase = database;
}
@Override
@ -69,7 +71,7 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> { @@ -69,7 +71,7 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> {
@NonNull
private String getHistoryPage() {
StringBuilder historyBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
List<HistoryItem> historyList = getWebHistory();
List<HistoryItem> historyList = mHistoryDatabase.getLastHundredItems();
Iterator<HistoryItem> it = historyList.iterator();
HistoryItem helper;
while (it.hasNext()) {
@ -98,11 +100,6 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> { @@ -98,11 +100,6 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> {
return Constants.FILE + historyWebPage;
}
private static List<HistoryItem> getWebHistory() {
HistoryDatabase databaseHandler = BrowserApp.getHistoryDatabase();
return databaseHandler.getLastHundredItems();
}
public void load() {
executeOnExecutor(BrowserApp.getIOThread());
}

10
app/src/main/java/acr/browser/lightning/constant/StartPage.java

@ -11,6 +11,8 @@ import java.io.FileWriter; @@ -11,6 +11,8 @@ import java.io.FileWriter;
import java.io.IOException;
import java.lang.ref.WeakReference;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.preference.PreferenceManager;
@ -54,9 +56,12 @@ public class StartPage extends AsyncTask<Void, Void, Void> { @@ -54,9 +56,12 @@ public class StartPage extends AsyncTask<Void, Void, Void> {
private final File mFilesDir;
private final WeakReference<LightningView> mTabReference;
@Inject PreferenceManager mPreferenceManager;
private String mStartpageUrl;
public StartPage(LightningView tab, Application app) {
BrowserApp.getAppComponent().inject(this);
mTitle = app.getString(R.string.home);
mFilesDir = app.getFilesDir();
mTabReference = new WeakReference<>(tab);
@ -87,12 +92,11 @@ public class StartPage extends AsyncTask<Void, Void, Void> { @@ -87,12 +92,11 @@ public class StartPage extends AsyncTask<Void, Void, Void> {
StringBuilder homepageBuilder = new StringBuilder(HEAD_1 + mTitle + HEAD_2);
String icon;
String searchUrl;
final PreferenceManager preferenceManager = BrowserApp.getPreferenceManager();
switch (preferenceManager.getSearchChoice()) {
switch (mPreferenceManager.getSearchChoice()) {
case 0:
// CUSTOM SEARCH
icon = "file:///android_asset/lightning.png";
searchUrl = preferenceManager.getSearchUrl();
searchUrl = mPreferenceManager.getSearchUrl();
break;
case 1:
// GOOGLE_SEARCH;

61
app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java

@ -26,27 +26,26 @@ import acr.browser.lightning.bus.BookmarkEvents; @@ -26,27 +26,26 @@ import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils;
/**
* TODO Rename this class it doesn't build dialogs only for bookmarks
*
* <p/>
* Created by Stefano Pacifici on 02/09/15, based on Anthony C. Restaino's code.
*/
public class LightningDialogBuilder {
@Inject
BookmarkManager bookmarkManager;
@Inject BookmarkManager mBookmarkManager;
@Inject
HistoryDatabase mHistoryDatabase;
@Inject PreferenceManager mPreferenceManager;
@Inject
Bus eventBus;
@Inject HistoryDatabase mHistoryDatabase;
@Inject Bus mEventBus;
@Inject
public LightningDialogBuilder() {
@ -56,8 +55,9 @@ public class LightningDialogBuilder { @@ -56,8 +55,9 @@ public class LightningDialogBuilder {
/**
* Show the appropriated dialog for the long pressed link. It means that we try to understand
* if the link is relative to a bookmark or is just a folder.
* @param context used to show the dialog
* @param url the long pressed url
*
* @param context used to show the dialog
* @param url the long pressed url
*/
public void showLongPressedDialogForBookmarkUrl(final Context context, final String url) {
final HistoryItem item;
@ -72,7 +72,7 @@ public class LightningDialogBuilder { @@ -72,7 +72,7 @@ public class LightningDialogBuilder {
item.setImageId(R.drawable.ic_folder);
item.setUrl(Constants.FOLDER + folderTitle);
} else {
item = bookmarkManager.findBookmarkForUrl(url);
item = mBookmarkManager.findBookmarkForUrl(url);
}
if (item != null) {
if (item.isFolder()) {
@ -90,11 +90,11 @@ public class LightningDialogBuilder { @@ -90,11 +90,11 @@ public class LightningDialogBuilder {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(item.getUrl()));
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(item.getUrl()));
break;
case DialogInterface.BUTTON_NEGATIVE:
if (bookmarkManager.deleteBookmark(item)) {
eventBus.post(new BookmarkEvents.Deleted(item));
if (mBookmarkManager.deleteBookmark(item)) {
mEventBus.post(new BookmarkEvents.Deleted(item));
}
break;
case DialogInterface.BUTTON_NEUTRAL:
@ -126,7 +126,7 @@ public class LightningDialogBuilder { @@ -126,7 +126,7 @@ public class LightningDialogBuilder {
(AutoCompleteTextView) dialogLayout.findViewById(R.id.bookmark_folder);
getFolder.setHint(R.string.folder);
getFolder.setText(item.getFolder());
final List<String> folders = bookmarkManager.getFolderTitles();
final List<String> folders = mBookmarkManager.getFolderTitles();
final ArrayAdapter<String> suggestionsAdapter = new ArrayAdapter<>(context,
android.R.layout.simple_dropdown_item_1line, folders);
getFolder.setThreshold(1);
@ -142,8 +142,8 @@ public class LightningDialogBuilder { @@ -142,8 +142,8 @@ public class LightningDialogBuilder {
editedItem.setUrl(getUrl.getText().toString());
editedItem.setUrl(getUrl.getText().toString());
editedItem.setFolder(getFolder.getText().toString());
bookmarkManager.editBookmark(item, editedItem);
eventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
mBookmarkManager.editBookmark(item, editedItem);
mEventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
}
});
editBookmarkDialog.show();
@ -161,9 +161,9 @@ public class LightningDialogBuilder { @@ -161,9 +161,9 @@ public class LightningDialogBuilder {
break;
case DialogInterface.BUTTON_NEGATIVE:
bookmarkManager.deleteFolder(item.getTitle());
mBookmarkManager.deleteFolder(item.getTitle());
// setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
eventBus.post(new BookmarkEvents.Deleted(item));
mEventBus.post(new BookmarkEvents.Deleted(item));
break;
}
}
@ -204,8 +204,8 @@ public class LightningDialogBuilder { @@ -204,8 +204,8 @@ public class LightningDialogBuilder {
editedItem.setUrl(Constants.FOLDER + newTitle);
editedItem.setFolder(item.getFolder());
editedItem.setIsFolder(true);
bookmarkManager.renameFolder(oldTitle, newTitle);
eventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
mBookmarkManager.renameFolder(oldTitle, newTitle);
mEventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
}
});
editFolderDialog.show();
@ -217,15 +217,15 @@ public class LightningDialogBuilder { @@ -217,15 +217,15 @@ public class LightningDialogBuilder {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break;
case DialogInterface.BUTTON_NEGATIVE:
mHistoryDatabase.deleteHistoryItem(url);
// openHistory();
eventBus.post(new BrowserEvents.OpenHistoryInCurrentTab());
mEventBus.post(new BrowserEvents.OpenHistoryInCurrentTab());
break;
case DialogInterface.BUTTON_NEUTRAL:
eventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break;
default:
break;
@ -245,20 +245,19 @@ public class LightningDialogBuilder { @@ -245,20 +245,19 @@ public class LightningDialogBuilder {
// TODO There should be a way in which we do not need an activity reference to dowload a file
public void showLongPressImageDialog(@NonNull final Activity activity, @NonNull final String url,
@NonNull final String userAgent) {
@NonNull final String userAgent) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break;
case DialogInterface.BUTTON_NEGATIVE:
eventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break;
case DialogInterface.BUTTON_NEUTRAL:
Utils.downloadFile(activity, url,
userAgent, "attachment");
Utils.downloadFile(activity, mPreferenceManager, url, userAgent, "attachment");
break;
}
}
@ -280,11 +279,11 @@ public class LightningDialogBuilder { @@ -280,11 +279,11 @@ public class LightningDialogBuilder {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break;
case DialogInterface.BUTTON_NEGATIVE:
eventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break;
case DialogInterface.BUTTON_NEUTRAL:

14
app/src/main/java/acr/browser/lightning/download/DownloadHandler.java

@ -29,6 +29,7 @@ import acr.browser.lightning.activity.MainActivity; @@ -29,6 +29,7 @@ import acr.browser.lightning.activity.MainActivity;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
/**
* Handle download requests
@ -53,7 +54,7 @@ public class DownloadHandler { @@ -53,7 +54,7 @@ public class DownloadHandler {
* @param contentDisposition Content-disposition http header, if present.
* @param mimetype The mimetype of the content reported by the server
*/
public static void onDownloadStart(Context context, String url, String userAgent,
public static void onDownloadStart(Context context, PreferenceManager manager, String url, String userAgent,
String contentDisposition, String mimetype) {
// if we're dealing wih A/V content that's not explicitly marked
// for download, check if it's streamable.
@ -88,7 +89,7 @@ public class DownloadHandler { @@ -88,7 +89,7 @@ public class DownloadHandler {
}
}
}
onDownloadStartNoStream(context, url, userAgent, contentDisposition, mimetype);
onDownloadStartNoStream(context, manager, url, userAgent, contentDisposition, mimetype);
}
// This is to work around the fact that java.net.URI throws Exceptions
@ -132,9 +133,10 @@ public class DownloadHandler { @@ -132,9 +133,10 @@ public class DownloadHandler {
* @param mimetype The mimetype of the content reported by the server
*/
/* package */
private static void onDownloadStartNoStream(final Context context, String url, String userAgent,
private static void onDownloadStartNoStream(final Context context, PreferenceManager preferences,
String url, String userAgent,
String contentDisposition, String mimetype) {
final Bus eventBus = BrowserApp.getBus();
final Bus eventBus = BrowserApp.getBus(context);
final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
// Check to see if we have an SDCard
@ -186,7 +188,7 @@ public class DownloadHandler { @@ -186,7 +188,7 @@ public class DownloadHandler {
// or, should it be set to one of several Environment.DIRECTORY* dirs
// depending on mimetype?
String location = BrowserApp.getPreferenceManager().getDownloadDirectory();
String location = preferences.getDownloadDirectory();
Uri downloadFolder;
if (location != null) {
location = addNecessarySlashes(location);
@ -194,7 +196,7 @@ public class DownloadHandler { @@ -194,7 +196,7 @@ public class DownloadHandler {
} else {
location = addNecessarySlashes(DEFAULT_DOWNLOAD_PATH);
downloadFolder = Uri.parse(location);
BrowserApp.getPreferenceManager().setDownloadDirectory(location);
preferences.setDownloadDirectory(location);
}
File dir = new File(downloadFolder.getPath());

2
app/src/main/java/acr/browser/lightning/download/FetchUrlMimeType.java

@ -54,7 +54,7 @@ class FetchUrlMimeType extends Thread { @@ -54,7 +54,7 @@ class FetchUrlMimeType extends Thread {
public void run() {
// User agent is likely to be null, though the AndroidHttpClient
// seems ok with that.
final Bus eventBus = BrowserApp.getBus();
final Bus eventBus = BrowserApp.getBus(mContext);
String mimeType = null;
String contentDisposition = null;
HttpURLConnection connection = null;

10
app/src/main/java/acr/browser/lightning/download/LightningDownloadListener.java

@ -12,15 +12,23 @@ import android.webkit.DownloadListener; @@ -12,15 +12,23 @@ import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;
import javax.inject.Inject;
public class LightningDownloadListener implements DownloadListener {
private final Activity mActivity;
@Inject PreferenceManager mPreferenceManager;
public LightningDownloadListener(Activity context) {
BrowserApp.getAppComponent().inject(this);
mActivity = context;
}
@ -38,7 +46,7 @@ public class LightningDownloadListener implements DownloadListener { @@ -38,7 +46,7 @@ public class LightningDownloadListener implements DownloadListener {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
DownloadHandler.onDownloadStart(mActivity, url, userAgent,
DownloadHandler.onDownloadStart(mActivity, mPreferenceManager, url, userAgent,
contentDisposition, mimetype);
break;

16
app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java

@ -14,8 +14,11 @@ import android.preference.Preference; @@ -14,8 +14,11 @@ import android.preference.Preference;
import android.support.v7.app.AlertDialog;
import android.webkit.WebView;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.utils.WebUtils;
import acr.browser.lightning.view.LightningView;
@ -37,11 +40,14 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme @@ -37,11 +40,14 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
private static final String SETTINGS_IDENTIFYINGHEADERS = "remove_identifying_headers";
private Activity mActivity;
private Handler messageHandler;
private Handler mMessageHandler;
@Inject HistoryDatabase mHistoryDatabase;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BrowserApp.getAppComponent().inject(this);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preference_privacy);
@ -96,7 +102,7 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme @@ -96,7 +102,7 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
cb3cookies.setEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
messageHandler = new MessageHandler(mActivity);
mMessageHandler = new MessageHandler(mActivity);
}
private static class MessageHandler extends Handler {
@ -188,13 +194,13 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme @@ -188,13 +194,13 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
}
private void clearHistory() {
WebUtils.clearHistory(getActivity());
messageHandler.sendEmptyMessage(1);
WebUtils.clearHistory(getActivity(), mHistoryDatabase);
mMessageHandler.sendEmptyMessage(1);
}
private void clearCookies() {
WebUtils.clearCookies(getActivity());
messageHandler.sendEmptyMessage(2);
mMessageHandler.sendEmptyMessage(2);
}
private void clearWebStorage() {

13
app/src/main/java/acr/browser/lightning/utils/AdBlock.java

@ -13,8 +13,11 @@ import java.util.HashSet; @@ -13,8 +13,11 @@ import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import javax.inject.Inject;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
public class AdBlock {
@ -33,6 +36,8 @@ public class AdBlock { @@ -33,6 +36,8 @@ public class AdBlock {
private static final Locale mLocale = Locale.getDefault();
private static AdBlock mInstance;
@Inject PreferenceManager mPreferenceManager;
public static AdBlock getInstance(Context context) {
if (mInstance == null) {
mInstance = new AdBlock(context);
@ -41,14 +46,15 @@ public class AdBlock { @@ -41,14 +46,15 @@ public class AdBlock {
}
private AdBlock(Context context) {
BrowserApp.getAppComponent().inject(this);
if (mBlockedDomainsList.isEmpty() && Constants.FULL_VERSION) {
loadHostsFile(context);
}
mBlockAds = BrowserApp.getPreferenceManager().getAdBlockEnabled();
mBlockAds = mPreferenceManager.getAdBlockEnabled();
}
public void updatePreference() {
mBlockAds = BrowserApp.getPreferenceManager().getAdBlockEnabled();
mBlockAds = mPreferenceManager.getAdBlockEnabled();
}
private void loadBlockedDomainsList(final Context context) {
@ -80,6 +86,7 @@ public class AdBlock { @@ -80,6 +86,7 @@ public class AdBlock {
/**
* a method that determines if the given URL is an ad or not. It performs
* a search of the URL's domain on the blocked domain hash set.
*
* @param url the URL to check for being an ad
* @return true if it is an ad, false if it is not an ad
*/
@ -105,6 +112,7 @@ public class AdBlock { @@ -105,6 +112,7 @@ public class AdBlock {
/**
* Returns the probable domain name for a given URL
*
* @param url the url to parse
* @return returns the domain
* @throws URISyntaxException throws an exception if the string cannot form a URI
@ -130,6 +138,7 @@ public class AdBlock { @@ -130,6 +138,7 @@ public class AdBlock {
* simply have a list of hostnames to block, or it can handle a full blown hosts file.
* It will strip out comments, references to the base IP address and just extract the
* domains to be used
*
* @param context the context needed to read the file
*/
private void loadHostsFile(final Context context) {

6
app/src/main/java/acr/browser/lightning/utils/Utils.java

@ -45,6 +45,7 @@ import java.util.Date; @@ -45,6 +45,7 @@ import java.util.Date;
import acr.browser.lightning.R;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.download.DownloadHandler;
import acr.browser.lightning.preference.PreferenceManager;
public final class Utils {
@ -58,15 +59,14 @@ public final class Utils { @@ -58,15 +59,14 @@ public final class Utils {
* @param userAgent the user agent of the browser.
* @param contentDisposition the content description of the file.
*/
public static void downloadFile(final Activity activity, final String url,
public static void downloadFile(final Activity activity, final PreferenceManager manager, final String url,
final String userAgent, final String contentDisposition) {
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsResultAction() {
@Override
public void onGranted() {
String fileName = URLUtil.guessFileName(url, null, null);
DownloadHandler.onDownloadStart(activity, url, userAgent, contentDisposition, null
);
DownloadHandler.onDownloadStart(activity, manager, url, userAgent, contentDisposition, null);
Log.i(Constants.TAG, "Downloading" + fileName);
}

5
app/src/main/java/acr/browser/lightning/utils/WebUtils.java

@ -11,6 +11,7 @@ import android.webkit.WebView; @@ -11,6 +11,7 @@ import android.webkit.WebView;
import android.webkit.WebViewDatabase;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryDatabase;
/**
* Copyright 8/4/2015 Anthony Restaino
@ -33,8 +34,8 @@ public class WebUtils { @@ -33,8 +34,8 @@ public class WebUtils {
WebStorage.getInstance().deleteAllData();
}
public static void clearHistory(@NonNull Context context) {
BrowserApp.getHistoryDatabase().deleteHistory();
public static void clearHistory(@NonNull Context context, HistoryDatabase historyDatabase) {
historyDatabase.deleteHistory();
WebViewDatabase m = WebViewDatabase.getInstance(context);
m.clearFormData();
m.clearHttpAuthUsernamePassword();

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

@ -52,7 +52,7 @@ class LightningChromeClient extends WebChromeClient { @@ -52,7 +52,7 @@ class LightningChromeClient extends WebChromeClient {
mActivity = activity;
mUIController = (UIController) activity;
mLightningView = lightningView;
eventBus = BrowserApp.getBus();
eventBus = BrowserApp.getBus(activity);
}
@Override

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

@ -63,7 +63,7 @@ public class LightningWebClient extends WebViewClient { @@ -63,7 +63,7 @@ public class LightningWebClient extends WebViewClient {
mLightningView = lightningView;
mAdBlock = AdBlock.getInstance(activity);
mAdBlock.updatePreference();
mEventBus = BrowserApp.getBus();
mEventBus = BrowserApp.getBus(activity);
mIntentUtils = new IntentUtils(activity);
}

Loading…
Cancel
Save