Преглед на файлове

moved current device in db helper

Julien Clauter преди 10 години
родител
ревизия
a25d8c43f0

+ 1 - 46
src/de/tudarmstadt/informatik/hostage/logging/SyncDevice.java

@@ -44,57 +44,12 @@ public class SyncDevice {
     public long getLast_sync_timestamp(){return this.last_sync_timestamp;}
 
 
-    public static  SyncDevice thisDevice = null;
     /**
      * Returns a SyncDevice Object representing the current device.
      * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
      */
     public static SyncDevice currentDevice()
     {
-        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getContext());
-        int attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
-
-        // IF THE SHARED INSTANCE IS NOT AVAILABLE GET IT
-        if (thisDevice == null){
-            String deviceUUID = pref.getString("CURRENT_DEVICE_IDENTIFIER", UUID.randomUUID().toString());
-
-            String selectQuery = "SELECT * FROM " + HostageDBContract.SyncDeviceEntry.TABLE_NAME + " D "
-                    + " WHERE " + " D." + HostageDBContract.SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " = " + "'"+deviceUUID+"'";
-            HostageDBOpenHelper dbh = new HostageDBOpenHelper(MainActivity.context);
-
-            SQLiteDatabase db = dbh.getReadableDatabase();
-            Cursor cursor = db.rawQuery(selectQuery, null);
-
-            // IF WE ALREADY HAVE A SYNC DEVICE FOR THE GIVEN DEVICE UUID
-            if (cursor.moveToFirst()) {
-                SyncDevice record = new SyncDevice();
-                record.setDeviceID(cursor.getString(0));
-                record.setLast_sync_timestamp(cursor.getLong(1));
-                record.setHighest_attack_id(cursor.getLong(2));
-                thisDevice = record;
-
-            } else {
-            // CREATE A NEW SYNC DEVICE
-                thisDevice = new SyncDevice();
-                // ITS IMPORTANT TO CREATE A COMPLETE NEW DEVICE UUID
-                deviceUUID = UUID.randomUUID().toString();
-                thisDevice.setDeviceID(deviceUUID);
-                SharedPreferences.Editor editor = pref.edit();
-                editor.putString("CURRENT_DEVICE_IDENTIFIER", thisDevice.getDeviceID());
-                editor.commit();
-                thisDevice.setLast_sync_timestamp(0);
-                thisDevice.setHighest_attack_id(-1);
-                ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
-                devices.add(thisDevice);
-                dbh.insertSyncDevices(devices);
-            }
-            cursor.close();
-
-            // return record list
-            db.close();
-        }
-
-        thisDevice.setHighest_attack_id(attack_id - 1);
-        return thisDevice;
+        return HostageDBOpenHelper.currentDevice();
     }
 }

+ 63 - 0
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -4,13 +4,16 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.UUID;
 
 import android.content.ContentValues;
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.CursorIndexOutOfBoundsException;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
+import android.preference.PreferenceManager;
 import android.util.Log;
 import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
 import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
@@ -28,6 +31,7 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEnt
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
+import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
 import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
 import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
@@ -139,6 +143,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	public HostageDBOpenHelper(Context context) {
 		super(context, DATABASE_NAME, null, DATABASE_VERSION);
 		this.context = context;
+
+        this.generateCurrentDevice();
 	}
 
 	@Override
@@ -162,6 +168,63 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		onCreate(db);
 	}
 
+    private static  SyncDevice thisDevice = null;
+    public static SyncDevice currentDevice(){
+        return thisDevice;
+    }
+    /**
+     * Returns a SyncDevice Object representing the current device.
+     * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
+     */
+    public SyncDevice generateCurrentDevice()
+    {
+        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this.context);
+        int attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
+
+        // IF THE SHARED INSTANCE IS NOT AVAILABLE GET IT
+        if (thisDevice == null){
+            String deviceUUID = pref.getString("CURRENT_DEVICE_IDENTIFIER", UUID.randomUUID().toString());
+
+            String selectQuery = "SELECT * FROM " + HostageDBContract.SyncDeviceEntry.TABLE_NAME + " D "
+                    + " WHERE " + " D." + HostageDBContract.SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " = " + "'"+deviceUUID+"'";
+            //HostageDBOpenHelper dbh = new HostageDBOpenHelper(MainActivity.context);
+
+            SQLiteDatabase db = this.getReadableDatabase();
+            Cursor cursor = db.rawQuery(selectQuery, null);
+
+            // IF WE ALREADY HAVE A SYNC DEVICE FOR THE GIVEN DEVICE UUID
+            if (cursor.moveToFirst()) {
+                SyncDevice record = new SyncDevice();
+                record.setDeviceID(cursor.getString(0));
+                record.setLast_sync_timestamp(cursor.getLong(1));
+                record.setHighest_attack_id(cursor.getLong(2));
+                thisDevice = record;
+
+            } else {
+                // CREATE A NEW SYNC DEVICE
+                thisDevice = new SyncDevice();
+                // ITS IMPORTANT TO CREATE A COMPLETE NEW DEVICE UUID
+                deviceUUID = UUID.randomUUID().toString();
+                thisDevice.setDeviceID(deviceUUID);
+                SharedPreferences.Editor editor = pref.edit();
+                editor.putString("CURRENT_DEVICE_IDENTIFIER", thisDevice.getDeviceID());
+                editor.commit();
+                thisDevice.setLast_sync_timestamp(0);
+                thisDevice.setHighest_attack_id(-1);
+                ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
+                devices.add(thisDevice);
+                this.insertSyncDevices(devices);
+            }
+            cursor.close();
+
+            // return record list
+            db.close();
+        }
+
+        thisDevice.setHighest_attack_id(attack_id - 1);
+        return thisDevice;
+    }
+
 	/**
 	 * Adds a given {@link MessageRecord} to the database.
 	 *