use of android.database.SQLException in project K6nele by Kaljurand.
the class AppsContentProvider method insert.
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
ContentValues values;
if (initialValues != null) {
values = new ContentValues(initialValues);
} else {
values = new ContentValues();
}
SQLiteDatabase db = dbHelper.getWritableDatabase();
long rowId = 0;
Uri returnUri = null;
switch(sUriMatcher.match(uri)) {
case APPS:
rowId = db.insert(APPS_TABLE_NAME, App.Columns.FNAME, values);
if (rowId <= 0) {
throw new SQLException("Failed to insert row into " + uri);
}
returnUri = ContentUris.withAppendedId(App.Columns.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(returnUri, null);
return returnUri;
case GRAMMARS:
rowId = db.insert(GRAMMARS_TABLE_NAME, Grammar.Columns.DESC, values);
if (rowId <= 0) {
throw new SQLException("Failed to insert row into " + uri);
}
returnUri = ContentUris.withAppendedId(Grammar.Columns.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(returnUri, null);
return returnUri;
case SERVERS:
rowId = db.insert(SERVERS_TABLE_NAME, Server.Columns.URL, values);
if (rowId <= 0) {
throw new SQLException("Failed to insert row into " + uri);
}
returnUri = ContentUris.withAppendedId(Server.Columns.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(returnUri, null);
return returnUri;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
use of android.database.SQLException in project aware-client by denzilferreira.
the class Significant_Provider method insert.
/**
* Insert entry to the database
*/
@Override
public synchronized Uri insert(Uri uri, ContentValues initialValues) {
initialiseDatabase();
ContentValues values = (initialValues != null) ? new ContentValues(initialValues) : new ContentValues();
database.beginTransaction();
switch(sUriMatcher.match(uri)) {
case SENSOR_DATA:
long accelData_id = database.insertWithOnConflict(DATABASE_TABLES[0], Significant_Data.DEVICE_ID, values, SQLiteDatabase.CONFLICT_IGNORE);
database.setTransactionSuccessful();
database.endTransaction();
if (accelData_id > 0) {
Uri accelDataUri = ContentUris.withAppendedId(Significant_Data.CONTENT_URI, accelData_id);
getContext().getContentResolver().notifyChange(accelDataUri, null, false);
return accelDataUri;
}
database.endTransaction();
throw new SQLException("Failed to insert row into " + uri);
default:
database.endTransaction();
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
use of android.database.SQLException in project aware-client by denzilferreira.
the class Significant_Provider method bulkInsert.
/**
* Batch insert for high performance sensors (e.g., accelerometer, etc)
*
* @param uri
* @param values
* @return values.length
*/
@Override
public synchronized int bulkInsert(Uri uri, ContentValues[] values) {
initialiseDatabase();
database.beginTransaction();
int count = 0;
switch(sUriMatcher.match(uri)) {
case SENSOR_DATA:
for (ContentValues v : values) {
long id;
try {
id = database.insertOrThrow(DATABASE_TABLES[0], Significant_Data.DEVICE_ID, v);
} catch (SQLException e) {
id = database.replace(DATABASE_TABLES[0], Significant_Data.DEVICE_ID, v);
}
if (id <= 0) {
Log.w(Accelerometer.TAG, "Failed to insert/replace row into " + uri);
} else {
count++;
}
}
break;
default:
database.endTransaction();
throw new IllegalArgumentException("Unknown URI " + uri);
}
database.setTransactionSuccessful();
database.endTransaction();
getContext().getContentResolver().notifyChange(uri, null, false);
return count;
}
use of android.database.SQLException in project aware-client by denzilferreira.
the class Temperature_Provider method insert.
/**
* Insert entry to the database
*/
@Override
public synchronized Uri insert(Uri uri, ContentValues initialValues) {
initialiseDatabase();
ContentValues values = (initialValues != null) ? new ContentValues(initialValues) : new ContentValues();
database.beginTransaction();
switch(sUriMatcher.match(uri)) {
case SENSOR_DEV:
long accel_id = database.insertWithOnConflict(DATABASE_TABLES[0], Temperature_Sensor.DEVICE_ID, values, SQLiteDatabase.CONFLICT_IGNORE);
database.setTransactionSuccessful();
database.endTransaction();
if (accel_id > 0) {
Uri accelUri = ContentUris.withAppendedId(Temperature_Sensor.CONTENT_URI, accel_id);
getContext().getContentResolver().notifyChange(accelUri, null, false);
return accelUri;
}
throw new SQLException("Failed to insert row into " + uri);
case SENSOR_DATA:
long accelData_id = database.insertWithOnConflict(DATABASE_TABLES[1], Temperature_Data.DEVICE_ID, values, SQLiteDatabase.CONFLICT_IGNORE);
database.setTransactionSuccessful();
database.endTransaction();
if (accelData_id > 0) {
Uri accelDataUri = ContentUris.withAppendedId(Temperature_Data.CONTENT_URI, accelData_id);
getContext().getContentResolver().notifyChange(accelDataUri, null, false);
return accelDataUri;
}
throw new SQLException("Failed to insert row into " + uri);
default:
database.endTransaction();
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
use of android.database.SQLException in project aware-client by denzilferreira.
the class Scheduler method saveSchedule.
/**
* Save the defined scheduled task
*
* @param context
* @param schedule
*/
public static void saveSchedule(Context context, Schedule schedule) {
try {
ArrayList<String> global_settings = new ArrayList<>();
// global_settings.add(Aware.SCHEDULE_SYNC_DATA);
boolean is_global = global_settings.contains(schedule.getScheduleID());
if (context.getResources().getBoolean(R.bool.standalone))
is_global = false;
if (schedule.getRandom().length() != 0 && schedule.getTimer() == -1) {
JSONObject random = schedule.getRandom();
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
Calendar now = Calendar.getInstance();
int earliest = schedule.getDailyEarliest();
int latest = schedule.getDailyLatest();
start.set(Calendar.HOUR_OF_DAY, earliest);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
end.set(Calendar.HOUR_OF_DAY, latest);
end.set(Calendar.MINUTE, 59);
end.set(Calendar.SECOND, 59);
end.set(Calendar.MILLISECOND, 999);
String original_id = schedule.getScheduleID();
String random_seed = original_id;
random_seed += "-" + Aware.getSetting(context, Aware_Preferences.DEVICE_ID);
// Get the random events for today
ArrayList<Long> randoms = random_times(start, end, random.getInt(RANDOM_TIMES), random.getInt(RANDOM_INTERVAL), random_seed);
// Remove events that are in the past
Iterator<Long> iter = randoms.iterator();
while (iter.hasNext()) {
if (iter.next() < now.getTimeInMillis() + 2 * 1000) {
iter.remove();
}
}
Log.d(TAG, "Random times for today between " + start.getTime().toString() + " and " + end.getTime().toString() + ": " + randoms.size() + " left");
// If we have no events left today, reschedule for tomorrow instead.
if (randoms.size() <= 0) {
start.add(Calendar.DAY_OF_YEAR, 1);
end.add(Calendar.DAY_OF_YEAR, 1);
randoms = random_times(start, end, random.getInt(RANDOM_TIMES), random.getInt(RANDOM_INTERVAL), random_seed);
Log.d(TAG, "Random times set for tomorrow between " + start.getTime().toString() + " and " + end.getTime().toString());
}
long max = getLastRandom(randoms);
for (Long r : randoms) {
Calendar timer = Calendar.getInstance();
timer.setTimeInMillis(r);
Log.d(TAG, "RANDOM TIME:" + timer.getTime().toString() + "\n");
schedule.setTimer(timer);
if (r == max) {
schedule.setScheduleID(original_id + "_random_" + r + "_last");
} else {
schedule.setScheduleID(original_id + "_random_" + r);
}
Log.d(Scheduler.TAG, "Random schedule: " + schedule.getScheduleID() + "\n");
// Recursively call saveSchedule. This does not end up here again, because
// now there is a timer set.
saveSchedule(context, schedule);
}
} else {
ContentValues data = new ContentValues();
data.put(Scheduler_Provider.Scheduler_Data.TIMESTAMP, System.currentTimeMillis());
data.put(Scheduler_Provider.Scheduler_Data.DEVICE_ID, Aware.getSetting(context, Aware_Preferences.DEVICE_ID));
data.put(Scheduler_Provider.Scheduler_Data.SCHEDULE_ID, schedule.getScheduleID());
data.put(Scheduler_Provider.Scheduler_Data.SCHEDULE, schedule.build().toString());
data.put(Scheduler_Provider.Scheduler_Data.PACKAGE_NAME, (is_global) ? "com.aware.phone" : context.getPackageName());
Cursor schedules = context.getContentResolver().query(Scheduler_Provider.Scheduler_Data.CONTENT_URI, null, Scheduler_Provider.Scheduler_Data.SCHEDULE_ID + " LIKE '" + schedule.getScheduleID() + "' AND " + Scheduler_Provider.Scheduler_Data.PACKAGE_NAME + " LIKE '" + context.getPackageName() + "'", null, null);
if (schedules != null && schedules.getCount() == 1) {
try {
Log.d(Scheduler.TAG, "Updating already existing schedule...");
context.getContentResolver().update(Scheduler_Provider.Scheduler_Data.CONTENT_URI, data, Scheduler_Provider.Scheduler_Data.SCHEDULE_ID + " LIKE '" + schedule.getScheduleID() + "' AND " + Scheduler_Provider.Scheduler_Data.PACKAGE_NAME + " LIKE '" + ((is_global) ? "com.aware.phone" : context.getPackageName()) + "'", null);
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
} else {
try {
Log.d(Scheduler.TAG, "New schedule: " + data.toString());
context.getContentResolver().insert(Scheduler_Provider.Scheduler_Data.CONTENT_URI, data);
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
}
if (schedules != null && !schedules.isClosed())
schedules.close();
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(Scheduler.TAG, "Error saving schedule");
}
}
Aggregations