package com.baclock.entity; import android.provider.BaseColumns; /**
* Created by Jack on 5/4/2016.
*/
public final class DBEntity {
// To prevent someone from accidentally instantiating the LocationTrackEntity class,
// give it an empty constructor.
public DBEntity() {}; public static abstract class SyncData implements BaseColumns {
public static final String TABLE_NAME = "syncdata";
public static final String COLUMN_NAME_ACTION_TYPE = "action_type";
public static final String COLUMN_NAME_TOKEN = "token";
public static final String COLUMN_NAME_TOKEN_SECRET = "token_secret";
public static final String COLUMN_NAME_LATITUDE = "latitude";
public static final String COLUMN_NAME_LONGITUDE = "longitude";
public static final String COLUMN_NAME_IP_ADDRESS = "ip";
public static final String COLUMN_NAME_HOST_NAME = "host_name";
public static final String COLUMN_NAME_REASON_START = "reason_start";
public static final String COLUMN_NAME_REASON_END = "reason_end";
public static final String COLUMN_NAME_REASON = "reason";
public static final String COLUMN_NAME_LOCAL_DATE = "local_date";
public static final String COLUMN_NAME_SERVICE = "service";
}
}
package com.baclock.utils; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import com.baclock.entity.DBEntity.*; /**
* Created by Jack on 5/4/2016.
*/
public class DBHelper extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "BAClock.db"; private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ","; private static final String SQL_CREATE_SYNC_DATA =
"CREATE TABLE " + SyncData.TABLE_NAME + " (" +
SyncData._ID + " INTEGER PRIMARY KEY," +
SyncData.COLUMN_NAME_ACTION_TYPE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_TOKEN + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_TOKEN_SECRET + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_LATITUDE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_LONGITUDE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_IP_ADDRESS + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_HOST_NAME + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_REASON_START + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_REASON_END + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_REASON + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_LOCAL_DATE + TEXT_TYPE + COMMA_SEP +
SyncData.COLUMN_NAME_SERVICE + TEXT_TYPE +
" )"; private static final String SQL_DELETE_SYNC_DATA =
"DROP TABLE IF EXISTS " + SyncData.TABLE_NAME; public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_SYNC_DATA);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_SYNC_DATA);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
public static DBHelper dbHelper = null;
public static SQLiteDatabase dbReader = null;
public static SQLiteDatabase dbWriter = null; try {
dbHelper = new DBHelper(getBaseContext());
dbWriter = dbHelper.getWritableDatabase();
dbReader = dbHelper.getReadableDatabase();
} catch (Exception e) {
e.printStackTrace();
} // Read data
try {
String[] scheduled_interval = {
DBEntity.ScheduledInterval._ID,
DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_INTERVAL,
DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_FROM,
DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_TO
}; Cursor cursor = MainActivity.dbReader.query(
DBEntity.ScheduledInterval.TABLE_NAME, // The table to query
scheduled_interval, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
DBEntity.ScheduledInterval._ID // The sort order
); if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
FrequencyEntity entity = new FrequencyEntity();
entity.setScheduledInterval(cursor.getInt(cursor.getColumnIndex(DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_INTERVAL)));
entity.setScheduledFrom(cursor.getString(cursor.getColumnIndex(DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_FROM)));
entity.setScheduledTo(cursor.getString(cursor.getColumnIndex(DBEntity.ScheduledInterval.COLUMN_NAME_SCHEDULED_TO)));
MainActivity.frequencyArrayList.add(entity);
}
}
} catch (Exception e) {
e.printStackTrace();
} // Insert Data
ContentValues params = new ContentValues();
params.put(DBEntity.SyncData.COLUMN_NAME_ACTION_TYPE, "");
params.put(DBEntity.SyncData.COLUMN_NAME_TOKEN, PersistenceUtils.getSharedInstance().get("token", ""));
params.put(DBEntity.SyncData.COLUMN_NAME_TOKEN_SECRET, PersistenceUtils.getSharedInstance().get("token_secret", ""));
if ("".equals(AppUtils.getSharedInstance().getIp()))
params.put(DBEntity.SyncData.COLUMN_NAME_IP_ADDRESS, AppUtils.getSharedInstance().getLocalIP(this));
else
params.put(DBEntity.SyncData.COLUMN_NAME_IP_ADDRESS, AppUtils.getSharedInstance().getIp());
params.put(DBEntity.SyncData.COLUMN_NAME_HOST_NAME, AppUtils.getSharedInstance().getPhoneName()); if (GPSLocationManager.getInstance().getMyLocation() != null) {
params.put(DBEntity.SyncData.COLUMN_NAME_LATITUDE, GPSLocationManager.getInstance().getMyLocation().getLatitude());
params.put(DBEntity.SyncData.COLUMN_NAME_LONGITUDE, GPSLocationManager.getInstance().getMyLocation().getLongitude());
} else {
params.put(DBEntity.SyncData.COLUMN_NAME_LATITUDE, 0);
params.put(DBEntity.SyncData.COLUMN_NAME_LONGITUDE, 0);
}
params.put(DBEntity.SyncData.COLUMN_NAME_REASON_START, "");
params.put(DBEntity.SyncData.COLUMN_NAME_REASON_END, "");
params.put(DBEntity.SyncData.COLUMN_NAME_REASON, ""); SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date curDate0 = new Date();
String date0 = sdf0.format(curDate0); SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm:ss aa", Locale.US);
String time = sdf1.format(curDate0); SimpleDateFormat sdf2 = new SimpleDateFormat("E, MMM dd", Locale.US);
String date1 = sdf2.format(curDate0); Log.e("date", date1 + " " + time); params.put(DBEntity.SyncData.COLUMN_NAME_LOCAL_DATE, date0);
params.put(DBEntity.SyncData.COLUMN_NAME_SERVICE, "ClockIn");
long newRowId = dbWriter.insert(DBEntity.SyncData.TABLE_NAME, null, params); // Delete Data
if (rowid > 0) {
// Define 'where' part of query.
String selection = DBEntity.ScheduledData._ID + " = ?";
// Specify arguments in placeholder order.
String[] selectionArgs = { String.valueOf(rowid) };
// Issue SQL statement.
MainActivity.dbWriter.delete(DBEntity.ScheduledData.TABLE_NAME, selection, selectionArgs);
}