RecordOverviewFragment.java 49 KB

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