use of android.database.sqlite.SQLiteOpenHelper in project symmetric-ds by JumpMind.
the class SymmetricService method onStartCommand.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (symmetricEngine == null) {
try {
Log.i(TAG, "creating engine");
String key = intent.getStringExtra(INTENTKEY_SQLITEOPENHELPER_REGISTRY_KEY);
if (key != null) {
SQLiteOpenHelper databaseHelper = SQLiteOpenHelperRegistry.lookup(key);
if (databaseHelper != null) {
String registrationUrl = intent.getStringExtra(INTENTKEY_REGISTRATION_URL);
String externalId = intent.getStringExtra(INTENTKEY_EXTERNAL_ID);
String nodeGroupId = intent.getStringExtra(INTENTKEY_NODE_GROUP_ID);
Serializable passedInProps = (Serializable) intent.getSerializableExtra(INTENTKEY_PROPERTIES);
Properties properties = null;
if (passedInProps instanceof Properties) {
properties = (Properties) passedInProps;
} else if (passedInProps instanceof Map) {
properties = new Properties();
@SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) passedInProps;
for (String propKey : map.keySet()) {
properties.setProperty(propKey, map.get(propKey));
}
} else {
properties = new Properties();
}
symmetricEngine = new AndroidSymmetricEngine(registrationUrl, externalId, nodeGroupId, properties, databaseHelper, getApplicationContext());
Log.i(TAG, "engine created");
} else {
Log.e(TAG, "Could not find SQLiteOpenHelper in registry using " + key);
}
} else {
Log.e(TAG, "Must provide valid " + INTENTKEY_SQLITEOPENHELPER_REGISTRY_KEY);
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage(), ex);
}
}
if (symmetricEngine != null) {
if (!symmetricEngine.isStarted() && !symmetricEngine.isStarting()) {
try {
Runnable runnable = new Runnable() {
public void run() {
try {
Log.i(TAG, "starting engine");
symmetricEngine.start();
Log.i(TAG, "engine started");
} catch (Exception ex) {
Log.e(TAG, ex.getMessage(), ex);
}
}
};
boolean startInBackground = intent.getBooleanExtra(INTENTKEY_START_IN_BACKGROUND, false);
if (startInBackground) {
new Thread(runnable).start();
} else {
runnable.run();
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage(), ex);
}
}
}
return START_REDELIVER_INTENT;
}
use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.
the class HistoryManager method addHistoryItemDetails.
public void addHistoryItemDetails(String itemID, String itemDetails) {
// As we're going to do an update only we don't need need to worry
// about the preferences; if the item wasn't saved it won't be udpated
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = helper.getWritableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, ID_DETAIL_COL_PROJECTION, DBHelper.TEXT_COL + "=?", new String[] { itemID }, null, null, DBHelper.TIMESTAMP_COL + " DESC", "1");
String oldID = null;
String oldDetails = null;
if (cursor.moveToNext()) {
oldID = cursor.getString(0);
oldDetails = cursor.getString(1);
}
if (oldID != null) {
String newDetails;
if (oldDetails == null) {
newDetails = itemDetails;
} else if (oldDetails.contains(itemDetails)) {
newDetails = null;
} else {
newDetails = oldDetails + " : " + itemDetails;
}
if (newDetails != null) {
ContentValues values = new ContentValues();
values.put(DBHelper.DETAILS_COL, newDetails);
db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID });
}
}
} finally {
close(cursor, db);
}
}
use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.
the class HistoryManager method clearHistory.
void clearHistory() {
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
try {
db = helper.getWritableDatabase();
db.delete(DBHelper.TABLE_NAME, null, null);
} finally {
close(null, db);
}
}
use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.
the class HistoryManager method buildHistoryItems.
public List<HistoryItem> buildHistoryItems() {
SQLiteOpenHelper helper = new DBHelper(activity);
List<HistoryItem> items = new ArrayList<>();
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = helper.getReadableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
while (cursor.moveToNext()) {
String text = cursor.getString(0);
String display = cursor.getString(1);
String format = cursor.getString(2);
long timestamp = cursor.getLong(3);
String details = cursor.getString(4);
Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
items.add(new HistoryItem(result, display, details));
}
} finally {
close(cursor, db);
}
return items;
}
use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.
the class HistoryManager method hasHistoryItems.
public boolean hasHistoryItems() {
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = helper.getReadableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
cursor.moveToFirst();
return cursor.getInt(0) > 0;
} finally {
close(cursor, db);
}
}
Aggregations