xenia/app/src/main/java/space/rocketnine/xenia/XeniaService.java

87 lines
2.9 KiB
Java

package space.rocketnine.xenia;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
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 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);
}
}
@Override
public void onCreate() {
Log.d("xenia", "service oncreate");
createNotificationChannel();
Notification.Builder builder = new Notification.Builder(this);
if (Build.VERSION.SDK_INT >= 26) {
builder.setChannelId(channelID);
}
builder.setSmallIcon(R.drawable.ic_stat_name);
builder.setContentText("Xenia is running. Tap to open");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder.setContentIntent(pendingIntent);
startForeground(1337, builder.build());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("xenia", "service starting");
try {
Gmitohtml.startDaemon("127.0.0.1:1967");
} catch (Exception e) {
e.printStackTrace();
}
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
Log.d("xenia", "service destroyed");
}
}