SettingsFragment.java 4.1 KB

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