Commits

littledot5566  committed 9fa34d3

Created helper function to populate Objects into ContentValues.
Moved Object specific database operations from Database.java to their respective classes.

  • Participants
  • Parent commits fe62d60

Comments (0)

Files changed (7)

File src/nctuw/littledot/localtreasure/ExpeditionActivity.java

 	private SensorManager mSM;
 	private Database mDB;
 	private SPManager mSP;
-	// private DataManager mDM;
 
 	private TextView tvInfo;
 	private MapView map;
 	// game states
 	private boolean isCalibrated = false;
 
-	// private Location lkGPS;
-	// private Location lkNetwork;
-	// private Location lkPassive;
-
 	private float mAcceler[] = new float[3];
 	private float mMagnetic[] = new float[3];
 	private MapOverlay destMarker; // destination
 		mDB = new Database(this);
 		mSP = new SPManager(this);
 
-		// tvCurLoc = (TextView) findViewById(R.id.hunt_tv_cur_location);
-		// tvDestLoc = (TextView) findViewById(R.id.hunt_tv_dest_location);
-		// tvDistance = (TextView) findViewById(R.id.hunt_tv_distance);
-		// tvBearing = (TextView) findViewById(R.id.hunt_tv_bearing);
+		// initialize map widgets
 		map = (MapView) findViewById(R.id.mapview);
 		mController = map.getController();
 
 				startMarker = new MapOverlay(getResources().getDrawable(
 						R.drawable.blue_map_marker_32_32), this));
 
-		// mAcceler = new float[3];
-		// mMagnetic = new float[3];
-
 		if (savedInstanceState != null) {
 			mExpedition = savedInstanceState.getParcelable(BUND_EXPEDITION);
-			// mDistance = savedInstanceState.getFloat(BUND_DIST);
-			// mDistanceTraveled = savedInstanceState.getFloat(BUND_DIST_TRAV);
-			// mBearing = savedInstanceState.getDouble(BUND_BEAR);
-			// mStartLocation = savedInstanceState.getParcelable(BUND_STARTLOC);
-			// mDestLocation = savedInstanceState.getParcelable(BUND_DESTLOC);
 			isCalibrated = true;
 			Echo.a(this, String.format("bundle=%s", mExpedition.toString()));
 
 		} else {
-			mExpedition = new Expedition().setStartTS(System
-					.currentTimeMillis());
+			// initialize Expedition
+			mExpedition = new Expedition();
+			mExpedition.setPID(mSP.getActiveProfileID());
+			mExpedition.setStartTS(System.currentTimeMillis());
 
 			Bundle extras = getIntent().getExtras();
-			// mDistance = extras.getInt(SelectDistanceActivity.BUNDLE_DISTANCE,
-			// 0);
 			mExpedition.setDistance(extras.getFloat(
 					SelectDistanceActivity.BUNDLE_DISTANCE, 0));
 
 			long eID = mDB.insertExpedition(mExpedition);
-			mExpedition.setID(eID);
+			mExpedition.setEID(eID);
 			mSP.setActiveExpeditionID(eID);
 
 			// randomize direction, calculate destination if current location is
 
 		mExpedition.setEndTS(System.currentTimeMillis());
 
+		// flush expedition to disk
+		mDB.updateExpedition(mExpedition);
+
 		mDB.close();
 		mLM.removeUpdates(this);
 		mSM.unregisterListener(this);
 			mExpedition.setEndTS(System.currentTimeMillis());
 			mExpedition.setPrize(Treasure.generateReward());
 
+			mSP.setActiveExpeditionID(0);
+
 			showDialog(EXPEDITION_COMPLETE_DIALOG);
 		}
 

File src/nctuw/littledot/localtreasure/MainActivity.java

 import android.widget.TextView;
 
 public class MainActivity extends Activity {
-	public static final String	BUN_PROFILE	= "bunProfile";
-	private TextView						tvInfo;
+	public static final String BUN_PROFILE = "bunProfile";
+	private TextView tvInfo;
 
-	private Database						mDB;
-	private SPManager						mSPManager;
-	private Profile							mProfile;
+	private Database mDB;
+	private SPManager mSPManager;
+	private Profile mProfile;
 
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		mProfile = mDB.queryProfile(mSPManager.getActiveProfileID());
 		Leg.a(mProfile.toString());
 
+		// check for active expeditions
+		long eID = mSPManager.getActiveExpeditionID();
+		Leg.a("Active expedition=" + eID);
+
 		tvInfo.setText(mProfile.toString());
 	}
 

