ViewLogTable.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.text.DateFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import android.annotation.SuppressLint;
  10. import android.app.Activity;
  11. import android.content.Context;
  12. import android.content.SharedPreferences;
  13. import android.os.Bundle;
  14. import android.view.Menu;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.AdapterView;
  18. import android.widget.ListAdapter;
  19. import android.widget.ListView;
  20. import android.widget.SimpleAdapter;
  21. import android.widget.Toast;
  22. import de.tudarmstadt.informatik.hostage.R;
  23. import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
  24. import de.tudarmstadt.informatik.hostage.logging.Record;
  25. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  26. /**
  27. * Creates a simple log view. Shows the Information for every attack. The format ist defined in {@link Record#toString(int)}.
  28. * @author Lars Pandikow
  29. *
  30. */
  31. public class ViewLogTable extends Activity{
  32. DatabaseHandler dbh;
  33. private ArrayList<String> selectedProtocols;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. this.selectedProtocols = new ArrayList<String>();
  38. for (String protocol : this.getResources().getStringArray( R.array.protocols)){
  39. this.selectedProtocols.add(protocol);
  40. }
  41. dbh = new DatabaseHandler(getBaseContext());
  42. setContentView(R.layout.activity_loglist);
  43. // StringBuffer log = new StringBuffer();
  44. // //Create a log entry for every attack in the Database
  45. // for(Record record: dbh.getAllReceivedRecordsOfEachAttack()) {
  46. // log.append(record.toString(0));
  47. // }
  48. // ScrollView scroll = new ScrollView(this);
  49. // TextView text = new TextView(getApplicationContext());
  50. // text.setText(log);
  51. // text.setTextAppearance(this, android.R.style.TextAppearance_Medium);
  52. // scroll.addView(text);
  53. this.addRecordToDB();
  54. populateListViewFromDB();
  55. registerListClickCallback();
  56. }
  57. private void addRecordToDB(){
  58. Record record = new Record();
  59. record.setBSSID("BSSID wwwww");
  60. record.setSSID("SSID wowy");
  61. record.setTimestamp(0);
  62. record.setProtocol("HTTP");
  63. try {
  64. InetAddress localIP = InetAddress.getByAddress("Digga",new byte[]{127, 0, 0, 1}); //.getByName("192.168.2.1");
  65. record.setLocalIP(localIP);
  66. record.setRemoteIP(InetAddress.getByAddress("Digga",new byte[]{127, 1, 1, 1}));
  67. record.setType(TYPE.SEND);
  68. } catch (UnknownHostException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. dbh.addRecord(record);
  73. }
  74. private void populateListViewFromDB() {
  75. ListView mylist = (ListView) findViewById(R.id.loglistview);
  76. ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();
  77. ArrayList<Record> data = dbh.getAllRecords();
  78. for (Record val : data) {
  79. String protocol = val.getProtocol();
  80. if (this.selectedProtocols.contains(protocol)){
  81. HashMap<String, String> map = new HashMap<String, String>();
  82. map.put(this.getString(R.string.RecordBSSID), val.getBSSID() );
  83. map.put(this.getString(R.string.RecordSSID), val.getSSID());
  84. map.put(this.getString(R.string.RecordProtocol), val.getProtocol());
  85. map.put(this.getString(R.string.RecordTimestamp), this.getDateAsString(val.getTimestamp()));
  86. Items.add(map);
  87. }
  88. }
  89. // Adding Items to ListView
  90. String keys[] = new String[] { this.getString(R.string.RecordBSSID), this.getString(R.string.RecordSSID), this.getString(R.string.RecordProtocol), this.getString(R.string.RecordTimestamp)};
  91. int ids[] = new int[] {R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };
  92. ListAdapter adapter = new SimpleAdapter(this, Items, R.layout.loglist_row, keys, ids);
  93. mylist.setAdapter(adapter);
  94. }
  95. @SuppressLint("SimpleDateFormat")
  96. private String getDateAsString(long timeStamp){
  97. try{
  98. DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  99. Date netDate = (new Date(timeStamp));
  100. return sdf.format(netDate);
  101. }
  102. catch(Exception ex){
  103. return "xx";
  104. }
  105. }
  106. private void registerListClickCallback() {
  107. ListView mylist = (ListView) findViewById(R.id.loglistview);
  108. mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  109. public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
  110. long idInDB) {
  111. DatabaseHandler dbh = new DatabaseHandler(getBaseContext());
  112. Record rec = dbh.getRecord((int) idInDB);
  113. String message = createInformationStringFromRecord(rec);
  114. Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
  115. }
  116. private String createInformationStringFromRecord(Record rec){
  117. String message = "id: " + rec.getId() + "\n" +
  118. "attack_id: " + rec.getAttack_id() +"\n" +
  119. "protocol: " + rec.getProtocol() +"\n" +
  120. "type: " + rec.getType() + "\n" +
  121. "externalIP: " + rec.getExternalIP() +"\n" +
  122. "localIP: " + rec.getLocalIP() +"\n" +
  123. "local port: " + rec.getLocalPort() +"\n" +
  124. "remoteIP: " + rec.getRemoteIP() +"\n" +
  125. "BSSID: " + rec.getBSSID() + "\n" +
  126. "SSID: " + rec.getSSID() +"\n" +
  127. "latitude: " + rec.getLatitude() +"\n" +
  128. "longitude: " + rec.getLongitude() + "\n" +
  129. "accuracy: " + rec.getAccuracy() +"\n" +
  130. "packet: " + rec.getPacket() + "\n" +
  131. getDateAsString(rec.getTimestamp()) +
  132. "";
  133. return message;
  134. }
  135. @SuppressLint("SimpleDateFormat")
  136. private String getDateAsString(long timeStamp){
  137. try{
  138. DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  139. Date netDate = (new Date(timeStamp));
  140. return sdf.format(netDate);
  141. }
  142. catch(Exception ex){
  143. return "xx";
  144. }
  145. }
  146. });
  147. }
  148. @Override
  149. public boolean onCreateOptionsMenu(Menu menu)
  150. {
  151. super.onCreateOptionsMenu(menu);
  152. for (String protocol : this.getResources().getStringArray( R.array.protocols)){
  153. MenuItem item = menu.add(protocol);
  154. item.setCheckable(true);
  155. boolean isChecked = this.selectedProtocols.contains(item.getTitle());
  156. item.setChecked(isChecked);
  157. }
  158. // MenuInflater inflater = getMenuInflater();
  159. // inflater.inflate(R.menu.listview_detail_menu, menu);
  160. return true;
  161. }
  162. @Override
  163. public boolean onOptionsItemSelected(MenuItem item)
  164. {
  165. boolean isChecked = this.selectedProtocols.contains(item.getTitle());
  166. if (isChecked){
  167. this.selectedProtocols.remove(item.getTitle());
  168. } else {
  169. this.selectedProtocols.add(item.getTitle().toString());
  170. }
  171. item.setChecked(!isChecked);
  172. this.populateListViewFromDB();
  173. return super.onOptionsItemSelected(item);
  174. }
  175. private void saveInSharedPreferences(String key, boolean value){
  176. //--SAVE Data
  177. SharedPreferences preferences = this.getSharedPreferences();
  178. SharedPreferences.Editor editor = preferences.edit();
  179. editor.putBoolean(key, value);
  180. editor.commit();
  181. }
  182. private boolean getBooleanInSharedPreferences(String key){
  183. SharedPreferences preferences = this.getSharedPreferences();
  184. return preferences.getBoolean(key, true);
  185. }
  186. private SharedPreferences getSharedPreferences(){
  187. return this.getSharedPreferences("HostagePreferences", Context.MODE_PRIVATE);
  188. }
  189. }