Skip to content

Commit 49e4ed0

Browse files
author
literacyapp
authored
Merge pull request #50 from jogrimst/master
#47 Add BootCompletedEvent
2 parents 417e91f + 2f54ba6 commit 49e4ed0

File tree

6 files changed

+257
-2
lines changed

6 files changed

+257
-2
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package org.literacyapp.analytics.dao;
2+
3+
import android.database.Cursor;
4+
import android.database.sqlite.SQLiteStatement;
5+
6+
import org.greenrobot.greendao.AbstractDao;
7+
import org.greenrobot.greendao.Property;
8+
import org.greenrobot.greendao.internal.DaoConfig;
9+
import org.greenrobot.greendao.database.Database;
10+
import org.greenrobot.greendao.database.DatabaseStatement;
11+
12+
import java.util.Calendar;
13+
import org.literacyapp.analytics.dao.converter.CalendarConverter;
14+
15+
import org.literacyapp.analytics.model.BootCompletedEvent;
16+
17+
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
18+
/**
19+
* DAO for table "BOOT_COMPLETED_EVENT".
20+
*/
21+
public class BootCompletedEventDao extends AbstractDao<BootCompletedEvent, Long> {
22+
23+
public static final String TABLENAME = "BOOT_COMPLETED_EVENT";
24+
25+
/**
26+
* Properties of entity BootCompletedEvent.<br/>
27+
* Can be used for QueryBuilder and for referencing column names.
28+
*/
29+
public static class Properties {
30+
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
31+
public final static Property DeviceId = new Property(1, String.class, "deviceId", false, "DEVICE_ID");
32+
public final static Property Time = new Property(2, long.class, "time", false, "TIME");
33+
}
34+
35+
private final CalendarConverter timeConverter = new CalendarConverter();
36+
37+
public BootCompletedEventDao(DaoConfig config) {
38+
super(config);
39+
}
40+
41+
public BootCompletedEventDao(DaoConfig config, DaoSession daoSession) {
42+
super(config, daoSession);
43+
}
44+
45+
/** Creates the underlying database table. */
46+
public static void createTable(Database db, boolean ifNotExists) {
47+
String constraint = ifNotExists? "IF NOT EXISTS ": "";
48+
db.execSQL("CREATE TABLE " + constraint + "\"BOOT_COMPLETED_EVENT\" (" + //
49+
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
50+
"\"DEVICE_ID\" TEXT NOT NULL ," + // 1: deviceId
51+
"\"TIME\" INTEGER NOT NULL );"); // 2: time
52+
}
53+
54+
/** Drops the underlying database table. */
55+
public static void dropTable(Database db, boolean ifExists) {
56+
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOT_COMPLETED_EVENT\"";
57+
db.execSQL(sql);
58+
}
59+
60+
@Override
61+
protected final void bindValues(DatabaseStatement stmt, BootCompletedEvent entity) {
62+
stmt.clearBindings();
63+
64+
Long id = entity.getId();
65+
if (id != null) {
66+
stmt.bindLong(1, id);
67+
}
68+
stmt.bindString(2, entity.getDeviceId());
69+
stmt.bindLong(3, timeConverter.convertToDatabaseValue(entity.getTime()));
70+
}
71+
72+
@Override
73+
protected final void bindValues(SQLiteStatement stmt, BootCompletedEvent entity) {
74+
stmt.clearBindings();
75+
76+
Long id = entity.getId();
77+
if (id != null) {
78+
stmt.bindLong(1, id);
79+
}
80+
stmt.bindString(2, entity.getDeviceId());
81+
stmt.bindLong(3, timeConverter.convertToDatabaseValue(entity.getTime()));
82+
}
83+
84+
@Override
85+
public Long readKey(Cursor cursor, int offset) {
86+
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
87+
}
88+
89+
@Override
90+
public BootCompletedEvent readEntity(Cursor cursor, int offset) {
91+
BootCompletedEvent entity = new BootCompletedEvent( //
92+
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
93+
cursor.getString(offset + 1), // deviceId
94+
timeConverter.convertToEntityProperty(cursor.getLong(offset + 2)) // time
95+
);
96+
return entity;
97+
}
98+
99+
@Override
100+
public void readEntity(Cursor cursor, BootCompletedEvent entity, int offset) {
101+
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
102+
entity.setDeviceId(cursor.getString(offset + 1));
103+
entity.setTime(timeConverter.convertToEntityProperty(cursor.getLong(offset + 2)));
104+
}
105+
106+
@Override
107+
protected final Long updateKeyAfterInsert(BootCompletedEvent entity, long rowId) {
108+
entity.setId(rowId);
109+
return rowId;
110+
}
111+
112+
@Override
113+
public Long getKey(BootCompletedEvent entity) {
114+
if(entity != null) {
115+
return entity.getId();
116+
} else {
117+
return null;
118+
}
119+
}
120+
121+
@Override
122+
public boolean hasKey(BootCompletedEvent entity) {
123+
return entity.getId() != null;
124+
}
125+
126+
@Override
127+
protected final boolean isEntityUpdateable() {
128+
return true;
129+
}
130+
131+
}

