/** Query entries from the database */
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    if (database == null || !database.isOpen()) database = databaseHelper.getWritableDatabase();

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    switch (sUriMatcher.match(uri)) {
      case SENSOR_DEV:
        qb.setTables(DATABASE_TABLES[0]);
        qb.setProjectionMap(sensorDeviceMap);
        break;
      case SENSOR_DATA:
        qb.setTables(DATABASE_TABLES[1]);
        qb.setProjectionMap(sensorDataMap);
        break;
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
    try {
      Cursor c = qb.query(database, projection, selection, selectionArgs, null, null, sortOrder);
      c.setNotificationUri(getContext().getContentResolver(), uri);
      return c;
    } catch (IllegalStateException e) {
      if (Aware.DEBUG) Log.e(Aware.TAG, e.getMessage());

      return null;
    }
  }
  /** Insert entry to the database */
  @Override
  public Uri insert(Uri uri, ContentValues initialValues) {
    if (database == null || !database.isOpen()) database = databaseHelper.getWritableDatabase();

    ContentValues values =
        (initialValues != null) ? new ContentValues(initialValues) : new ContentValues();

    switch (sUriMatcher.match(uri)) {
      case SENSOR_DEV:
        long accel_id = database.insert(DATABASE_TABLES[0], Gravity_Sensor.DEVICE_ID, values);

        if (accel_id > 0) {
          Uri accelUri = ContentUris.withAppendedId(Gravity_Sensor.CONTENT_URI, accel_id);
          getContext().getContentResolver().notifyChange(accelUri, null);
          return accelUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
      case SENSOR_DATA:
        long accelData_id = database.insert(DATABASE_TABLES[1], Gravity_Data.DEVICE_ID, values);

        if (accelData_id > 0) {
          Uri accelDataUri = ContentUris.withAppendedId(Gravity_Data.CONTENT_URI, accelData_id);
          getContext().getContentResolver().notifyChange(accelDataUri, null);
          return accelDataUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
  }
 @Override
 public boolean onCreate() {
   if (databaseHelper == null)
     databaseHelper =
         new DatabaseHelper(
             getContext(), DATABASE_NAME, null, DATABASE_VERSION, DATABASE_TABLES, TABLES_FIELDS);
   database = databaseHelper.getWritableDatabase();
   return (databaseHelper != null);
 }
  /** Update application on the database */
  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    if (database == null || !database.isOpen()) database = databaseHelper.getWritableDatabase();
    int count = 0;
    switch (sUriMatcher.match(uri)) {
      case SENSOR_DEV:
        count = database.update(DATABASE_TABLES[0], values, selection, selectionArgs);
        break;
      case SENSOR_DATA:
        count = database.update(DATABASE_TABLES[1], values, selection, selectionArgs);
        break;
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }