Refactor notification channel setup code

This commit is contained in:
Paul Schaub 2019-12-13 11:05:33 +01:00
parent 6d4033e923
commit 7f2fe135b3
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
4 changed files with 114 additions and 80 deletions

View File

@ -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);
}
}

View File

@ -1,11 +1,6 @@
package org.mercury_im.messenger;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
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.module.AppModule;
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 {
@ -27,13 +18,8 @@ public class MercuryImApplication extends Application {
}
private static MercuryImApplication INSTANCE;
AppComponent appComponent;
// 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);
private AppComponent appComponent;
private ClientStateHandler clientStateHandler = new ClientStateHandler();
public static MercuryImApplication getApplication() {
return INSTANCE;
@ -42,29 +28,18 @@ public class MercuryImApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(lifecycleCallbacks);
INSTANCE = this;
appComponent = createAppComponent();
initializeNotificationChannels(this);
registerActivityLifecycleCallbacks(clientStateHandler);
Notifications.initializeNotificationChannels(this);
Intent serviceIntent = new Intent(getApplicationContext(), MercuryConnectionService.class);
serviceIntent.setAction(MercuryConnectionService.ACTION_START);
if (Build.VERSION.SDK_INT < 26) {
startService(serviceIntent);
} else {
startForegroundService(serviceIntent);
}
startForegroundService();
}
/**
* Create the Dependency Injection graph.
* For testing, overwrite this method with custom modules.
*/
public AppComponent createAppComponent() {
AppComponent appComponent = DaggerAppComponent.builder()
@ -76,58 +51,17 @@ public class MercuryImApplication extends Application {
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() {
return appComponent;
}
private final AbstractActivityLifecycleCallbacks lifecycleCallbacks = new AbstractActivityLifecycleCallbacks() {
@Override
public void onActivityStarted(Activity activity) {
if (activityReferences.incrementAndGet() == 1 && !isActivityChangingConfiguration.get()) {
// App enters foreground
//connectionCenter.clientStateActive();
}
private void startForegroundService() {
Intent foregroundService = new Intent(getApplicationContext(), MercuryConnectionService.class);
foregroundService.setAction(MercuryConnectionService.ACTION_START);
if (Build.VERSION.SDK_INT < 26) {
startService(foregroundService);
} else {
startForegroundService(foregroundService);
}
@Override
public void onActivityStopped(Activity activity) {
isActivityChangingConfiguration.set(activity.isChangingConfigurations());
if (activityReferences.decrementAndGet() == 0 && !isActivityChangingConfiguration.get()) {
// App enters background
//connectionCenter.clientStateInactive();
}
}
};
}
}

View File

@ -1,8 +1,12 @@
package org.mercury_im.messenger;
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
@ -18,7 +22,43 @@ public class Notifications {
public static final String NOTIFICATION_CHANNEL__DEBUG = "debug";
// 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) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);

View File

@ -0,0 +1,8 @@
package org.mercury_im.messenger;
public interface ClientStateListener {
void onClientInForeground();
void onClientInBackground();
}