lightning-i2p/src/acr/browser/lightning/SettingsActivity.java

476 lines
14 KiB
Java
Raw Normal View History

/*
* Copyright 2014 A.C.R. Development
*/
package acr.browser.lightning;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import info.guardianproject.onionkit.ui.OrbotHelper;
public class SettingsActivity extends ActionBarActivity {
private static int API = android.os.Build.VERSION.SDK_INT;
private SharedPreferences.Editor mEditPrefs;
private SharedPreferences mPreferences;
private Context mContext;
private Activity mActivity;
private boolean mDark;
@Override
protected void onCreate(Bundle savedInstanceState) {
mPreferences = getSharedPreferences(PreferenceConstants.PREFERENCES, 0);
if (mPreferences.getBoolean(PreferenceConstants.DARK_THEME, false)) {
this.setTheme(R.style.Theme_SettingsTheme_Dark);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
mDark = mPreferences.getBoolean(PreferenceConstants.DARK_THEME, false);
mContext = this;
mActivity = this;
init();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
finish();
return true;
}
@Override
protected void onResume() {
super.onResume();
if (mPreferences != null
&& mPreferences.getBoolean(PreferenceConstants.DARK_THEME, false) != mDark) {
this.recreate();
}
}
@SuppressLint("NewApi")
public void init() {
// set up ActionBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// mPreferences storage
if (mPreferences.getBoolean(PreferenceConstants.HIDE_STATUS_BAR, false)) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
mEditPrefs = mPreferences.edit();
// initialize UI
RelativeLayout layoutFlash = (RelativeLayout) findViewById(R.id.layoutFlash);
RelativeLayout layoutBlockAds = (RelativeLayout) findViewById(R.id.layoutAdBlock);
RelativeLayout layoutImages = (RelativeLayout) findViewById(R.id.layoutImages);
RelativeLayout layoutEnableJS = (RelativeLayout) findViewById(R.id.layoutEnableJS);
RelativeLayout layoutOrbot = (RelativeLayout) findViewById(R.id.layoutUseOrbot);
RelativeLayout layoutColor = (RelativeLayout) findViewById(R.id.layoutColorMode);
RelativeLayout layoutBookmarks = (RelativeLayout) findViewById(R.id.layoutBookmarks);
layoutBookmarks.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, BookmarkActivity.class));
}
});
if (API >= 19) {
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0);
mEditPrefs.apply();
}
2014-08-08 22:17:52 +00:00
int flashNum = mPreferences.getInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0);
boolean imagesBool = mPreferences.getBoolean(PreferenceConstants.BLOCK_IMAGES, false);
boolean enableJSBool = mPreferences.getBoolean(PreferenceConstants.JAVASCRIPT, true);
2015-01-29 18:25:19 +00:00
CheckBox flash = (CheckBox) findViewById(R.id.cbFlash);
CheckBox adblock = (CheckBox) findViewById(R.id.cbAdblock);
CheckBox images = (CheckBox) findViewById(R.id.cbImageBlock);
CheckBox enablejs = (CheckBox) findViewById(R.id.cbJavascript);
CheckBox orbot = (CheckBox) findViewById(R.id.cbOrbot);
CheckBox color = (CheckBox) findViewById(R.id.cbColorMode);
2015-01-29 18:25:19 +00:00
images.setChecked(imagesBool);
enablejs.setChecked(enableJSBool);
if (flashNum > 0) {
flash.setChecked(true);
} else {
flash.setChecked(false);
}
2014-08-08 22:17:52 +00:00
adblock.setChecked(mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS, false));
orbot.setChecked(mPreferences.getBoolean(PreferenceConstants.USE_PROXY, false));
color.setChecked(mPreferences.getBoolean(PreferenceConstants.ENABLE_COLOR_MODE, true));
initCheckBox(flash, adblock, images, enablejs, orbot, color);
clickListenerForCheckBoxes(layoutFlash, layoutBlockAds, layoutImages, layoutEnableJS,
layoutOrbot, layoutColor, flash, adblock, images, enablejs, orbot, color);
RelativeLayout general = (RelativeLayout) findViewById(R.id.layoutGeneral);
RelativeLayout display = (RelativeLayout) findViewById(R.id.layoutDisplay);
RelativeLayout privacy = (RelativeLayout) findViewById(R.id.layoutPrivacy);
RelativeLayout advanced = (RelativeLayout) findViewById(R.id.layoutAdvanced);
RelativeLayout about = (RelativeLayout) findViewById(R.id.layoutAbout);
general(general);
display(display);
privacy(privacy);
advanced(advanced);
about(about);
}
public void clickListenerForCheckBoxes(RelativeLayout layoutFlash,
RelativeLayout layoutBlockAds, RelativeLayout layoutImages,
RelativeLayout layoutEnableJS, RelativeLayout layoutOrbot, RelativeLayout layoutColor,
final CheckBox flash, final CheckBox adblock, final CheckBox images,
final CheckBox enablejs, final CheckBox orbot, final CheckBox color) {
layoutFlash.setOnClickListener(new OnClickListener() {
2014-08-19 19:10:54 +00:00
@Override
public void onClick(View v) {
if (API < 19) {
flash.setChecked(!flash.isChecked());
} else {
Utils.createInformativeDialog(mContext,
getResources().getString(R.string.title_warning), getResources()
.getString(R.string.dialog_adobe_dead));
}
2014-08-19 19:10:54 +00:00
}
});
layoutBlockAds.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adblock.setChecked(!adblock.isChecked());
}
});
layoutImages.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
images.setChecked(!images.isChecked());
}
});
layoutEnableJS.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
enablejs.setChecked(!enablejs.isChecked());
}
});
layoutOrbot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
2014-07-09 01:35:13 +00:00
if (orbot.isEnabled()) {
orbot.setChecked(!orbot.isChecked());
} else {
2014-08-08 22:17:52 +00:00
Utils.showToast(mContext, getResources().getString(R.string.install_orbot));
2014-07-09 01:35:13 +00:00
}
}
});
layoutColor.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
color.setChecked(!color.isChecked());
}
});
}
2015-01-29 18:25:19 +00:00
public void initCheckBox(CheckBox flash, CheckBox adblock, CheckBox images, CheckBox enablejs,
CheckBox orbot, CheckBox color) {
flash.setEnabled(API < 19);
flash.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
2014-08-08 22:17:52 +00:00
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
getFlashChoice();
} else {
2014-08-08 22:17:52 +00:00
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0);
mEditPrefs.apply();
}
boolean flashInstalled = false;
try {
PackageManager pm = getPackageManager();
2014-08-08 22:17:52 +00:00
ApplicationInfo ai = pm.getApplicationInfo("com.adobe.flashplayer", 0);
if (ai != null) {
flashInstalled = true;
}
} catch (NameNotFoundException e) {
flashInstalled = false;
}
if (!flashInstalled && isChecked) {
2014-08-08 22:17:52 +00:00
Utils.createInformativeDialog(SettingsActivity.this,
getResources().getString(R.string.title_warning), getResources()
.getString(R.string.dialog_adobe_not_installed));
buttonView.setChecked(false);
2014-08-08 22:17:52 +00:00
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0);
mEditPrefs.apply();
2014-01-20 19:14:07 +00:00
} else if ((API >= 17) && isChecked) {
2014-08-08 22:17:52 +00:00
Utils.createInformativeDialog(SettingsActivity.this,
getResources().getString(R.string.title_warning), getResources()
.getString(R.string.dialog_adobe_unsupported));
}
}
});
adblock.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mEditPrefs.putBoolean(PreferenceConstants.BLOCK_ADS, isChecked);
mEditPrefs.apply();
}
});
images.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
2014-08-08 22:17:52 +00:00
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mEditPrefs.putBoolean(PreferenceConstants.BLOCK_IMAGES, isChecked);
mEditPrefs.apply();
}
});
enablejs.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mEditPrefs.putBoolean(PreferenceConstants.JAVASCRIPT, isChecked);
mEditPrefs.apply();
}
});
2014-07-09 01:35:13 +00:00
OrbotHelper oh = new OrbotHelper(this);
if (!oh.isOrbotInstalled()) {
orbot.setEnabled(false);
}
orbot.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
2014-08-08 22:17:52 +00:00
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2014-07-09 01:35:13 +00:00
mEditPrefs.putBoolean(PreferenceConstants.USE_PROXY, isChecked);
mEditPrefs.apply();
}
});
color.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mEditPrefs.putBoolean(PreferenceConstants.ENABLE_COLOR_MODE, isChecked);
mEditPrefs.apply();
}
});
}
private void getFlashChoice() {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
2014-08-08 22:17:52 +00:00
builder.setTitle(mContext.getResources().getString(R.string.title_flash));
builder.setMessage(getResources().getString(R.string.flash))
.setCancelable(true)
2014-08-08 22:17:52 +00:00
.setPositiveButton(getResources().getString(R.string.action_manual),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
2014-08-08 22:17:52 +00:00
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 1);
mEditPrefs.apply();
}
})
2014-08-08 22:17:52 +00:00
.setNegativeButton(getResources().getString(R.string.action_auto),
new DialogInterface.OnClickListener() {
@Override
2014-08-08 22:17:52 +00:00
public void onClick(DialogInterface dialog, int which) {
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 2);
mEditPrefs.apply();
}
}).setOnCancelListener(new OnCancelListener() {
2014-08-08 22:17:52 +00:00
@Override
public void onCancel(DialogInterface dialog) {
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0);
mEditPrefs.apply();
2014-08-08 22:17:52 +00:00
}
2014-08-08 22:17:52 +00:00
});
AlertDialog alert = builder.create();
alert.show();
}
public void initCheckBox(CheckBox flash, CheckBox images, CheckBox enablejs) {
flash.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
2014-08-08 22:17:52 +00:00
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int n = 0;
if (isChecked) {
n = 1;
}
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, n);
mEditPrefs.apply();
boolean flashInstalled = false;
try {
PackageManager pm = getPackageManager();
2014-08-08 22:17:52 +00:00
ApplicationInfo ai = pm.getApplicationInfo("com.adobe.flashplayer", 0);
if (ai != null) {
flashInstalled = true;
}
} catch (NameNotFoundException e) {
flashInstalled = false;
}
if (!flashInstalled && isChecked) {
2014-08-08 22:17:52 +00:00
Utils.createInformativeDialog(SettingsActivity.this,
getResources().getString(R.string.title_warning), getResources()
.getString(R.string.dialog_adobe_not_installed));
buttonView.setChecked(false);
2014-08-08 22:17:52 +00:00
mEditPrefs.putInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0);
mEditPrefs.apply();
} else if ((API > 17) && isChecked) {
2014-08-08 22:17:52 +00:00
Utils.createInformativeDialog(SettingsActivity.this,
getResources().getString(R.string.title_warning), getResources()
.getString(R.string.dialog_adobe_unsupported));
}
}
});
images.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
2014-08-08 22:17:52 +00:00
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mEditPrefs.putBoolean(PreferenceConstants.BLOCK_IMAGES, isChecked);
mEditPrefs.apply();
}
});
enablejs.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mEditPrefs.putBoolean(PreferenceConstants.JAVASCRIPT, isChecked);
mEditPrefs.apply();
}
});
}
public void agentPicker() {
2014-08-08 22:17:52 +00:00
final AlertDialog.Builder agentStringPicker = new AlertDialog.Builder(mActivity);
2014-08-08 22:17:52 +00:00
agentStringPicker.setTitle(getResources().getString(R.string.title_user_agent));
final EditText getAgent = new EditText(this);
getAgent.append(mPreferences.getString(PreferenceConstants.USER_AGENT_STRING, ""));
agentStringPicker.setView(getAgent);
2014-08-08 22:17:52 +00:00
agentStringPicker.setPositiveButton(getResources().getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = getAgent.getText().toString();
2014-08-08 22:17:52 +00:00
mEditPrefs.putString(PreferenceConstants.USER_AGENT_STRING, text);
mEditPrefs.apply();
getAgent.setText(getResources().getString(R.string.agent_custom));
}
});
agentStringPicker.show();
}
public void general(RelativeLayout view) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, GeneralSettingsActivity.class));
}
});
}
public void display(RelativeLayout view) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, DisplaySettingsActivity.class));
}
});
}
public void privacy(RelativeLayout view) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, PrivacySettingsActivity.class));
}
});
}
public void advanced(RelativeLayout view) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
2014-08-08 22:17:52 +00:00
startActivity(new Intent(mContext, AdvancedSettingsActivity.class));
}
});
}
public void about(RelativeLayout view) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, AboutSettingsActivity.class));
}
});
}
}