RecordOverviewFragment.java 38 KB

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