123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package de.tudarmstadt.informatik.hostage.ui;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.HashMap;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
- import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- 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.support.v4.app.FragmentTransaction;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ViewLog extends Activity {
- DatabaseHandler dbh;
- private final Context context = this;
- @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));
- 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() {
- 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.setMessage(R.string.dialog_clear_database)
- .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // Clear all Data
- dbh.clearData();
- SharedPreferences pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
- Editor editor = pref.edit();
- editor.clear();
- 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() {
- 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)));
-
- TableRow tableHeader = new TableRow(this);
- TableRow tableContent = new TableRow(this);
- tableHeader.setBackgroundResource(R.color.dark_grey);
- tableContent.setBackgroundResource(R.color.light_grey);
-
-
- for (String protocol : protocols) {
- TextView protocolName = new TextView(this);
- protocolName.setText(protocol);
- protocolName.setTextAppearance(this, android.R.style.TextAppearance_Medium);
- protocolName.setPadding(3, 0, 3, 0);
- tableHeader.addView(protocolName);
- TextView value = new TextView(this);
- value.setTextAppearance(this, android.R.style.TextAppearance_Medium);
- value.setGravity(Gravity.CENTER);
- tableContent.addView(value);
- if(protocol.equals("Total")){
- value.setText("" + dbh.getAttackCount());
- }else{
- value.setText("" + dbh.getAttackPerProtokolCount(protocol));
- }
- }
- table.addView(tableHeader);
- table.addView(tableContent);
-
- setFirstAndLastAttack();
- }
-
- private void setFirstAndLastAttack(){
- int attackCount = dbh.getRecordCount();
- if(attackCount > 0){
- SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
- Date resultdate = new Date(dbh.getRecordOfAttackId(0).getTimestamp());
- TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
- text.setText(sdf.format(resultdate));
- text = (TextView) findViewById(R.id.textLastAttackValue);
- resultdate = new Date(dbh.getRecordOfAttackId(dbh.getAttackCount() - 1).getTimestamp());
- text.setText(sdf.format(resultdate));
- } else {
- TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
- text.setText("-");
- text = (TextView) findViewById(R.id.textLastAttackValue);
- text.setText("-");
- }
- }
- }
|