app/src/main/java/org/literacyapp/analytics/dao/CustomDaoMaster.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.util.Log;
66

77
import org.greenrobot.greendao.database.Database;
8+
import org.literacyapp.analytics.model.BootCompletedEvent;
89

910
public class CustomDaoMaster extends DaoMaster {
1011

@@ -30,7 +31,8 @@ public void onUpgrade(Database db, int oldVersion, int newVersion) {
3031
DbMigrationHelper.migrate(db,
3132
LetterLearningEventDao.class,
3233
NumberLearningEventDao.class,
33-
VideoLearningEventDao.class
34+
VideoLearningEventDao.class,
35+
BootCompletedEventDao.class
3436
);
3537
}
3638
}

app/src/main/java/org/literacyapp/analytics/dao/DaoMaster.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class DaoMaster extends AbstractDaoMaster {
2222
/** Creates underlying database table using DAOs. */
2323
public static void createAllTables(Database db, boolean ifNotExists) {
2424
ApplicationOpenedEventDao.createTable(db, ifNotExists);
25+
BootCompletedEventDao.createTable(db, ifNotExists);
2526
LetterLearningEventDao.createTable(db, ifNotExists);
2627
NumberLearningEventDao.createTable(db, ifNotExists);
2728
VideoLearningEventDao.createTable(db, ifNotExists);
@@ -30,6 +31,7 @@ public static void createAllTables(Database db, boolean ifNotExists) {
3031
/** Drops underlying database table using DAOs. */
3132
public static void dropAllTables(Database db, boolean ifExists) {
3233
ApplicationOpenedEventDao.dropTable(db, ifExists);
34+
BootCompletedEventDao.dropTable(db, ifExists);
3335
LetterLearningEventDao.dropTable(db, ifExists);
3436
NumberLearningEventDao.dropTable(db, ifExists);
3537
VideoLearningEventDao.dropTable(db, ifExists);
@@ -52,6 +54,7 @@ public DaoMaster(SQLiteDatabase db) {
5254
public DaoMaster(Database db) {
5355
super(db, SCHEMA_VERSION);
5456
registerDaoClass(ApplicationOpenedEventDao.class);
57+
registerDaoClass(BootCompletedEventDao.class);
5558
registerDaoClass(LetterLearningEventDao.class);
5659
registerDaoClass(NumberLearningEventDao.class);
5760
registerDaoClass(VideoLearningEventDao.class);

app/src/main/java/org/literacyapp/analytics/dao/DaoSession.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import org.greenrobot.greendao.internal.DaoConfig;
1010

1111
import org.literacyapp.analytics.model.ApplicationOpenedEvent;
12+
import org.literacyapp.analytics.model.BootCompletedEvent;
1213
import org.literacyapp.analytics.model.LetterLearningEvent;
1314
import org.literacyapp.analytics.model.NumberLearningEvent;
1415
import org.literacyapp.analytics.model.VideoLearningEvent;
1516

1617
import org.literacyapp.analytics.dao.ApplicationOpenedEventDao;
18+
import org.literacyapp.analytics.dao.BootCompletedEventDao;
1719
import org.literacyapp.analytics.dao.LetterLearningEventDao;
1820
import org.literacyapp.analytics.dao.NumberLearningEventDao;
1921
import org.literacyapp.analytics.dao.VideoLearningEventDao;
@@ -28,11 +30,13 @@
2830
public class DaoSession extends AbstractDaoSession {
2931

3032
private final DaoConfig applicationOpenedEventDaoConfig;
33+
private final DaoConfig bootCompletedEventDaoConfig;
3134
private final DaoConfig letterLearningEventDaoConfig;
3235
private final DaoConfig numberLearningEventDaoConfig;
3336
private final DaoConfig videoLearningEventDaoConfig;
3437

3538
private final ApplicationOpenedEventDao applicationOpenedEventDao;
39+
private final BootCompletedEventDao bootCompletedEventDao;
3640
private final LetterLearningEventDao letterLearningEventDao;
3741
private final NumberLearningEventDao numberLearningEventDao;
3842
private final VideoLearningEventDao videoLearningEventDao;
@@ -44,6 +48,9 @@ public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends Abstr
4448
applicationOpenedEventDaoConfig = daoConfigMap.get(ApplicationOpenedEventDao.class).clone();
4549
applicationOpenedEventDaoConfig.initIdentityScope(type);
4650

51+
bootCompletedEventDaoConfig = daoConfigMap.get(BootCompletedEventDao.class).clone();
52+
bootCompletedEventDaoConfig.initIdentityScope(type);
53+
4754
letterLearningEventDaoConfig = daoConfigMap.get(LetterLearningEventDao.class).clone();
4855
letterLearningEventDaoConfig.initIdentityScope(type);
4956

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

5663
applicationOpenedEventDao = new ApplicationOpenedEventDao(applicationOpenedEventDaoConfig, this);
64+
bootCompletedEventDao = new BootCompletedEventDao(bootCompletedEventDaoConfig, this);
5765
letterLearningEventDao = new LetterLearningEventDao(letterLearningEventDaoConfig, this);
5866
numberLearningEventDao = new NumberLearningEventDao(numberLearningEventDaoConfig, this);
5967
videoLearningEventDao = new VideoLearningEventDao(videoLearningEventDaoConfig, this);
6068

6169
registerDao(ApplicationOpenedEvent.class, applicationOpenedEventDao);
70+
registerDao(BootCompletedEvent.class, bootCompletedEventDao);
6271
registerDao(LetterLearningEvent.class, letterLearningEventDao);
6372
registerDao(NumberLearningEvent.class, numberLearningEventDao);
6473
registerDao(VideoLearningEvent.class, videoLearningEventDao);
6574
}
6675

6776
public void clear() {
6877
applicationOpenedEventDaoConfig.clearIdentityScope();
78+
bootCompletedEventDaoConfig.clearIdentityScope();
6979
letterLearningEventDaoConfig.clearIdentityScope();
7080
numberLearningEventDaoConfig.clearIdentityScope();
7181
videoLearningEventDaoConfig.clearIdentityScope();
@@ -75,6 +85,10 @@ public ApplicationOpenedEventDao getApplicationOpenedEventDao() {
7585
return applicationOpenedEventDao;
7686
}
7787

88+
public BootCompletedEventDao getBootCompletedEventDao() {
89+
return bootCompletedEventDao;
90+
}
91+
7892
public LetterLearningEventDao getLetterLearningEventDao() {
7993
return letterLearningEventDao;
8094
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.literacyapp.analytics.model;
2+
3+
import org.greenrobot.greendao.annotation.Convert;
4+
import org.greenrobot.greendao.annotation.Entity;
5+
import org.greenrobot.greendao.annotation.Generated;
6+
import org.greenrobot.greendao.annotation.Id;
7+
import org.greenrobot.greendao.annotation.NotNull;
8+
import org.literacyapp.analytics.dao.converter.CalendarConverter;
9+
10+
import java.util.Calendar;
11+
12+
@Entity
13+
public class BootCompletedEvent {
14+
15+
@Id(autoincrement = true)
16+
private Long id;
17+
18+
// TODO: replace with Device?
19+
@NotNull
20+
private String deviceId;
21+
22+
@NotNull
23+
@Convert(converter = CalendarConverter.class, columnType = Long.class)
24+
private Calendar time;
25+
26+
@Generated(hash = 1698024943)
27+
public BootCompletedEvent(Long id, @NotNull String deviceId,
28+
@NotNull Calendar time) {
29+
this.id = id;
30+
this.deviceId = deviceId;
31+
this.time = time;
32+
}
33+
34+
@Generated(hash = 447047454)
35+
public BootCompletedEvent() {
36+
}
37+
38+
public Long getId() {
39+
return this.id;
40+
}
41+
42+
public void setId(Long id) {
43+
this.id = id;
44+
}
45+
46+
public String getDeviceId() {
47+
return this.deviceId;
48+
}
49+
50+
public void setDeviceId(String deviceId) {
51+
this.deviceId = deviceId;
52+
}
53+
54+
public Calendar getTime() {
55+
return this.time;
56+
}
57+
58+
public void setTime(Calendar time) {
59+
this.time = time;
60+
}
61+
}

app/src/main/java/org/literacyapp/analytics/receiver/BootReceiver.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,21 @@
66
import android.content.ComponentName;
77
import android.content.Context;
88
import android.content.Intent;
9+
import android.os.Environment;
10+
import android.text.TextUtils;
11+
import android.text.format.DateFormat;
912
import android.util.Log;
1013

14+
import org.apache.commons.io.FileUtils;
15+
import org.literacyapp.analytics.AnalyticsApplication;
16+
import org.literacyapp.analytics.dao.BootCompletedEventDao;
17+
import org.literacyapp.analytics.model.BootCompletedEvent;
1118
import org.literacyapp.analytics.service.ScreenshotJobService;
19+
import org.literacyapp.analytics.util.DeviceInfoHelper;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.util.Calendar;
1224

1325
public class BootReceiver extends BroadcastReceiver {
1426

@@ -21,13 +33,45 @@ public void onReceive(Context context, Intent intent) {
2133
JobInfo.Builder builder = new JobInfo.Builder(1, componentName);
2234
builder.setPeriodic(5 * 60 * 1000); // Every 5 minutes
2335
JobInfo screenshotJobInfo = builder.build();
24-
2536
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
2637
int resultId = jobScheduler.schedule(screenshotJobInfo);
2738
if (resultId > 0) {
2839
Log.i(getClass().getName(), "Job scheduled with id: " + resultId);
2940
} else {
3041
Log.w(getClass().getName(), "Job scheduling failed. Error id: " + resultId);
3142
}
43+
44+
45+
// Store event in database
46+
BootCompletedEvent bootCompletedEvent = new BootCompletedEvent();
47+
bootCompletedEvent.setDeviceId(DeviceInfoHelper.getDeviceId(context));
48+
bootCompletedEvent.setTime(Calendar.getInstance());
49+
AnalyticsApplication analyticsApplication = (AnalyticsApplication) context.getApplicationContext();
50+
BootCompletedEventDao bootCompletedEventDao = analyticsApplication.getDaoSession().getBootCompletedEventDao();
51+
long id = bootCompletedEventDao.insert(bootCompletedEvent);
52+
Log.i(getClass().getName(), "BootCompletedEvent saved in database with id " + id);
53+
54+
// Store event in log file
55+
// Expected format: id:1|deviceId:4113947bec18b7ad|time:1481916197273
56+
String logLine = "id:" + id
57+
+ "|deviceId:" + bootCompletedEvent.getDeviceId()
58+
+ "|time:" + bootCompletedEvent.getTime().getTimeInMillis()
59+
+ "\n";
60+
Log.i(getClass().getName(), "logLine: " + logLine);
61+
String logsPath = Environment.getExternalStorageDirectory() + "/.literacyapp-analytics/events/device_" + bootCompletedEvent.getDeviceId();
62+
File logsDir = new File(logsPath);
63+
Log.i(getClass().getName(), "logsDir: " + logsDir);
64+
if (!logsDir.exists()) {
65+
logsDir.mkdirs();
66+
}
67+
String dateFormatted = (String) DateFormat.format("yyyy-MM-dd", Calendar.getInstance());
68+
String fileName = "boot_completed_events_" + dateFormatted + ".log";
69+
File logFile = new File(logsDir, fileName);
70+
Log.i(getClass().getName(), "logFile: " + logFile);
71+
try {
72+
FileUtils.writeStringToFile(logFile, logLine, "UTF-8", true);
73+
} catch (IOException e) {
74+
Log.e(getClass().getName(), null, e);
75+
}
3276
}
3377
}

0 commit comments

Comments
 (0)