Skip to content

Commit b65f5fe

Browse files
committed
Supports Context passing to LokiLogger
1 parent f857edd commit b65f5fe

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

app/src/main/java/com/example/roadquality/services/MainService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public IBinder onBind(Intent intent) {
5959
@RequiresApi(api = Build.VERSION_CODES.S)
6060
@Override
6161
public void onStart(Intent intent, int startId) {
62+
// This is to ensure the logger has a valid Context.
63+
this.logger = new LokiLogger(this, "MainService.java");
64+
6265
logger.log("onStart fired...");
6366
this.journey = new Journey(this);
6467
logger.log("new Journey() created...");
@@ -108,6 +111,9 @@ public void onStart(Intent intent, int startId) {
108111
public void onCreate() {
109112
super.onCreate();
110113

114+
// This is to ensure the logger has a valid Context.
115+
this.logger = new LokiLogger(this, "MainService.java");
116+
111117
logger.log("onCreate started...");
112118
accelerometerSensor = new AccelerometerSensor(this) {
113119
@Override

app/src/main/java/com/example/roadquality/utils/LokiLogger.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ public LokiLogger(String tag) {
2525
}
2626

2727
public LokiLogger(Context context, String tag) {
28-
if (context != null) {
29-
this.device_id = context.getSharedPreferences("Via Preferences", Context.MODE_PRIVATE)
30-
.getString("device_id", "device_id_not_set");
31-
} else {
32-
this.device_id = "device_id_not_set";
28+
try {
29+
if (context != null) {
30+
SharedPreferences sp = context.getSharedPreferences("Via Preferences", Context.MODE_PRIVATE);
31+
this.device_id = sp.getString("device_id", "device_id_not_set");
32+
} else {
33+
this.device_id = "device_id_not_set";
34+
}
35+
} catch (Exception e) {
36+
this.device_id = "no_context_for_logger";
3337
}
3438

3539
this.tag = tag;

0 commit comments

Comments
 (0)