Skip to content

Commit

Permalink
Merge pull request #50 from jogrimst/master
Browse files Browse the repository at this point in the history
#47 Add BootCompletedEvent
  • Loading branch information
literacyapp committed May 27, 2017
2 parents 417e91f + 2f54ba6 commit 49e4ed0
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<BootCompletedEvent, Long> {

public static final String TABLENAME = "BOOT_COMPLETED_EVENT";

/**
* Properties of entity BootCompletedEvent.<br/>
* 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/literacyapp/analytics/dao/DaoSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,6 +48,9 @@ public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends Abstr
applicationOpenedEventDaoConfig = daoConfigMap.get(ApplicationOpenedEventDao.class).clone();
applicationOpenedEventDaoConfig.initIdentityScope(type);

bootCompletedEventDaoConfig = daoConfigMap.get(BootCompletedEventDao.class).clone();
bootCompletedEventDaoConfig.initIdentityScope(type);

letterLearningEventDaoConfig = daoConfigMap.get(LetterLearningEventDao.class).clone();
letterLearningEventDaoConfig.initIdentityScope(type);

Expand All @@ -54,18 +61,21 @@ public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends Abstr
videoLearningEventDaoConfig.initIdentityScope(type);

applicationOpenedEventDao = new ApplicationOpenedEventDao(applicationOpenedEventDaoConfig, this);
bootCompletedEventDao = new BootCompletedEventDao(bootCompletedEventDaoConfig, this);
letterLearningEventDao = new LetterLearningEventDao(letterLearningEventDaoConfig, this);
numberLearningEventDao = new NumberLearningEventDao(numberLearningEventDaoConfig, this);
videoLearningEventDao = new VideoLearningEventDao(videoLearningEventDaoConfig, this);

registerDao(ApplicationOpenedEvent.class, applicationOpenedEventDao);
registerDao(BootCompletedEvent.class, bootCompletedEventDao);
registerDao(LetterLearningEvent.class, letterLearningEventDao);
registerDao(NumberLearningEvent.class, numberLearningEventDao);
registerDao(VideoLearningEvent.class, videoLearningEventDao);
}

public void clear() {
applicationOpenedEventDaoConfig.clearIdentityScope();
bootCompletedEventDaoConfig.clearIdentityScope();
letterLearningEventDaoConfig.clearIdentityScope();
numberLearningEventDaoConfig.clearIdentityScope();
videoLearningEventDaoConfig.clearIdentityScope();
Expand All @@ -75,6 +85,10 @@ public ApplicationOpenedEventDao getApplicationOpenedEventDao() {
return applicationOpenedEventDao;
}

public BootCompletedEventDao getBootCompletedEventDao() {
return bootCompletedEventDao;
}

public LetterLearningEventDao getLetterLearningEventDao() {
return letterLearningEventDao;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.literacyapp.analytics.model;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.literacyapp.analytics.dao.converter.CalendarConverter;

import java.util.Calendar;

@Entity
public class BootCompletedEvent {

@Id(autoincrement = true)
private Long id;

// TODO: replace with Device?
@NotNull
private String deviceId;

@NotNull
@Convert(converter = CalendarConverter.class, columnType = Long.class)
private Calendar time;

@Generated(hash = 1698024943)
public BootCompletedEvent(Long id, @NotNull String deviceId,
@NotNull Calendar time) {
this.id = id;
this.deviceId = deviceId;
this.time = time;
}

@Generated(hash = 447047454)
public BootCompletedEvent() {
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getDeviceId() {
return this.deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public Calendar getTime() {
return this.time;
}

public void setTime(Calendar time) {
this.time = time;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;

import org.apache.commons.io.FileUtils;
import org.literacyapp.analytics.AnalyticsApplication;
import org.literacyapp.analytics.dao.BootCompletedEventDao;
import org.literacyapp.analytics.model.BootCompletedEvent;
import org.literacyapp.analytics.service.ScreenshotJobService;
import org.literacyapp.analytics.util.DeviceInfoHelper;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

public class BootReceiver extends BroadcastReceiver {

Expand All @@ -21,13 +33,45 @@ public void onReceive(Context context, Intent intent) {
JobInfo.Builder builder = new JobInfo.Builder(1, componentName);
builder.setPeriodic(5 * 60 * 1000); // Every 5 minutes
JobInfo screenshotJobInfo = builder.build();

JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
int resultId = jobScheduler.schedule(screenshotJobInfo);
if (resultId > 0) {
Log.i(getClass().getName(), "Job scheduled with id: " + resultId);
} 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);
}
}
}

0 comments on commit 49e4ed0

Please sign in to comment.