Browse Source

Variable renaming, moving fields around, more injection, move ProxyUtils out of flavor specific code

master
Anthony Restaino 9 years ago
parent
commit
ac107d6704
  1. 210
      app/src/LightningLite/java/acr/browser/lightning/utils/ProxyUtils.java
  2. 2
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  3. 8
      app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java
  4. 7
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  5. 20
      app/src/main/java/acr/browser/lightning/app/AppModule.java
  6. 16
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  7. 6
      app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java
  8. 3
      app/src/main/java/acr/browser/lightning/database/BookmarkManager.java
  9. 3
      app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java
  10. 11
      app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java
  11. 3
      app/src/main/java/acr/browser/lightning/receiver/NetworkReceiver.java
  12. 13
      app/src/main/java/acr/browser/lightning/utils/AdBlock.java
  13. 0
      app/src/main/java/acr/browser/lightning/utils/ProxyUtils.java
  14. 9
      app/src/main/java/acr/browser/lightning/view/IconCacheTask.java
  15. 2
      app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java
  16. 6
      app/src/main/java/acr/browser/lightning/view/LightningViewTitle.java
  17. 3
      app/src/main/java/acr/browser/lightning/view/LightningWebClient.java

210
app/src/LightningLite/java/acr/browser/lightning/utils/ProxyUtils.java

