diff --git a/app/src/main/java/org/literacyapp/analytics/dao/BootCompletedEventDao.java b/app/src/main/java/org/literacyapp/analytics/dao/BootCompletedEventDao.java new file mode 100644 index 0000000..d744261 --- /dev/null +++ b/app/src/main/java/org/literacyapp/analytics/dao/BootCompletedEventDao.java @@ -0,0 +1,131 @@ +package org.literacyapp.analytics.dao; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.internal.DaoConfig; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; + +import java.util.Calendar; +import org.literacyapp.analytics.dao.converter.CalendarConverter; + +import org.literacyapp.analytics.model.BootCompletedEvent; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "BOOT_COMPLETED_EVENT". +*/ +public class BootCompletedEventDao extends AbstractDao { + + public static final String TABLENAME = "BOOT_COMPLETED_EVENT"; + + /** + * Properties of entity BootCompletedEvent.
+ * Can be used for QueryBuilder and for referencing column names. + */ + public static class Properties { + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); + public final static Property DeviceId = new Property(1, String.class, "deviceId", false, "DEVICE_ID"); + public final static Property Time = new Property(2, long.class, "time", false, "TIME"); + } + + private final CalendarConverter timeConverter = new CalendarConverter(); + + public BootCompletedEventDao(DaoConfig config) { + super(config); + } + + public BootCompletedEventDao(DaoConfig config, DaoSession daoSession) { + super(config, daoSession); + } + + /** Creates the underlying database table. */ + public static void createTable(Database db, boolean ifNotExists) { + String constraint = ifNotExists? "IF NOT EXISTS ": ""; + db.execSQL("CREATE TABLE " + constraint + "\"BOOT_COMPLETED_EVENT\" (" + // + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id + "\"DEVICE_ID\" TEXT NOT NULL ," + // 1: deviceId + "\"TIME\" INTEGER NOT NULL );"); // 2: time + } + + /** Drops the underlying database table. */ + public static void dropTable(Database db, boolean ifExists) { + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOT_COMPLETED_EVENT\""; + db.execSQL(sql); + } + + @Override + protected final void bindValues(DatabaseStatement stmt, BootCompletedEvent entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindString(2, entity.getDeviceId()); + stmt.bindLong(3, timeConverter.convertToDatabaseValue(entity.getTime())); + } + + @Override + protected final void bindValues(SQLiteStatement stmt, BootCompletedEvent entity) { + stmt.clearBindings(); + + Long id = entity.getId(); + if (id != null) { + stmt.bindLong(1, id); + } + stmt.bindString(2, entity.getDeviceId()); + stmt.bindLong(3, timeConverter.convertToDatabaseValue(entity.getTime())); + } + + @Override + public Long readKey(Cursor cursor, int offset) { + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); + } + + @Override + public BootCompletedEvent readEntity(Cursor cursor, int offset) { + BootCompletedEvent entity = new BootCompletedEvent( // + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id + cursor.getString(offset + 1), // deviceId + timeConverter.convertToEntityProperty(cursor.getLong(offset + 2)) // time + ); + return entity; + } + + @Override + public void readEntity(Cursor cursor, BootCompletedEvent entity, int offset) { + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); + entity.setDeviceId(cursor.getString(offset + 1)); + entity.setTime(timeConverter.convertToEntityProperty(cursor.getLong(offset + 2))); + } + + @Override + protected final Long updateKeyAfterInsert(BootCompletedEvent entity, long rowId) { + entity.setId(rowId); + return rowId; + } + + @Override + public Long getKey(BootCompletedEvent entity) { + if(entity != null) { + return entity.getId(); + } else { + return null; + } + } + + @Override + public boolean hasKey(BootCompletedEvent entity) { + return entity.getId() != null; + } + + @Override + protected final boolean isEntityUpdateable() { + return true; + } + +} diff --git a/app/src/main/java/org/literacyapp/analytics/dao/CustomDaoMaster.java b/app/src/main/java/org/literacyapp/analytics/dao/CustomDaoMaster.java index 07de96b..b9636f7 100644 --- a/app/src/main/java/org/literacyapp/analytics/dao/CustomDaoMaster.java +++ b/app/src/main/java/org/literacyapp/analytics/dao/CustomDaoMaster.java @@ -5,6 +5,7 @@ import android.util.Log; import org.greenrobot.greendao.database.Database; +import org.literacyapp.analytics.model.BootCompletedEvent; public class CustomDaoMaster extends DaoMaster { @@ -30,7 +31,8 @@ public void onUpgrade(Database db, int oldVersion, int newVersion) { DbMigrationHelper.migrate(db, LetterLearningEventDao.class, NumberLearningEventDao.class, - VideoLearningEventDao.class + VideoLearningEventDao.class, + BootCompletedEventDao.class ); } } diff --git a/app/src/main/java/org/literacyapp/analytics/dao/DaoMaster.java b/app/src/main/java/org/literacyapp/analytics/dao/DaoMaster.java index 6ed64e3..b6a5ef4 100644 --- a/app/src/main/java/org/literacyapp/analytics/dao/DaoMaster.java +++ b/app/src/main/java/org/literacyapp/analytics/dao/DaoMaster.java @@ -22,6 +22,7 @@ public class DaoMaster extends AbstractDaoMaster { /** Creates underlying database table using DAOs. */ public static void createAllTables(Database db, boolean ifNotExists) { ApplicationOpenedEventDao.createTable(db, ifNotExists); + BootCompletedEventDao.createTable(db, ifNotExists); LetterLearningEventDao.createTable(db, ifNotExists); NumberLearningEventDao.createTable(db, ifNotExists); VideoLearningEventDao.createTable(db, ifNotExists); @@ -30,6 +31,7 @@ public static void createAllTables(Database db, boolean ifNotExists) { /** Drops underlying database table using DAOs. */ public static void dropAllTables(Database db, boolean ifExists) { ApplicationOpenedEventDao.dropTable(db, ifExists); + BootCompletedEventDao.dropTable(db, ifExists); LetterLearningEventDao.dropTable(db, ifExists); NumberLearningEventDao.dropTable(db, ifExists); VideoLearningEventDao.dropTable(db, ifExists); @@ -52,6 +54,7 @@ public DaoMaster(SQLiteDatabase db) { public DaoMaster(Database db) { super(db, SCHEMA_VERSION); registerDaoClass(ApplicationOpenedEventDao.class); + registerDaoClass(BootCompletedEventDao.class); registerDaoClass(LetterLearningEventDao.class); registerDaoClass(NumberLearningEventDao.class); registerDaoClass(VideoLearningEventDao.class); diff --git a/app/src/main/java/org/literacyapp/analytics/dao/DaoSession.java b/app/src/main/java/org/literacyapp/analytics/dao/DaoSession.java index 8f0b887..9be519e 100644 --- a/app/src/main/java/org/literacyapp/analytics/dao/DaoSession.java +++ b/app/src/main/java/org/literacyapp/analytics/dao/DaoSession.java @@ -9,11 +9,13 @@ import org.greenrobot.greendao.internal.DaoConfig; import org.literacyapp.analytics.model.ApplicationOpenedEvent; +import org.literacyapp.analytics.model.BootCompletedEvent; import org.literacyapp.analytics.model.LetterLearningEvent; import org.literacyapp.analytics.model.NumberLearningEvent; import org.literacyapp.analytics.model.VideoLearningEvent; import org.literacyapp.analytics.dao.ApplicationOpenedEventDao; +import org.literacyapp.analytics.dao.BootCompletedEventDao; import org.literacyapp.analytics.dao.LetterLearningEventDao; import org.literacyapp.analytics.dao.NumberLearningEventDao; import org.literacyapp.analytics.dao.VideoLearningEventDao; @@ -28,11 +30,13 @@ public class DaoSession extends AbstractDaoSession { private final DaoConfig applicationOpenedEventDaoConfig; + private final DaoConfig bootCompletedEventDaoConfig; private final DaoConfig letterLearningEventDaoConfig; private final DaoConfig numberLearningEventDaoConfig; private final DaoConfig videoLearningEventDaoConfig; private final ApplicationOpenedEventDao applicationOpenedEventDao; + private final BootCompletedEventDao bootCompletedEventDao; private final LetterLearningEventDao letterLearningEventDao; private final NumberLearningEventDao numberLearningEventDao; private final VideoLearningEventDao videoLearningEventDao; @@ -44,6 +48,9 @@ public DaoSession(Database db, IdentityScopeType type, Map 0) { @@ -29,5 +40,38 @@ public void onReceive(Context context, Intent intent) { } else { Log.w(getClass().getName(), "Job scheduling failed. Error id: " + resultId); } + + + // Store event in database + BootCompletedEvent bootCompletedEvent = new BootCompletedEvent(); + bootCompletedEvent.setDeviceId(DeviceInfoHelper.getDeviceId(context)); + bootCompletedEvent.setTime(Calendar.getInstance()); + AnalyticsApplication analyticsApplication = (AnalyticsApplication) context.getApplicationContext(); + BootCompletedEventDao bootCompletedEventDao = analyticsApplication.getDaoSession().getBootCompletedEventDao(); + long id = bootCompletedEventDao.insert(bootCompletedEvent); + Log.i(getClass().getName(), "BootCompletedEvent saved in database with id " + id); + + // Store event in log file + // Expected format: id:1|deviceId:4113947bec18b7ad|time:1481916197273 + String logLine = "id:" + id + + "|deviceId:" + bootCompletedEvent.getDeviceId() + + "|time:" + bootCompletedEvent.getTime().getTimeInMillis() + + "\n"; + Log.i(getClass().getName(), "logLine: " + logLine); + String logsPath = Environment.getExternalStorageDirectory() + "/.literacyapp-analytics/events/device_" + bootCompletedEvent.getDeviceId(); + File logsDir = new File(logsPath); + Log.i(getClass().getName(), "logsDir: " + logsDir); + if (!logsDir.exists()) { + logsDir.mkdirs(); + } + String dateFormatted = (String) DateFormat.format("yyyy-MM-dd", Calendar.getInstance()); + String fileName = "boot_completed_events_" + dateFormatted + ".log"; + File logFile = new File(logsDir, fileName); + Log.i(getClass().getName(), "logFile: " + logFile); + try { + FileUtils.writeStringToFile(logFile, logLine, "UTF-8", true); + } catch (IOException e) { + Log.e(getClass().getName(), null, e); + } } }