SettingsFragment.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.AlertDialog;
  3. import android.app.DownloadManager;
  4. import android.app.DownloadManager.Query;
  5. import android.app.DownloadManager.Request;
  6. import android.app.Fragment;
  7. import android.app.FragmentManager;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.DialogInterface;
  11. import android.content.Intent;
  12. import android.content.IntentFilter;
  13. import android.database.Cursor;
  14. import android.net.Uri;
  15. import android.os.Bundle;
  16. import android.os.Environment;
  17. import android.util.Log;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.ImageView;
  22. import android.widget.TextView;
  23. import java.io.BufferedReader;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.io.InputStreamReader;
  30. import java.nio.channels.FileChannel;
  31. import java.util.zip.ZipInputStream;
  32. import de.tudarmstadt.informatik.hostage.Hostage;
  33. import de.tudarmstadt.informatik.hostage.R;
  34. import de.tudarmstadt.informatik.hostage.system.Decompress;
  35. import de.tudarmstadt.informatik.hostage.system.Device;
  36. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  37. /**
  38. * Creates the view to edit the preferences of the app and shows the porthack and rooted state of the device
  39. *
  40. * @author Alexander Brakowski
  41. * @created 24.02.14 23:37
  42. */
  43. public class SettingsFragment extends UpNavigatibleFragment {
  44. /**
  45. * {@inheritDoc}
  46. */
  47. private long enqueue;
  48. private DownloadManager dm;
  49. @Override
  50. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  51. super.onCreateView(inflater, container, savedInstanceState);
  52. getActivity().setTitle(getResources().getString(R.string.drawer_settings));
  53. View v = inflater.inflate(R.layout.fragment_settings, container, false);
  54. final TextView rootedText = (TextView) v.findViewById(R.id.settings_device_rooted);
  55. final TextView porthackText = (TextView) v.findViewById(R.id.settings_porthack_installed);
  56. if (Device.isRooted()) {
  57. rootedText.setText(R.string.yes);
  58. rootedText.setTextColor(getResources().getColor(R.color.holo_dark_green));
  59. } else {
  60. rootedText.setText(R.string.no);
  61. rootedText.setTextColor(getResources().getColor(R.color.holo_red));
  62. }
  63. if (Device.isPorthackInstalled()) {
  64. porthackText.setText(R.string.yes);
  65. porthackText.setTextColor(getResources().getColor(R.color.holo_dark_green));
  66. } else {
  67. porthackText.setText(R.string.no);
  68. porthackText.setTextColor(getResources().getColor(R.color.holo_red));
  69. }
  70. //Handle if a download 'may' be needed
  71. BroadcastReceiver receiver = new BroadcastReceiver() {
  72. @Override
  73. public void onReceive(Context context, Intent intent) {
  74. String action = intent.getAction();
  75. if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
  76. long downloadId = intent.getLongExtra(
  77. DownloadManager.EXTRA_DOWNLOAD_ID, 0);
  78. Query query = new Query();
  79. query.setFilterById(enqueue);
  80. Cursor c = dm.query(query);
  81. if (c.moveToFirst()) {
  82. int columnIndex = c
  83. .getColumnIndex(DownloadManager.COLUMN_STATUS);
  84. if (DownloadManager.STATUS_SUCCESSFUL == c
  85. .getInt(columnIndex)) {
  86. String downloadFile = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
  87. String makeWritable[]= {"su","-c","chmod","777","/data/local"}; //change permission to allow rw access from x only
  88. String allowExec[]= {"su","-c","chmod","711","/data/local/bind"}; //change permission to allow x access
  89. String revert[]= {"su","-c","chmod","751","/data/local"}; //change permission back to x only
  90. try {
  91. //Chmod the local directory for write access
  92. Process process = Runtime.getRuntime().exec(makeWritable);
  93. process.waitFor();
  94. Log.d("portbinder:","Changing permission on /data/local to allow write access");
  95. //Decompressing downloaded zip to local directory
  96. Decompress dwnld = new Decompress(downloadFile,"/data/local/" );
  97. dwnld.unzip();
  98. Log.v("portbinder:","Decompressing downloaded file");
  99. //Chmod the Portbinder to allow it to be executable
  100. process = Runtime.getRuntime().exec(allowExec);
  101. process.waitFor();
  102. Log.v("portbinder:","Changing permission on /data/local/bind to allow the binary to be executed");
  103. //Chmod the local directory to back to non-read/write access
  104. process = Runtime.getRuntime().exec(revert);
  105. process.waitFor();
  106. Log.v("portbinder:","Changing permission on /data/local back to no-write access");
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. } catch (InterruptedException e) {
  110. e.printStackTrace();
  111. }
  112. if(Device.updatePorthack()) //if successful
  113. {
  114. porthackText.setText(R.string.yes);
  115. porthackText.setTextColor(getResources().getColor(R.color.holo_dark_green));
  116. }
  117. }
  118. }
  119. }
  120. }
  121. };
  122. getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  123. //Allow no porthack text box clickable
  124. if (Device.isRooted() && !Device.isPorthackInstalled())
  125. {
  126. porthackText.setOnClickListener(new View.OnClickListener() {
  127. @Override
  128. public void onClick(View arg0) {
  129. new AlertDialog.Builder(getActivity())
  130. .setTitle(R.string.information)
  131. .setMessage(R.string.no_portbinder_msg)
  132. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  133. public void onClick(DialogInterface dialog, int which) {
  134. }
  135. })
  136. .setNegativeButton(R.string.help, new DialogInterface.OnClickListener() {
  137. public void onClick(DialogInterface dialog, int which) {
  138. dialog.dismiss();
  139. final AlertDialog alert;
  140. new AlertDialog.Builder(getActivity())
  141. .setTitle(R.string.portbinder)
  142. .setMessage(R.string.helpPortbinder)
  143. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  144. public void onClick(DialogInterface dialog, int which) {
  145. }
  146. })
  147. /*.setNeutralButton(R.string.portbinder_tutorial, new DialogInterface.OnClickListener() {
  148. public void onClick(DialogInterface dialog, int which) {
  149. Uri uri = Uri.parse("https://www.youtube.com/watch?v=gi6fSiIJASk");
  150. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  151. startActivity(intent);
  152. }
  153. })*/
  154. .setNegativeButton(R.string.portbinder_website, new DialogInterface.OnClickListener() {
  155. public void onClick(DialogInterface dialog, int which) {
  156. Uri uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/de/research/secure-smart-infrastructures/hostage/");
  157. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  158. startActivity(intent);
  159. }
  160. })
  161. //Testing automated installation of portbinder
  162. .setNeutralButton(R.string.help_me, new DialogInterface.OnClickListener() {
  163. public void onClick(DialogInterface dialog, int which) {
  164. //Download Portbinder
  165. dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
  166. //Identify architecture
  167. String arch = System.getProperty("os.arch"); //get the device architecture
  168. //selecting necessary PortBinder
  169. Uri uri = Uri.EMPTY; //initialize variable
  170. if (arch.matches("arm.*")) //arm
  171. uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/fileadmin/user_upload/Group_TK/bind-arm.zip");
  172. else if (arch.matches("x86")) //x86
  173. uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/fileadmin/user_upload/Group_TK/bind-x86.zip");
  174. else if (arch.matches("mips")) //mips
  175. uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/fileadmin/user_upload/Group_TK/bind-mips.zip");
  176. if (uri != Uri.EMPTY) {
  177. Request request = new Request(uri)
  178. .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "bind.zip");
  179. enqueue = dm.enqueue(request);
  180. // dm.enqueue(request);
  181. }
  182. else {
  183. //report to user of an unknown architecture
  184. }
  185. }
  186. })
  187. .setIcon(android.R.drawable.ic_dialog_info).show();
  188. }
  189. })
  190. .setIcon(android.R.drawable.ic_dialog_info).show();}
  191. });
  192. }
  193. return v;
  194. }
  195. /**
  196. * {@inheritDoc}
  197. */
  198. @Override
  199. public void onViewCreated(View view, Bundle savedInstanceState) {
  200. super.onViewCreated(view, savedInstanceState);
  201. FragmentManager manager = this.getFragmentManager();
  202. manager.beginTransaction().replace(R.id.settings_fragment_container, new PreferenceHostageFrament()).commit();
  203. }
  204. }