diff --git a/app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java b/app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java index 3942f7d..3847e58 100644 --- a/app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java +++ b/app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java @@ -11,6 +11,7 @@ import net.i2p.android.ui.I2PAndroidHelper; import acr.browser.lightning.R; import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.constant.Constants; import acr.browser.lightning.preference.PreferenceManager; import info.guardianproject.netcipher.proxy.OrbotHelper; @@ -144,13 +145,15 @@ public class ProxyUtils { } - public boolean isProxyReady(Activity activity) { + public boolean isProxyReady() { if (mPreferences.getProxyChoice() == Constants.PROXY_I2P) { if (!mI2PHelper.isI2PAndroidRunning()) { - Utils.showSnackbar(activity, R.string.i2p_not_running); + BrowserApp.getAppComponent().getBus() + .post(new BrowserEvents.ShowSnackBarMessage(R.string.i2p_not_running)); return false; } else if (!mI2PHelper.areTunnelsActive()) { - Utils.showSnackbar(activity, R.string.i2p_tunnels_not_ready); + BrowserApp.getAppComponent().getBus() + .post(new BrowserEvents.ShowSnackBarMessage(R.string.i2p_tunnels_not_ready)); return false; } } @@ -201,7 +204,7 @@ public class ProxyUtils { break; case Constants.PROXY_I2P: - I2PAndroidHelper ih = new I2PAndroidHelper(activity.getApplicationContext()); + I2PAndroidHelper ih = new I2PAndroidHelper(BrowserApp.getAppContext()); if (!ih.isI2PAndroidInstalled()) { choice = Constants.NO_PROXY; ih.promptToInstall(activity); diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index eb3821d..09f27dd 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -38,6 +38,7 @@ import android.provider.MediaStore; import android.support.annotation.IdRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewCompat; import android.support.v4.widget.DrawerLayout; @@ -1012,7 +1013,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements if (show) { showTab(tabsManager.size() - 1); } - updateTabs(); + // TODO Check is this is callable directly from LightningView + eventBus.post(new BrowserEvents.TabsChanged()); // TODO Restore this // new Handler().postDelayed(new Runnable() { @@ -1048,14 +1050,14 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements if (current > position) { tabsManager.deleteTab(position); showTab(current - 1); - updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); tabToDelete.onDestroy(); } else if (tabsManager.size() > position + 1) { if (current == position) { showTab(position + 1); tabsManager.deleteTab(position); showTab(position); - updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); } else { tabsManager.deleteTab(position); } @@ -1066,7 +1068,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements showTab(position - 1); tabsManager.deleteTab(position); showTab(position - 1); - updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); } else { tabsManager.deleteTab(position); } @@ -1424,11 +1426,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements getUrl.setAdapter(mSearchAdapter); } - @Override - public boolean proxyIsNotReady() { - return !mProxyUtils.isProxyReady(this); - } - /** * function that opens the HTML history page in the browser */ @@ -1471,11 +1468,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements return super.onCreateOptionsMenu(menu); } - @Override - public void updateTabs() { - eventBus.post(new BrowserEvents.TabsChanged()); - } - /** * opens a file chooser * param ValueCallback is the message from the WebView indicating a file chooser @@ -2326,9 +2318,18 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements String url = mPreferences.getSavedUrl(); if (url != null) { BrowserActivity.this.newTab(url, true); + Utils.showSnackbar(mActivity, R.string.deleted_tab); } mPreferences.setSavedUrl(null); } + + @Subscribe + public void displayInSnackbar(final BrowserEvents.ShowSnackBarMessage event) { + if (event.message != null) { + Utils.showSnackbar(BrowserActivity.this, event.message); + } else { + Utils.showSnackbar(mActivity, event.stringRes); } + } }; } diff --git a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java index c3dd109..1510cb7 100644 --- a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java +++ b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java @@ -119,7 +119,7 @@ public class TabsManager { final String url, final boolean darkTheme, final boolean isIncognito, final BrowserController controller) { - final LightningView tab = new LightningView(activity, url, darkTheme, isIncognito, controller); + final LightningView tab = new LightningView(activity, url, darkTheme, isIncognito); mWebViewList.add(tab); return tab; } diff --git a/app/src/main/java/acr/browser/lightning/app/AppComponent.java b/app/src/main/java/acr/browser/lightning/app/AppComponent.java index 14e0b56..76e1e46 100644 --- a/app/src/main/java/acr/browser/lightning/app/AppComponent.java +++ b/app/src/main/java/acr/browser/lightning/app/AppComponent.java @@ -1,5 +1,9 @@ package acr.browser.lightning.app; +import android.content.Context; + +import com.squareup.otto.Bus; + import javax.inject.Singleton; import acr.browser.lightning.activity.BrowserActivity; @@ -39,4 +43,8 @@ public interface AppComponent { void inject(LightningPreferenceFragment fragment); BookmarkPage getBookmarkPage(); + + Bus getBus(); + + Context getApplicationContext(); } diff --git a/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java b/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java index d461205..250d8d2 100644 --- a/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java +++ b/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java @@ -1,5 +1,10 @@ package acr.browser.lightning.bus; +import android.support.annotation.IdRes; +import android.support.annotation.StringRes; + +import acr.browser.lightning.R; + /** * Created by Stefano Pacifici on 26/08/15. */ @@ -46,4 +51,23 @@ public final class BrowserEvents { */ public static class TabsChanged { } + + /** + * Notify the Browser to display a SnackBar in the main activity + */ + public static class ShowSnackBarMessage { + public final String message; + @StringRes + public final int stringRes; + + public ShowSnackBarMessage(final String message) { + this.message = message; + this.stringRes = -1; + } + + public ShowSnackBarMessage(@StringRes final int stringRes) { + this.message = null; + this.stringRes = stringRes; + } + } } diff --git a/app/src/main/java/acr/browser/lightning/controller/BrowserController.java b/app/src/main/java/acr/browser/lightning/controller/BrowserController.java index 7f4944a..51e4528 100644 --- a/app/src/main/java/acr/browser/lightning/controller/BrowserController.java +++ b/app/src/main/java/acr/browser/lightning/controller/BrowserController.java @@ -24,8 +24,6 @@ public interface BrowserController { void openFileChooser(ValueCallback uploadMsg); - void updateTabs(); - void onLongPress(); void onShowCustomView(View view, CustomViewCallback callback); @@ -52,8 +50,6 @@ public interface BrowserController { void closeEmptyTab(); - boolean proxyIsNotReady(); - // void updateBookmarkIndicator(String url); } diff --git a/app/src/main/java/acr/browser/lightning/view/LightningView.java b/app/src/main/java/acr/browser/lightning/view/LightningView.java index c3bcb82..cdce9a3 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -49,6 +49,8 @@ import android.webkit.WebViewClient; import android.widget.EditText; import android.widget.LinearLayout; +import com.squareup.otto.Bus; + import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; @@ -60,6 +62,7 @@ import javax.inject.Inject; import acr.browser.lightning.R; import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.controller.BrowserController; @@ -68,6 +71,7 @@ import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.utils.AdBlock; import acr.browser.lightning.utils.IntentUtils; import acr.browser.lightning.utils.PermissionsManager; +import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.Utils; @@ -103,8 +107,11 @@ public class LightningView { private final PermissionsManager mPermissionsManager; private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION}; + private final Bus eventBus; + @SuppressLint("NewApi") public LightningView(Activity activity, String url, boolean darkTheme, boolean isIncognito) { + eventBus = BrowserApp.getAppComponent().getBus(); mPreferences = BrowserApp.getAppComponent().getPreferenceManager(); mActivity = activity; mWebView = new WebView(activity); @@ -419,7 +426,7 @@ public class LightningView { public void setForegroundTab(boolean isForeground) { isForegroundTab = isForeground; - mBrowserController.updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); } public boolean isForegroundTab() { @@ -522,7 +529,7 @@ public class LightningView { public synchronized void reload() { // Check if configured proxy is available - if (mBrowserController.proxyIsNotReady()) { + if (!ProxyUtils.getInstance().isProxyReady()) { // User has been notified return; } @@ -625,8 +632,7 @@ public class LightningView { public synchronized void loadUrl(String url) { // Check if configured proxy is available - if (mBrowserController.proxyIsNotReady()) { - // User has been notified + if (!ProxyUtils.getInstance().isProxyReady()) { return; } @@ -697,7 +703,7 @@ public class LightningView { if (API >= android.os.Build.VERSION_CODES.KITKAT && mInvertPage) { view.evaluateJavascript(Constants.JAVASCRIPT_INVERT_PAGE, null); } - mBrowserController.updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); } @Override @@ -707,7 +713,7 @@ public class LightningView { mBrowserController.showActionBar(); } mTitle.setFavicon(mWebpageBitmap); - mBrowserController.updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); } @Override @@ -839,7 +845,7 @@ public class LightningView { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // Check if configured proxy is available - if (mBrowserController.proxyIsNotReady()) { + if (!ProxyUtils.getInstance().isProxyReady()) { // User has been notified return true; } @@ -897,7 +903,7 @@ public class LightningView { if (icon == null) return; mTitle.setFavicon(icon); - mBrowserController.updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); ; cacheFavicon(icon); } @@ -908,7 +914,7 @@ public class LightningView { } else { mTitle.setTitle(mActivity.getString(R.string.untitled)); } - mBrowserController.updateTabs(); + eventBus.post(new BrowserEvents.TabsChanged()); ; if (view != null) mBrowserController.updateHistory(title, view.getUrl()); }