Shut down daemon after 10 minutes of inactivity
This commit is contained in:
parent
a43b58c5c8
commit
b9f0680195
3 changed files with 34 additions and 8 deletions
|
@ -1,2 +1,5 @@
|
|||
0.1.1:
|
||||
- Shut down daemon after 10 minutes of inactivity
|
||||
|
||||
0.1.0:
|
||||
- Initial release
|
||||
|
|
|
@ -10,8 +10,8 @@ android {
|
|||
applicationId "space.rocketnine.xenia"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 30
|
||||
versionCode 10
|
||||
versionName "0.1.0"
|
||||
versionCode 11
|
||||
versionName "0.1.1"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ android {
|
|||
|
||||
ext {
|
||||
// https://gitlab.com/tslocum/gmitohtml
|
||||
gmitohtmlVersion = "72c8172ab88880a02fbc50fb15773b30959ccae8"
|
||||
gmitohtmlVersion = "fb5e7f4ea4639983fcf950ad6f5c38333c9b7208"
|
||||
}
|
||||
|
||||
task bindLibrary(type: Exec) {
|
||||
|
|
|
@ -11,27 +11,48 @@ import android.os.Build;
|
|||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import space.rocketnine.gmitohtml.Gmitohtml;
|
||||
|
||||
public class XeniaService extends Service {
|
||||
private final String channelID = "space.rocketnine.xenia";
|
||||
public Context context = this;
|
||||
public Handler handler = null;
|
||||
private final String channelName = "Xenia";
|
||||
private final String channelDescription = "Foreground service";
|
||||
private final Handler handler = new Handler();
|
||||
private final long timeout = 600; // Seconds
|
||||
|
||||
private final Runnable shutDown = () -> {
|
||||
Intent intent = new Intent(getApplicationContext(), XeniaService.class);
|
||||
getApplication().stopService(intent);
|
||||
|
||||
int pid = android.os.Process.myPid();
|
||||
android.os.Process.killProcess(pid);
|
||||
};
|
||||
|
||||
private final Runnable checkTimeout = () -> {
|
||||
if ((System.currentTimeMillis() / 1000) - Gmitohtml.lastRequestTime() >= timeout) {
|
||||
Toast.makeText(getApplicationContext(), "Shutting Xenia down due to inactivity", Toast.LENGTH_LONG).show();
|
||||
|
||||
handler.postDelayed(shutDown, 60000);
|
||||
return;
|
||||
}
|
||||
|
||||
scheduleCheckTimeout();
|
||||
};
|
||||
|
||||
private void scheduleCheckTimeout() {
|
||||
handler.postDelayed(checkTimeout, 60000);
|
||||
}
|
||||
|
||||
private void createNotificationChannel() {
|
||||
// Create the NotificationChannel, but only on API 26+ because
|
||||
// the NotificationChannel class is new and not in the support library
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
CharSequence name = channelName;
|
||||
String description = channelDescription;
|
||||
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
||||
NotificationChannel channel = new NotificationChannel(channelID, name, importance);
|
||||
channel.setDescription(description);
|
||||
// Register the channel with the system; you can't change the importance
|
||||
// or other notification behaviors after this
|
||||
NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
}
|
||||
|
@ -69,6 +90,8 @@ public class XeniaService extends Service {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
scheduleCheckTimeout();
|
||||
|
||||
// If we get killed, after returning from here, restart
|
||||
return START_STICKY;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue