PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/acr/browser/barebones/activities/SettingsActivity.java

https://github.com/Snowblond/Lightning-Browser
Java | 721 lines | 604 code | 112 blank | 5 comment | 36 complexity | 098bf299f0bad8a50806022769068cc6 MD5 | raw file
Possible License(s): Apache-2.0
  1. package acr.browser.barebones.activities;
  2. import acr.browser.barebones.R;
  3. import acr.browser.barebones.R.drawable;
  4. import acr.browser.barebones.R.id;
  5. import acr.browser.barebones.R.layout;
  6. import acr.browser.barebones.variables.FinalVariables;
  7. import acr.browser.barebones.variables.Utils;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.os.Environment;
  11. import android.annotation.SuppressLint;
  12. import android.app.Activity;
  13. import android.app.AlertDialog;
  14. import android.content.DialogInterface;
  15. import android.content.Intent;
  16. import android.content.SharedPreferences;
  17. import android.content.pm.ApplicationInfo;
  18. import android.content.pm.PackageInfo;
  19. import android.content.pm.PackageManager;
  20. import android.content.pm.PackageManager.NameNotFoundException;
  21. import android.util.Log;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.view.animation.Animation;
  25. import android.view.animation.AnimationUtils;
  26. import android.widget.CheckBox;
  27. import android.widget.CompoundButton;
  28. import android.widget.CompoundButton.OnCheckedChangeListener;
  29. import android.widget.EditText;
  30. import android.widget.ImageView;
  31. import android.widget.RadioButton;
  32. import android.widget.RadioGroup;
  33. import android.widget.RelativeLayout;
  34. import android.widget.ScrollView;
  35. import android.widget.Switch;
  36. import android.widget.TextView;
  37. import android.widget.Toast;
  38. public class SettingsActivity extends Activity {
  39. static int API = FinalVariables.API;
  40. static final String preferences = "settings";
  41. static SharedPreferences.Editor edit;
  42. static int agentChoice;
  43. static String homepage;
  44. static TextView agentText;
  45. static TextView download;
  46. static int egg = 0;
  47. static String downloadLocation;
  48. static TextView homepageText;
  49. static SharedPreferences settings;
  50. static TextView searchText;
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.settings);
  55. init();
  56. }
  57. @SuppressLint("NewApi")
  58. public void init() {
  59. // settings storage
  60. settings = getSharedPreferences(preferences, 0);
  61. edit = settings.edit();
  62. // initialize UI
  63. RelativeLayout layoutLocation = (RelativeLayout) findViewById(R.id.layoutLocation);
  64. RelativeLayout layoutFullScreen = (RelativeLayout) findViewById(R.id.layoutFullScreen);
  65. RelativeLayout layoutFlash = (RelativeLayout) findViewById(R.id.layoutFlash);
  66. ImageView back = (ImageView) findViewById(R.id.back);
  67. searchText = (TextView) findViewById(R.id.searchText);
  68. switch (settings.getInt("search", 1)) {
  69. case 1:
  70. searchText.setText("Google");
  71. break;
  72. case 2:
  73. searchText.setText("Bing");
  74. break;
  75. case 3:
  76. searchText.setText("Yahoo");
  77. break;
  78. case 4:
  79. searchText.setText("StartPage");
  80. break;
  81. case 5:
  82. searchText.setText("DuckDuckGo");
  83. break;
  84. }
  85. back.setBackgroundResource(R.drawable.button);
  86. agentText = (TextView) findViewById(R.id.agentText);
  87. homepageText = (TextView) findViewById(R.id.homepageText);
  88. download = (TextView) findViewById(R.id.downloadText);
  89. boolean locationBool = settings.getBoolean("location", false);
  90. int flashNum = settings.getInt("enableflash", 0);
  91. boolean fullScreenBool = settings.getBoolean("fullscreen", false);
  92. agentChoice = settings.getInt("agentchoose", 1);
  93. homepage = settings.getString("home", FinalVariables.HOMEPAGE);
  94. downloadLocation = settings.getString("download",
  95. FinalVariables.DOWNLOAD_LOCATION);
  96. download.setText(downloadLocation);
  97. int code = 0;
  98. try {
  99. PackageInfo p = getPackageManager().getPackageInfo(
  100. getPackageName(), 0);
  101. code = p.versionCode;
  102. } catch (NameNotFoundException e) {
  103. // TODO Auto-generated catch block
  104. e.printStackTrace();
  105. }
  106. TextView version = (TextView) findViewById(R.id.versionCode);
  107. version.setText(code + "");
  108. if (homepage.contains("about:home")) {
  109. homepageText.setText("Bookmarks");
  110. } else if (homepage.contains("about:blank")) {
  111. homepageText.setText("Blank Page");
  112. } else {
  113. homepageText.setText(homepage);
  114. }
  115. switch (agentChoice) {
  116. case 1:
  117. agentText.setText("Default");
  118. break;
  119. case 2:
  120. agentText.setText("Desktop");
  121. break;
  122. case 3:
  123. agentText.setText("Mobile");
  124. break;
  125. }
  126. RelativeLayout r1, r2, r3;
  127. r1 = (RelativeLayout)findViewById(R.id.setR1);
  128. r2 = (RelativeLayout)findViewById(R.id.setR2);
  129. r3 = (RelativeLayout)findViewById(R.id.setR3);
  130. if (API >= 14) {
  131. Switch location = new Switch(this);
  132. Switch fullScreen = new Switch(this);
  133. Switch flash = new Switch(this);
  134. r1.addView(location);
  135. r2.addView(fullScreen);
  136. r3.addView(flash);
  137. location.setChecked(locationBool);
  138. fullScreen.setChecked(fullScreenBool);
  139. if (flashNum > 0) {
  140. flash.setChecked(true);
  141. } else {
  142. flash.setChecked(false);
  143. }
  144. initSwitch(location, fullScreen, flash);
  145. clickListenerForSwitches(
  146. layoutLocation, layoutFullScreen, layoutFlash, location, fullScreen, flash);
  147. } else {
  148. CheckBox location = new CheckBox(this);
  149. CheckBox fullScreen = new CheckBox(this);
  150. CheckBox flash = new CheckBox(this);
  151. r1.addView(location);
  152. r2.addView(fullScreen);
  153. r3.addView(flash);
  154. location.setChecked(locationBool);
  155. fullScreen.setChecked(fullScreenBool);
  156. if (flashNum > 0) {
  157. flash.setChecked(true);
  158. } else {
  159. flash.setChecked(false);
  160. }
  161. initCheckBox(location, fullScreen, flash);
  162. clickListenerForCheckBoxes(
  163. layoutLocation, layoutFullScreen, layoutFlash, location, fullScreen, flash);
  164. }
  165. RelativeLayout agent = (RelativeLayout) findViewById(R.id.layoutUserAgent);
  166. RelativeLayout download = (RelativeLayout) findViewById(R.id.layoutDownload);
  167. RelativeLayout homepage = (RelativeLayout) findViewById(R.id.layoutHomepage);
  168. RelativeLayout advanced = (RelativeLayout) findViewById(R.id.layoutAdvanced);
  169. RelativeLayout source = (RelativeLayout) findViewById(R.id.layoutSource);
  170. RelativeLayout license = (RelativeLayout) findViewById(R.id.layoutLicense);
  171. back(back);
  172. agent(agent);
  173. download(download);
  174. homepage(homepage);
  175. advanced(advanced);
  176. source(source);
  177. license(license);
  178. search();
  179. easterEgg();
  180. }
  181. public void search(){
  182. RelativeLayout search = (RelativeLayout)findViewById(R.id.layoutSearch);
  183. search.setOnClickListener(new OnClickListener(){
  184. @Override
  185. public void onClick(View v) {
  186. AlertDialog.Builder picker = new AlertDialog.Builder(
  187. SettingsActivity.this);
  188. picker.setTitle("Search Engine");
  189. CharSequence[] chars = { "Google (Suggested)", "Bing", "Yahoo", "StartPage", "DuckDuckGo (Privacy)" };
  190. int n = settings.getInt("search", 1);
  191. picker.setSingleChoiceItems(chars, n - 1,
  192. new DialogInterface.OnClickListener() {
  193. @Override
  194. public void onClick(DialogInterface dialog,
  195. int which) {
  196. edit.putInt("search", which+1);
  197. edit.commit();
  198. switch (which+1) {
  199. case 1:
  200. searchText.setText("Google");
  201. break;
  202. case 2:
  203. searchText.setText("Bing");
  204. break;
  205. case 3:
  206. searchText.setText("Yahoo");
  207. break;
  208. case 4:
  209. searchText.setText("StartPage");
  210. break;
  211. case 5:
  212. searchText.setText("DuckDuckGo");
  213. break;
  214. }
  215. }
  216. });
  217. picker.setNeutralButton("OK",
  218. new DialogInterface.OnClickListener() {
  219. @Override
  220. public void onClick(DialogInterface dialog,
  221. int which) {
  222. }
  223. });
  224. picker.show();
  225. }
  226. });
  227. }
  228. public void clickListenerForCheckBoxes(
  229. RelativeLayout one, RelativeLayout two, RelativeLayout three, final CheckBox loc,
  230. final CheckBox full, final CheckBox flash) {
  231. one.setOnClickListener(new OnClickListener(){
  232. @Override
  233. public void onClick(View v) {
  234. loc.setChecked(!loc.isChecked());
  235. }
  236. });
  237. two.setOnClickListener(new OnClickListener(){
  238. @Override
  239. public void onClick(View v) {
  240. full.setChecked(!full.isChecked());
  241. }
  242. });
  243. three.setOnClickListener(new OnClickListener(){
  244. @Override
  245. public void onClick(View v) {
  246. flash.setChecked(!flash.isChecked());
  247. }
  248. });
  249. }
  250. public void clickListenerForSwitches(
  251. RelativeLayout one, RelativeLayout two, RelativeLayout three, final Switch loc,
  252. final Switch full, final Switch flash) {
  253. one.setOnClickListener(new OnClickListener(){
  254. @Override
  255. public void onClick(View v) {
  256. loc.setChecked(!loc.isChecked());
  257. }
  258. });
  259. two.setOnClickListener(new OnClickListener(){
  260. @Override
  261. public void onClick(View v) {
  262. full.setChecked(!full.isChecked());
  263. }
  264. });
  265. three.setOnClickListener(new OnClickListener(){
  266. @Override
  267. public void onClick(View v) {
  268. flash.setChecked(!flash.isChecked());
  269. }
  270. });
  271. }
  272. public void easterEgg() {
  273. RelativeLayout easter = (RelativeLayout) findViewById(R.id.layoutVersion);
  274. easter.setOnClickListener(new OnClickListener() {
  275. @Override
  276. public void onClick(View v) {
  277. egg++;
  278. if (egg == 10) {
  279. Toast.makeText(SettingsActivity.this, "Almost There",
  280. Toast.LENGTH_SHORT).show();
  281. }
  282. if (egg == 15) {
  283. Toast.makeText(SettingsActivity.this, "Easter Egg goes here",
  284. Toast.LENGTH_SHORT).show();
  285. egg = 0;
  286. }
  287. }
  288. });
  289. }
  290. public void initSwitch(Switch location, Switch fullscreen, Switch flash) {
  291. location.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  292. @Override
  293. public void onCheckedChanged(CompoundButton buttonView,
  294. boolean isChecked) {
  295. edit.putBoolean("location", isChecked);
  296. edit.commit();
  297. }
  298. });
  299. flash.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  300. @Override
  301. public void onCheckedChanged(CompoundButton buttonView,
  302. boolean isChecked) {
  303. int n = 0;
  304. if (isChecked) {
  305. n = 1;
  306. }
  307. edit.putInt("enableflash", n);
  308. edit.commit();
  309. boolean flashInstalled = false;
  310. try {
  311. PackageManager pm = getPackageManager();
  312. ApplicationInfo ai = pm.getApplicationInfo("com.adobe.flashplayer", 0);
  313. if (ai != null)
  314. flashInstalled = true;
  315. } catch (NameNotFoundException e) {
  316. flashInstalled = false;
  317. }
  318. if(!flashInstalled){
  319. Utils.createInformativeDialog(SettingsActivity.this, "Warning", "Adobe Flash Player was not detected.\n" +
  320. "Please install Flash Player.");
  321. buttonView.setChecked(false);
  322. edit.putInt("enableflash", 0);
  323. edit.commit();
  324. }
  325. }
  326. });
  327. fullscreen.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  328. @Override
  329. public void onCheckedChanged(CompoundButton buttonView,
  330. boolean isChecked) {
  331. edit.putBoolean("fullscreen", isChecked);
  332. edit.commit();
  333. }
  334. });
  335. }
  336. public void initCheckBox(CheckBox location, CheckBox fullscreen, CheckBox flash) {
  337. location.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  338. @Override
  339. public void onCheckedChanged(CompoundButton buttonView,
  340. boolean isChecked) {
  341. edit.putBoolean("location", isChecked);
  342. edit.commit();
  343. }
  344. });
  345. flash.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  346. @Override
  347. public void onCheckedChanged(CompoundButton buttonView,
  348. boolean isChecked) {
  349. int n = 0;
  350. if (isChecked) {
  351. n = 1;
  352. }
  353. edit.putInt("enableflash", n);
  354. edit.commit();
  355. boolean flashInstalled = false;
  356. try {
  357. PackageManager pm = getPackageManager();
  358. ApplicationInfo ai = pm.getApplicationInfo("com.adobe.flashplayer", 0);
  359. if (ai != null)
  360. flashInstalled = true;
  361. } catch (NameNotFoundException e) {
  362. flashInstalled = false;
  363. }
  364. if(!flashInstalled){
  365. Utils.createInformativeDialog(SettingsActivity.this, "Warning", "Adobe Flash Player was not detected.\n" +
  366. "Please install Flash Player.");
  367. buttonView.setChecked(false);
  368. edit.putInt("enableflash", 0);
  369. edit.commit();
  370. }
  371. }
  372. });
  373. fullscreen.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  374. @Override
  375. public void onCheckedChanged(CompoundButton buttonView,
  376. boolean isChecked) {
  377. edit.putBoolean("fullscreen", isChecked);
  378. edit.commit();
  379. }
  380. });
  381. }
  382. public void back(ImageView view) {
  383. view.setOnClickListener(new OnClickListener() {
  384. @Override
  385. public void onClick(View v) {
  386. finish();
  387. }
  388. });
  389. }
  390. public void agent(RelativeLayout view) {
  391. view.setOnClickListener(new OnClickListener() {
  392. @Override
  393. public void onClick(View v) {
  394. AlertDialog.Builder agentPicker = new AlertDialog.Builder(
  395. SettingsActivity.this);
  396. agentPicker.setTitle("User Agent");
  397. CharSequence[] chars = { "Default", "Desktop", "Mobile" };
  398. agentChoice = settings.getInt("agentchoose", 1);
  399. agentPicker.setSingleChoiceItems(chars, agentChoice - 1,
  400. new DialogInterface.OnClickListener() {
  401. @Override
  402. public void onClick(DialogInterface dialog,
  403. int which) {
  404. edit.putInt("agentchoose", which + 1);
  405. edit.commit();
  406. switch (which + 1) {
  407. case 1:
  408. agentText.setText("Default");
  409. break;
  410. case 2:
  411. agentText.setText("Desktop");
  412. break;
  413. case 3:
  414. agentText.setText("Mobile");
  415. break;
  416. }
  417. }
  418. });
  419. agentPicker.setNeutralButton("OK",
  420. new DialogInterface.OnClickListener() {
  421. @Override
  422. public void onClick(DialogInterface dialog,
  423. int which) {
  424. // TODO Auto-generated method stub
  425. }
  426. });
  427. agentPicker
  428. .setOnCancelListener(new DialogInterface.OnCancelListener() {
  429. @Override
  430. public void onCancel(DialogInterface dialog) {
  431. // TODO Auto-generated method stub
  432. Log.i("Cancelled", "");
  433. }
  434. });
  435. agentPicker.show();
  436. }
  437. });
  438. }
  439. public void download(RelativeLayout view) {
  440. view.setOnClickListener(new OnClickListener() {
  441. @Override
  442. public void onClick(View v) {
  443. AlertDialog.Builder picker = new AlertDialog.Builder(
  444. SettingsActivity.this);
  445. picker.setTitle("Download Location");
  446. CharSequence[] chars = { "Default", "Custom" };
  447. downloadLocation = settings.getString("download",
  448. FinalVariables.DOWNLOAD_LOCATION);
  449. int n = -1;
  450. if (downloadLocation.contains(FinalVariables.DOWNLOAD_LOCATION)) {
  451. n = 1;
  452. } else {
  453. n = 2;
  454. }
  455. picker.setSingleChoiceItems(chars, n - 1,
  456. new DialogInterface.OnClickListener() {
  457. @Override
  458. public void onClick(DialogInterface dialog,
  459. int which) {
  460. switch (which + 1) {
  461. case 1:
  462. edit.putString("download",
  463. FinalVariables.DOWNLOAD_LOCATION);
  464. edit.commit();
  465. download.setText(FinalVariables.DOWNLOAD_LOCATION);
  466. break;
  467. case 2:
  468. downPicker();
  469. break;
  470. }
  471. }
  472. });
  473. picker.setNeutralButton("OK",
  474. new DialogInterface.OnClickListener() {
  475. @Override
  476. public void onClick(DialogInterface dialog,
  477. int which) {
  478. }
  479. });
  480. picker.show();
  481. }
  482. });
  483. }
  484. public void homePicker() {
  485. final AlertDialog.Builder homePicker = new AlertDialog.Builder(
  486. SettingsActivity.this);
  487. homePicker.setTitle("Custom Homepage");
  488. final EditText getHome = new EditText(SettingsActivity.this);
  489. homepage = settings.getString("home", FinalVariables.HOMEPAGE);
  490. if (!homepage.startsWith("about:")) {
  491. getHome.setText(homepage);
  492. } else {
  493. getHome.setText("http://www.google.com");
  494. }
  495. homePicker.setView(getHome);
  496. homePicker.setPositiveButton("OK",
  497. new DialogInterface.OnClickListener() {
  498. @Override
  499. public void onClick(DialogInterface dialog, int which) {
  500. String text = getHome.getText().toString();
  501. edit.putString("home", text);
  502. edit.commit();
  503. homepageText.setText(text);
  504. }
  505. });
  506. homePicker.show();
  507. }
  508. public void downPicker() {
  509. final AlertDialog.Builder downLocationPicker = new AlertDialog.Builder(
  510. SettingsActivity.this);
  511. downLocationPicker.setTitle("Custom Location");
  512. final EditText getDownload = new EditText(SettingsActivity.this);
  513. downloadLocation = settings.getString("download",
  514. FinalVariables.DOWNLOAD_LOCATION);
  515. getDownload.setText(downloadLocation);
  516. downLocationPicker.setView(getDownload);
  517. downLocationPicker.setPositiveButton("OK",
  518. new DialogInterface.OnClickListener() {
  519. @Override
  520. public void onClick(DialogInterface dialog, int which) {
  521. String text = getDownload.getText().toString();
  522. edit.putString("download", text);
  523. edit.commit();
  524. download.setText(text);
  525. }
  526. });
  527. downLocationPicker.show();
  528. }
  529. public void homepage(RelativeLayout view) {
  530. view.setOnClickListener(new OnClickListener() {
  531. @Override
  532. public void onClick(View v) {
  533. AlertDialog.Builder picker = new AlertDialog.Builder(
  534. SettingsActivity.this);
  535. picker.setTitle("Homepage");
  536. CharSequence[] chars = { "Bookmarks", "Blank Page", "Webpage" };
  537. homepage = settings.getString("home", FinalVariables.HOMEPAGE);
  538. int n = -1;
  539. if (homepage.contains("about:home")) {
  540. n = 1;
  541. } else if (homepage.contains("about:blank")) {
  542. n = 2;
  543. } else {
  544. n = 3;
  545. }
  546. picker.setSingleChoiceItems(chars, n - 1,
  547. new DialogInterface.OnClickListener() {
  548. @Override
  549. public void onClick(DialogInterface dialog,
  550. int which) {
  551. switch (which + 1) {
  552. case 1:
  553. edit.putString("home", "about:home");
  554. edit.commit();
  555. homepageText.setText("Bookmarks");
  556. break;
  557. case 2:
  558. edit.putString("home", "about:blank");
  559. edit.commit();
  560. homepageText.setText("Blank Page");
  561. break;
  562. case 3:
  563. homePicker();
  564. break;
  565. }
  566. }
  567. });
  568. picker.setNeutralButton("OK",
  569. new DialogInterface.OnClickListener() {
  570. @Override
  571. public void onClick(DialogInterface dialog,
  572. int which) {
  573. }
  574. });
  575. picker.show();
  576. }
  577. });
  578. }
  579. public void advanced(RelativeLayout view) {
  580. view.setOnClickListener(new OnClickListener() {
  581. @Override
  582. public void onClick(View v) {
  583. startActivity(new Intent(
  584. "android.intent.action.ADVANCEDSETTINGS"));
  585. }
  586. });
  587. }
  588. public void source(RelativeLayout view) {
  589. view.setOnClickListener(new OnClickListener() {
  590. @Override
  591. public void onClick(View v) {
  592. startActivity(new Intent(
  593. Intent.ACTION_VIEW,
  594. Uri.parse("https://github.com/anthonycr/Lightning-Browser")));
  595. finish();
  596. }
  597. });
  598. }
  599. public void license(RelativeLayout view) {
  600. view.setOnClickListener(new OnClickListener() {
  601. @Override
  602. public void onClick(View v) {
  603. startActivity(new Intent(Intent.ACTION_VIEW, Uri
  604. .parse("http://www.apache.org/licenses/LICENSE-2.0")));
  605. finish();
  606. }
  607. });
  608. }
  609. }