SettingsFragment.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package de.tudarmstadt.informatik.hostage.ui.fragment;
  2. import android.app.AlertDialog;
  3. import android.app.FragmentManager;
  4. import android.os.Bundle;
  5. import android.text.Html;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import de.tudarmstadt.informatik.hostage.R;
  12. import de.tudarmstadt.informatik.hostage.system.Device;
  13. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  14. /**
  15. * @author Alexander Brakowski
  16. * @created 24.02.14 23:37
  17. */
  18. public class SettingsFragment extends UpNavigatibleFragment {
  19. private TextView mPorthackText;
  20. private Button mPorthackInstallButton;
  21. private Button mPorthackUninstallButton;
  22. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  23. super.onCreateView(inflater, container, savedInstanceState);
  24. getActivity().setTitle(getResources().getString(R.string.drawer_settings));
  25. View v = inflater.inflate(R.layout.fragment_settings, container, false);
  26. TextView rootedText = (TextView) v.findViewById(R.id.settings_device_rooted);
  27. TextView iptablesText = (TextView) v.findViewById(R.id.settings_iptables_available);
  28. mPorthackText = (TextView) v.findViewById(R.id.settings_porthack_installed);
  29. mPorthackInstallButton = (Button) v.findViewById(R.id.settings_deploy_porthack);
  30. mPorthackUninstallButton = (Button) v.findViewById(R.id.settings_uninstall_porthack);
  31. if (Device.isRooted()) {
  32. rootedText.setText(R.string.yes);
  33. rootedText.setTextColor(getResources().getColor(R.color.holo_dark_green));
  34. } else {
  35. rootedText.setText(R.string.no);
  36. rootedText.setTextColor(getResources().getColor(R.color.holo_red));
  37. }
  38. if (Device.isPortRedirectionAvailable()) {
  39. iptablesText.setText(R.string.yes);
  40. iptablesText.setTextColor(getResources().getColor(R.color.holo_dark_green));
  41. } else {
  42. iptablesText.setText(R.string.no);
  43. iptablesText.setTextColor(getResources().getColor(R.color.holo_red));
  44. }
  45. updatePorthackStatus();
  46. mPorthackInstallButton.setOnClickListener(new View.OnClickListener() {
  47. @Override
  48. public void onClick(View v) {
  49. Device.deployPorthack();
  50. updatePorthackStatus();
  51. }
  52. });
  53. mPorthackUninstallButton.setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. Device.uninstallPorthack();
  57. updatePorthackStatus();
  58. }
  59. });
  60. v.findViewById(R.id.porthack_info_button).setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.getInstance());
  64. builder.setMessage(Html.fromHtml(getString(R.string.porthack_explanation)));
  65. AlertDialog alert = builder.create();
  66. alert.show();
  67. }
  68. });
  69. return v;
  70. }
  71. private void updatePorthackStatus() {
  72. Device.checkCapabilities(); // get current situation
  73. if (Device.isPorthackInstalled()) {
  74. mPorthackText.setText(R.string.yes);
  75. mPorthackText.setTextColor(getResources().getColor(R.color.holo_dark_green));
  76. mPorthackInstallButton.setEnabled(false); // we're only able to deploy if the device is rooted
  77. mPorthackInstallButton.setVisibility(View.GONE);
  78. mPorthackUninstallButton.setEnabled(true);
  79. mPorthackUninstallButton.setVisibility(View.VISIBLE);
  80. } else {
  81. mPorthackText.setText(R.string.no);
  82. mPorthackText.setTextColor(getResources().getColor(R.color.holo_red));
  83. // we're only able to deploy if the device is rooted
  84. mPorthackInstallButton.setEnabled(Device.isRooted());
  85. mPorthackInstallButton.setVisibility(Device.isRooted() ? View.VISIBLE : View.GONE);
  86. mPorthackUninstallButton.setEnabled(false);
  87. mPorthackUninstallButton.setVisibility(View.GONE);
  88. }
  89. }
  90. public void onViewCreated(View view, Bundle savedInstanceState) {
  91. super.onViewCreated(view, savedInstanceState);
  92. FragmentManager manager = this.getFragmentManager();
  93. manager.beginTransaction().replace(R.id.settings_fragment_container, new PreferenceHostageFragment()).commit();
  94. }
  95. }