SettingsFragment.java 13 KB

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