Skip to content

Commit

Permalink
foregroundservice
Browse files Browse the repository at this point in the history
  • Loading branch information
wipedlifepotato committed Mar 15, 2024
1 parent 9251d5c commit 31f481a
Showing 1 changed file with 77 additions and 44 deletions.
121 changes: 77 additions & 44 deletions app/src/main/java/org/purplei2p/i2pd/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,51 @@

public class ForegroundService extends Service {
private static final String TAG = "FgService";
private volatile boolean shown;
private static ForegroundService instance;
private static volatile DaemonWrapper daemon;
private static final Object initDeinitLock = new Object();
private boolean shown;

private final DaemonWrapper.StateUpdateListener daemonStateUpdatedListener =
new DaemonWrapper.StateUpdateListener() {

@Override
public void daemonStateUpdate(DaemonWrapper.State oldValue, DaemonWrapper.State newValue) {
updateNotificationText();
}
};

private void updateNotificationText() {
try {
synchronized (initDeinitLock) {
if (shown) cancelNotification();
showNotification();
}
} catch (Throwable tr) {
Log.e(TAG,"error ignored",tr);
}
}


private NotificationManager notificationManager;
private static final int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "I2P_CHANNEL";

// Unique Identification Number for the Notification.
// We use it on Notification start, and to cancel it.
private static final int NOTIFICATION = 1;

/**
* Class for clients to access. Because we know this service always
* runs in the same process as its clients, we don't need to deal with
* IPC.
*/
public class LocalBinder extends Binder {
ForegroundService getService() {
return ForegroundService.this;
}
}

public static void init(DaemonWrapper daemonInstance) {
daemon = daemonInstance;
public static void init(DaemonWrapper daemon) {
ForegroundService.daemon = daemon;
initCheck();
}

Expand All @@ -53,7 +74,7 @@ private static void initCheck() {

@Override
public void onCreate() {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
instance = this;
initCheck();
}
Expand All @@ -65,7 +86,7 @@ private void setListener() {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Received start id " + startId + ": " + intent);
Log.d("ForegroundService", "Received start id " + startId + ": " + intent);
return START_STICKY;
}

Expand All @@ -89,65 +110,77 @@ private static void deinitCheck() {

private void cancelNotification() {
synchronized (initDeinitLock) {
notificationManager.cancel(NOTIFICATION_ID);
// Cancel the persistent notification.
notificationManager.cancel(NOTIFICATION);

stopForeground(true);

// Tell the user we stopped.
//Toast.makeText(this, R.string.i2pd_service_stopped, Toast.LENGTH_SHORT).show();
shown = false;
}
}

@Override
public IBinder onBind(Intent intent) {
return new LocalBinder();
return mBinder;
}

// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

/**
* Show a notification while this service is running.
*/
private void showNotification() {
synchronized (initDeinitLock) {
if (daemon != null) {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(daemon.getState().getStatusStringResourceId());
PendingIntent contentIntent = PendingIntent.getActivity(
this,
0,

// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, I2PDActivity.class),
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? FLAG_IMMUTABLE : 0
);
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? FLAG_IMMUTABLE : 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
// If earlier version channel ID is not used
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel() : "";

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
// Set the info for the views that show in the notification panel.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification_icon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setTicker(text)
.setWhen(System.currentTimeMillis())
.setContentTitle(getText(R.string.app_name))
.setContentText(text)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_notification_icon); // the status icon
builder = builder.setPriority(Notification.PRIORITY_DEFAULT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
builder = builder.setCategory(Notification.CATEGORY_SERVICE);
Notification notification = builder
.setTicker(text) // the status text
.setWhen(System.currentTimeMillis()) // the time stamp
.setContentTitle(getText(R.string.app_name)) // the label of the entry
.setContentText(text) // the contents of the entry
.setContentIntent(contentIntent) // The intent to send when the entry is clicked
.build();

startForeground(NOTIFICATION_ID, notification);
// Send the notification.
//mNM.notify(NOTIFICATION, notification);
startForeground(NOTIFICATION, notification);
shown = true;
}
}
}

@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_LOW
);
notificationManager.createNotificationChannel(channel);
}

private void updateNotificationText() {
try {
synchronized (initDeinitLock) {
if (shown) cancelNotification();
showNotification();
}
} catch (Throwable tr) {
Log.e(TAG, "Error ignored", tr);
}
private synchronized String createNotificationChannel() {
String channelId = getString(R.string.app_name);
CharSequence channelName = "I2Pd service";
NotificationChannel chan = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
//chan.setLightColor(Color.PURPLE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (service != null) service.createNotificationChannel(chan);
else Log.e(TAG, "error: NOTIFICATION_SERVICE is null");
return channelId;
}
}

0 comments on commit 31f481a

Please sign in to comment.