Browse Source

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

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

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

@ -1066,7 +1066,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
Log.d(Constants.TAG, "Cache Cleared"); Log.d(Constants.TAG, "Cache Cleared");
} }
if (mPreferences.getClearHistoryExitEnabled() && !isIncognito()) { if (mPreferences.getClearHistoryExitEnabled() && !isIncognito()) {
WebUtils.clearHistory(this); WebUtils.clearHistory(this, mHistoryDatabase);
Log.d(Constants.TAG, "History Cleared"); Log.d(Constants.TAG, "History Cleared");
} }
if (mPreferences.getClearCookiesExitEnabled() && !isIncognito()) { if (mPreferences.getClearCookiesExitEnabled() && !isIncognito()) {
@ -1400,7 +1400,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* function that opens the HTML history page in the browser * function that opens the HTML history page in the browser
*/ */
private void openHistory() { 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
@Subscribe @Subscribe
public void loadHistory(final BrowserEvents.OpenHistoryInCurrentTab event) { 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;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.os.Bundle; import android.os.Bundle;
import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
public abstract class ThemableSettingsActivity extends AppCompatPreferenceActivity { public abstract class ThemableSettingsActivity extends AppCompatPreferenceActivity {
private int mTheme; private int mTheme;
@Inject PreferenceManager mPreferenceManager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
mTheme = BrowserApp.getPreferenceManager().getUseTheme(); BrowserApp.getAppComponent().inject(this);
mTheme = mPreferenceManager.getUseTheme();
// set the theme // set the theme
if (mTheme == 0) { if (mTheme == 0) {
@ -32,7 +38,7 @@ public abstract class ThemableSettingsActivity extends AppCompatPreferenceActivi
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if (BrowserApp.getPreferenceManager().getUseTheme() != mTheme) { if (mPreferenceManager.getUseTheme() != mTheme) {
restart(); restart();
} }
} }

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

@ -5,13 +5,18 @@ import javax.inject.Singleton;
import acr.browser.lightning.activity.BrowserActivity; import acr.browser.lightning.activity.BrowserActivity;
import acr.browser.lightning.activity.ReadingActivity; import acr.browser.lightning.activity.ReadingActivity;
import acr.browser.lightning.activity.ThemableBrowserActivity; import acr.browser.lightning.activity.ThemableBrowserActivity;
import acr.browser.lightning.activity.ThemableSettingsActivity;
import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.fragment.BookmarkSettingsFragment; import acr.browser.lightning.fragment.BookmarkSettingsFragment;
import acr.browser.lightning.fragment.BookmarksFragment; import acr.browser.lightning.fragment.BookmarksFragment;
import acr.browser.lightning.fragment.LightningPreferenceFragment; import acr.browser.lightning.fragment.LightningPreferenceFragment;
import acr.browser.lightning.fragment.PrivacySettingsFragment;
import acr.browser.lightning.fragment.TabsFragment; import acr.browser.lightning.fragment.TabsFragment;
import acr.browser.lightning.object.SearchAdapter; import acr.browser.lightning.object.SearchAdapter;
import acr.browser.lightning.utils.AdBlock;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.LightningView;
import acr.browser.lightning.view.LightningWebClient; import acr.browser.lightning.view.LightningWebClient;
@ -47,4 +52,14 @@ public interface AppComponent {
void inject(LightningWebClient webClient); 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 {
private static AppComponent appComponent; private static AppComponent appComponent;
private static final Executor mIOThread = Executors.newSingleThreadExecutor(); private static final Executor mIOThread = Executors.newSingleThreadExecutor();
@Inject static HistoryDatabase historyDatabase; @Inject Bus bus;
@Inject static Bus bus;
@Inject static PreferenceManager preferenceManager;
@Override @Override
public void onCreate() { public void onCreate() {
@ -40,20 +38,12 @@ public class BrowserApp extends Application {
return appComponent; return appComponent;
} }
public static HistoryDatabase getHistoryDatabase() {
return historyDatabase;
}
public static Executor getIOThread() { public static Executor getIOThread() {
return mIOThread; return mIOThread;
} }
public static PreferenceManager getPreferenceManager() { public static Bus getBus(Context context) {
return preferenceManager; return get(context).bus;
}
public static Bus getBus() {
return bus;
} }
} }

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

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

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

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

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

@ -26,27 +26,26 @@ import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.database.BookmarkManager; import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase; import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
/** /**
* TODO Rename this class it doesn't build dialogs only for bookmarks * 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. * Created by Stefano Pacifici on 02/09/15, based on Anthony C. Restaino's code.
*/ */
public class LightningDialogBuilder { public class LightningDialogBuilder {
@Inject @Inject BookmarkManager mBookmarkManager;
BookmarkManager bookmarkManager;
@Inject @Inject PreferenceManager mPreferenceManager;
HistoryDatabase mHistoryDatabase;
@Inject @Inject HistoryDatabase mHistoryDatabase;
Bus eventBus;
@Inject Bus mEventBus;
@Inject @Inject
public LightningDialogBuilder() { public LightningDialogBuilder() {
@ -56,6 +55,7 @@ public class LightningDialogBuilder {
/** /**
* Show the appropriated dialog for the long pressed link. It means that we try to understand * 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. * if the link is relative to a bookmark or is just a folder.
*
* @param context used to show the dialog * @param context used to show the dialog
* @param url the long pressed url * @param url the long pressed url
*/ */
@ -72,7 +72,7 @@ public class LightningDialogBuilder {
item.setImageId(R.drawable.ic_folder); item.setImageId(R.drawable.ic_folder);
item.setUrl(Constants.FOLDER + folderTitle); item.setUrl(Constants.FOLDER + folderTitle);
} else { } else {
item = bookmarkManager.findBookmarkForUrl(url); item = mBookmarkManager.findBookmarkForUrl(url);
} }
if (item != null) { if (item != null) {
if (item.isFolder()) { if (item.isFolder()) {
@ -90,11 +90,11 @@ public class LightningDialogBuilder {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(item.getUrl())); mEventBus.post(new BrowserEvents.OpenUrlInNewTab(item.getUrl()));
break; break;
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
if (bookmarkManager.deleteBookmark(item)) { if (mBookmarkManager.deleteBookmark(item)) {
eventBus.post(new BookmarkEvents.Deleted(item)); mEventBus.post(new BookmarkEvents.Deleted(item));
} }
break; break;
case DialogInterface.BUTTON_NEUTRAL: case DialogInterface.BUTTON_NEUTRAL:
@ -126,7 +126,7 @@ public class LightningDialogBuilder {
(AutoCompleteTextView) dialogLayout.findViewById(R.id.bookmark_folder); (AutoCompleteTextView) dialogLayout.findViewById(R.id.bookmark_folder);
getFolder.setHint(R.string.folder); getFolder.setHint(R.string.folder);
getFolder.setText(item.getFolder()); getFolder.setText(item.getFolder());
final List<String> folders = bookmarkManager.getFolderTitles(); final List<String> folders = mBookmarkManager.getFolderTitles();
final ArrayAdapter<String> suggestionsAdapter = new ArrayAdapter<>(context, final ArrayAdapter<String> suggestionsAdapter = new ArrayAdapter<>(context,
android.R.layout.simple_dropdown_item_1line, folders); android.R.layout.simple_dropdown_item_1line, folders);
getFolder.setThreshold(1); getFolder.setThreshold(1);
@ -142,8 +142,8 @@ public class LightningDialogBuilder {
editedItem.setUrl(getUrl.getText().toString()); editedItem.setUrl(getUrl.getText().toString());
editedItem.setUrl(getUrl.getText().toString()); editedItem.setUrl(getUrl.getText().toString());
editedItem.setFolder(getFolder.getText().toString()); editedItem.setFolder(getFolder.getText().toString());
bookmarkManager.editBookmark(item, editedItem); mBookmarkManager.editBookmark(item, editedItem);
eventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem)); mEventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
} }
}); });
editBookmarkDialog.show(); editBookmarkDialog.show();
@ -161,9 +161,9 @@ public class LightningDialogBuilder {
break; break;
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
bookmarkManager.deleteFolder(item.getTitle()); mBookmarkManager.deleteFolder(item.getTitle());
// setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); // setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
eventBus.post(new BookmarkEvents.Deleted(item)); mEventBus.post(new BookmarkEvents.Deleted(item));
break; break;
} }
} }
@ -204,8 +204,8 @@ public class LightningDialogBuilder {
editedItem.setUrl(Constants.FOLDER + newTitle); editedItem.setUrl(Constants.FOLDER + newTitle);
editedItem.setFolder(item.getFolder()); editedItem.setFolder(item.getFolder());
editedItem.setIsFolder(true); editedItem.setIsFolder(true);
bookmarkManager.renameFolder(oldTitle, newTitle); mBookmarkManager.renameFolder(oldTitle, newTitle);
eventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem)); mEventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
} }
}); });
editFolderDialog.show(); editFolderDialog.show();
@ -217,15 +217,15 @@ public class LightningDialogBuilder {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(url)); mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break; break;
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
mHistoryDatabase.deleteHistoryItem(url); mHistoryDatabase.deleteHistoryItem(url);
// openHistory(); // openHistory();
eventBus.post(new BrowserEvents.OpenHistoryInCurrentTab()); mEventBus.post(new BrowserEvents.OpenHistoryInCurrentTab());
break; break;
case DialogInterface.BUTTON_NEUTRAL: case DialogInterface.BUTTON_NEUTRAL:
eventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url)); mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break; break;
default: default:
break; break;
@ -251,14 +251,13 @@ public class LightningDialogBuilder {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(url)); mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break; break;
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
eventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url)); mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break; break;
case DialogInterface.BUTTON_NEUTRAL: case DialogInterface.BUTTON_NEUTRAL:
Utils.downloadFile(activity, url, Utils.downloadFile(activity, mPreferenceManager, url, userAgent, "attachment");
userAgent, "attachment");
break; break;
} }
} }
@ -280,11 +279,11 @@ public class LightningDialogBuilder {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
eventBus.post(new BrowserEvents.OpenUrlInNewTab(url)); mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break; break;
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
eventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url)); mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break; break;
case DialogInterface.BUTTON_NEUTRAL: 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;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
/** /**
* Handle download requests * Handle download requests
@ -53,7 +54,7 @@ public class DownloadHandler {
* @param contentDisposition Content-disposition http header, if present. * @param contentDisposition Content-disposition http header, if present.
* @param mimetype The mimetype of the content reported by the server * @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) { String contentDisposition, String mimetype) {
// if we're dealing wih A/V content that's not explicitly marked // if we're dealing wih A/V content that's not explicitly marked
// for download, check if it's streamable. // for download, check if it's streamable.
@ -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 // This is to work around the fact that java.net.URI throws Exceptions
@ -132,9 +133,10 @@ public class DownloadHandler {
* @param mimetype The mimetype of the content reported by the server * @param mimetype The mimetype of the content reported by the server
*/ */
/* package */ /* 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) { String contentDisposition, String mimetype) {
final Bus eventBus = BrowserApp.getBus(); final Bus eventBus = BrowserApp.getBus(context);
final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
// Check to see if we have an SDCard // Check to see if we have an SDCard
@ -186,7 +188,7 @@ public class DownloadHandler {
// or, should it be set to one of several Environment.DIRECTORY* dirs // or, should it be set to one of several Environment.DIRECTORY* dirs
// depending on mimetype? // depending on mimetype?
String location = BrowserApp.getPreferenceManager().getDownloadDirectory(); String location = preferences.getDownloadDirectory();
Uri downloadFolder; Uri downloadFolder;
if (location != null) { if (location != null) {
location = addNecessarySlashes(location); location = addNecessarySlashes(location);
@ -194,7 +196,7 @@ public class DownloadHandler {
} else { } else {
location = addNecessarySlashes(DEFAULT_DOWNLOAD_PATH); location = addNecessarySlashes(DEFAULT_DOWNLOAD_PATH);
downloadFolder = Uri.parse(location); downloadFolder = Uri.parse(location);
BrowserApp.getPreferenceManager().setDownloadDirectory(location); preferences.setDownloadDirectory(location);
} }
File dir = new File(downloadFolder.getPath()); 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 {
public void run() { public void run() {
// User agent is likely to be null, though the AndroidHttpClient // User agent is likely to be null, though the AndroidHttpClient
// seems ok with that. // seems ok with that.
final Bus eventBus = BrowserApp.getBus(); final Bus eventBus = BrowserApp.getBus(mContext);
String mimeType = null; String mimeType = null;
String contentDisposition = null; String contentDisposition = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;

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

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

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

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

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

@ -13,8 +13,11 @@ import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import javax.inject.Inject;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
public class AdBlock { public class AdBlock {
@ -33,6 +36,8 @@ public class AdBlock {
private static final Locale mLocale = Locale.getDefault(); private static final Locale mLocale = Locale.getDefault();
private static AdBlock mInstance; private static AdBlock mInstance;
@Inject PreferenceManager mPreferenceManager;
public static AdBlock getInstance(Context context) { public static AdBlock getInstance(Context context) {
if (mInstance == null) { if (mInstance == null) {
mInstance = new AdBlock(context); mInstance = new AdBlock(context);
@ -41,14 +46,15 @@ public class AdBlock {
} }
private AdBlock(Context context) { private AdBlock(Context context) {
BrowserApp.getAppComponent().inject(this);
if (mBlockedDomainsList.isEmpty() && Constants.FULL_VERSION) { if (mBlockedDomainsList.isEmpty() && Constants.FULL_VERSION) {
loadHostsFile(context); loadHostsFile(context);
} }
mBlockAds = BrowserApp.getPreferenceManager().getAdBlockEnabled(); mBlockAds = mPreferenceManager.getAdBlockEnabled();
} }
public void updatePreference() { public void updatePreference() {
mBlockAds = BrowserApp.getPreferenceManager().getAdBlockEnabled(); mBlockAds = mPreferenceManager.getAdBlockEnabled();
} }
private void loadBlockedDomainsList(final Context context) { private void loadBlockedDomainsList(final Context context) {
@ -80,6 +86,7 @@ public class AdBlock {
/** /**
* a method that determines if the given URL is an ad or not. It performs * 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. * a search of the URL's domain on the blocked domain hash set.
*
* @param url the URL to check for being an ad * @param url the URL to check for being an ad
* @return true if it is an ad, false if it is not an ad * @return true if it is an ad, false if it is not an ad
*/ */
@ -105,6 +112,7 @@ public class AdBlock {
/** /**
* Returns the probable domain name for a given URL * Returns the probable domain name for a given URL
*
* @param url the url to parse * @param url the url to parse
* @return returns the domain * @return returns the domain
* @throws URISyntaxException throws an exception if the string cannot form a URI * @throws URISyntaxException throws an exception if the string cannot form a URI
@ -130,6 +138,7 @@ public class AdBlock {
* simply have a list of hostnames to block, or it can handle a full blown hosts file. * 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 * It will strip out comments, references to the base IP address and just extract the
* domains to be used * domains to be used
*
* @param context the context needed to read the file * @param context the context needed to read the file
*/ */
private void loadHostsFile(final Context context) { 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;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.download.DownloadHandler; import acr.browser.lightning.download.DownloadHandler;
import acr.browser.lightning.preference.PreferenceManager;
public final class Utils { public final class Utils {
@ -58,15 +59,14 @@ public final class Utils {
* @param userAgent the user agent of the browser. * @param userAgent the user agent of the browser.
* @param contentDisposition the content description of the file. * @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) { final String userAgent, final String contentDisposition) {
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsResultAction() { Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsResultAction() {
@Override @Override
public void onGranted() { public void onGranted() {
String fileName = URLUtil.guessFileName(url, null, null); 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); 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;
import android.webkit.WebViewDatabase; import android.webkit.WebViewDatabase;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryDatabase;
/** /**
* Copyright 8/4/2015 Anthony Restaino * Copyright 8/4/2015 Anthony Restaino
@ -33,8 +34,8 @@ public class WebUtils {
WebStorage.getInstance().deleteAllData(); WebStorage.getInstance().deleteAllData();
} }
public static void clearHistory(@NonNull Context context) { public static void clearHistory(@NonNull Context context, HistoryDatabase historyDatabase) {
BrowserApp.getHistoryDatabase().deleteHistory(); historyDatabase.deleteHistory();
WebViewDatabase m = WebViewDatabase.getInstance(context); WebViewDatabase m = WebViewDatabase.getInstance(context);
m.clearFormData(); m.clearFormData();
m.clearHttpAuthUsernamePassword(); m.clearHttpAuthUsernamePassword();

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

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

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

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

Loading…
Cancel
Save