Commit 1ec07c29 authored by Румен Петров's avatar Румен Петров
Browse files

INPROGRESS: run "path"-services as "foreground"

parent 7579dd78
......@@ -16,38 +16,114 @@
package com.termoneplus.securebox;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import com.termoneplus.services.PathResolver;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
public class MainService extends Service {
private static final String NOTIFICATION_CHANNEL_ID = "MAIN";
private static final int NOTIFICATION_ID = 101;
private final IBinder binder = new Binder();
private PathResolver path_resolver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
return binder;
}
@Override
public void onCreate() {
super.onCreate();
Notification notification = buildNotification();
// run service in the foreground
startForeground(NOTIFICATION_ID, notification);
path_resolver = new PathResolver();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_REDELIVER_INTENT;
return START_STICKY;
}
@Override
public void onDestroy() {
path_resolver.stop();
stopForeground(true);
super.onDestroy();
}
private Notification buildNotification() {
NotificationChannelCompat.create(this);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
NOTIFICATION_CHANNEL_ID)
.setContentTitle(getText(R.string.app_name))
.setSmallIcon(R.drawable.notification_sessions)
.setContentText(getText(R.string.path_notification))
.setTicker(getText(R.string.path_notification_ticker))
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setWhen(System.currentTimeMillis());
return builder.build();
}
private static class NotificationChannelCompat {
private static void create(MainService service) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O /*API Level 26*/) return;
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
Compat26.create(service);
}
@RequiresApi(26)
private static class Compat26 {
private static void create(MainService service) {
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
service.getString(R.string.path_notification_channel_name),
NotificationManager.IMPORTANCE_LOW);
channel.setDescription(service.getString(R.string.path_notification_channel_description));
channel.setShowBadge(false);
// Register the channel with the system ...
// Note we can't change the importance or other notification behaviors after this.
NotificationManager notificationManager = service.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
private class Binder extends android.os.Binder {
public MainService getService() {
return MainService.this;
}
}
}
......@@ -41,6 +41,11 @@
<string name="action_account">Virtual Account</string>
<string name="action_help">Help</string>
<string name="action_about">About</string>
<!-- service "Main" -->
<string name="path_notification_channel_name" translatable="false">SecureBox command helper</string>
<string name="path_notification_channel_description" translatable="false">SecureBox command helper services</string>
<string name="path_notification">(running)</string>
<string name="path_notification_ticker">SecureBox command helper is running</string>
<!-- activity "Manage Space" -->
<string name="manage_space_message">Application data is required and cannot be cleared!</string>
<!-- activity "Virtual Account" -->
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment