use of android.database.sqlite.SQLiteException 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");
}
}
use of android.database.sqlite.SQLiteException in project aware-client by denzilferreira.
the class Magnetometer method onSensorChanged.
@Override
public void onSensorChanged(SensorEvent event) {
long TS = System.currentTimeMillis();
if (ENFORCE_FREQUENCY && TS < LAST_TS + FREQUENCY / 1000)
return;
if (LAST_VALUES != null && THRESHOLD > 0 && Math.abs(event.values[0] - LAST_VALUES[0]) < THRESHOLD && Math.abs(event.values[0] - LAST_VALUES[1]) < THRESHOLD && Math.abs(event.values[0] - LAST_VALUES[2]) < THRESHOLD) {
return;
}
LAST_VALUES = new Float[] { event.values[0], event.values[1], event.values[2] };
ContentValues rowData = new ContentValues();
rowData.put(Magnetometer_Data.DEVICE_ID, Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID));
rowData.put(Magnetometer_Data.TIMESTAMP, TS);
rowData.put(Magnetometer_Data.VALUES_0, event.values[0]);
rowData.put(Magnetometer_Data.VALUES_1, event.values[1]);
rowData.put(Magnetometer_Data.VALUES_2, event.values[2]);
rowData.put(Magnetometer_Data.ACCURACY, event.accuracy);
rowData.put(Magnetometer_Data.LABEL, LABEL);
if (awareSensor != null)
awareSensor.onMagnetometerChanged(rowData);
data_values.add(rowData);
LAST_TS = TS;
if (data_values.size() < 250 && TS < LAST_SAVE + 300000) {
return;
}
final ContentValues[] data_buffer = new ContentValues[data_values.size()];
data_values.toArray(data_buffer);
try {
if (!Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_DB_SLOW).equals("true")) {
new Thread(new Runnable() {
@Override
public void run() {
getContentResolver().bulkInsert(Magnetometer_Provider.Magnetometer_Data.CONTENT_URI, data_buffer);
Intent newData = new Intent(ACTION_AWARE_MAGNETOMETER);
sendBroadcast(newData);
}
}).run();
}
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
data_values.clear();
LAST_SAVE = TS;
}
use of android.database.sqlite.SQLiteException in project aware-client by denzilferreira.
the class Proximity method onSensorChanged.
@Override
public void onSensorChanged(SensorEvent event) {
long TS = System.currentTimeMillis();
if (ENFORCE_FREQUENCY && TS < LAST_TS + FREQUENCY / 1000)
return;
if (LAST_VALUE != null && THRESHOLD > 0 && Math.abs(event.values[0] - LAST_VALUE) < THRESHOLD) {
return;
}
LAST_VALUE = event.values[0];
ContentValues rowData = new ContentValues();
rowData.put(Proximity_Data.DEVICE_ID, Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID));
rowData.put(Proximity_Data.TIMESTAMP, TS);
rowData.put(Proximity_Data.PROXIMITY, event.values[0]);
rowData.put(Proximity_Data.ACCURACY, event.accuracy);
rowData.put(Proximity_Data.LABEL, LABEL);
if (awareSensor != null)
awareSensor.onProximityChanged(rowData);
data_values.add(rowData);
LAST_TS = TS;
if (data_values.size() < 250 && TS < LAST_SAVE + 300000) {
return;
}
final ContentValues[] data_buffer = new ContentValues[data_values.size()];
data_values.toArray(data_buffer);
try {
if (!Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_DB_SLOW).equals("true")) {
new Thread(new Runnable() {
@Override
public void run() {
getContentResolver().bulkInsert(Proximity_Provider.Proximity_Data.CONTENT_URI, data_buffer);
Intent newData = new Intent(ACTION_AWARE_PROXIMITY);
sendBroadcast(newData);
}
}).run();
}
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
data_values.clear();
LAST_SAVE = TS;
}
use of android.database.sqlite.SQLiteException in project aware-client by denzilferreira.
the class Aware method setSetting.
/**
* Insert / Update settings of the framework
*
* @param key
* @param value
*/
public static void setSetting(Context context, String key, Object value) {
boolean is_global;
ArrayList<String> global_settings = new ArrayList<String>();
global_settings.add(Aware_Preferences.DEBUG_FLAG);
global_settings.add(Aware_Preferences.DEBUG_TAG);
global_settings.add(Aware_Preferences.DEVICE_ID);
global_settings.add(Aware_Preferences.DEVICE_LABEL);
global_settings.add(Aware_Preferences.STATUS_WEBSERVICE);
global_settings.add(Aware_Preferences.FREQUENCY_WEBSERVICE);
global_settings.add(Aware_Preferences.WEBSERVICE_WIFI_ONLY);
global_settings.add(Aware_Preferences.WEBSERVICE_SERVER);
global_settings.add(Aware_Preferences.WEBSERVICE_SIMPLE);
global_settings.add(Aware_Preferences.WEBSERVICE_REMOVE_DATA);
global_settings.add(Aware_Preferences.WEBSERVICE_SILENT);
global_settings.add(Aware_Preferences.STATUS_APPLICATIONS);
global_settings.add(Applications.STATUS_AWARE_ACCESSIBILITY);
// allow standalone apps to react to MQTT
if (context.getResources().getBoolean(R.bool.standalone)) {
global_settings.add(Aware_Preferences.STATUS_MQTT);
global_settings.add(Aware_Preferences.MQTT_USERNAME);
global_settings.add(Aware_Preferences.MQTT_PASSWORD);
global_settings.add(Aware_Preferences.MQTT_SERVER);
global_settings.add(Aware_Preferences.MQTT_PORT);
global_settings.add(Aware_Preferences.MQTT_KEEP_ALIVE);
global_settings.add(Aware_Preferences.MQTT_QOS);
}
is_global = global_settings.contains(key);
if (context.getResources().getBoolean(R.bool.standalone))
is_global = false;
// We already have a Device ID, do nothing!
if (key.equals(Aware_Preferences.DEVICE_ID) && Aware.getSetting(context, Aware_Preferences.DEVICE_ID).length() > 0) {
Log.d(Aware.TAG, "AWARE UUID: " + Aware.getSetting(context, Aware_Preferences.DEVICE_ID) + " in " + context.getPackageName());
return;
}
if (key.equals(Aware_Preferences.DEVICE_LABEL) && ((String) value).length() > 0) {
ContentValues newLabel = new ContentValues();
newLabel.put(Aware_Provider.Aware_Device.LABEL, (String) value);
context.getApplicationContext().getContentResolver().update(Aware_Provider.Aware_Device.CONTENT_URI, newLabel, Aware_Provider.Aware_Device.DEVICE_ID + " LIKE '" + Aware.getSetting(context, Aware_Preferences.DEVICE_ID) + "'", null);
}
ContentValues setting = new ContentValues();
setting.put(Aware_Settings.SETTING_KEY, key);
setting.put(Aware_Settings.SETTING_VALUE, value.toString());
if (is_global) {
setting.put(Aware_Settings.SETTING_PACKAGE_NAME, "com.aware.phone");
} else {
setting.put(Aware_Settings.SETTING_PACKAGE_NAME, context.getPackageName());
}
Cursor qry = context.getApplicationContext().getContentResolver().query(Aware_Settings.CONTENT_URI, null, Aware_Settings.SETTING_KEY + " LIKE '" + key + "' AND " + Aware_Settings.SETTING_PACKAGE_NAME + " LIKE " + ((is_global) ? "'com.aware.phone'" : "'" + context.getPackageName() + "'"), null, null);
// update
if (qry != null && qry.moveToFirst()) {
try {
if (!qry.getString(qry.getColumnIndex(Aware_Settings.SETTING_VALUE)).equals(value.toString())) {
context.getApplicationContext().getContentResolver().update(Aware_Settings.CONTENT_URI, setting, Aware_Settings.SETTING_ID + "=" + qry.getInt(qry.getColumnIndex(Aware_Settings.SETTING_ID)), null);
if (Aware.DEBUG)
Log.d(Aware.TAG, "Updated: " + key + "=" + value);
}
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
// insert
} else {
try {
context.getApplicationContext().getContentResolver().insert(Aware_Settings.CONTENT_URI, setting);
if (Aware.DEBUG)
Log.d(Aware.TAG, "Added: " + key + "=" + value);
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
}
if (qry != null && !qry.isClosed())
qry.close();
}
use of android.database.sqlite.SQLiteException in project aware-client by denzilferreira.
the class Gyroscope method onSensorChanged.
@Override
public void onSensorChanged(SensorEvent event) {
if (SignificantMotion.isSignificantMotionActive && !SignificantMotion.CURRENT_SIGMOTION_STATE) {
if (data_values.size() > 0) {
final ContentValues[] data_buffer = new ContentValues[data_values.size()];
data_values.toArray(data_buffer);
try {
if (!Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_DB_SLOW).equals("true")) {
new Thread(new Runnable() {
@Override
public void run() {
getContentResolver().bulkInsert(Gyroscope_Provider.Gyroscope_Data.CONTENT_URI, data_buffer);
Intent newData = new Intent(ACTION_AWARE_GYROSCOPE);
sendBroadcast(newData);
}
}).run();
}
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
data_values.clear();
}
return;
}
long TS = System.currentTimeMillis();
if (ENFORCE_FREQUENCY && TS < LAST_TS + FREQUENCY / 1000)
return;
if (LAST_VALUES != null && THRESHOLD > 0 && Math.abs(event.values[0] - LAST_VALUES[0]) < THRESHOLD && Math.abs(event.values[1] - LAST_VALUES[1]) < THRESHOLD && Math.abs(event.values[2] - LAST_VALUES[2]) < THRESHOLD) {
return;
}
LAST_VALUES = new Float[] { event.values[0], event.values[1], event.values[2] };
// Proceed with saving as usual.
ContentValues rowData = new ContentValues();
rowData.put(Gyroscope_Data.DEVICE_ID, Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID));
rowData.put(Gyroscope_Data.TIMESTAMP, TS);
rowData.put(Gyroscope_Data.VALUES_0, event.values[0]);
rowData.put(Gyroscope_Data.VALUES_1, event.values[1]);
rowData.put(Gyroscope_Data.VALUES_2, event.values[2]);
rowData.put(Gyroscope_Data.ACCURACY, event.accuracy);
rowData.put(Gyroscope_Data.LABEL, LABEL);
if (awareSensor != null)
awareSensor.onGyroscopeChanged(rowData);
data_values.add(rowData);
LAST_TS = TS;
if (data_values.size() < 250 && TS < LAST_SAVE + 300000) {
return;
}
final ContentValues[] data_buffer = new ContentValues[data_values.size()];
data_values.toArray(data_buffer);
try {
if (!Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_DB_SLOW).equals("true")) {
new Thread(new Runnable() {
@Override
public void run() {
getContentResolver().bulkInsert(Gyroscope_Provider.Gyroscope_Data.CONTENT_URI, data_buffer);
Intent newData = new Intent(ACTION_AWARE_GYROSCOPE);
sendBroadcast(newData);
}
}).run();
}
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
} catch (SQLException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
data_values.clear();
LAST_SAVE = TS;
}
Aggregations