Skip to content
Snippets Groups Projects
Commit bfe85e85 authored by maniac103's avatar maniac103 Committed by mueller-ma
Browse files

Convert GcmRegistrationService to a JobIntentService. (#980)


* Convert GcmRegistrationService to a JobIntentService.

Otherwise crashes might occur when app is in the background and
connectivity changes on Android 8+.

Fixes #953

Signed-off-by: default avatarDanny Baumann <dannybaumann@web.de>

* Consistently schedule GcmRegistrationService via enqueueWork().

Signed-off-by: default avatarDanny Baumann <dannybaumann@web.de>

* Refactor all invocations of GcmRegistrationService for JobIntentService.

Signed-off-by: default avatarDanny Baumann <dannybaumann@web.de>

* Override CloudMessagingHelper for test.

We don't want to include the complexity of the full flavor
CloudMessagingHelper in the ConnectionFactoryTest.

Signed-off-by: default avatarDanny Baumann <dannybaumann@web.de>
parent 7ca70139
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,13 @@ ...@@ -14,6 +14,13 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver
android:name="org.openhab.habdroid.core.GcmRegistrationService$ProxyReceiver">
<intent-filter>
<action android:name="org.openhab.habdroid.action.HIDE_NOTIFICATION" />
</intent-filter>
</receiver>
<service <service
android:name="org.openhab.habdroid.core.GcmMessageListenerService" android:name="org.openhab.habdroid.core.GcmMessageListenerService"
android:exported="false"> android:exported="false">
...@@ -32,7 +39,7 @@ ...@@ -32,7 +39,7 @@
<service <service
android:name="org.openhab.habdroid.core.GcmRegistrationService" android:name="org.openhab.habdroid.core.GcmRegistrationService"
android:exported="false" /> android:permission="android.permission.BIND_JOB_SERVICE" />
<meta-data <meta-data
android:name="com.google.android.gms.version" android:name="com.google.android.gms.version"
......
...@@ -25,19 +25,14 @@ public class CloudMessagingHelper { ...@@ -25,19 +25,14 @@ public class CloudMessagingHelper {
public static void onConnectionUpdated(Context context, CloudConnection connection) { public static void onConnectionUpdated(Context context, CloudConnection connection) {
sRegistrationDone = false; sRegistrationDone = false;
if (connection != null) { if (connection != null) {
Intent intent = new Intent(context, GcmRegistrationService.class) GcmRegistrationService.scheduleRegistration(context);
.setAction(GcmRegistrationService.ACTION_REGISTER);
context.startService(intent);
} }
} }
public static void onNotificationSelected(Context context, Intent intent) { public static void onNotificationSelected(Context context, Intent intent) {
int notificationId = intent.getIntExtra(GcmMessageListenerService.EXTRA_NOTIFICATION_ID, -1); int notificationId = intent.getIntExtra(GcmMessageListenerService.EXTRA_NOTIFICATION_ID, -1);
if (notificationId >= 0) { if (notificationId >= 0) {
Intent serviceIntent = new Intent(context, GcmRegistrationService.class) GcmRegistrationService.scheduleHideNotification(context, notificationId);
.setAction(GcmRegistrationService.ACTION_HIDE_NOTIFICATION)
.putExtra(GcmRegistrationService.EXTRA_NOTIFICATION_ID, notificationId);
context.startService(serviceIntent);
} }
} }
......
...@@ -9,15 +9,11 @@ ...@@ -9,15 +9,11 @@
package org.openhab.habdroid.core; package org.openhab.habdroid.core;
import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService; import com.google.android.gms.iid.InstanceIDListenerService;
public class GcmInstanceIDListenerService extends InstanceIDListenerService { public class GcmInstanceIDListenerService extends InstanceIDListenerService {
@Override @Override
public void onTokenRefresh() { public void onTokenRefresh() {
Intent intent = new Intent(this, GcmRegistrationService.class) GcmRegistrationService.scheduleRegistration(this);
.setAction(GcmRegistrationService.ACTION_REGISTER);
startService(intent);
} }
} }
...@@ -116,15 +116,6 @@ public class GcmMessageListenerService extends GcmListenerService { ...@@ -116,15 +116,6 @@ public class GcmMessageListenerService extends GcmListenerService {
contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
} }
private PendingIntent makeDeleteIntent(int notificationId) {
Intent deleteIntent = new Intent(this, GcmRegistrationService.class)
.setAction(GcmRegistrationService.ACTION_HIDE_NOTIFICATION)
.putExtra(GcmRegistrationService.EXTRA_NOTIFICATION_ID, notificationId);
return PendingIntent.getService(this, notificationId,
deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private Notification makeNotification(String msg, String channelId, String icon, private Notification makeNotification(String msg, String channelId, String icon,
long timestamp, String persistedId, int notificationId) { long timestamp, String persistedId, int notificationId) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
...@@ -158,7 +149,7 @@ public class GcmMessageListenerService extends GcmListenerService { ...@@ -158,7 +149,7 @@ public class GcmMessageListenerService extends GcmListenerService {
.setSound(Uri.parse(toneSetting)) .setSound(Uri.parse(toneSetting))
.setContentText(msg) .setContentText(msg)
.setContentIntent(contentIntent) .setContentIntent(contentIntent)
.setDeleteIntent(makeDeleteIntent(notificationId)) .setDeleteIntent(GcmRegistrationService.createHideNotificationIntent(this, notificationId))
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setPublicVersion(publicVersion) .setPublicVersion(publicVersion)
.build(); .build();
...@@ -181,7 +172,7 @@ public class GcmMessageListenerService extends GcmListenerService { ...@@ -181,7 +172,7 @@ public class GcmMessageListenerService extends GcmListenerService {
.setContentText(text) .setContentText(text)
.setPublicVersion(publicVersion) .setPublicVersion(publicVersion)
.setContentIntent(clickIntent) .setContentIntent(clickIntent)
.setDeleteIntent(makeDeleteIntent(SUMMARY_NOTIFICATION_ID)) .setDeleteIntent(GcmRegistrationService.createHideNotificationIntent(this, SUMMARY_NOTIFICATION_ID))
.build(); .build();
} }
......
...@@ -9,12 +9,15 @@ ...@@ -9,12 +9,15 @@
package org.openhab.habdroid.core; package org.openhab.habdroid.core;
import android.app.IntentService; import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.Nullable; import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log; import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.gcm.GoogleCloudMessaging;
...@@ -29,19 +32,39 @@ import java.io.IOException; ...@@ -29,19 +32,39 @@ import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Locale; import java.util.Locale;
public class GcmRegistrationService extends IntentService { public class GcmRegistrationService extends JobIntentService {
private static final String TAG = GcmRegistrationService.class.getSimpleName(); private static final String TAG = GcmRegistrationService.class.getSimpleName();
static final String ACTION_REGISTER = "org.openhab.habdroid.action.REGISTER_GCM"; private static final int JOB_ID = 1000;
static final String ACTION_HIDE_NOTIFICATION = "org.openhab.habdroid.action.HIDE_NOTIFICATION";
static final String EXTRA_NOTIFICATION_ID = "notificationId";
public GcmRegistrationService() { private static final String ACTION_REGISTER = "org.openhab.habdroid.action.REGISTER_GCM";
super("GcmRegistrationService"); private static final String ACTION_HIDE_NOTIFICATION = "org.openhab.habdroid.action.HIDE_NOTIFICATION";
private static final String EXTRA_NOTIFICATION_ID = "notificationId";
static void scheduleRegistration(Context context) {
Intent intent = new Intent(context, GcmRegistrationService.class)
.setAction(GcmRegistrationService.ACTION_REGISTER);
JobIntentService.enqueueWork(context, GcmRegistrationService.class, JOB_ID, intent);
}
static void scheduleHideNotification(Context context, int notificationId) {
JobIntentService.enqueueWork(context, GcmRegistrationService.class, JOB_ID,
makeHideNotificationIntent(context, notificationId));
}
static PendingIntent createHideNotificationIntent(Context context, int notificationId) {
return ProxyReceiver.wrap(context, makeHideNotificationIntent(context, notificationId),
notificationId);
}
private static Intent makeHideNotificationIntent(Context context, int notificationId) {
return new Intent(context, GcmRegistrationService.class)
.setAction(GcmRegistrationService.ACTION_HIDE_NOTIFICATION)
.putExtra(GcmRegistrationService.EXTRA_NOTIFICATION_ID, notificationId);
} }
@Override @Override
protected void onHandleIntent(@Nullable Intent intent) { protected void onHandleWork(@NonNull Intent intent) {
ConnectionFactory.waitForInitialization(); ConnectionFactory.waitForInitialization();
CloudConnection connection = CloudConnection connection =
(CloudConnection) ConnectionFactory.getConnection(Connection.TYPE_CLOUD); (CloudConnection) ConnectionFactory.getConnection(Connection.TYPE_CLOUD);
...@@ -104,4 +127,19 @@ public class GcmRegistrationService extends IntentService { ...@@ -104,4 +127,19 @@ public class GcmRegistrationService extends IntentService {
sendBundle.putString("notificationId", String.valueOf(notificationId)); sendBundle.putString("notificationId", String.valueOf(notificationId));
gcm.send(senderId + "@gcm.googleapis.com", "1", sendBundle); gcm.send(senderId + "@gcm.googleapis.com", "1", sendBundle);
} }
public static class ProxyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent actual = intent.getParcelableExtra("intent");
JobIntentService.enqueueWork(context, GcmRegistrationService.class, JOB_ID, actual);
}
private static PendingIntent wrap(Context context, Intent intent, int id) {
Intent wrapped = new Intent(context, ProxyReceiver.class)
.putExtra("intent", intent);
return PendingIntent.getBroadcast(context, id,
wrapped, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
} }
/*
* Copyright (c) 2010-2018, openHAB.org and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.habdroid.core;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.StringRes;
import org.openhab.habdroid.core.connection.CloudConnection;
public class CloudMessagingHelper {
public static void onConnectionUpdated(Context context, CloudConnection connection) {
}
public static void onNotificationSelected(Context context, Intent intent) {
}
public static @StringRes int getPushNotificationStatusResId() {
return 0;
}
public static boolean isSupported() {
return false;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment