RecordOverviewFragment.java 38 KB

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