Search in sources :

Example 56 with SQLException

use of android.database.SQLException 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;
}
Also used : ContentValues(android.content.ContentValues) SQLException(android.database.SQLException) Intent(android.content.Intent) SQLiteException(android.database.sqlite.SQLiteException) HandlerThread(android.os.HandlerThread)

Example 57 with SQLException

use of android.database.SQLException 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;
}
Also used : ContentValues(android.content.ContentValues) SQLException(android.database.SQLException) Intent(android.content.Intent) SQLiteException(android.database.sqlite.SQLiteException) HandlerThread(android.os.HandlerThread)

Example 58 with SQLException

use of android.database.SQLException in project aware-client by denzilferreira.

the class Accelerometer_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 ACCEL_DEV:
            long accel_id = database.insertWithOnConflict(DATABASE_TABLES[0], Accelerometer_Sensor.DEVICE_ID, values, SQLiteDatabase.CONFLICT_IGNORE);
            if (accel_id > 0) {
                Uri accelUri = ContentUris.withAppendedId(Accelerometer_Sensor.CONTENT_URI, accel_id);
                getContext().getContentResolver().notifyChange(accelUri, null, false);
                database.setTransactionSuccessful();
                database.endTransaction();
                return accelUri;
            }
            database.endTransaction();
            throw new SQLException("Failed to insert row into " + uri);
        case ACCEL_DATA:
            long accelData_id = database.insertWithOnConflict(DATABASE_TABLES[1], Accelerometer_Data.DEVICE_ID, values, SQLiteDatabase.CONFLICT_IGNORE);
            if (accelData_id > 0) {
                Uri accelDataUri = ContentUris.withAppendedId(Accelerometer_Data.CONTENT_URI, accelData_id);
                getContext().getContentResolver().notifyChange(accelDataUri, null, false);
                database.setTransactionSuccessful();
                database.endTransaction();
                return accelDataUri;
            }
            database.endTransaction();
            throw new SQLException("Failed to insert row into " + uri);
        default:
            database.endTransaction();
            throw new IllegalArgumentException("Unknown URI " + uri);
    }
}
Also used : ContentValues(android.content.ContentValues) SQLException(android.database.SQLException) Uri(android.net.Uri)

Example 59 with SQLException

use of android.database.SQLException in project aware-client by denzilferreira.

the class Accelerometer_Provider method bulkInsert.

/**
 * Batch insert for high performance sensors (e.g., sync_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 ACCEL_DEV:
            for (ContentValues v : values) {
                long id;
                try {
                    id = database.insertOrThrow(DATABASE_TABLES[0], Accelerometer_Sensor.DEVICE_ID, v);
                } catch (SQLException e) {
                    id = database.replace(DATABASE_TABLES[0], Accelerometer_Sensor.DEVICE_ID, v);
                }
                if (id <= 0) {
                    Log.w(Accelerometer.TAG, "Failed to insert/replace row into " + uri);
                } else {
                    count++;
                }
            }
            break;
        case ACCEL_DATA:
            for (ContentValues v : values) {
                long id;
                try {
                    id = database.insertOrThrow(DATABASE_TABLES[1], Accelerometer_Data.DEVICE_ID, v);
                } catch (SQLException e) {
                    id = database.replace(DATABASE_TABLES[1], Accelerometer_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;
}
Also used : ContentValues(android.content.ContentValues) SQLException(android.database.SQLException)

Example 60 with SQLException

use of android.database.SQLException in project aware-client by denzilferreira.

the class Applications_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 FOREGROUND:
            long foreground_id = database.insertWithOnConflict(DATABASE_TABLES[0], Applications_Foreground.APPLICATION_NAME, values, SQLiteDatabase.CONFLICT_IGNORE);
            if (foreground_id > 0) {
                Uri foregroundUri = ContentUris.withAppendedId(Applications_Foreground.CONTENT_URI, foreground_id);
                getContext().getContentResolver().notifyChange(foregroundUri, null, false);
                database.setTransactionSuccessful();
                database.endTransaction();
                return foregroundUri;
            }
            database.endTransaction();
            throw new SQLException("Failed to insert row into " + uri);
        case APPLICATIONS:
            long applications_id = database.insertWithOnConflict(DATABASE_TABLES[1], Applications_History.PACKAGE_NAME, values, SQLiteDatabase.CONFLICT_IGNORE);
            if (applications_id > 0) {
                Uri applicationsUri = ContentUris.withAppendedId(Applications_History.CONTENT_URI, applications_id);
                getContext().getContentResolver().notifyChange(applicationsUri, null, false);
                database.setTransactionSuccessful();
                database.endTransaction();
                return applicationsUri;
            }
            throw new SQLException("Failed to insert row into " + uri);
        case NOTIFICATIONS:
            long notifications_id = database.insertWithOnConflict(DATABASE_TABLES[2], Applications_Notifications.PACKAGE_NAME, values, SQLiteDatabase.CONFLICT_IGNORE);
            if (notifications_id > 0) {
                Uri notificationsUri = ContentUris.withAppendedId(Applications_Notifications.CONTENT_URI, notifications_id);
                getContext().getContentResolver().notifyChange(notificationsUri, null, false);
                database.setTransactionSuccessful();
                database.endTransaction();
                return notificationsUri;
            }
            database.endTransaction();
            throw new SQLException("Failed to insert row into " + uri);
        case ERROR:
            long error_id = database.insertWithOnConflict(DATABASE_TABLES[3], Applications_Crashes.PACKAGE_NAME, values, SQLiteDatabase.CONFLICT_IGNORE);
            if (error_id > 0) {
                Uri errorsUri = ContentUris.withAppendedId(Applications_Crashes.CONTENT_URI, error_id);
                getContext().getContentResolver().notifyChange(errorsUri, null, false);
                database.setTransactionSuccessful();
                database.endTransaction();
                return errorsUri;
            }
            database.endTransaction();
            throw new SQLException("Failed to insert row into " + uri);
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }
}
Also used : ContentValues(android.content.ContentValues) SQLException(android.database.SQLException) Uri(android.net.Uri)

Aggregations

SQLException (android.database.SQLException)224 ContentValues (android.content.ContentValues)114 Uri (android.net.Uri)60 Cursor (android.database.Cursor)57 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)39 SQLiteException (android.database.sqlite.SQLiteException)18 Intent (android.content.Intent)16 ArrayList (java.util.ArrayList)14 SQLiteStatement (android.database.sqlite.SQLiteStatement)13 HandlerThread (android.os.HandlerThread)10 DatabaseUtils (android.database.DatabaseUtils)8 Gson (com.google.gson.Gson)8 RAction (io.github.mthli.Bitocle.Database.Repo.RAction)7 Repo (io.github.mthli.Bitocle.Database.Repo.Repo)6 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)5 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)5 IOException (java.io.IOException)5 BAction (io.github.mthli.Bitocle.Database.Bookmark.BAction)4 Test (org.junit.Test)4 Activity (android.app.Activity)3