File src/nctuw/littledot/localtreasure/database/Database.java

 package nctuw.littledot.localtreasure.database;
 
-import java.util.ArrayList;
-
 import nctuw.littledot.util.Leg;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
-import android.location.Location;
 
 public class Database {
 	private static final int DB_VERSION = 4;
 	public static final String KEY_PROFILE_EXPEDITIONS = "pExpeditions";
 	public static final String KEY_PROFILE_DISTANCE_TRAVELED = "pDistanceTraveled";
 
-	public static final String DBTABLE_EXPEDITION = "expedition";
-	public static final String KEY_EXPEDITION_ID = "eID";
-	public static final String KEY_EXPEDITION_DISTANCE = "eDistance";
-	public static final String KEY_EXPEDITION_TRAVELED = "eTraveled";
-	public static final String KEY_EXPEDITION_START_LAT = "eStartLat";
-	public static final String KEY_EXPEDITION_START_LONG = "eStartLong";
-	public static final String KEY_EXPEDITION_END_LAT = "eEndLat";
-	public static final String KEY_EXPEDITION_END_LONG = "eEndLong";
-	public static final String KEY_EXPEDITION_START_TIMESTAMP = "eStartTimestamp";
-	public static final String KEY_EXPEDITION_END_TIMESTAMP = "eEndTimestamp";
-	public static final String KEY_EXPEDITION_TIME = "eTime";
-	public static final String KEY_EXPEDITION_TREASURE = "eTreasure";
+	/*
+	 * public static final String DBTABLE_EXPEDITION = "expedition"; public
+	 * static final String KEY_EXPEDITION_ID = "eID"; public static final String
+	 * KEY_EXPEDITION_USER_ID = "ePID"; public static final String
+	 * KEY_EXPEDITION_DISTANCE = "eDistance"; public static final String
+	 * KEY_EXPEDITION_TRAVELED = "eTraveled"; public static final String
+	 * KEY_EXPEDITION_START_LAT = "eStartLat"; public static final String
+	 * KEY_EXPEDITION_START_LONG = "eStartLong"; public static final String
+	 * KEY_EXPEDITION_END_LAT = "eEndLat"; public static final String
+	 * KEY_EXPEDITION_END_LONG = "eEndLong"; public static final String
+	 * KEY_EXPEDITION_START_TIMESTAMP = "eStartTimestamp"; public static final
+	 * String KEY_EXPEDITION_END_TIMESTAMP = "eEndTimestamp"; public static
+	 * final String KEY_EXPEDITION_TIME = "eTime"; public static final String
+	 * KEY_EXPEDITION_TREASURE = "eTreasure";
+	 */
 
 	private static final String SQL_CREATE_TREASURE = "create table if not exists "
 			+ DBTABLE_TREASURE
 			+ KEY_PROFILE_DISTANCE_TRAVELED
 			+ " real not null default 0" + ");";
 
-	private static final String SQL_CREATE_EXPEDITION = "create table if not exists "
-			+ DBTABLE_EXPEDITION
-			+ "("
-			+ KEY_EXPEDITION_ID
-			+ " integer not null primary key autoincrement,"
-			+ KEY_EXPEDITION_DISTANCE
-			+ " real not null default 0,"
-			+ KEY_EXPEDITION_TRAVELED
-			+ " real default 0,"
-			+ KEY_EXPEDITION_START_LAT
-			+ " real,"
-			+ KEY_EXPEDITION_START_LONG
-			+ " real,"
-			+ KEY_EXPEDITION_END_LAT
-			+ " real,"
-			+ KEY_EXPEDITION_END_LONG
-			+ " real,"
-			+ KEY_EXPEDITION_START_TIMESTAMP
-			+ " integer,"
-			+ KEY_EXPEDITION_END_TIMESTAMP
-			+ " integer,"
-			+ KEY_EXPEDITION_TIME
-			+ " integer,"
-			+ KEY_EXPEDITION_TREASURE
-			+ " integer"
-			+ ");";
+	/*
+	 * private static final String SQL_CREATE_EXPEDITION =
+	 * "create table if not exists " + DBTABLE_EXPEDITION + "(" +
+	 * KEY_EXPEDITION_ID + " integer not null primary key autoincrement," +
+	 * KEY_EXPEDITION_USER_ID + " integer not null," + KEY_EXPEDITION_DISTANCE +
+	 * " real not null default 0," + KEY_EXPEDITION_TRAVELED +
+	 * " real default 0," + KEY_EXPEDITION_START_LAT + " real," +
+	 * KEY_EXPEDITION_START_LONG + " real," + KEY_EXPEDITION_END_LAT + " real,"
+	 * + KEY_EXPEDITION_END_LONG + " real," + KEY_EXPEDITION_START_TIMESTAMP +
+	 * " integer," + KEY_EXPEDITION_END_TIMESTAMP + " integer," +
+	 * KEY_EXPEDITION_TIME + " integer," + KEY_EXPEDITION_TREASURE + " integer"
+	 * + ");";
+	 */
 
 	public static final int MODE_READ = 1;
 	public static final int MODE_WRITE = 2;
 		public void onCreate(SQLiteDatabase db) {
 			db.execSQL(SQL_CREATE_TREASURE);
 			db.execSQL(SQL_CREATE_PROFILE);
-			db.execSQL(SQL_CREATE_EXPEDITION);
+			db.execSQL(Expedition.SQL_CREATE_EXPEDITION);
 
 			db.execSQL("insert into " + DBTABLE_PROFILE + "(" + KEY_PROFILE_ID
 					+ ") values (1);");
 
 				db.execSQL("drop table if exists " + DBTABLE_TREASURE + ";");
 				db.execSQL("drop table if exists " + DBTABLE_PROFILE + ";");
-				db.execSQL("drop table if exists " + DBTABLE_EXPEDITION + ";");
+				db.execSQL("drop table if exists "
+						+ Expedition.DBTABLE_EXPEDITION + ";");
 				onCreate(db);
 
 				db.setTransactionSuccessful();
 			return null;
 	}
 
-	public int queryTreasureNum(Treasure treasure) {
-		Cursor cur = rawQuery(
-				"select * from ? where ? = ? ",
-				new String[] { DBTABLE_TREASURE, KEY_TREASURE_ID,
-						Integer.toString(treasure.getDrawableId()) });
-
-		if (cur.moveToFirst())
-			return cur.getInt(cur.getColumnIndexOrThrow(KEY_TREASURE_NUM));
-		else
-			return -1;
-	}
-
-	public int queryExpeditionNum(int profileID) {
-		Cursor cur = rawQuery("select * from ? where ? = ? ", new String[] {
-				DBTABLE_PROFILE, KEY_PROFILE_ID, Integer.toString(profileID) });
-
-		if (cur.moveToFirst())
-			return cur.getInt(cur
-					.getColumnIndexOrThrow(KEY_PROFILE_EXPEDITIONS));
-		else
-			return -1;
-	}
-
-	public double queryDistanceTraveled(int profileID) {
-		Cursor cur = rawQuery("select * from ? where ? = ? ", new String[] {
-				DBTABLE_PROFILE, KEY_PROFILE_ID, Integer.toString(profileID) });
-
-		if (cur.moveToFirst())
-			return cur.getDouble(cur
-					.getColumnIndexOrThrow(KEY_PROFILE_DISTANCE_TRAVELED));
-		else
-			return -1;
-	}
+	/*
+	 * public int queryTreasureNum(Treasure treasure) { Cursor cur = rawQuery(
+	 * "select * from ? where ? = ? ", new String[] { DBTABLE_TREASURE,
+	 * KEY_TREASURE_ID, Integer.toString(treasure.getDrawableID()) });
+	 * 
+	 * if (cur.moveToFirst()) return
+	 * cur.getInt(cur.getColumnIndexOrThrow(KEY_TREASURE_NUM)); else return -1;
+	 * }
+	 * 
+	 * public int queryExpeditionNum(int profileID) { Cursor cur =
+	 * rawQuery("select * from ? where ? = ? ", new String[] { DBTABLE_PROFILE,
+	 * KEY_PROFILE_ID, Integer.toString(profileID) });
+	 * 
+	 * if (cur.moveToFirst()) return cur.getInt(cur
+	 * .getColumnIndexOrThrow(KEY_PROFILE_EXPEDITIONS)); else return -1; }
+	 * 
+	 * public double queryDistanceTraveled(int profileID) { Cursor cur =
+	 * rawQuery("select * from ? where ? = ? ", new String[] { DBTABLE_PROFILE,
+	 * KEY_PROFILE_ID, Integer.toString(profileID) });
+	 * 
+	 * if (cur.moveToFirst()) return cur.getDouble(cur
+	 * .getColumnIndexOrThrow(KEY_PROFILE_DISTANCE_TRAVELED)); else return -1; }
+	 */
 
-	public Profile queryProfile(int profileID) {
+	public Profile queryProfile(long profileID) {
 		Cursor cur = rawQuery(String.format("select * from %s where %s=%d",
 				DBTABLE_PROFILE, KEY_PROFILE_ID, profileID));
 
 	/*************/
 
 	public long insert(String table, String[] keys, Object[] values) {
+		// keys and values must match
 		if (keys.length != values.length)
 			throw new IllegalArgumentException(
 					"keys-values must be of the same length");
 
-		ContentValues kvPair = toContentValue(keys, values);
-
-		// ContentValues kvPair = new ContentValues();
-		//
-		// for (int i = 0; i < keys.length; i++) {
-		// if (values[i] instanceof String)
-		// kvPair.put(keys[i], (String) values[i]);
-		// else if (values[i] instanceof Integer)
-		// kvPair.put(keys[i], (Integer) values[i]);
-		// else if (values[i] instanceof Double)
-		// kvPair.put(keys[i], (Double) values[i]);
-		// else if (values[i] instanceof Float)
-		// kvPair.put(keys[i], (Float) values[i]);
-		// else if (values[i] instanceof Long)
-		// kvPair.put(keys[i], (Long) values[i]);
-		// else if (values[i] instanceof Short)
-		// kvPair.put(keys[i], (Short) values[i]);
-		// else if (values[i] instanceof Boolean)
-		// kvPair.put(keys[i], (Boolean) values[i]);
-		// else if (values[i] instanceof Byte)
-		// kvPair.put(keys[i], (Byte) values[i]);
-		// else if (values[i] instanceof byte[])
-		// kvPair.put(keys[i], (byte[]) values[i]);
-		// else
-		// throw new IllegalArgumentException("Type "
-		// + values[i].getClass().getName() + " is not supported.");
-		// }
-
+		ContentValues kvPair = toContentValues(keys, values);
 		long result = db.insert(table, null, kvPair);
-
 		return result;
 	}
 
 	public long insertExpedition(Expedition expedition) {
-		ArrayList<String> keys = new ArrayList<String>();
-		ArrayList<Object> vals = new ArrayList<Object>();
-		Location loc;
-
-		keys.add(KEY_EXPEDITION_DISTANCE);
-		vals.add(expedition.getDistance());
-
-		if (expedition.getTravelled() > 0) {
-			keys.add(KEY_EXPEDITION_TRAVELED);
-			vals.add(expedition.getTravelled());
-		}
-		if ((loc = expedition.getStartLoc()) != null) {
-			keys.add(KEY_EXPEDITION_START_LAT);
-			vals.add(loc.getLatitude());
-			keys.add(KEY_EXPEDITION_START_LONG);
-			vals.add(loc.getLongitude());
-		}
-		if ((loc = expedition.getDestLoc()) != null) {
-			keys.add(KEY_EXPEDITION_END_LAT);
-			vals.add(loc.getLatitude());
-			keys.add(KEY_EXPEDITION_END_LONG);
-			vals.add(loc.getLongitude());
-		}
-		if (expedition.getStartTS() > 0) {
-			keys.add(KEY_EXPEDITION_START_TIMESTAMP);
-			vals.add(expedition.getStartTS());
-		}
-		if (expedition.getEndTS() > 0) {
-			keys.add(KEY_EXPEDITION_END_TIMESTAMP);
-			vals.add(expedition.getEndTS());
-			keys.add(KEY_EXPEDITION_TIME);
-			vals.add(expedition.getEndTS() - expedition.getStartTS());
-		}
-		if (expedition.getPrize() != null) {
-			keys.add(KEY_EXPEDITION_TREASURE);
-			vals.add(expedition.getPrize().getId());
-		}
-
-		return insert(DBTABLE_EXPEDITION, (String[]) keys.toArray(),
-				vals.toArray());
+		return db.insert(Expedition.DBTABLE_EXPEDITION, null,
+				expedition.toContentValues());
 	}
 
 	/*************/
 	/** Updates **/
 	/*************/
 
-	public boolean update(String table, String[] keys, Object[] values,
+	public int update(String table, String[] keys, Object[] values,
 			String whereClause) {
+		// keys and values must match
 		if (keys.length != values.length)
 			throw new IllegalArgumentException(
 					"keys-values must be of the same length");
 
-		ContentValues kvPair = toContentValue(keys, values);
-		// new ContentValues();
-		// for (int i = 0; i < keys.length; i++) {
-		// if (values[i] instanceof String)
-		// kvPair.put(keys[i], (String) values[i]);
-		// else if (values[i] instanceof Integer)
-		// kvPair.put(keys[i], (Integer) values[i]);
-		// else if (values[i] instanceof Double)
-		// kvPair.put(keys[i], (Double) values[i]);
-		// else if (values[i] instanceof Float)
-		// kvPair.put(keys[i], (Float) values[i]);
-		// else if (values[i] instanceof Long)
-		// kvPair.put(keys[i], (Long) values[i]);
-		// else if (values[i] instanceof Short)
-		// kvPair.put(keys[i], (Short) values[i]);
-		// else if (values[i] instanceof Boolean)
-		// kvPair.put(keys[i], (Boolean) values[i]);
-		// else if (values[i] instanceof Byte)
-		// kvPair.put(keys[i], (Byte) values[i]);
-		// else if (values[i] instanceof byte[])
-		// kvPair.put(keys[i], (byte[]) values[i]);
-		// else
-		// throw new IllegalArgumentException("Type "
-		// + values[i].getClass().getName() + " is not supported.");
-		// }
-
-		boolean result = db.update(table, kvPair, whereClause, null) > 0;
+		ContentValues kvPair = toContentValues(keys, values);
+
+		int result = db.update(table, kvPair, whereClause, null);
 
 		return result;
 	}
 
-	public boolean update(String table, String key, Object value,
+	/**
+	 * Single column version of
+	 * {@link #update(String, String[], Object[], String)}.
+	 * 
+	 * @param table
+	 * @param key
+	 * @param value
+	 * @param whereClause
+	 * @return
+	 */
+	public int update(String table, String key, Object value,
 			String whereClause) {
 		return update(table, new String[] { key }, new Object[] { value },
 				whereClause);
 	}
 
 	/**
-	 * Increment the number of accumulated treasure by the specified amount.
+	 * Flushes an Expedition to database.
 	 * 
-	 * @param treasure
-	 * @param increment
+	 * @param expedition
 	 * @return
 	 */
-	public void incrementTreasure(Treasure treasure, int increment) {
-		int ret = queryTreasureNum(treasure);
-
-		if (ret == -1) {
-			// do an insert if key does not exist yet
-			insert(DBTABLE_TREASURE, new String[] { KEY_TREASURE_NAME,
-					KEY_TREASURE_NUM }, new Object[] { treasure, increment });
-		} else {
-			// do an update if key exists
-			int newNum = ret + increment;
-
-			update(DBTABLE_TREASURE, KEY_TREASURE_NUM, newNum,
-					KEY_TREASURE_NAME + " = " + treasure);
-		}
-	}
-
-	public void incrementExpeditions(int profileID, int increment) {
-		int ret = queryExpeditionNum(profileID);
+	public int updateExpedition(Expedition expedition) {
+		// WHERE eID = %l
+		String where = new StringBuilder(Expedition.KEY_EXPEDITION_ID)
+				.append("=").append(expedition.getEID()).toString();
 
-		if (ret == -1) {
-			insert(DBTABLE_PROFILE, new String[] { KEY_PROFILE_EXPEDITIONS,
-					KEY_PROFILE_DISTANCE_TRAVELED }, new Object[] { 0, 0 });
-		} else {
-			int newNum = ret + increment;
-
-			update(DBTABLE_PROFILE, KEY_PROFILE_EXPEDITIONS, newNum,
-					KEY_PROFILE_ID + "=" + profileID);
-		}
+		return db.update(Expedition.DBTABLE_EXPEDITION,
+				expedition.toContentValues(), where, null);
 	}
 
-	public void incrementDistance(int profileID, double amount) {
-		double ret = queryDistanceTraveled(profileID);
-
-		if (ret == -1) {
-			insert(DBTABLE_PROFILE, new String[] { KEY_PROFILE_EXPEDITIONS,
-					KEY_PROFILE_DISTANCE_TRAVELED }, new Object[] { 0, 0 });
-		} else {
-			double newNum = ret + amount;
-
-			update(DBTABLE_PROFILE, KEY_PROFILE_DISTANCE_TRAVELED, newNum,
-					KEY_PROFILE_ID + "=" + profileID);
-		}
-	}
+	/*
+	 * /** Increment the number of accumulated treasure by the specified amount.
+	 * 
+	 * @param treasure
+	 * 
+	 * @param increment
+	 * 
+	 * @return public void incrementTreasure(Treasure treasure, int increment) {
+	 * int ret = queryTreasureNum(treasure);
+	 * 
+	 * if (ret == -1) { // do an insert if key does not exist yet
+	 * insert(DBTABLE_TREASURE, new String[] { KEY_TREASURE_NAME,
+	 * KEY_TREASURE_NUM }, new Object[] { treasure, increment }); } else { // do
+	 * an update if key exists int newNum = ret + increment;
+	 * 
+	 * update(DBTABLE_TREASURE, KEY_TREASURE_NUM, newNum, KEY_TREASURE_NAME +
+	 * " = " + treasure); } }
+	 * 
+	 * public void incrementExpeditions(int profileID, int increment) { int ret
+	 * = queryExpeditionNum(profileID);
+	 * 
+	 * if (ret == -1) { insert(DBTABLE_PROFILE, new String[] {
+	 * KEY_PROFILE_EXPEDITIONS, KEY_PROFILE_DISTANCE_TRAVELED }, new Object[] {
+	 * 0, 0 }); } else { int newNum = ret + increment;
+	 * 
+	 * update(DBTABLE_PROFILE, KEY_PROFILE_EXPEDITIONS, newNum, KEY_PROFILE_ID +
+	 * "=" + profileID); } }
+	 * 
+	 * public void incrementDistance(int profileID, double amount) { double ret
+	 * = queryDistanceTraveled(profileID);
+	 * 
+	 * if (ret == -1) { insert(DBTABLE_PROFILE, new String[] {
+	 * KEY_PROFILE_EXPEDITIONS, KEY_PROFILE_DISTANCE_TRAVELED }, new Object[] {
+	 * 0, 0 }); } else { double newNum = ret + amount;
+	 * 
+	 * update(DBTABLE_PROFILE, KEY_PROFILE_DISTANCE_TRAVELED, newNum,
+	 * KEY_PROFILE_ID + "=" + profileID); } }
+	 */
 
 	/*************/
 	/** Deletes **/
 		return retArr;
 	}
 
-	public static ContentValues toContentValue(String[] keys, Object[] values) {
+	public static ContentValues toContentValues(String[] keys, Object[] values) {
 		if (keys.length != values.length)
 			throw new IllegalArgumentException(
 					"The number of keys("

File src/nctuw/littledot/localtreasure/database/Expedition.java

 package nctuw.littledot.localtreasure.database;
 
+import java.util.ArrayList;
 import java.util.Date;
 
+import android.content.ContentValues;
 import android.location.Location;
 import android.os.Parcel;
 import android.os.Parcelable;
 public class Expedition implements Parcelable {
 	private long eID = 0;
 
+	private long pID = 0;
 	private Location mStartLoc;
 	private Location mDestLoc;
 	private float mDistance;
 
 	private Location mLKLocation;
 
+	public static final String DBTABLE_EXPEDITION = "expedition";
+	public static final String KEY_EXPEDITION_ID = "eID";
+	public static final String KEY_EXPEDITION_USER_ID = "ePID";
+	public static final String KEY_EXPEDITION_DISTANCE = "eDistance";
+	public static final String KEY_EXPEDITION_TRAVELED = "eTraveled";
+	public static final String KEY_EXPEDITION_START_LAT = "eStartLat";
+	public static final String KEY_EXPEDITION_START_LONG = "eStartLong";
+	public static final String KEY_EXPEDITION_END_LAT = "eEndLat";
+	public static final String KEY_EXPEDITION_END_LONG = "eEndLong";
+	public static final String KEY_EXPEDITION_START_TIMESTAMP = "eStartTimestamp";
+	public static final String KEY_EXPEDITION_END_TIMESTAMP = "eEndTimestamp";
+	public static final String KEY_EXPEDITION_TIME = "eTime";
+	public static final String KEY_EXPEDITION_TREASURE = "eTreasure";
+
+	public static final String SQL_CREATE_EXPEDITION = "create table if not exists "
+			+ DBTABLE_EXPEDITION
+			+ "("
+			+ KEY_EXPEDITION_ID
+			+ " integer not null primary key autoincrement,"
+			+ KEY_EXPEDITION_USER_ID
+			+ " integer not null,"
+			+ KEY_EXPEDITION_DISTANCE
+			+ " real not null default 0,"
+			+ KEY_EXPEDITION_TRAVELED
+			+ " real default 0,"
+			+ KEY_EXPEDITION_START_LAT
+			+ " real,"
+			+ KEY_EXPEDITION_START_LONG
+			+ " real,"
+			+ KEY_EXPEDITION_END_LAT
+			+ " real,"
+			+ KEY_EXPEDITION_END_LONG
+			+ " real,"
+			+ KEY_EXPEDITION_START_TIMESTAMP
+			+ " integer,"
+			+ KEY_EXPEDITION_END_TIMESTAMP
+			+ " integer,"
+			+ KEY_EXPEDITION_TIME
+			+ " integer,"
+			+ KEY_EXPEDITION_TREASURE
+			+ " integer"
+			+ ");";
+
 	public Expedition() {
 	}
 
 		return sb.toString();
 	}
 
-	public long getID() {
+	public ContentValues toContentValues() {
+		ArrayList<String> keys = new ArrayList<String>();
+		ArrayList<Object> vals = new ArrayList<Object>();
+
+		keys.add(KEY_EXPEDITION_USER_ID);
+		vals.add(pID);
+
+		keys.add(KEY_EXPEDITION_DISTANCE);
+		vals.add(mDistance);
+
+		if (mTravelled > 0) {
+			keys.add(KEY_EXPEDITION_TRAVELED);
+			vals.add(mTravelled);
+		}
+		if (mStartLoc != null) {
+			keys.add(KEY_EXPEDITION_START_LAT);
+			vals.add(mStartLoc.getLatitude());
+			keys.add(KEY_EXPEDITION_START_LONG);
+			vals.add(mStartLoc.getLongitude());
+		}
+		if (mDestLoc != null) {
+			keys.add(KEY_EXPEDITION_END_LAT);
+			vals.add(mDestLoc.getLatitude());
+			keys.add(KEY_EXPEDITION_END_LONG);
+			vals.add(mDestLoc.getLongitude());
+		}
+		if (mStartTS > 0) {
+			keys.add(KEY_EXPEDITION_START_TIMESTAMP);
+			vals.add(mStartTS);
+		}
+		if (mEndTS > 0) {
+			keys.add(KEY_EXPEDITION_END_TIMESTAMP);
+			vals.add(mEndTS);
+			keys.add(KEY_EXPEDITION_TIME);
+			vals.add(mStartTS - mEndTS);
+		}
+		if (mPrize != null) {
+			keys.add(KEY_EXPEDITION_TREASURE);
+			vals.add(mPrize.getID());
+		}
+		return Database.toContentValues((String[]) keys.toArray(),
+				vals.toArray());
+	}
+
+	public long getEID() {
 		return eID;
 	}
 
-	public Expedition setID(long eID) {
+	public Expedition setEID(long eID) {
 		this.eID = eID;
 		return this;
 	}
 
+	public long getPID() {
+		return pID;
+	}
+
+	public Expedition setPID(long pID) {
+		this.pID = pID;
+		return this;
+	}
+
 	public Location getStartLoc() {
 		return mStartLoc;
 	}

File src/nctuw/littledot/localtreasure/database/Profile.java

 package nctuw.littledot.localtreasure.database;
 
-import java.util.ArrayList;
-
 import android.os.Parcel;
 import android.os.Parcelable;
 
-
 public class Profile implements Parcelable {
-	private int			mProfileID;
-	private String	mName;
-	private long		mTotalExpeditions;
-	private double	mTotalDistance;
+	private long mProfileID;
+	private String mName;
+	private long mTotalExpeditions;
+	private double mTotalDistance;
 
 	public Profile() {
 	}
 		mTotalDistance = distance;
 	}
 
-	public int getProfileID() {
+	public long getProfileID() {
 		return mProfileID;
 	}
 
-	public void setProfileID(int mProfileID) {
+	public void setProfileID(long mProfileID) {
 		this.mProfileID = mProfileID;
 	}
 
 	}
 
 	public void writeToParcel(Parcel dest, int flags) {
-		dest.writeInt(mProfileID);
+		dest.writeLong(mProfileID);
 		dest.writeString(mName);
 		dest.writeLong(mTotalExpeditions);
 		dest.writeDouble(mTotalDistance);
 	}
 
-	public static final Parcelable.Creator<Profile>	CREATOR	= new Parcelable.Creator<Profile>() {
-																														public Profile createFromParcel(
-																																Parcel in) {
-																															return new Profile(
-																																	in);
-																														}
+	public static final Parcelable.Creator<Profile> CREATOR = new Parcelable.Creator<Profile>() {
+		public Profile createFromParcel(
+				Parcel in) {
+			return new Profile(
+					in);
+		}
 
-																														public Profile[] newArray(
-																																int size) {
-																															return new Profile[size];
-																														}
-																													};
+		public Profile[] newArray(
+				int size) {
+			return new Profile[size];
+		}
+	};
 
 	private Profile(Parcel in) {
-		mProfileID = in.readInt();
+		mProfileID = in.readLong();
 		mName = in.readString();
 		mTotalExpeditions = in.readLong();
 		mTotalDistance = in.readDouble();

File src/nctuw/littledot/localtreasure/database/SPManager.java

 		// editor.commit();
 	}
 
-	public int getActiveProfileID() {
-		return mPrefs.getInt(KEY_ACTIVE_PROFILE_ID, 1);
+	public long getActiveProfileID() {
+		return mPrefs.getLong(KEY_ACTIVE_PROFILE_ID, 1);
 	}
 
-	public void setActiveProfileID(int id) {
-		mEditor.putInt(KEY_ACTIVE_PROFILE_ID, id);
+	public void setActiveProfileID(long id) {
+		mEditor.putLong(KEY_ACTIVE_PROFILE_ID, id);
 		mEditor.commit();
 	}
 

File src/nctuw/littledot/localtreasure/database/Treasure.java

 		mName = n;
 	}
 
-	public int getId() {
+	public int getID() {
 		return mID;
 	}
 
-	public int getDrawableId() {
+	public int getDrawableID() {
 		return mDrawableID;
 	}