-
Notifications
You must be signed in to change notification settings - Fork 42
/
WidgetService.java
75 lines (60 loc) · 2.08 KB
/
WidgetService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.opendashcam;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.view.WindowManager;
import com.opendashcam.models.Widget;
public class WidgetService extends Service {
private WindowManager windowManager;
private Widget overlayWidget;
private PowerManager.WakeLock mWakeLock;
@Override
public IBinder onBind(Intent intent) {
// Return the communication channel to the service.
// Not used
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
overlayWidget = new Widget(this, windowManager);
overlayWidget.show();
// Start in foreground to avoid unexpected kill
startForeground(
Util.FOREGROUND_NOTIFICATION_ID,
Util.createStatusBarNotification(this)
);
//Prevent going to sleep mode while service is working
//https://developer.android.com/reference/android/os/PowerManager.html
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
if (powerManager != null) {
mWakeLock = powerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
WidgetService.class.getSimpleName()
);
mWakeLock.acquire();
}
}
@Override
public void onDestroy() {
// Remove rootView views from display
if (overlayWidget != null) {
overlayWidget.hide();
}
// Close DB connection
DBHelper dbHelper = DBHelper.getInstance(this);
dbHelper.close();
// Return to home screen
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
//remove wakelock
if (mWakeLock != null) {
mWakeLock.release();
}
stopForeground(true);
}
}