Handle devices disallowing start of FcmFetchService.

Some devices are overzealous with battery management and disallow
starting services even when they're in response to a high-priority FCM
message (which should be allowed). So in these situations, we just
fall back to what we were doing before.
fork-5.53.8
Greyson Parrelli 2020-06-01 11:51:00 -04:00
rodzic 989b071a6d
commit c4ec0c9897
2 zmienionych plików z 29 dodań i 15 usunięć

Wyświetl plik

@ -1,10 +1,12 @@
package org.thoughtcrime.securesms.gcm;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.messaging.RemoteMessage;
@ -70,25 +72,29 @@ public class FcmFetchService extends Service {
}
private void fetch() {
BackgroundMessageRetriever retriever = ApplicationDependencies.getBackgroundMessageRetriever();
boolean success = retriever.retrieveMessages(this, new RestStrategy(), new RestStrategy());
if (success) {
Log.i(TAG, "Successfully retrieved messages.");
} else {
if (Build.VERSION.SDK_INT >= 26) {
Log.w(TAG, "Failed to retrieve messages. Scheduling on the system JobScheduler (API " + Build.VERSION.SDK_INT + ").");
FcmJobService.schedule(this);
} else {
Log.w(TAG, "Failed to retrieve messages. Scheduling on JobManager (API " + Build.VERSION.SDK_INT + ").");
ApplicationDependencies.getJobManager().add(new PushNotificationReceiveJob(this));
}
}
retrieveMessages(this);
if (activeCount.decrementAndGet() == 0) {
Log.e(TAG, "stopping");
stopSelf();
}
}
static void retrieveMessages(@NonNull Context context) {
BackgroundMessageRetriever retriever = ApplicationDependencies.getBackgroundMessageRetriever();
boolean success = retriever.retrieveMessages(context, new RestStrategy(), new RestStrategy());
if (success) {
Log.i(TAG, "Successfully retrieved messages.");
} else {
if (Build.VERSION.SDK_INT >= 26) {
Log.w(TAG, "Failed to retrieve messages. Scheduling on the system JobScheduler (API " + Build.VERSION.SDK_INT + ").");
FcmJobService.schedule(context);
} else {
Log.w(TAG, "Failed to retrieve messages. Scheduling on JobManager (API " + Build.VERSION.SDK_INT + ").");
ApplicationDependencies.getJobManager().add(new PushNotificationReceiveJob(context));
}
}
}
}

Wyświetl plik

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.gcm;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.NonNull;
@ -10,7 +11,9 @@ import com.google.firebase.messaging.RemoteMessage;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.FcmRefreshJob;
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.messages.RestStrategy;
import org.thoughtcrime.securesms.registration.PushChallengeRequest;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
@ -43,7 +46,12 @@ public class FcmReceiveService extends FirebaseMessagingService {
}
private static void handleReceivedNotification(Context context) {
context.startService(new Intent(context, FcmFetchService.class));
try {
context.startService(new Intent(context, FcmFetchService.class));
} catch (Exception e) {
Log.w(TAG, "Failed to start service. Falling back to legacy approach.");
FcmFetchService.retrieveMessages(context);
}
}
private static void handlePushChallenge(@NonNull String challenge) {