@ -1,210 +0,0 @@ @@ -1,210 +0,0 @@
package acr.browser.lightning.utils;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import net.i2p.android.ui.I2PAndroidHelper;
import javax.inject.Inject;
import javax.inject.Singleton;
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;
import info.guardianproject.netcipher.web.WebkitProxy;
@Singleton
public class ProxyUtils {
// Helper
private static boolean mI2PHelperBound;
private static boolean mI2PProxyInitialized;
@Inject PreferenceManager mPreferences;
@Inject I2PAndroidHelper mI2PHelper;
@Inject
public ProxyUtils() {
BrowserApp.getAppComponent().inject(this);
}
/*
* If Orbot/Tor or I2P is installed, prompt the user if they want to enable
* proxying for this session
*/
public void checkForProxy(final Activity activity) {
boolean useProxy = mPreferences.getUseProxy();
final boolean orbotInstalled = OrbotHelper.isOrbotInstalled(activity);
boolean orbotChecked = mPreferences.getCheckedForTor();
boolean orbot = orbotInstalled && !orbotChecked;
boolean i2pInstalled = mI2PHelper.isI2PAndroidInstalled();
boolean i2pChecked = mPreferences.getCheckedForI2P();
boolean i2p = i2pInstalled && !i2pChecked;
// TODO Is the idea to show this per-session, or only once?
if (!useProxy && (orbot || i2p)) {
if (orbot) mPreferences.setCheckedForTor(true);
if (i2p) mPreferences.setCheckedForI2P(true);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
if (orbotInstalled && i2pInstalled) {
String[] proxyChoices = activity.getResources().getStringArray(R.array.proxy_choices_array);
builder.setTitle(activity.getResources().getString(R.string.http_proxy))
.setSingleChoiceItems(proxyChoices, mPreferences.getProxyChoice(),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mPreferences.setProxyChoice(which);
}
})
.setNeutralButton(activity.getResources().getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mPreferences.getUseProxy())
initializeProxy(activity);
}
});
} else {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
mPreferences.setProxyChoice(orbotInstalled ?
Constants.PROXY_ORBOT : Constants.PROXY_I2P);
initializeProxy(activity);
break;
case DialogInterface.BUTTON_NEGATIVE:
mPreferences.setProxyChoice(Constants.NO_PROXY);
break;
}
}
};
builder.setMessage(orbotInstalled ? R.string.use_tor_prompt : R.string.use_i2p_prompt)
.setPositiveButton(R.string.yes, dialogClickListener)
.setNegativeButton(R.string.no, dialogClickListener);
}
builder.show();
}
}
/*
* Initialize WebKit Proxying
*/
private void initializeProxy(Activity activity) {
String host;
int port;
switch (mPreferences.getProxyChoice()) {
case Constants.NO_PROXY:
// We shouldn't be here
return;
case Constants.PROXY_ORBOT:
if (!OrbotHelper.isOrbotRunning(activity))
OrbotHelper.requestStartTor(activity);
host = "localhost";
port = 8118;
break;
case Constants.PROXY_I2P:
mI2PProxyInitialized = true;
if (mI2PHelperBound && !mI2PHelper.isI2PAndroidRunning()) {
mI2PHelper.requestI2PAndroidStart(activity);
}
host = "localhost";
port = 4444;
break;
default:
host = mPreferences.getProxyHost();
port = mPreferences.getProxyPort();
}
try {
WebkitProxy.setProxy(BrowserApp.class.getName(), activity.getApplicationContext(), null, host, port);
} catch (Exception e) {
Log.d(Constants.TAG, "error enabling web proxying", e);
}
}
public boolean isProxyReady() {
if (mPreferences.getProxyChoice() == Constants.PROXY_I2P) {
if (!mI2PHelper.isI2PAndroidRunning()) {
BrowserApp.getBus()
.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));
return false;
}
}
return true;
}
public void updateProxySettings(Activity activity) {
if (mPreferences.getUseProxy()) {
initializeProxy(activity);
} else {
try {
WebkitProxy.resetProxy(BrowserApp.class.getName(), activity.getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mI2PProxyInitialized = false;
}
}
public void onStop() {
mI2PHelper.unbind();
mI2PHelperBound = false;
}
public void onStart(final Activity activity) {
if (mPreferences.getProxyChoice() == Constants.PROXY_I2P) {
// Try to bind to I2P Android
mI2PHelper.bind(new I2PAndroidHelper.Callback() {
@Override
public void onI2PAndroidBound() {
mI2PHelperBound = true;
if (mI2PProxyInitialized && !mI2PHelper.isI2PAndroidRunning())
mI2PHelper.requestI2PAndroidStart(activity);
}
});
}
}
public static int setProxyChoice(int choice, Activity activity) {
switch (choice) {
case Constants.PROXY_ORBOT:
if (!OrbotHelper.isOrbotInstalled(activity)) {
choice = Constants.NO_PROXY;
Utils.showSnackbar(activity, R.string.install_orbot);
}
break;
case Constants.PROXY_I2P:
I2PAndroidHelper ih = new I2PAndroidHelper(BrowserApp.getContext());
if (!ih.isI2PAndroidInstalled()) {
choice = Constants.NO_PROXY;
ih.promptToInstall(activity);
}
break;
case Constants.PROXY_MANUAL:
break;
}
return choice;
}
}

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

@ -183,7 +183,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -183,7 +183,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// Event bus
@Inject Bus mEventBus;
@Inject LightningDialogBuilder bookmarksDialogBuilder;
@Inject LightningDialogBuilder mBookmarksDialogBuilder;
@Inject TabsManager mTabsManager;

8
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> fragments = new ArrayList<>();
private static final List<String> mFragments = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -44,19 +44,19 @@ public class SettingsActivity extends ThemableSettingsActivity { @@ -44,19 +44,19 @@ public class SettingsActivity extends ThemableSettingsActivity {
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preferences_headers, target);
fragments.clear();
mFragments.clear();
for (Header header : target) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// Workaround for bug in the AppCompat support library
header.iconRes = R.drawable.empty;
}
fragments.add(header.fragment);
mFragments.add(header.fragment);
}
}
@Override
protected boolean isValidFragment(String fragmentName) {
return fragments.contains(fragmentName);
return mFragments.contains(fragmentName);
}
@Override

7
app/src/main/java/acr/browser/lightning/activity/TabsManager.java

