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 @@ @@ -3,6 +3,11 @@
*/
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;
public final class Constants {
@ -66,6 +71,10 @@ public final class Constants { @@ -66,6 +71,10 @@ public final class Constants {
public static final String TAG = "Lightning";
// 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 PROXY_ORBOT = 1;
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 @@ -231,7 +231,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
BrowserDialog.setDialogSize(mActivity, dialog);
}
private void setProxyChoice(int choice) {
private void setProxyChoice(@Constants.PROXY int choice) {
switch (choice) {
case Constants.PROXY_ORBOT:
choice = ProxyUtils.setProxyChoice(choice, mActivity);
@ -316,17 +316,22 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme @@ -316,17 +316,22 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
picker.setTitle(getResources().getString(R.string.home));
mHomepage = mPreferenceManager.getHomepage();
int n;
if (mHomepage.startsWith(Constants.SCHEME_HOMEPAGE)) {
n = 1;
} else if (mHomepage.startsWith(Constants.SCHEME_BLANK)) {
n = 2;
} else if (mHomepage.startsWith(Constants.SCHEME_BOOKMARKS)) {
n = 3;
} else {
n = 4;
switch (mHomepage) {
case Constants.SCHEME_HOMEPAGE:
n = 0;
break;
case Constants.SCHEME_BLANK:
n = 1;
break;
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() {
@Override
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 { @@ -251,8 +251,18 @@ public class PreferenceManager {
return mPrefs.getBoolean(Name.USE_PROXY, false);
}
@Constants.PROXY
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() {
@ -463,7 +473,7 @@ public class PreferenceManager { @@ -463,7 +473,7 @@ public class PreferenceManager {
*
* @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);
putInt(Name.PROXY_CHOICE, choice);
}

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

@ -62,21 +62,21 @@ public class ProxyUtils { @@ -62,21 +62,21 @@ public class ProxyUtils {
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);
}
})
.setPositiveButton(activity.getResources().getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mPreferences.getUseProxy())
initializeProxy(activity);
}
});
.setSingleChoiceItems(proxyChoices, mPreferences.getProxyChoice(),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mPreferences.setProxyChoice(which);
}
})
.setPositiveButton(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
@ -84,7 +84,7 @@ public class ProxyUtils { @@ -84,7 +84,7 @@ public class ProxyUtils {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
mPreferences.setProxyChoice(orbotInstalled ?
Constants.PROXY_ORBOT : Constants.PROXY_I2P);
Constants.PROXY_ORBOT : Constants.PROXY_I2P);
initializeProxy(activity);
break;
case DialogInterface.BUTTON_NEGATIVE:
@ -95,8 +95,8 @@ public class ProxyUtils { @@ -95,8 +95,8 @@ public class ProxyUtils {
};
builder.setMessage(orbotInstalled ? R.string.use_tor_prompt : R.string.use_i2p_prompt)
.setPositiveButton(R.string.yes, dialogClickListener)
.setNegativeButton(R.string.no, dialogClickListener);
.setPositiveButton(R.string.yes, dialogClickListener)
.setNegativeButton(R.string.no, dialogClickListener);
}
Dialog dialog = builder.show();
BrowserDialog.setDialogSize(activity, dialog);
@ -114,14 +114,12 @@ public class ProxyUtils { @@ -114,14 +114,12 @@ public class ProxyUtils {
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()) {
@ -130,10 +128,14 @@ public class ProxyUtils { @@ -130,10 +128,14 @@ public class ProxyUtils {
host = "localhost";
port = 4444;
break;
default:
host = mPreferences.getProxyHost();
port = mPreferences.getProxyPort();
break;
case Constants.PROXY_MANUAL:
host = mPreferences.getProxyHost();
port = mPreferences.getProxyPort();
break;
}
try {
@ -191,6 +193,7 @@ public class ProxyUtils { @@ -191,6 +193,7 @@ public class ProxyUtils {
}
}
@Constants.PROXY
public static int setProxyChoice(int choice, @NonNull Activity activity) {
switch (choice) {
case Constants.PROXY_ORBOT:

Loading…
Cancel
Save