RecordOverviewFragment.java 43 KB

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