RecordOverviewFragment.java 43 KB

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