@ -34,11 +34,8 @@ public class TabsManager { @@ -34,11 +34,8 @@ public class TabsManager {
private final List<LightningView> mWebViewList = new ArrayList<>();
private LightningView mCurrentTab;
@Inject
PreferenceManager mPreferenceManager;
@Inject
Bus mEventBus;
@Inject PreferenceManager mPreferenceManager;
@Inject Bus mEventBus;
@Inject
public TabsManager() {}

20
app/src/main/java/acr/browser/lightning/app/AppModule.java

@ -14,34 +14,28 @@ import dagger.Provides; @@ -14,34 +14,28 @@ import dagger.Provides;
@Module
public class AppModule {
private final BrowserApp app;
private final Bus bus;
private final BrowserApp mApp;
private final Bus mBus;
public AppModule(BrowserApp app) {
this.app = app;
this.bus = new Bus();
this.mApp = app;
this.mBus = new Bus();
}
@Provides
public Context provideContext() {
return app.getApplicationContext();
}
@Provides
@Singleton
public BookmarkManager provideBookmarkManager() {
return new BookmarkManager(app.getApplicationContext());
return mApp.getApplicationContext();
}
@Provides
public Bus provideBus() {
return bus;
return mBus;
}
@Provides
@Singleton
public I2PAndroidHelper provideI2PAndroidHelper() {
return new I2PAndroidHelper(app.getApplicationContext());
return new I2PAndroidHelper(mApp.getApplicationContext());
}
}

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

@ -11,22 +11,18 @@ import java.util.concurrent.Executors; @@ -11,22 +11,18 @@ import java.util.concurrent.Executors;
import javax.inject.Inject;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.preference.PreferenceManager;
public class BrowserApp extends Application {
private static AppComponent appComponent;
private static AppComponent mAppComponent;
private static final Executor mIOThread = Executors.newSingleThreadExecutor();
@Inject Bus bus;
@Inject Bus mBus;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
appComponent.inject(this);
mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
mAppComponent.inject(this);
LeakCanary.install(this);
}
@ -35,7 +31,7 @@ public class BrowserApp extends Application { @@ -35,7 +31,7 @@ public class BrowserApp extends Application {
}
public static AppComponent getAppComponent() {
return appComponent;
return mAppComponent;
}
public static Executor getIOThread() {
@ -43,7 +39,7 @@ public class BrowserApp extends Application { @@ -43,7 +39,7 @@ public class BrowserApp extends Application {
}
public static Bus getBus(Context context) {
return get(context).bus;
return get(context).mBus;
}
}

6
app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java

@ -25,7 +25,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> { @@ -25,7 +25,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {
private static final String TAG = ImageDownloadTask.class.getSimpleName();
private final File mCacheDir;
private final WeakReference<ImageView> bmImage;
private final WeakReference<ImageView> mFaviconImage;
private final HistoryItem mWeb;
private final String mUrl;
private final Bitmap mDefaultBitmap;
@ -37,7 +37,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> { @@ -37,7 +37,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {
// Set a tag on the ImageView so we know if the view
// has gone out of scope and should not be used
bmImage.setTag(web.getUrl().hashCode());
this.bmImage = new WeakReference<>(bmImage);
this.mFaviconImage = new WeakReference<>(bmImage);
this.mWeb = web;
this.mUrl = web.getUrl();
this.mDefaultBitmap = defaultBitmap;
@ -135,7 +135,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> { @@ -135,7 +135,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {
super.onPostExecute(bitmap);
AsyncExecutor.getInstance().notifyThreadFinish();
final Bitmap fav = Utils.padFavicon(bitmap);
ImageView view = bmImage.get();
ImageView view = mFaviconImage.get();
if (view != null && view.getTag().equals(mWeb.getUrl().hashCode())) {
view.setImageBitmap(fav);
}

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

@ -32,6 +32,7 @@ import java.util.Set; @@ -32,6 +32,7 @@ import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.inject.Inject;
import javax.inject.Singleton;
import acr.browser.lightning.R;
@ -52,12 +53,12 @@ public class BookmarkManager { @@ -52,12 +53,12 @@ public class BookmarkManager {
private final String DEFAULT_BOOKMARK_TITLE;
private Map<String, HistoryItem> mBookmarksMap;
// private final List<HistoryItem> mBookmarkList = new ArrayList<>();
private String mCurrentFolder = "";
private final ExecutorService mExecutor;
private boolean mReady = false;
private final File mFilesDir;
@Inject
public BookmarkManager(Context context) {
mExecutor = Executors.newSingleThreadExecutor();
mFilesDir = context.getFilesDir();

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

@ -40,11 +40,8 @@ import acr.browser.lightning.utils.Utils; @@ -40,11 +40,8 @@ import acr.browser.lightning.utils.Utils;
public class LightningDialogBuilder {
@Inject BookmarkManager mBookmarkManager;
@Inject PreferenceManager mPreferenceManager;
@Inject HistoryDatabase mHistoryDatabase;
@Inject Bus mEventBus;
@Inject

11
app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java

@ -73,14 +73,9 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View @@ -73,14 +73,9 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View
private LightningViewAdapter mTabsAdapter;
private UIController mUiController;
@Inject
TabsManager tabsManager;
@Inject
Bus mBus;
@Inject
PreferenceManager mPreferences;
@Inject TabsManager tabsManager;
@Inject Bus mBus;
@Inject PreferenceManager mPreferences;
public TabsFragment() {
BrowserApp.getAppComponent().inject(this);

3
app/src/main/java/acr/browser/lightning/receiver/NetworkReceiver.java

@ -9,8 +9,7 @@ import android.net.NetworkInfo; @@ -9,8 +9,7 @@ import android.net.NetworkInfo;
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
public void onReceive(Context context, Intent intent) {}
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

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

@ -14,11 +14,13 @@ import java.util.Locale; @@ -14,11 +14,13 @@ import java.util.Locale;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager;
@Singleton
public class AdBlock {
private static final String TAG = "AdBlock";
@ -34,18 +36,11 @@ public class AdBlock { @@ -34,18 +36,11 @@ public class AdBlock {
private final Set<String> mBlockedDomainsList = new HashSet<>();
private boolean mBlockAds;
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);
}
return mInstance;
}
private AdBlock(Context context) {
@Inject
public AdBlock(Context context) {
BrowserApp.getAppComponent().inject(this);
if (mBlockedDomainsList.isEmpty() && Constants.FULL_VERSION) {
loadHostsFile(context);

0
app/src/LightningPlus/java/acr/browser/lightning/utils/ProxyUtils.java → app/src/main/java/acr/browser/lightning/utils/ProxyUtils.java

9
app/src/main/java/acr/browser/lightning/view/IconCacheTask.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package acr.browser.lightning.view;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
@ -16,12 +17,12 @@ import acr.browser.lightning.utils.Utils; @@ -16,12 +17,12 @@ import acr.browser.lightning.utils.Utils;
class IconCacheTask implements Runnable {
private final Uri uri;
private final Bitmap icon;
private final Context context;
private final Application app;
public IconCacheTask(final Uri uri, final Bitmap icon, final Context context) {
public IconCacheTask(final Uri uri, final Bitmap icon, final Application app) {
this.uri = uri;
this.icon = icon;
this.context = BrowserApp.get(context);
this.app = app;
}
@Override
@ -30,7 +31,7 @@ class IconCacheTask implements Runnable { @@ -30,7 +31,7 @@ class IconCacheTask implements Runnable {
Log.d(Constants.TAG, "Caching icon for " + uri.getHost());
FileOutputStream fos = null;
try {
File image = new File(context.getCacheDir(), hash + ".png");
File image = new File(app.getCacheDir(), hash + ".png");
fos = new FileOutputStream(image);
icon.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();

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

@ -80,7 +80,7 @@ class LightningChromeClient extends WebChromeClient { @@ -80,7 +80,7 @@ class LightningChromeClient extends WebChromeClient {
if (uri.getHost() == null) {
return;
}
BrowserApp.getIOThread().execute(new IconCacheTask(uri, icon, context));
BrowserApp.getIOThread().execute(new IconCacheTask(uri, icon, BrowserApp.get(context)));
}

6
app/src/main/java/acr/browser/lightning/view/LightningViewTitle.java

@ -18,10 +18,8 @@ class LightningViewTitle { @@ -18,10 +18,8 @@ class LightningViewTitle {
private static Bitmap DEFAULT_ICON = null;
@NonNull
private Bitmap mFavicon;
@NonNull
private String mTitle;
@NonNull private Bitmap mFavicon;
@NonNull private String mTitle;
public LightningViewTitle(@NonNull Context context, boolean darkTheme) {
if (DEFAULT_ICON == null) {

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

@ -50,18 +50,17 @@ public class LightningWebClient extends WebViewClient { @@ -50,18 +50,17 @@ public class LightningWebClient extends WebViewClient {
private final Activity mActivity;
private final LightningView mLightningView;
private final UIController mUIController;
private final AdBlock mAdBlock;
private final Bus mEventBus;
private final IntentUtils mIntentUtils;
@Inject ProxyUtils mProxyUtils;
@Inject AdBlock mAdBlock;
LightningWebClient(Activity activity, LightningView lightningView) {
BrowserApp.getAppComponent().inject(this);
mActivity = activity;
mUIController = (UIController) activity;
mLightningView = lightningView;
mAdBlock = AdBlock.getInstance(activity);
mAdBlock.updatePreference();
mEventBus = BrowserApp.getBus(activity);
mIntentUtils = new IntentUtils(activity);

Loading…
Cancel
Save