Browse Source

Create PROXY IntDef to enforce values

master
Anthony Restaino 8 years ago
parent
commit
716efd7640
  1. 9
      app/src/main/java/acr/browser/lightning/constant/Constants.java
  2. 25
      app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java
  3. 14
      app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java
  4. 45
      app/src/main/java/acr/browser/lightning/utils/ProxyUtils.java

9
app/src/main/java/acr/browser/lightning/constant/Constants.java

@ -3,6 +3,11 @@
*/ */
package acr.browser.lightning.constant; package acr.browser.lightning.constant;
import android.support.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import acr.browser.lightning.BuildConfig; import acr.browser.lightning.BuildConfig;
public final class Constants { public final class Constants {
@ -66,6 +71,10 @@ public final class Constants {
public static final String TAG = "Lightning"; public static final String TAG = "Lightning";
// These should match the order of @array/proxy_choices_array // These should match the order of @array/proxy_choices_array
@IntDef({NO_PROXY, PROXY_ORBOT, PROXY_I2P, PROXY_MANUAL})
@Retention(RetentionPolicy.SOURCE)
public @interface PROXY {}
public static final int NO_PROXY = 0; public static final int NO_PROXY = 0;
public static final int PROXY_ORBOT = 1; public static final int PROXY_ORBOT = 1;
public static final int PROXY_I2P = 2; public static final int PROXY_I2P = 2;

25
app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java

@ -231,7 +231,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
BrowserDialog.setDialogSize(mActivity, dialog); BrowserDialog.setDialogSize(mActivity, dialog);
} }
private void setProxyChoice(int choice) { private void setProxyChoice(@Constants.PROXY int choice) {
switch (choice) { switch (choice) {
case Constants.PROXY_ORBOT: case Constants.PROXY_ORBOT:
choice = ProxyUtils.setProxyChoice(choice, mActivity); choice = ProxyUtils.setProxyChoice(choice, mActivity);
@ -316,17 +316,22 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
picker.setTitle(getResources().getString(R.string.home)); picker.setTitle(getResources().getString(R.string.home));
mHomepage = mPreferenceManager.getHomepage(); mHomepage = mPreferenceManager.getHomepage();
int n; int n;
if (mHomepage.startsWith(Constants.SCHEME_HOMEPAGE)) { switch (mHomepage) {
n = 1; case Constants.SCHEME_HOMEPAGE:
} else if (mHomepage.startsWith(Constants.SCHEME_BLANK)) { n = 0;
n = 2; break;
} else if (mHomepage.startsWith(Constants.SCHEME_BOOKMARKS)) { case Constants.SCHEME_BLANK:
n = 3; n = 1;
} else { break;
n = 4; case Constants.SCHEME_BOOKMARKS:
n = 2;
break;
default:
n = 3;
break;
} }
picker.setSingleChoiceItems(R.array.homepage, n - 1, picker.setSingleChoiceItems(R.array.homepage, n,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {

14
app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java

@ -251,8 +251,18 @@ public class PreferenceManager {
return mPrefs.getBoolean(Name.USE_PROXY, false); return mPrefs.getBoolean(Name.USE_PROXY, false);
} }
@Constants.PROXY
public int getProxyChoice() { public int getProxyChoice() {
return mPrefs.getInt(Name.PROXY_CHOICE, Constants.NO_PROXY); @Constants.PROXY int proxy = mPrefs.getInt(Name.PROXY_CHOICE, Constants.NO_PROXY);
switch (proxy) {
case Constants.NO_PROXY:
case Constants.PROXY_ORBOT:
case Constants.PROXY_I2P:
case Constants.PROXY_MANUAL:
return proxy;
default:
return Constants.NO_PROXY;
}
} }
public int getUserAgentChoice() { public int getUserAgentChoice() {
@ -463,7 +473,7 @@ public class PreferenceManager {
* *
* @param choice the proxy to use. * @param choice the proxy to use.
*/ */
public void setProxyChoice(int choice) { public void setProxyChoice(@Constants.PROXY int choice) {
putBoolean(Name.USE_PROXY, choice != Constants.NO_PROXY); putBoolean(Name.USE_PROXY, choice != Constants.NO_PROXY);
putInt(Name.PROXY_CHOICE, choice); putInt(Name.PROXY_CHOICE, choice);
} }

45
app/src/main/java/acr/browser/lightning/utils/ProxyUtils.java

@ -62,21 +62,21 @@ public class ProxyUtils {
if (orbotInstalled && i2pInstalled) { if (orbotInstalled && i2pInstalled) {
String[] proxyChoices = activity.getResources().getStringArray(R.array.proxy_choices_array); String[] proxyChoices = activity.getResources().getStringArray(R.array.proxy_choices_array);
builder.setTitle(activity.getResources().getString(R.string.http_proxy)) builder.setTitle(activity.getResources().getString(R.string.http_proxy))
.setSingleChoiceItems(proxyChoices, mPreferences.getProxyChoice(), .setSingleChoiceItems(proxyChoices, mPreferences.getProxyChoice(),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
mPreferences.setProxyChoice(which); mPreferences.setProxyChoice(which);
} }
}) })
.setPositiveButton(activity.getResources().getString(R.string.action_ok), .setPositiveButton(activity.getResources().getString(R.string.action_ok),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (mPreferences.getUseProxy()) if (mPreferences.getUseProxy())
initializeProxy(activity); initializeProxy(activity);
} }
}); });
} else { } else {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override @Override
@ -84,7 +84,7 @@ public class ProxyUtils {
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
mPreferences.setProxyChoice(orbotInstalled ? mPreferences.setProxyChoice(orbotInstalled ?
Constants.PROXY_ORBOT : Constants.PROXY_I2P); Constants.PROXY_ORBOT : Constants.PROXY_I2P);
initializeProxy(activity); initializeProxy(activity);
break; break;
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
@ -95,8 +95,8 @@ public class ProxyUtils {
}; };
builder.setMessage(orbotInstalled ? R.string.use_tor_prompt : R.string.use_i2p_prompt) builder.setMessage(orbotInstalled ? R.string.use_tor_prompt : R.string.use_i2p_prompt)
.setPositiveButton(R.string.yes, dialogClickListener) .setPositiveButton(R.string.yes, dialogClickListener)
.setNegativeButton(R.string.no, dialogClickListener); .setNegativeButton(R.string.no, dialogClickListener);
} }
Dialog dialog = builder.show(); Dialog dialog = builder.show();
BrowserDialog.setDialogSize(activity, dialog); BrowserDialog.setDialogSize(activity, dialog);
@ -114,14 +114,12 @@ public class ProxyUtils {
case Constants.NO_PROXY: case Constants.NO_PROXY:
// We shouldn't be here // We shouldn't be here
return; return;
case Constants.PROXY_ORBOT: case Constants.PROXY_ORBOT:
if (!OrbotHelper.isOrbotRunning(activity)) if (!OrbotHelper.isOrbotRunning(activity))
OrbotHelper.requestStartTor(activity); OrbotHelper.requestStartTor(activity);
host = "localhost"; host = "localhost";
port = 8118; port = 8118;
break; break;
case Constants.PROXY_I2P: case Constants.PROXY_I2P:
mI2PProxyInitialized = true; mI2PProxyInitialized = true;
if (mI2PHelperBound && !mI2PHelper.isI2PAndroidRunning()) { if (mI2PHelperBound && !mI2PHelper.isI2PAndroidRunning()) {
@ -130,10 +128,14 @@ public class ProxyUtils {
host = "localhost"; host = "localhost";
port = 4444; port = 4444;
break; break;
default: default:
host = mPreferences.getProxyHost(); host = mPreferences.getProxyHost();
port = mPreferences.getProxyPort(); port = mPreferences.getProxyPort();
break;
case Constants.PROXY_MANUAL:
host = mPreferences.getProxyHost();
port = mPreferences.getProxyPort();
break;
} }
try { try {
@ -191,6 +193,7 @@ public class ProxyUtils {
} }
} }
@Constants.PROXY
public static int setProxyChoice(int choice, @NonNull Activity activity) { public static int setProxyChoice(int choice, @NonNull Activity activity) {
switch (choice) { switch (choice) {
case Constants.PROXY_ORBOT: case Constants.PROXY_ORBOT:

Loading…
Cancel
Save