diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java index 6ff9824a..fa940eb2 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java @@ -42,10 +42,8 @@ import com.health.openscale.core.bodymetric.EstimatedFatMetric; import com.health.openscale.core.bodymetric.EstimatedLBMMetric; import com.health.openscale.core.bodymetric.EstimatedWaterMetric; import com.health.openscale.core.database.AppDatabase; -import com.health.openscale.core.database.ScaleDatabase; import com.health.openscale.core.database.ScaleMeasurementDAO; import com.health.openscale.core.database.ScaleUserDAO; -import com.health.openscale.core.database.ScaleUserDatabase; import com.health.openscale.core.datatypes.ScaleMeasurement; import com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.core.utils.Converters; @@ -103,7 +101,6 @@ public class OpenScale { reopenDatabase(); - migrateSQLtoRoom(); updateScaleData(); } @@ -143,33 +140,6 @@ public class OpenScale { userDAO = appDB.userDAO(); } - private void migrateSQLtoRoom() { - if (!context.getDatabasePath(ScaleUserDatabase.DATABASE_NAME).exists() - || !context.getDatabasePath(ScaleDatabase.DATABASE_NAME).exists()) { - return; - } - - ScaleDatabase scaleDB = new ScaleDatabase(context); - ScaleUserDatabase scaleUserDB = new ScaleUserDatabase(context); - - List oldScaleUserList = scaleUserDB.getScaleUserList(); - - if (scaleDB.getReadableDatabase().getVersion() == 6 && userDAO.getAll().isEmpty() && !oldScaleUserList.isEmpty()) { - Toast.makeText(context, "Migrating old SQL database to new database format...", Toast.LENGTH_LONG).show(); - userDAO.insertAll(oldScaleUserList); - - for (ScaleUser user : oldScaleUserList) { - List oldScaleMeasurementList = scaleDB.getScaleDataList(user.getId()); - measurementDAO.insertAll(oldScaleMeasurementList); - } - - Toast.makeText(context, "Finished migrating old SQL database to new database format", Toast.LENGTH_LONG).show(); - } - - scaleUserDB.close(); - scaleDB.close(); - } - public void triggerWidgetUpdate() { int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds( new ComponentName(context, WidgetProvider.class)); diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleDatabase.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleDatabase.java deleted file mode 100644 index dad10e63..00000000 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleDatabase.java +++ /dev/null @@ -1,181 +0,0 @@ -/* Copyright (C) 2014 olie.xdev -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see -*/ - -package com.health.openscale.core.database; - -import android.content.Context; -import android.database.Cursor; -import android.database.SQLException; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; - -import com.health.openscale.core.datatypes.ScaleMeasurement; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Locale; - -import timber.log.Timber; - -public class ScaleDatabase extends SQLiteOpenHelper { - private static final int DATABASE_VERSION = 6; - public static final String DATABASE_NAME = "openScaleDatabase.db"; - - private static final String TABLE_NAME = "scaledata"; - private static final String COLUMN_NAME_ID = "id"; - private static final String COLUMN_NAME_USER_ID = "user_id"; - private static final String COLUMN_NAME_DATE_TIME = "date_time"; - private static final String COLUMN_NAME_WEIGHT = "weight"; - private static final String COLUMN_NAME_FAT = "fat"; - private static final String COLUMN_NAME_WATER = "water"; - private static final String COLUMN_NAME_MUSCLE = "muscle"; - private static final String COLUMN_NAME_LBW = "lbw"; - private static final String COLUMN_NAME_BONE = "bone"; - private static final String COLUMN_NAME_WAIST = "waist"; - private static final String COLUMN_NAME_HIP = "hip"; - private static final String COLUMN_NAME_COMMENT = "comment"; - private static final String COLUMN_NAME_ENABLE = "enable"; - - private static final String SQL_CREATE_ENTRIES = - "CREATE TABLE " + TABLE_NAME + " (" + - COLUMN_NAME_ID + " INTEGER PRIMARY KEY," + - COLUMN_NAME_USER_ID + " INTEGER," + - COLUMN_NAME_DATE_TIME + " TEXT," + - COLUMN_NAME_WEIGHT + " REAL," + - COLUMN_NAME_FAT + " REAL," + - COLUMN_NAME_WATER + " REAL," + - COLUMN_NAME_MUSCLE + " REAL," + - COLUMN_NAME_LBW + " REAL," + - COLUMN_NAME_BONE + " REAL," + - COLUMN_NAME_WAIST + " REAL," + - COLUMN_NAME_HIP + " REAL," + - COLUMN_NAME_COMMENT + " TEXT," + - COLUMN_NAME_ENABLE + " INTEGER" + - ")"; - - private static String[] projection = { - COLUMN_NAME_ID, - COLUMN_NAME_USER_ID, - COLUMN_NAME_DATE_TIME, - COLUMN_NAME_WEIGHT, - COLUMN_NAME_FAT, - COLUMN_NAME_WATER, - COLUMN_NAME_MUSCLE, - COLUMN_NAME_LBW, - COLUMN_NAME_BONE, - COLUMN_NAME_WAIST, - COLUMN_NAME_HIP, - COLUMN_NAME_COMMENT, - COLUMN_NAME_ENABLE - }; - - private SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); - - public ScaleDatabase(Context context) { - super(context, DATABASE_NAME, null, DATABASE_VERSION); - } - - @Override - public void onCreate(SQLiteDatabase db) { - db.execSQL(SQL_CREATE_ENTRIES); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion == 1 && newVersion == 2) { - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_COMMENT + " TEXT DEFAULT ''"); - } - - if (oldVersion == 2 && newVersion == 3) { - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_WAIST + " REAL DEFAULT 0"); - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_HIP + " REAL DEFAULT 0"); - } - - if (oldVersion == 3 && newVersion == 4) { - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_ENABLE + " INTEGER DEFAULT 1"); - } - - if (oldVersion == 4 && newVersion == 5) { - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_BONE + " REAL DEFAULT 0"); - } - - if (oldVersion == 5 && newVersion == 6) { - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_LBW + " REAL DEFAULT 0"); - } - } - - public ArrayList getScaleDataList(int userId) { - ArrayList scaleMeasurementList = new ArrayList<>(); - - try { - String sortOrder = COLUMN_NAME_DATE_TIME + " DESC"; - - Cursor cursorScaleDB = getReadableDatabase().query( - TABLE_NAME, // The table to query - projection, // The columns to return - COLUMN_NAME_USER_ID + "=? AND " + COLUMN_NAME_ENABLE + "=1", // The columns for the WHERE clause - new String[]{Integer.toString(userId)}, // The values for the WHERE clause - null, // don't group the rows - null, // don't filter by row groups - sortOrder // The sort order - ); - - cursorScaleDB.moveToFirst(); - - while (!cursorScaleDB.isAfterLast()) { - scaleMeasurementList.add(readAtCursor(cursorScaleDB)); - - cursorScaleDB.moveToNext(); - } - - cursorScaleDB.close(); - } catch (SQLException ex) { - Timber.e(ex, "SQL exception occurred while getting scale data list"); - } - - return scaleMeasurementList; - } - - - private ScaleMeasurement readAtCursor (Cursor cur) { - ScaleMeasurement scaleMeasurement = new ScaleMeasurement(); - - try { - scaleMeasurement.setId(cur.getInt(cur.getColumnIndexOrThrow(COLUMN_NAME_ID))); - scaleMeasurement.setUserId(cur.getInt(cur.getColumnIndexOrThrow(COLUMN_NAME_USER_ID))); - String date_time = cur.getString(cur.getColumnIndexOrThrow(COLUMN_NAME_DATE_TIME)); - scaleMeasurement.setWeight(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_WEIGHT))); - scaleMeasurement.setFat(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_FAT))); - scaleMeasurement.setWater(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_WATER))); - scaleMeasurement.setMuscle(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_MUSCLE))); - scaleMeasurement.setLbm(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_LBW))); - scaleMeasurement.setBone(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_BONE))); - scaleMeasurement.setWaist(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_WAIST))); - scaleMeasurement.setHip(cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_HIP))); - scaleMeasurement.setComment(cur.getString(cur.getColumnIndexOrThrow(COLUMN_NAME_COMMENT))); - - scaleMeasurement.setDateTime(formatDateTime.parse(date_time)); - } catch (ParseException ex) { - Timber.e(ex, "Can't parse the date time string"); - } - catch (IllegalArgumentException ex) { - Timber.e(ex, "Illegal argument while reading from scale database"); - } - - return scaleMeasurement; - } -} diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDatabase.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDatabase.java deleted file mode 100644 index faae555b..00000000 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDatabase.java +++ /dev/null @@ -1,155 +0,0 @@ -/* Copyright (C) 2014 olie.xdev -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see -*/ - -package com.health.openscale.core.database; - -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; - -import com.health.openscale.core.datatypes.ScaleUser; -import com.health.openscale.core.utils.Converters; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Locale; - -import timber.log.Timber; - -public class ScaleUserDatabase extends SQLiteOpenHelper { - private static final int DATABASE_VERSION = 3; - public static final String DATABASE_NAME = "openScaleUserDatabase.db"; - - private static final String TABLE_NAME = "scaleuserdata"; - private static final String COLUMN_NAME_ID = "id"; - private static final String COLUMN_NAME_USER_NAME = "user_name"; - private static final String COLUMN_NAME_BIRTHDAY = "birthday"; - private static final String COLUMN_NAME_BODY_HEIGHT = "body_height"; - private static final String COLUMN_NAME_SCALE_UNIT = "scale_unit"; - private static final String COLUMN_NAME_GENDER = "gender"; - private static final String COLUMN_NAME_INITIAL_WEIGHT = "initial_weight"; - private static final String COLUMN_NAME_GOAL_WEIGHT = "goal_weight"; - private static final String COLUMN_NAME_GOAL_DATE = "goal_date"; - - private static final String SQL_CREATE_ENTRIES = - "CREATE TABLE " + TABLE_NAME + " (" + - COLUMN_NAME_ID + " INTEGER PRIMARY KEY," + - COLUMN_NAME_USER_NAME + " TEXT," + - COLUMN_NAME_BIRTHDAY + " TEXT," + - COLUMN_NAME_BODY_HEIGHT + " INTEGER," + - COLUMN_NAME_SCALE_UNIT + " INTEGER," + - COLUMN_NAME_GENDER + " INTEGER," + - COLUMN_NAME_INITIAL_WEIGHT + " REAL," + - COLUMN_NAME_GOAL_WEIGHT + " REAL," + - COLUMN_NAME_GOAL_DATE + " TEXT" + - ")"; - - private static String[] projection = { - COLUMN_NAME_ID, - COLUMN_NAME_USER_NAME, - COLUMN_NAME_BIRTHDAY, - COLUMN_NAME_BODY_HEIGHT, - COLUMN_NAME_SCALE_UNIT, - COLUMN_NAME_GENDER, - COLUMN_NAME_INITIAL_WEIGHT, - COLUMN_NAME_GOAL_WEIGHT, - COLUMN_NAME_GOAL_DATE - }; - - private SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); - - public ScaleUserDatabase(Context context) { - super(context, DATABASE_NAME, null, DATABASE_VERSION); - } - - @Override - public void onCreate(SQLiteDatabase db) { - db.execSQL(SQL_CREATE_ENTRIES); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion == 1 && newVersion == 2) { - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_GENDER + " INTEGER DEFAULT 0"); - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_GOAL_WEIGHT + " REAL DEFAULT 0"); - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_GOAL_DATE + " TEXT DEFAULT '2014-01-01 00:00'"); - } - - if (oldVersion == 2 && newVersion == 3) { - db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_NAME_INITIAL_WEIGHT + " REAL DEFAULT 0"); - } - } - - public ArrayList getScaleUserList() { - SQLiteDatabase db = getReadableDatabase(); - ArrayList scaleUserDBEntries = new ArrayList<>(); - - String sortOrder = COLUMN_NAME_ID + " ASC"; - - Cursor cursorScaleDB = db.query( - TABLE_NAME, // The table to query - projection, // The columns to return - null, // The columns for the WHERE clause - null, // The values for the WHERE clause - null, // don't group the rows - null, // don't filter by row groups - sortOrder // The sort order - ); - - cursorScaleDB.moveToFirst(); - - while (!cursorScaleDB.isAfterLast()) { - scaleUserDBEntries.add(readAtCursor(cursorScaleDB)); - - cursorScaleDB.moveToNext(); - } - - cursorScaleDB.close(); - - return scaleUserDBEntries; - } - - private ScaleUser readAtCursor (Cursor cur) { - ScaleUser scaleUser = new ScaleUser(); - - try { - scaleUser.setId(cur.getInt(cur.getColumnIndexOrThrow(COLUMN_NAME_ID))); - scaleUser.setUserName(cur.getString(cur.getColumnIndexOrThrow(COLUMN_NAME_USER_NAME))); - String birthday = cur.getString(cur.getColumnIndexOrThrow(COLUMN_NAME_BIRTHDAY)); - scaleUser.setBodyHeight(cur.getInt(cur.getColumnIndexOrThrow(COLUMN_NAME_BODY_HEIGHT))); - scaleUser.setScaleUnit(Converters.fromWeightUnitInt(cur.getInt(cur.getColumnIndexOrThrow(COLUMN_NAME_SCALE_UNIT)))); - scaleUser.setGender(Converters.fromGenderInt(cur.getInt(cur.getColumnIndexOrThrow(COLUMN_NAME_GENDER)))); - double initial_weight = cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_INITIAL_WEIGHT)); - double goal_weight = cur.getFloat(cur.getColumnIndexOrThrow(COLUMN_NAME_GOAL_WEIGHT)); - String goal_date = cur.getString(cur.getColumnIndexOrThrow(COLUMN_NAME_GOAL_DATE)); - - scaleUser.setBirthday(formatDateTime.parse(birthday)); - scaleUser.setGoalDate(formatDateTime.parse(goal_date)); - - scaleUser.setInitialWeight(Math.round(initial_weight * 100.0f) / 100.0f); - scaleUser.setGoalWeight(Math.round(goal_weight * 100.0f) / 100.0f); - } catch (ParseException ex) { - Timber.e(ex, "Can't parse the date time string"); - } - catch (IllegalArgumentException ex) { - Timber.e(ex, "Illegal argument while reading from scale database"); - } - - return scaleUser; - } -}