mirror of
https://codeberg.org/Mercury-IM/Mercury-IM
synced 2024-06-20 10:34:50 +02:00
Refactor notification channel setup code
This commit is contained in:
parent
6d4033e923
commit
7f2fe135b3
|
@ -0,0 +1,52 @@
|
||||||
|
package org.mercury_im.messenger;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
|
||||||
|
import org.mercury_im.messenger.util.AbstractActivityLifecycleCallbacks;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep track of activities in "started" state.
|
||||||
|
* This will come in handy for things like XMPPs CSI.
|
||||||
|
*
|
||||||
|
* @see <a href="https://medium.com/@iamsadesh/android-how-to-detect-when-app-goes-background-foreground-fd5a4d331f8a>
|
||||||
|
* How to detect when app goes foreground/background</a>
|
||||||
|
*/
|
||||||
|
public class ClientStateHandler extends AbstractActivityLifecycleCallbacks {
|
||||||
|
|
||||||
|
private AtomicInteger activityReferences = new AtomicInteger(0);
|
||||||
|
private AtomicBoolean isActivityChangingConfiguration = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
private final List<ClientStateListener> listeners = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityStarted(Activity activity) {
|
||||||
|
if (activityReferences.incrementAndGet() == 1 && !isActivityChangingConfiguration.get()) {
|
||||||
|
for (ClientStateListener listener : listeners) {
|
||||||
|
listener.onClientInForeground();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityStopped(Activity activity) {
|
||||||
|
isActivityChangingConfiguration.set(activity.isChangingConfigurations());
|
||||||
|
if (activityReferences.decrementAndGet() == 0 && !isActivityChangingConfiguration.get()) {
|
||||||
|
for (ClientStateListener listener : listeners) {
|
||||||
|
listener.onClientInBackground();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addClientStateListener(ClientStateListener listener) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeClientStateListener(ClientStateListener listener) {
|
||||||
|
this.listeners.remove(listener);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,6 @@
|
||||||
package org.mercury_im.messenger;
|
package org.mercury_im.messenger;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.app.NotificationChannel;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
|
||||||
|
@ -13,10 +8,6 @@ import org.mercury_im.messenger.di.component.AppComponent;
|
||||||
import org.mercury_im.messenger.di.component.DaggerAppComponent;
|
import org.mercury_im.messenger.di.component.DaggerAppComponent;
|
||||||
import org.mercury_im.messenger.di.module.AppModule;
|
import org.mercury_im.messenger.di.module.AppModule;
|
||||||
import org.mercury_im.messenger.service.MercuryConnectionService;
|
import org.mercury_im.messenger.service.MercuryConnectionService;
|
||||||
import org.mercury_im.messenger.util.AbstractActivityLifecycleCallbacks;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
|
|
||||||
public class MercuryImApplication extends Application {
|
public class MercuryImApplication extends Application {
|
||||||
|
@ -27,13 +18,8 @@ public class MercuryImApplication extends Application {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MercuryImApplication INSTANCE;
|
private static MercuryImApplication INSTANCE;
|
||||||
AppComponent appComponent;
|
private AppComponent appComponent;
|
||||||
|
private ClientStateHandler clientStateHandler = new ClientStateHandler();
|
||||||
// Keep track of activities in "started" state.
|
|
||||||
// This will come in handy for CSI
|
|
||||||
// see https://medium.com/@iamsadesh/android-how-to-detect-when-app-goes-background-foreground-fd5a4d331f8a
|
|
||||||
private AtomicInteger activityReferences = new AtomicInteger(0);
|
|
||||||
private AtomicBoolean isActivityChangingConfiguration = new AtomicBoolean(false);
|
|
||||||
|
|
||||||
public static MercuryImApplication getApplication() {
|
public static MercuryImApplication getApplication() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
|
@ -42,29 +28,18 @@ public class MercuryImApplication extends Application {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
|
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
registerActivityLifecycleCallbacks(lifecycleCallbacks);
|
|
||||||
|
|
||||||
INSTANCE = this;
|
INSTANCE = this;
|
||||||
|
|
||||||
appComponent = createAppComponent();
|
appComponent = createAppComponent();
|
||||||
|
|
||||||
initializeNotificationChannels(this);
|
registerActivityLifecycleCallbacks(clientStateHandler);
|
||||||
|
Notifications.initializeNotificationChannels(this);
|
||||||
|
|
||||||
Intent serviceIntent = new Intent(getApplicationContext(), MercuryConnectionService.class);
|
startForegroundService();
|
||||||
|
|
||||||
serviceIntent.setAction(MercuryConnectionService.ACTION_START);
|
|
||||||
if (Build.VERSION.SDK_INT < 26) {
|
|
||||||
startService(serviceIntent);
|
|
||||||
} else {
|
|
||||||
startForegroundService(serviceIntent);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the Dependency Injection graph.
|
* Create the Dependency Injection graph.
|
||||||
* For testing, overwrite this method with custom modules.
|
|
||||||
*/
|
*/
|
||||||
public AppComponent createAppComponent() {
|
public AppComponent createAppComponent() {
|
||||||
AppComponent appComponent = DaggerAppComponent.builder()
|
AppComponent appComponent = DaggerAppComponent.builder()
|
||||||
|
@ -76,58 +51,17 @@ public class MercuryImApplication extends Application {
|
||||||
return appComponent;
|
return appComponent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initializeNotificationChannels(Context context) {
|
|
||||||
// Only necessary on Android O and upwards.
|
|
||||||
if (Build.VERSION.SDK_INT < 26) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
NotificationManager notificationManager =
|
|
||||||
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
|
|
||||||
// Foreground notification channel
|
|
||||||
String fName = getResources().getString(R.string.channel_name_foreground);
|
|
||||||
String fDescription = getResources().getString(R.string.channel_description_foreground);
|
|
||||||
|
|
||||||
@SuppressLint("WrongConstant")
|
|
||||||
NotificationChannel foreground = new NotificationChannel(Notifications.NOTIFICATION_CHANNEL__FOREGROUND_SERVICE,
|
|
||||||
fName, NotificationManager.IMPORTANCE_MIN);
|
|
||||||
foreground.setDescription(fDescription);
|
|
||||||
foreground.setShowBadge(false);
|
|
||||||
notificationManager.createNotificationChannel(foreground);
|
|
||||||
|
|
||||||
// Incoming Messages notification channel
|
|
||||||
String mName = getResources().getString(R.string.channel_name_message);
|
|
||||||
String mDescription = getResources().getString(R.string.channel_description_message);
|
|
||||||
|
|
||||||
@SuppressLint("WrongConstant")
|
|
||||||
NotificationChannel messages = new NotificationChannel(Notifications.NOTIFICATION_CHANNEL__NEW_MESSAGE,
|
|
||||||
mName, NotificationManager.IMPORTANCE_DEFAULT);
|
|
||||||
messages.setDescription(mDescription);
|
|
||||||
notificationManager.createNotificationChannel(messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AppComponent getAppComponent() {
|
public AppComponent getAppComponent() {
|
||||||
return appComponent;
|
return appComponent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final AbstractActivityLifecycleCallbacks lifecycleCallbacks = new AbstractActivityLifecycleCallbacks() {
|
private void startForegroundService() {
|
||||||
|
Intent foregroundService = new Intent(getApplicationContext(), MercuryConnectionService.class);
|
||||||
@Override
|
foregroundService.setAction(MercuryConnectionService.ACTION_START);
|
||||||
public void onActivityStarted(Activity activity) {
|
if (Build.VERSION.SDK_INT < 26) {
|
||||||
if (activityReferences.incrementAndGet() == 1 && !isActivityChangingConfiguration.get()) {
|
startService(foregroundService);
|
||||||
// App enters foreground
|
} else {
|
||||||
//connectionCenter.clientStateActive();
|
startForegroundService(foregroundService);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityStopped(Activity activity) {
|
|
||||||
isActivityChangingConfiguration.set(activity.isChangingConfigurations());
|
|
||||||
if (activityReferences.decrementAndGet() == 0 && !isActivityChangingConfiguration.get()) {
|
|
||||||
// App enters background
|
|
||||||
//connectionCenter.clientStateInactive();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package org.mercury_im.messenger;
|
package org.mercury_im.messenger;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
|
import android.app.NotificationChannel;
|
||||||
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.app.NotificationManagerCompat;
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
|
@ -18,7 +22,43 @@ public class Notifications {
|
||||||
public static final String NOTIFICATION_CHANNEL__DEBUG = "debug";
|
public static final String NOTIFICATION_CHANNEL__DEBUG = "debug";
|
||||||
|
|
||||||
// Notification IDs
|
// Notification IDs
|
||||||
public static final int FOREGROUND_SERVICE_ID = 1; // must not be 0
|
public static final int FOREGROUND_SERVICE_ID = 1;
|
||||||
|
|
||||||
|
public static void initializeNotificationChannels(Context context) {
|
||||||
|
if (Build.VERSION.SDK_INT < 26) {
|
||||||
|
// No not call below code on lower API levels
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
createForegroundNotificationChannel(context);
|
||||||
|
createMessageNotificationChannel(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(26)
|
||||||
|
private static void createForegroundNotificationChannel(Context context) {
|
||||||
|
NotificationManager notificationManager =
|
||||||
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
String channelName = context.getResources().getString(R.string.channel_name_foreground);
|
||||||
|
String channelDescription = context.getResources().getString(R.string.channel_description_foreground);
|
||||||
|
|
||||||
|
NotificationChannel channel = new NotificationChannel(
|
||||||
|
NOTIFICATION_CHANNEL__FOREGROUND_SERVICE, channelName, NotificationManager.IMPORTANCE_MIN);
|
||||||
|
channel.setDescription(channelDescription);
|
||||||
|
channel.setShowBadge(false);
|
||||||
|
notificationManager.createNotificationChannel(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(26)
|
||||||
|
private static void createMessageNotificationChannel(Context context) {
|
||||||
|
NotificationManager notificationManager =
|
||||||
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
String channelName = context.getResources().getString(R.string.channel_name_message);
|
||||||
|
String channelDescription = context.getResources().getString(R.string.channel_description_message);
|
||||||
|
|
||||||
|
NotificationChannel channel = new NotificationChannel(
|
||||||
|
NOTIFICATION_CHANNEL__NEW_MESSAGE, channelName, NotificationManager.IMPORTANCE_DEFAULT);
|
||||||
|
channel.setDescription(channelDescription);
|
||||||
|
notificationManager.createNotificationChannel(channel);
|
||||||
|
}
|
||||||
|
|
||||||
public static int directChatMessageReceived(Context context, DirectChat chat, String contactName, String body) {
|
public static int directChatMessageReceived(Context context, DirectChat chat, String contactName, String body) {
|
||||||
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
|
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package org.mercury_im.messenger;
|
||||||
|
|
||||||
|
public interface ClientStateListener {
|
||||||
|
|
||||||
|
void onClientInForeground();
|
||||||
|
|
||||||
|
void onClientInBackground();
|
||||||
|
}
|
Loading…
Reference in a new issue