FCM improvements.

This commit is contained in:
Greyson Parrelli
2019-02-15 11:40:03 -08:00
parent 4dbe165c18
commit a6564f8f84
8 changed files with 89 additions and 58 deletions

View File

@@ -19,6 +19,7 @@ import org.thoughtcrime.securesms.service.GenericForegroundService;
import org.thoughtcrime.securesms.util.PowerManagerCompat;
import org.thoughtcrime.securesms.util.ServiceUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.WakeLockUtil;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
@@ -36,6 +37,7 @@ public class FcmService extends FirebaseMessagingService implements InjectableTy
private static final String TAG = FcmService.class.getSimpleName();
private static final Executor MESSAGE_EXECUTOR = SignalExecutors.newCachedSingleThreadExecutor("FcmMessageProcessing");
private static final String WAKE_LOCK_TAG = "FcmMessageProcessing";
@Inject SignalServiceMessageReceiver messageReceiver;
@@ -45,7 +47,10 @@ public class FcmService extends FirebaseMessagingService implements InjectableTy
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i(TAG, "FCM message... Original Priority: " + remoteMessage.getOriginalPriority() + ", Actual Priority: " + remoteMessage.getPriority());
ApplicationContext.getInstance(getApplicationContext()).injectDependencies(this);
handleReceivedNotification(getApplicationContext());
WakeLockUtil.runWithLock(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK, 60000, WAKE_LOCK_TAG, () -> {
handleReceivedNotification(getApplicationContext());
});
}
@Override

View File

@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.gcm;
import android.support.annotation.WorkerThread;
import android.text.TextUtils;
import com.google.firebase.iid.FirebaseInstanceId;
@@ -23,7 +24,7 @@ public final class FcmUtil {
AtomicReference<String> token = new AtomicReference<>(null);
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
if (task.isSuccessful() && task.getResult() != null) {
if (task.isSuccessful() && task.getResult() != null && !TextUtils.isEmpty(task.getResult().getToken())) {
token.set(task.getResult().getToken());
} else {
Log.w(TAG, "Failed to get the token.", task.getException());

View File

@@ -91,8 +91,13 @@ public class FcmRefreshJob extends ContextJob implements InjectableType {
Optional<String> token = FcmUtil.getToken();
if (token.isPresent()) {
if (!token.get().equals(TextSecurePreferences.getFcmToken(context))) {
Log.i(TAG, "New token differs from the old token.");
String oldToken = TextSecurePreferences.getFcmToken(context);
if (!token.get().equals(oldToken)) {
int oldLength = oldToken != null ? oldToken.length() : -1;
Log.i(TAG, "Token changed. oldLength: " + oldLength + " newLength: " + token.get().length());
} else {
Log.i(TAG, "Token didn't change.");
}
textSecureAccountManager.setGcmId(token);

View File

@@ -0,0 +1,56 @@
package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.logging.Log;
public class WakeLockUtil {
private static final String TAG = WakeLockUtil.class.getSimpleName();
/**
* Run a runnable with a wake lock. Ensures that the lock is safely acquired and released.
*/
public static void runWithLock(@NonNull Context context, int lockType, long timeout, @NonNull String tag, @NonNull Runnable task) {
WakeLock wakeLock = null;
try {
wakeLock = acquire(context, lockType, timeout, tag);
task.run();
} finally {
if (wakeLock != null) {
release(wakeLock, tag);
}
}
}
public static WakeLock acquire(@NonNull Context context, int lockType, long timeout, @NonNull String tag) {
try {
PowerManager powerManager = ServiceUtil.getPowerManager(context);
WakeLock wakeLock = powerManager.newWakeLock(lockType, tag);
wakeLock.acquire(timeout);
Log.d(TAG, "Acquired wakelock with tag: " + tag);
return wakeLock;
} catch (Exception e) {
Log.w(TAG, "Failed to acquire wakelock with tag: " + tag, e);
return null;
}
}
public static void release(@NonNull WakeLock wakeLock, @NonNull String tag) {
try {
if (wakeLock.isHeld()) {
wakeLock.release();
Log.d(TAG, "Released wakelock with tag: " + tag);
} else {
Log.d(TAG, "Wakelock wasn't held at time of release: " + tag);
}
} catch (Exception e) {
Log.w(TAG, "Failed to release wakelock with tag: " + tag, e);
}
}
}