123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- package de.tudarmstadt.informatik.hostage.ui;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Date;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
- import de.tudarmstadt.informatik.hostage.logging.Record;
- import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.DatePickerDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Build;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.DatePicker;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.TimePicker;
- import android.widget.Toast;
- public class ViewLog extends Activity {
- DatabaseHandler dbh;
- private final Context context = this;
- private final SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_viewlog);
- dbh = new DatabaseHandler(getApplicationContext());
- initStatistic();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle item selection
- switch (item.getItemId()) {
- case R.id.action_settings:
- startActivity(new Intent(this, SettingsActivity.class));
- break;
- case R.id.action_about:
- startActivity(new Intent(this, AboutActivity.class));
- break;
- default:
- }
- return super.onOptionsItemSelected(item);
- }
- public void exportDatabase(View view) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(R.string.export_dialog_title);
- builder.setItems(R.array.format, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int position) {
- SQLLogger logger = new SQLLogger(context);
- logger.exportDatabase(position);
- }
- });
- builder.create();
- builder.show();
- }
- public void uploadDatabase(View view) {
- SQLLogger log = new SQLLogger(this);
- log.uploadDatabase();
- }
- public void showLog(View view) {
- startActivity(new Intent(this, ViewLogTable.class));
- }
- @SuppressLint("NewApi")
- public void deleteLog(View view) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(R.string.delete_dialog_title);
- builder.setItems(R.array.delete_criteria,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int position) {
- switch (position) {
- case 0:
- deleteByBSSID();
- break;
- case 1:
- deleteByDate();
- break;
- case 2:
- deleteAll();
- }
- }
- });
- builder.create();
- builder.show();
- }
- private void deleteByDate() {
- showDialog(0);
- }
- private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
- @Override
- public void onDateSet(DatePicker view, int year, int monthOfYear,
- int dayOfMonth) {
- deleteByDate(year, monthOfYear, dayOfMonth);
- }
- };
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case 0:
- final Calendar cal = Calendar.getInstance();
- int pYear = cal.get(Calendar.YEAR);
- int pMonth = cal.get(Calendar.MONTH);
- int pDay = cal.get(Calendar.DAY_OF_MONTH);
- return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
- pDay);
- }
- return null;
- }
- private void deleteByDate(int year, int monthOfYear, int dayOfMonth) {
- TimePicker timePicker = new TimePicker(context);
- final Calendar calendar = Calendar.getInstance();
- calendar.set(year, monthOfYear, dayOfMonth,
- timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(R.string.dialog_clear_database_date)
- .setMessage(sdf.format(calendar.getTime()))
- .setPositiveButton(R.string.delete,
- new DialogInterface.OnClickListener() {
- @Override
- @SuppressLint("NewApi")
- public void onClick(DialogInterface dialog, int id) {
- long time = calendar.getTimeInMillis();
- // Delete Data
- dbh.deleteByDate(time);
- Toast.makeText(getApplicationContext(),
- "Data sets deleted!",
- Toast.LENGTH_SHORT).show();
- // Recreate the activity
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- recreate();
- } else {
- Intent intent = getIntent();
- finish();
- startActivity(intent);
- }
- }
- })
- .setNegativeButton(R.string.cancel,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int id) {
- // User cancelled the dialog
- }
- });
- // Create the AlertDialog object
- builder.create();
- builder.show();
- }
- private void deleteByBSSID() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- final String[] bssidArray = dbh.getAllBSSIDS();
- for(int i = 0; i < bssidArray.length; i++){
- bssidArray[i] = bssidArray[i] + " (" + dbh.getSSID(bssidArray[i]) +")";
- }
- builder.setTitle(R.string.delete_dialog_title);
- builder.setItems(bssidArray, new DialogInterface.OnClickListener() {
- @Override
- @SuppressLint("NewApi")
- public void onClick(DialogInterface dialog, int position) {
- dbh.deleteByBSSID(bssidArray[position]);
- Toast.makeText(
- getApplicationContext(),
- "All entries with bssid '" + bssidArray[position]
- + "' deleted.", Toast.LENGTH_SHORT).show();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- recreate();
- } else {
- Intent intent = getIntent();
- finish();
- startActivity(intent);
- }
- }
- });
- builder.create();
- builder.show();
- }
- private void deleteAll() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage(R.string.dialog_clear_database)
- .setPositiveButton(R.string.clear,
- new DialogInterface.OnClickListener() {
- @Override
- @SuppressLint("NewApi")
- public void onClick(DialogInterface dialog, int id) {
- // Clear all Data
- dbh.clearData();
- SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
- Editor editor = pref.edit();
- editor.putInt("ATTACK_ID_COUNTER", 0);
- editor.putInt("LAST_UPLOADED_ATTACK_ID", -1);
- editor.commit();
- Toast.makeText(getApplicationContext(),
- "Database cleared!", Toast.LENGTH_SHORT)
- .show();
- // Recreate the activity
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- recreate();
- } else {
- Intent intent = getIntent();
- finish();
- startActivity(intent);
- }
- }
- })
- .setNegativeButton(R.string.cancel,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int id) {
- // User cancelled the dialog
- }
- });
- // Create the AlertDialog object
- builder.create();
- builder.show();
- }
- private void initStatistic() {
- TableLayout table = (TableLayout) findViewById(R.id.layoutContainer);
- ArrayList<String> protocols = new ArrayList<String>();
- protocols.add("Total");
- protocols.addAll(Arrays.asList(getResources().getStringArray(
- R.array.protocols)));
- for (String protocol : protocols) {
- TableRow row = new TableRow(this);
- TextView protocolName = new TextView(this);
- protocolName.setBackgroundResource(R.color.dark_grey);
- protocolName.setText(protocol);
- protocolName.setTextAppearance(this,
- android.R.style.TextAppearance_Medium);
- protocolName.setPadding(6, 0, 0, 0);
- row.addView(protocolName);
- TextView value = new TextView(this);
- value.setBackgroundResource(R.color.light_grey);
- value.setTextAppearance(this, android.R.style.TextAppearance_Medium);
- value.setGravity(Gravity.RIGHT);
- value.setPadding(3, 0, 3, 0);
- row.addView(value);
- if (protocol.equals("Total")) {
- value.setText("" + dbh.getAttackCount());
- } else {
- value.setText("" + dbh.getAttackPerProtokolCount(protocol));
- }
- table.addView(row);
- }
- setFirstAndLastAttack();
- }
- private void setFirstAndLastAttack() {
- Record firstAttack = dbh.getRecordOfAttackId(dbh.getSmallestAttackId());
- Record lastAttack = dbh.getRecordOfAttackId(dbh.getHighestAttackId());
- if (firstAttack != null) {
- Date resultdate = new Date(firstAttack.getTimestamp());
- TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
- text.setText(sdf.format(resultdate));
- text = (TextView) findViewById(R.id.textLastAttackValue);
- resultdate = new Date(lastAttack.getTimestamp());
- text.setText(sdf.format(resultdate));
- } else {
- TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
- text.setText("-");
- text = (TextView) findViewById(R.id.textLastAttackValue);
- text.setText("-");
- }
- }
- }
|