RecordOverviewFragment.java 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.FragmentManager;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.os.Bundle;
  11. import android.os.Environment;
  12. import android.os.Message;
  13. import android.preference.PreferenceManager;
  14. import android.util.Log;
  15. import android.view.LayoutInflater;
  16. import android.view.Menu;
  17. import android.view.MenuInflater;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.ExpandableListView;
  22. import android.widget.ImageButton;
  23. import android.widget.ProgressBar;
  24. import android.widget.Toast;
  25. import com.google.android.gms.maps.model.LatLng;
  26. import java.io.File;
  27. import java.io.FileOutputStream;
  28. import java.text.DateFormat;
  29. import java.text.SimpleDateFormat;
  30. import java.util.ArrayList;
  31. import java.util.Calendar;
  32. import java.util.Collections;
  33. import java.util.Comparator;
  34. import java.util.Date;
  35. import java.util.HashMap;
  36. import java.util.Random;
  37. import de.tudarmstadt.informatik.hostage.R;
  38. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  39. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  40. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  41. import de.tudarmstadt.informatik.hostage.logging.Record;
  42. import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
  43. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  44. import de.tudarmstadt.informatik.hostage.sync.bluetooth.BluetoothSync;
  45. import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync;
  46. import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;
  47. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  48. import de.tudarmstadt.informatik.hostage.ui.LogFilter.SortType;
  49. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  50. import de.tudarmstadt.informatik.hostage.ui2.adapter.RecordListAdapter;
  51. import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
  52. import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
  53. import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
  54. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
  55. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
  56. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
  57. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
  58. import de.tudarmstadt.informatik.hostage.ui2.popup.SplitPopupItem;
  59. public class RecordOverviewFragment extends UpNavigatibleFragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
  60. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  61. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  62. static final String FILTER_MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.rec_protocol);
  63. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = MainActivity.getContext().getString(
  64. R.string.rec_latest);
  65. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = MainActivity.getContext().getString(
  66. R.string.rec_earliest);
  67. static final String FILTER_MENU_TITLE_SORTING = MainActivity.getContext().getString(R.string.rec_sortby);
  68. static final String FILTER_MENU_TITLE_REMOVE = MainActivity.getContext().getString(R.string.rec_reset_filter);
  69. static final String FILTER_MENU_TITLE_GROUP = MainActivity.getContext().getString(
  70. R.string.rec_group_by);
  71. static final String FILTER_MENU_POPUP_TITLE = MainActivity.getContext().getString(
  72. R.string.rec_filter_by);
  73. private boolean wasBelowTimePicker;
  74. private LogFilter filter;
  75. private boolean showFilterButton;
  76. private View rootView;
  77. private int mListPosition = -1;
  78. private int mItemPosition = -1;
  79. public String groupingKey;
  80. private ExpandableListView expListView;
  81. private ProgressBar spinner;
  82. HostageDBOpenHelper dbh;
  83. private String sectionToOpen = "";
  84. private ArrayList<Integer> openSections;
  85. private SharedPreferences pref;
  86. public void setFilter(LogFilter filter){
  87. this.filter = filter;
  88. }
  89. Thread loader;
  90. public RecordOverviewFragment(){}
  91. public void setGroupKey(String key){
  92. this.groupingKey = key;
  93. }
  94. @Override
  95. public void onCreate(Bundle savedInstanceState) {
  96. super.onCreate(savedInstanceState);
  97. setHasOptionsMenu(true);
  98. }
  99. @Override
  100. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  101. Bundle savedInstanceState) {
  102. setHasOptionsMenu(true);
  103. getActivity().setTitle(getResources().getString(R.string.drawer_records));
  104. dbh = new HostageDBOpenHelper(this.getActivity().getBaseContext());
  105. pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
  106. //this.addRecordToDB(5, 2);
  107. // Get the message from the intent
  108. if (this.filter == null){
  109. Intent intent = this.getActivity().getIntent();
  110. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  111. if(filter == null){
  112. this.clearFilter();
  113. } else {
  114. this.filter = filter;
  115. }
  116. }
  117. if (this.groupingKey == null) this.groupingKey = this.groupingTitles().get(0);
  118. this.setShowFilterButton(!this.filter.isNotEditable());
  119. View rootView = inflater.inflate(this.getLayoutId(), container, false);
  120. this.rootView = rootView;
  121. ExpandableListView mylist = (ExpandableListView) rootView.findViewById(R.id.loglistview);
  122. this.spinner =(ProgressBar) rootView.findViewById(R.id.progressBar1);
  123. this.spinner.setVisibility(View.GONE);
  124. this.expListView = mylist;
  125. this.initialiseListView();
  126. ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.FilterButton);
  127. filterButton.setOnClickListener(new View.OnClickListener() {
  128. public void onClick(View v) {
  129. RecordOverviewFragment.this.openFilterPopupMenuOnView(v);
  130. }
  131. });
  132. filterButton.setVisibility(this.showFilterButton? View.VISIBLE : View.INVISIBLE);
  133. ImageButton sortButton = (ImageButton) rootView.findViewById(R.id.SortButton);
  134. sortButton.setOnClickListener(new View.OnClickListener() {
  135. public void onClick(View v) {
  136. // Open SortMenu
  137. RecordOverviewFragment.this.openSortingDialog();
  138. }
  139. });
  140. ImageButton groupButton = (ImageButton) rootView.findViewById(R.id.GroupButton);
  141. groupButton.setOnClickListener(new View.OnClickListener() {
  142. public void onClick(View v) {
  143. // Open SortMenu
  144. RecordOverviewFragment.this.openGroupingDialog();
  145. }
  146. });
  147. return rootView;
  148. }
  149. /**Initialises the expandable list view in a backgorund thread*/
  150. private void initialiseListView(){
  151. if (loader != null) loader.interrupt();
  152. this.spinner.setVisibility(View.VISIBLE);
  153. loader = new Thread(new Runnable(){
  154. private void updateUI(final RecordListAdapter currentAdapter)
  155. {
  156. if(loader.isInterrupted()){
  157. return;
  158. }
  159. Activity activity = RecordOverviewFragment.this.getActivity();
  160. if (activity != null){
  161. activity.runOnUiThread(new Runnable() {
  162. @Override
  163. public void run() {
  164. RecordOverviewFragment.this.expListView.setAdapter(currentAdapter);
  165. // Update view and remove loading spinner etc...
  166. RecordListAdapter adapter = (RecordListAdapter) RecordOverviewFragment.this.expListView.getExpandableListAdapter();
  167. if (adapter != null){
  168. adapter.notifyDataSetChanged();
  169. if (adapter.getGroupCount() == 1){
  170. RecordOverviewFragment.this.expListView.expandGroup(0);
  171. } else {
  172. RecordOverviewFragment.this.setSectionToOpen(RecordOverviewFragment.this.sectionToOpen);
  173. }
  174. }
  175. if (RecordOverviewFragment.this.openSections != null && RecordOverviewFragment.this.openSections.size() != 0){
  176. for (int i = 0; i < RecordOverviewFragment.this.openSections.size(); i++){
  177. int index = RecordOverviewFragment.this.openSections.get(i);
  178. RecordOverviewFragment.this.expListView.expandGroup(index);
  179. }
  180. } else {
  181. RecordOverviewFragment.this.openSections = new ArrayList<Integer>();
  182. }
  183. if (mListPosition != -1 && mItemPosition != -1)
  184. RecordOverviewFragment.this.expListView.setSelectedChild(mListPosition, mItemPosition, true);
  185. mListPosition = -1;
  186. mItemPosition = -1;
  187. registerListClickCallback(RecordOverviewFragment.this.expListView);
  188. RecordOverviewFragment.this.spinner.setVisibility(View.GONE);
  189. RecordOverviewFragment.this.actualiseFilterButton();
  190. }
  191. });
  192. }
  193. }
  194. private RecordListAdapter doInBackground()
  195. {
  196. return populateListViewFromDB(RecordOverviewFragment.this.expListView);
  197. }
  198. @Override
  199. public void run()
  200. {
  201. //RecordOverviewFragment.this.addRecordToDB(5, 10);
  202. updateUI(doInBackground());
  203. }
  204. });
  205. loader.start();
  206. this.actualiseFilterButton();
  207. }
  208. /**
  209. * Returns the Fragment layout ID
  210. * @return int The fragment layout ID
  211. * */
  212. public int getLayoutId(){
  213. return R.layout.fragment_record_list;
  214. }
  215. /**
  216. * Gets called if the user clicks on item in the filter menu.
  217. *
  218. * @param AbstractPopupItem item
  219. * */
  220. public void onFilterMenuItemSelected(AbstractPopupItem item) {
  221. String title = item.getTitle();
  222. if (item instanceof SplitPopupItem){
  223. SplitPopupItem splitItem = (SplitPopupItem)item;
  224. if (splitItem.wasRightTouch){
  225. this.openTimestampToFilterDialog();
  226. } else {
  227. this.openTimestampFromFilterDialog();
  228. }
  229. return;
  230. }
  231. if (title != null){
  232. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  233. this.openBSSIDFilterDialog();
  234. }
  235. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  236. this.openESSIDFilterDialog();
  237. }
  238. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  239. this.openProtocolsFilterDialog();
  240. }
  241. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  242. this.openSortingDialog();
  243. }
  244. if(title.equals(FILTER_MENU_TITLE_REMOVE)){
  245. this.clearFilter();
  246. this.actualiseListViewInBackground();
  247. }
  248. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  249. this.openTimestampToFilterDialog();
  250. }
  251. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  252. this.openTimestampFromFilterDialog();
  253. }
  254. }
  255. //return super.onOptionsItemSelected(item);
  256. }
  257. @Override
  258. public void onStart() {
  259. super.onStart();
  260. if (this.expListView.getExpandableListAdapter() != null){
  261. if (this.expListView.getExpandableListAdapter().getGroupCount() == 1){
  262. this.expListView.expandGroup(0);
  263. } else {
  264. this.setSectionToOpen(this.sectionToOpen);
  265. }
  266. }
  267. }
  268. @Override
  269. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  270. // Inflate the menu items for use in the action bar
  271. inflater.inflate(R.menu.records_overview_actions, menu);
  272. }
  273. @Override
  274. public boolean onOptionsItemSelected(MenuItem item) {
  275. switch (item.getItemId()) {
  276. case R.id.records_action_synchronize:
  277. AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
  278. builder.setTitle("Synchronize records");
  279. builder.setItems(new String[]{
  280. "Via Bluetooth",
  281. "Via NFC",
  282. "Via Online Database"
  283. }, new DialogInterface.OnClickListener() {
  284. @Override
  285. public void onClick(DialogInterface dialog, int position) {
  286. switch(position){
  287. case 0:
  288. getActivity().startActivity(new Intent(getActivity(), BluetoothSync.class));
  289. break;
  290. case 1:
  291. getActivity().startActivity(new Intent(getActivity(), NFCSync.class));
  292. break;
  293. case 2:
  294. getActivity().startActivity(new Intent(getActivity(), TracingSyncActivity.class));
  295. break;
  296. }
  297. }
  298. });
  299. builder.create();
  300. builder.show();
  301. return true;
  302. case R.id.records_action_export:
  303. AlertDialog.Builder builderExport = new AlertDialog.Builder(getActivity());
  304. builderExport.setTitle("Choose export format");
  305. builderExport.setItems(R.array.format, new DialogInterface.OnClickListener() {
  306. @Override
  307. public void onClick(DialogInterface dialog, int position) {
  308. RecordOverviewFragment.this.exportDatabase(position);
  309. }
  310. });
  311. builderExport.create();
  312. builderExport.show();
  313. return true;
  314. }
  315. return false;
  316. }
  317. private void exportDatabase(int format) {
  318. try {
  319. FileOutputStream log;
  320. String filename = "hostage_" + format + "_" + System.currentTimeMillis() + ".log";
  321. String externalLocation = pref.getString("pref_external_location",
  322. "");
  323. String root = Environment.getExternalStorageDirectory()
  324. .toString();
  325. if (root != null && HelperUtils.isExternalStorageWritable()) {
  326. File dir = new File(root + externalLocation);
  327. dir.mkdirs();
  328. File file = new File(dir, filename);
  329. log = new FileOutputStream(file);
  330. } else {
  331. Toast.makeText(getActivity(), "Could not write to SD Card",
  332. Toast.LENGTH_SHORT).show();
  333. return;
  334. }
  335. ArrayList<Record> records = dbh.getAllRecords();
  336. for (Record record : records) {
  337. log.write((record.toString((format == 1) ? TraCINgFormatter
  338. .getInstance() : null)).getBytes());
  339. }
  340. log.flush();
  341. log.close();
  342. Toast.makeText(
  343. getActivity(),
  344. "Exported records to " + externalLocation + filename, Toast.LENGTH_LONG)
  345. .show();
  346. } catch (Exception e) {
  347. Toast.makeText(getActivity(), "Could not write to SD Card",
  348. Toast.LENGTH_SHORT).show();
  349. e.printStackTrace();
  350. }
  351. }
  352. /*****************************
  353. *
  354. * Public API
  355. *
  356. * ***************************/
  357. /**
  358. * Group records by SSID and expand given SSID
  359. *
  360. * @param SSID the SSID
  361. */
  362. public void showDetailsForSSID(Context context, String SSID) {
  363. Log.e("RecordOverviewFragment", "Implement showDetailsForSSID!!");
  364. this.clearFilter();
  365. int ESSID_INDEX = 2;
  366. ArrayList<String> ssids = new ArrayList<String>();
  367. this.sectionToOpen = SSID;
  368. this.groupingKey = this.groupingTitles(context).get(ESSID_INDEX);
  369. }
  370. /*****************************
  371. *
  372. * ListView Stuff
  373. *
  374. * ***************************/
  375. /**
  376. * Reloads the data in the ExpandableListView for the given filter object.
  377. * @param ExpandableListView listview
  378. * */
  379. private RecordListAdapter populateListViewFromDB(ExpandableListView mylist) {
  380. HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
  381. ArrayList<Record> data = dbh.getRecordsForFilter(RecordOverviewFragment.this.filter);
  382. // Adding Items to ListView
  383. String keys[] = new String[] { RecordOverviewFragment.this.getString(R.string.RecordBSSID), RecordOverviewFragment.this.getString(R.string.RecordSSID), RecordOverviewFragment.this.getString(R.string.RecordProtocol), RecordOverviewFragment.this.getString(R.string.RecordTimestamp)};
  384. int ids[] = new int[] {R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };
  385. HashMap<String, Integer> mapping = new HashMap<String, Integer>();
  386. int i = 0;
  387. for(String key : keys){
  388. mapping.put(key, ids[i]);
  389. i++;
  390. }
  391. ArrayList<String>groupTitle = new ArrayList<String>();
  392. for (Record val : data) {
  393. // DO GROUPING IN HERE
  394. HashMap<String, String> map = new HashMap<String, String>();
  395. map.put(RecordOverviewFragment.this.getString(R.string.RecordBSSID), val.getBssid());
  396. map.put(RecordOverviewFragment.this.getString(R.string.RecordSSID), val.getSsid());
  397. map.put(RecordOverviewFragment.this.getString(R.string.RecordProtocol), val.getProtocol());
  398. map.put(RecordOverviewFragment.this.getString(R.string.RecordTimestamp),
  399. RecordOverviewFragment.this.getDateAsString(val.getTimestamp()));
  400. ExpandableListItem item = new ExpandableListItem();
  401. item.setData(map);
  402. item.setId_Mapping(mapping);
  403. item.setTag(val.getAttack_id());
  404. String groupID = RecordOverviewFragment.this.getGroupValue(val);
  405. ArrayList<ExpandableListItem> items = sectionData.get(groupID);
  406. if (items == null) {
  407. items = new ArrayList<ExpandableListItem>();
  408. sectionData.put(groupID, items);
  409. groupTitle.add(groupID);
  410. }
  411. items.add(item);
  412. }
  413. Collections.sort(groupTitle, new Comparator<String>() {
  414. @Override
  415. public int compare(String s1, String s2) {
  416. return s1.compareToIgnoreCase(s2);
  417. }
  418. });
  419. RecordListAdapter adapter = null;
  420. if (mylist.getAdapter() != null && mylist.getAdapter() instanceof RecordListAdapter){
  421. adapter = (RecordListAdapter) mylist.getAdapter();
  422. adapter.setData(sectionData);
  423. adapter.setSectionHeader(groupTitle);
  424. } else {
  425. adapter = new RecordListAdapter( RecordOverviewFragment.this.getApplicationContext(), groupTitle, sectionData);
  426. }
  427. return adapter;
  428. }
  429. /**
  430. * Actualises the list in a background thread
  431. */
  432. private void actualiseListViewInBackground(){
  433. if (loader != null && loader.isAlive()) loader.interrupt();
  434. loader = null;
  435. this.spinner.setVisibility(View.VISIBLE);
  436. this.actualiseFilterButton();
  437. loader = new Thread(new Runnable() {
  438. @Override
  439. public void run() {
  440. this.runOnUiThread(this.doInBackground());
  441. }
  442. private RecordListAdapter doInBackground(){
  443. return RecordOverviewFragment.this.populateListViewFromDB(RecordOverviewFragment.this.expListView);
  444. }
  445. private void runOnUiThread(final RecordListAdapter adapter){
  446. Activity actv = RecordOverviewFragment.this.getActivity();
  447. if (actv != null){
  448. actv.runOnUiThread(new Runnable() {
  449. @Override
  450. public void run() {
  451. this.actualiseUI();
  452. }
  453. private void actualiseUI(){
  454. if (adapter != null){
  455. RecordOverviewFragment.this.expListView.setAdapter(adapter);
  456. adapter.notifyDataSetChanged();
  457. RecordOverviewFragment.this.spinner.setVisibility(View.GONE);
  458. }
  459. }
  460. });
  461. }
  462. }
  463. });
  464. loader.start();
  465. }
  466. /**This will open a section in the ExpandableListView with the same title as the parameter s.
  467. *
  468. * @param String s (the section title to open)
  469. *
  470. * */
  471. private void setSectionToOpen(String s){
  472. this.sectionToOpen = s;
  473. if (this.sectionToOpen != null && this.sectionToOpen.length() != 0){
  474. if (this.getGroupTitles().contains(this.sectionToOpen)){
  475. int section = this.getGroupTitles().indexOf(this.sectionToOpen);
  476. this.expListView.expandGroup(section);
  477. this.sectionToOpen = "";
  478. }
  479. }
  480. }
  481. /**
  482. * Returns the base context.
  483. * @return Context baseContext
  484. * */
  485. private Context getBaseContext(){
  486. return this.getActivity().getBaseContext();
  487. }
  488. /**Returns the application context.
  489. * @return Context application context
  490. * */
  491. private Context getApplicationContext(){
  492. return this.getActivity().getApplicationContext();
  493. }
  494. /**Sets the list view listener on the given ExpandableListView.
  495. *
  496. * @param ExpandableListView listview
  497. * */
  498. private void registerListClickCallback(ExpandableListView mylist) {
  499. mylist.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  500. @Override
  501. public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
  502. RecordListAdapter adapter = (RecordListAdapter)expandableListView.getExpandableListAdapter();
  503. ExpandableListItem item = (ExpandableListItem)adapter.getChild(i,i2);
  504. mListPosition = i;
  505. mItemPosition = i2;
  506. HostageDBOpenHelper dbh = new HostageDBOpenHelper(getBaseContext());
  507. Record rec = dbh.getRecordOfAttackId((int) item.getTag());
  508. RecordOverviewFragment.this.pushRecordDetailViewForRecord(rec);
  509. return true;
  510. }
  511. });
  512. mylist.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
  513. @Override
  514. public void onGroupExpand(int i) {
  515. RecordOverviewFragment.this.openSections.add(new Integer(i));
  516. }
  517. });
  518. mylist.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
  519. @Override
  520. public void onGroupCollapse(int i) {
  521. RecordOverviewFragment.this.openSections.remove(new Integer(i));
  522. }
  523. });
  524. }
  525. /*****************************
  526. *
  527. * Date Transform
  528. *
  529. * ***************************/
  530. /**Returns the date format "H:mm d.M.yy" for the given timestamp (long)
  531. * @param Long timestamp*/
  532. @SuppressLint("SimpleDateFormat")
  533. private String getDateAsString(long timeStamp) {
  534. try {
  535. DateFormat sdf = new SimpleDateFormat("H:mm d.M.yy");
  536. Date netDate = (new Date(timeStamp));
  537. return sdf.format(netDate);
  538. } catch (Exception ex) {
  539. return "xx";
  540. }
  541. }
  542. /*****************************
  543. *
  544. * Getter / Setter
  545. *
  546. * ***************************/
  547. public boolean isShowFilterButton() {
  548. return showFilterButton;
  549. }
  550. public void setShowFilterButton(boolean showFilterButton) {
  551. this.showFilterButton = showFilterButton;
  552. }
  553. /*****************************
  554. *
  555. * Open Dialog Methods
  556. *
  557. * ***************************/
  558. /**Opens the grouping dialog*/
  559. private void openGroupingDialog(){
  560. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_GROUP, this.groupingTitles(), this.selectedGroup(), false , this);
  561. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_GROUP);
  562. }
  563. /**opens the bssid filter dialog*/
  564. private void openBSSIDFilterDialog(){
  565. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID,this.bssids(), this.selectedBSSIDs(), true , this);
  566. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  567. }
  568. /**opens the essid filter dialog*/
  569. private void openESSIDFilterDialog(){
  570. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID,this.essids(), this.selectedESSIDs(), true , this);
  571. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  572. }
  573. /**opens the protocol filter dialog*/
  574. private void openProtocolsFilterDialog(){
  575. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,this.protocolTitles(), this.selectedProtocols(), true , this);
  576. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  577. }
  578. /**opens the timestamp filter dialog (minimal timestamp required)*/
  579. private void openTimestampFromFilterDialog(){
  580. this.wasBelowTimePicker = false;
  581. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  582. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  583. if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
  584. }
  585. /**opens time timestamp filter dialog (maximal timestamp required)*/
  586. private void openTimestampToFilterDialog(){
  587. this.wasBelowTimePicker = true;
  588. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  589. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  590. if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
  591. }
  592. /**opens the sorting dialog*/
  593. private void openSortingDialog(){
  594. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_SORTING,this.sortTypeTiles(), this.selectedSorttype(), false , this);
  595. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  596. }
  597. /*****************************
  598. *
  599. * Grouping Stuff
  600. *
  601. * ***************************/
  602. /**returns the group title for the given record. Uses the groupingKey to decied which value of the record should be used.
  603. * @param Record rec
  604. * @return String grouptitle*/
  605. public String getGroupValue(Record rec){
  606. int index = this.groupingTitles().indexOf(this.groupingKey);
  607. switch (index){
  608. case 0:
  609. return rec.getProtocol();
  610. case 1:
  611. return rec.getBssid();
  612. case 2:
  613. return rec.getSsid();
  614. default:
  615. return rec.getProtocol();
  616. }
  617. }
  618. /**Returns the Group titles for the specified grouping key. e.g. groupingKey is "ESSID" it returns all available essids.
  619. * @return ArrayList<String> grouptitles*/
  620. public ArrayList<String> getGroupTitles(){
  621. int index = this.groupingTitles().indexOf(this.groupingKey);
  622. switch (index){
  623. case 0:
  624. return this.protocolTitles();
  625. case 1:
  626. return this.bssids();
  627. case 2:
  628. return this.essids();
  629. default:
  630. return this.protocolTitles();
  631. }
  632. }
  633. /*****************************
  634. *
  635. * Filter Stuff
  636. *
  637. * ***************************/
  638. /**Returns the FilterButton.
  639. * @return ImageButton filterButton*/
  640. private ImageButton getFilterButton(){
  641. return (ImageButton) this.rootView.findViewById(R.id.FilterButton);
  642. }
  643. /**Opens the filter menu on a anchor view. The filter menu will always be on top of the anchor.
  644. * @param View anchorView*/
  645. private void openFilterPopupMenuOnView(View v){
  646. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  647. public void onItemClick(Object ob) {
  648. if (ob instanceof AbstractPopupItem){
  649. AbstractPopupItem item = (AbstractPopupItem) ob;
  650. RecordOverviewFragment.this.onFilterMenuItemSelected(item);
  651. }
  652. }
  653. });
  654. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  655. for(String title : RecordOverviewFragment.this.filterMenuTitles()){
  656. AbstractPopupItem item = null;
  657. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) continue;
  658. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  659. item = new SplitPopupItem(this.getActivity());
  660. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  661. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  662. if (this.filter.hasBelowTimestamp()){
  663. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  664. }
  665. if (this.filter.hasAboveTimestamp()){
  666. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  667. }
  668. } else {
  669. item = new SimplePopupItem(this.getActivity());
  670. item.setTitle(title);
  671. ((SimplePopupItem)item).setSelected(this.isFilterSetForTitle(title));
  672. }
  673. filterMenu.addItem(item);
  674. }
  675. filterMenu.showOnView(v);
  676. }
  677. /**Returns true if the filter object is set for the given title otherwise false. e.g. the filter object has protocols,
  678. * so the method will return for the title FILTER_MENU_TITLE_PROTOCOLS TRUE.
  679. * @param String title
  680. * @return boolean value
  681. * */
  682. private boolean isFilterSetForTitle(String title){
  683. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  684. return this.filter.hasBSSIDs();
  685. }
  686. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  687. return this.filter.hasESSIDs();
  688. }
  689. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  690. return this.filter.hasProtocols();
  691. }
  692. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  693. return this.filter.hasBelowTimestamp();
  694. }
  695. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  696. return this.filter.hasAboveTimestamp();
  697. }
  698. return false;
  699. }
  700. /**clears the filter. Does not invoke populatelistview!*/
  701. private void clearFilter(){
  702. if(filter == null) this.filter = new LogFilter();
  703. this.filter.clear();
  704. }
  705. /**Returns all grouping titles
  706. * @param Context context
  707. * @return ArrayList<String> titles*/
  708. public ArrayList<String> groupingTitles(Context context){
  709. ArrayList<String> titles = new ArrayList<String>();
  710. for (String groupTitle : context.getResources().getStringArray(
  711. R.array.Grouping)) {
  712. titles.add(groupTitle);
  713. }
  714. return titles;
  715. }
  716. /**Returns all grouping titles.
  717. * @return ArrayList<String> tiles*/
  718. public ArrayList<String> groupingTitles(){
  719. ArrayList<String> titles = new ArrayList<String>();
  720. for (String groupTitle : this.getResources().getStringArray(
  721. R.array.Grouping)) {
  722. titles.add(groupTitle);
  723. }
  724. return titles;
  725. }
  726. /**
  727. * Returns a bool array. This array is true at the index of the groupingKey in groupingTitles(), otherwise false.
  728. * @return boolean[] selection
  729. * */
  730. public boolean[] selectedGroup(){
  731. ArrayList<String> groups = this.groupingTitles();
  732. boolean[] selected = new boolean[groups.size()];
  733. int i = 0;
  734. for(String group : groups){
  735. selected[i] =(group.equals(this.groupingKey));
  736. i++;
  737. }
  738. return selected;
  739. }
  740. /**Returns all protocol titles / names.
  741. * @return ArrayList<String> protocolTitles
  742. * */
  743. public ArrayList<String> protocolTitles(){
  744. ArrayList<String> titles = new ArrayList<String>();
  745. for (String protocol : this.getResources().getStringArray(
  746. R.array.protocols)) {
  747. titles.add(protocol);
  748. }
  749. return titles;
  750. }
  751. /**Return a boolean array of the selected / filtered protocols. If the filter object has
  752. * an protocol from the protocolTitles() array, the index of it will be true, otherwise false.
  753. * @return boolean[] protocol selection
  754. * */
  755. public boolean[] selectedProtocols(){
  756. ArrayList<String> protocols = this.protocolTitles();
  757. boolean[] selected = new boolean[protocols.size()];
  758. int i = 0;
  759. for(String protocol : protocols){
  760. selected[i] =(this.filter.protocols.contains(protocol));
  761. i++;
  762. }
  763. return selected;
  764. }
  765. /**
  766. * Returns the Sorttype Titles
  767. * @return ArayList<String> Sort type titles
  768. * */
  769. public ArrayList<String> sortTypeTiles(){
  770. ArrayList<String> titles = new ArrayList<String>();
  771. titles.add(MainActivity.getContext().getString(R.string.rec_time));
  772. titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
  773. titles.add(MainActivity.getContext().getString(R.string.BSSID));
  774. titles.add(MainActivity.getContext().getString(R.string.ESSID));
  775. return titles;
  776. }
  777. /**
  778. * Returns an boolean array. The array is true at the index of the selected sort type..
  779. * The index of the selected sort type is the same index in the sortTypeTiles array.
  780. * @return boolean array, length == sortTypeTiles().length
  781. * */
  782. public boolean[] selectedSorttype(){
  783. ArrayList<String> types = this.sortTypeTiles();
  784. boolean[] selected = new boolean[types.size()];
  785. int i = 0;
  786. for(String sorttype : types){
  787. selected[i] =(this.filter.sorttype.toString().equals(sorttype));
  788. i++;
  789. }
  790. return selected;
  791. }
  792. /**
  793. * Returns all unique bssids.
  794. * @return ArrayList<String>
  795. * */
  796. public ArrayList<String> bssids(){
  797. ArrayList<String> records = dbh.getUniqueBSSIDRecords();
  798. return records;
  799. }
  800. /**
  801. * Returns an boolean array. The array is true at the indices of the selected bssids.
  802. * The index of the selected bssid is the same index in the bssids() array.
  803. * @return boolean array, length == bssids().length
  804. * */
  805. public boolean[] selectedBSSIDs(){
  806. ArrayList<String> bssids = this.bssids();
  807. boolean[] selected = new boolean[bssids.size()];
  808. int i = 0;
  809. for(String bssid : bssids){
  810. selected[i] =(this.filter.BSSIDs.contains(bssid));
  811. i++;
  812. }
  813. return selected;
  814. }
  815. /**
  816. * Returns all unique essids.
  817. * @return ArrayList<String>
  818. * */
  819. public ArrayList<String> essids(){
  820. ArrayList<String> records = dbh.getUniqueESSIDRecords();
  821. return records;
  822. }
  823. /**
  824. * Returns an boolean array. The array is true at the indices of the selected essids.
  825. * The index of the selected essid is the same index in the essids() array.
  826. * @return boolean array, length == essids().length
  827. * */
  828. public boolean[] selectedESSIDs(){
  829. ArrayList<String> essids = this.essids();
  830. boolean[] selected = new boolean[essids.size()];
  831. int i = 0;
  832. for(String essid : essids){
  833. selected[i] =(this.filter.ESSIDs.contains(essid));
  834. i++;
  835. }
  836. return selected;
  837. }
  838. /**
  839. * Returns all filter menu titles.
  840. * @return ArrayList<String>
  841. * */
  842. private ArrayList<String> filterMenuTitles(){
  843. ArrayList<String> titles = new ArrayList<String>();
  844. titles.add(FILTER_MENU_TITLE_BSSID);
  845. titles.add(FILTER_MENU_TITLE_ESSID);
  846. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  847. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  848. titles.add(FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  849. if (this.filter.isSet())titles.add(FILTER_MENU_TITLE_REMOVE);
  850. return titles;
  851. }
  852. /*****************************
  853. *
  854. * Listener Actions
  855. *
  856. * ***************************/
  857. /**
  858. * Will be called if the users selects a timestamp.
  859. * @param DateTimeDialogFragment dialog
  860. * */
  861. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  862. if(this.wasBelowTimePicker){
  863. this.filter.setBelowTimestamp(dialog.getDate());
  864. } else {
  865. this.filter.setAboveTimestamp(dialog.getDate());
  866. }
  867. this.actualiseListViewInBackground();
  868. this.actualiseFilterButton();
  869. }
  870. /**
  871. * Will be called if the users cancels a timestamp selection.
  872. * @param DateTimeDialogFragment dialog
  873. * */
  874. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  875. if(this.wasBelowTimePicker){
  876. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  877. } else {
  878. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  879. }
  880. this.actualiseFilterButton();
  881. }
  882. /**
  883. * Will be called if the users clicks the positiv button on a ChechlistDialog.
  884. * @param ChecklistDialog dialog
  885. */
  886. public void onDialogPositiveClick(ChecklistDialog dialog) {
  887. String title = dialog.getTitle();
  888. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  889. ArrayList<String> titles =dialog.getSelectedItemTitles();
  890. if (titles.size() == this.bssids().size()){
  891. this.filter.setBSSIDs(new ArrayList<String>());
  892. } else {
  893. this.filter.setBSSIDs(titles);
  894. }
  895. }
  896. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  897. ArrayList<String> titles =dialog.getSelectedItemTitles();
  898. if (titles.size() == this.essids().size()){
  899. this.filter.setESSIDs(new ArrayList<String>());
  900. } else {
  901. this.filter.setESSIDs(titles);
  902. }
  903. }
  904. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  905. ArrayList<String> protocols = dialog.getSelectedItemTitles();
  906. if (protocols.size() == this.protocolTitles().size()){
  907. this.filter.setProtocols(new ArrayList<String>());
  908. } else {
  909. this.filter.setProtocols(dialog.getSelectedItemTitles());
  910. }
  911. }
  912. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  913. ArrayList<String> titles = dialog.getSelectedItemTitles();
  914. if (titles.size() == 0) return;
  915. String t = titles.get(0);
  916. int sortType = this.sortTypeTiles().indexOf(t);
  917. this.filter.setSorttype(SortType.values()[sortType]);
  918. }
  919. if (title.equals(FILTER_MENU_TITLE_GROUP)){
  920. ArrayList<String> titles = dialog.getSelectedItemTitles();
  921. if (titles.size() == 0) return;
  922. this.groupingKey = titles.get(0);
  923. }
  924. this.actualiseListViewInBackground();
  925. this.actualiseFilterButton();
  926. }
  927. /**Paints the filter button if the current filter object is set.*/
  928. private void actualiseFilterButton(){
  929. if (this.filter.isSet() ){
  930. ImageButton filterButton = this.getFilterButton();
  931. if (filterButton != null){
  932. filterButton.setImageResource(R.drawable.ic_filter_pressed);
  933. filterButton.invalidate();
  934. }
  935. } else {
  936. ImageButton filterButton = this.getFilterButton();
  937. if (filterButton != null){
  938. filterButton.setImageResource(R.drawable.ic_filter);
  939. filterButton.invalidate();
  940. }
  941. }
  942. }
  943. /**
  944. * Will be called if the users clicks the negativ button on a ChechlistDialog.
  945. * @param ChecklistDialog dialog
  946. */
  947. public void onDialogNegativeClick(ChecklistDialog dialog) {}
  948. /*****************************
  949. *
  950. * TEST
  951. *
  952. * ***************************/
  953. /**
  954. * This will clear the database at first and than add new attacks.
  955. * @param int number of networks to create
  956. * @param int maximal number of attack per network
  957. * */
  958. private void addRecordToDB( int createNetworks, int attacksPerNetwork) {
  959. if ((dbh.getRecordCount() > 0)) dbh.clearData();
  960. Calendar cal = Calendar.getInstance();
  961. int maxProtocolsIndex = this.getResources().getStringArray(
  962. R.array.protocols).length;
  963. Random random = new Random();
  964. LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
  965. final double ssidRadius = 0.1;
  966. final double bssidRadius = 0.004;
  967. int attackId = 0;
  968. for (int numOfNetworks = 0; numOfNetworks < createNetworks; numOfNetworks++){
  969. String ssidName = "WiFi" + ((numOfNetworks) + 1);
  970. String bssidName = "127.0.0." + ((numOfNetworks) + 1);
  971. int protocolIndex = numOfNetworks % maxProtocolsIndex;
  972. String protocolName = this.getResources().getStringArray(
  973. R.array.protocols)[protocolIndex];
  974. int numOfAttackPerNetwork = (Math.abs(random.nextInt()) % attacksPerNetwork) + 1;
  975. // ATTACKS PER NETWORK
  976. for (int attack = 0; attack < numOfAttackPerNetwork; attack++) {
  977. LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius + 2.0 * ssidRadius * Math.random());
  978. int numRecordsPerAttack = (Math.abs(random.nextInt()) % 5) + 1;
  979. double latitude = ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  980. double longitude = ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  981. /*
  982. * ADD A ATTACK*/
  983. AttackRecord record = new AttackRecord();
  984. record.setAttack_id(attackId);
  985. record.setBssid(bssidName);
  986. record.setProtocol(protocolName);
  987. record.setLocalIP(bssidName);
  988. dbh.addAttackRecord(record);
  989. // RECORDS PER ATTACK
  990. for (int messageID = attackId; messageID < attackId + numRecordsPerAttack; messageID++) {
  991. MessageRecord message = new MessageRecord();
  992. message.setId(messageID);
  993. message.setAttack_id(attackId);
  994. // GO BACK IN TIME
  995. message.setTimestamp(cal.getTimeInMillis()
  996. - ((messageID * 60 * 60 * 24) * 1000) + (1000 * ((messageID - attackId) + 1)));
  997. message.setType(MessageRecord.TYPE.SEND);
  998. message.setPacket("");
  999. dbh.addMessageRecord(message);
  1000. }
  1001. attackId+=numRecordsPerAttack;
  1002. }
  1003. }
  1004. // int countAllLogs = dbh.getAllRecords().size();
  1005. // int countRecords = dbh.getRecordCount();
  1006. // int countAttacks = dbh.getAttackCount();
  1007. //
  1008. // if ((countRecords == 0)) {
  1009. // Record rec = dbh.getRecordOfAttackId(0);
  1010. // Record rec2 = dbh.getRecord(0);
  1011. //
  1012. // System.out.println("" + "Could not create logs!");
  1013. // }
  1014. }
  1015. /**Navigation. Shows the record detail view for the given record
  1016. * @param Record record to show
  1017. * */
  1018. private void pushRecordDetailViewForRecord(Record record){
  1019. FragmentManager fm = this.getActivity().getFragmentManager();
  1020. if (fm != null){
  1021. RecordDetailFragment newFragment = new RecordDetailFragment();
  1022. newFragment.setRecord(record);
  1023. newFragment.setUpNavigatible(true);
  1024. MainActivity.getInstance().injectFragment(newFragment);
  1025. }
  1026. }
  1027. }