Browse Source

Add possibility to delete only selected parts of the database(by Date
and by BSSID)

lp-tu 10 years ago
parent
commit
38133c0b27

+ 6 - 0
res/values/export_formats.xml → res/values/arrays.xml

@@ -5,5 +5,11 @@
         <item>DEFAULT</item>
         <item>JSON</item>
     </string-array>
+    
+    <string-array name="delete_criteria">
+        <item>BSSID</item>
+        <item>Date</item>
+        <item>All</item>
+    </string-array>
 
 </resources>

+ 4 - 0
res/values/strings.xml

@@ -21,8 +21,12 @@
 	<string name="firstAttack">First Attack:</string>
 	<string name="lastAttack">Last Attack:</string>
 	<string name="clear">Clear</string>
+	<string name="delete">Delete</string>
 	<string name="cancel">Cancel</string>
 	<string name="dialog_clear_database">Clear all data?</string>	
 	<string name="export_dialog_title">Choose Export Format</string>		
+	<string name="delete_dialog_title">Delete data sets by:</string>	
+	<string name="dialog_clear_database_date">Delete all data before:</string>
+	
 
 </resources>

+ 32 - 0
src/de/tudarmstadt/informatik/hostage/logging/DatabaseHandler.java

@@ -266,6 +266,38 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return result > 0;        
     }
     
+    public String[] getAllBSSIDS(){
+        String selectQuery = "SELECT  * FROM " + TABLE_RECORDS + " GROUP BY " + KEY_BSSID;
+        SQLiteDatabase db = this.getReadableDatabase();
+        Cursor cursor = db.rawQuery(selectQuery, null);
+        String[] bssidList = new String[cursor.getCount()];
+        int counter = 0;
+        // looping through all rows and adding to list
+		if (cursor.moveToFirst()) {
+			do {
+				bssidList[counter] = cursor.getString(11);
+			} while (cursor.moveToNext());
+		}       
+        cursor.close();
+ 
+        // return count
+        db.close();
+        return bssidList;
+    }
+    
+    public void deleteByBSSID(String bssid){
+    	SQLiteDatabase db = this.getReadableDatabase();
+    	db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[]{bssid});
+    	db.close();
+    }
+    
+    public void deleteByDate(long date){
+    	SQLiteDatabase db = this.getReadableDatabase();
+    	String deleteQuery = "DELETE  FROM " + TABLE_RECORDS + " WHERE " + KEY_TIME + " < " + date;
+    	db.execSQL(deleteQuery);
+    	db.close();
+    }
+    
     //Delete all Data from Database
     public void clearData(){
     	SQLiteDatabase db = this.getReadableDatabase();

+ 200 - 80
src/de/tudarmstadt/informatik/hostage/ui/ViewLog.java

@@ -3,6 +3,7 @@ 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 java.util.HashMap;
 
@@ -12,6 +13,8 @@ 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;
@@ -25,16 +28,19 @@ import android.view.Gravity;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
+import android.widget.DatePicker;
 import android.widget.LinearLayout;
 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 Context context = this;
+	private final SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
 
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
@@ -49,126 +55,240 @@ public class ViewLog extends Activity {
 		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);
-	    }
+		// 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 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){
+
+	public void uploadDatabase(View view) {
 		SQLLogger log = new SQLLogger(this);
 		log.uploadDatabase();
 	}
-	
-	public void showLog(View view){
+
+	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();
+	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() {
+					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() {
+
+		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() {
+							@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() {
+							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();
+		builder.setTitle(R.string.delete_dialog_title);
+		builder.setItems(bssidArray, new DialogInterface.OnClickListener() {
+			@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() {
+							@SuppressLint("NewApi")
+							public void onClick(DialogInterface dialog, int id) {
+								// Clear all Data
+								dbh.clearData();
+								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.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")){
+			if (protocol.equals("Total")) {
 				value.setText("" + dbh.getAttackCount());
-			}else{
+			} else {
 				value.setText("" + dbh.getAttackPerProtokolCount(protocol));
-			}		
+			}
 		}
 		table.addView(tableHeader);
-		table.addView(tableContent);	
-		
+		table.addView(tableContent);
+
 		setFirstAndLastAttack();
 	}
-	
-	private void 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));
+		if (attackCount > 0) {
+
+			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("-");
+			TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
+			text.setText("-");
+			text = (TextView) findViewById(R.id.textLastAttackValue);
+			text.setText("-");
 		}
 	}
 }