RecordOverviewFragment.java 43 KB

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