2019-05-31 17:12:41 -04:00
|
|
|
package org.thoughtcrime.securesms.gcm;
|
|
|
|
|
|
|
|
import android.app.job.JobInfo;
|
|
|
|
import android.app.job.JobParameters;
|
|
|
|
import android.app.job.JobService;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2019-07-15 11:12:26 -04:00
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
2019-05-31 17:12:41 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2019-07-03 18:23:26 -04:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2019-05-31 17:12:41 -04:00
|
|
|
import org.thoughtcrime.securesms.util.ServiceUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls down messages. Used when we fail to pull down messages in {@link FcmService}.
|
|
|
|
*/
|
|
|
|
@RequiresApi(26)
|
2019-07-15 11:12:26 -04:00
|
|
|
public class FcmJobService extends JobService {
|
2019-05-31 17:12:41 -04:00
|
|
|
|
|
|
|
private static final String TAG = FcmJobService.class.getSimpleName();
|
|
|
|
|
|
|
|
private static final int ID = 1337;
|
|
|
|
|
|
|
|
@RequiresApi(26)
|
|
|
|
public static void schedule(@NonNull Context context) {
|
|
|
|
JobInfo.Builder jobInfoBuilder = new JobInfo.Builder(ID, new ComponentName(context, FcmJobService.class))
|
|
|
|
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
|
|
|
|
.setBackoffCriteria(0, JobInfo.BACKOFF_POLICY_LINEAR)
|
|
|
|
.setPersisted(true);
|
|
|
|
|
|
|
|
ServiceUtil.getJobScheduler(context).schedule(jobInfoBuilder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onStartJob(JobParameters params) {
|
|
|
|
Log.d(TAG, "onStartJob()");
|
|
|
|
|
|
|
|
if (ApplicationContext.getInstance(getApplicationContext()).isAppVisible()) {
|
|
|
|
Log.i(TAG, "App is foregrounded. No need to run.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SignalExecutors.UNBOUNDED.execute(() -> {
|
2019-07-03 18:23:26 -04:00
|
|
|
Context context = getApplicationContext();
|
|
|
|
MessageRetriever retriever = ApplicationDependencies.getMessageRetriever();
|
|
|
|
boolean success = retriever.retrieveMessages(context, new RestStrategy(context));
|
|
|
|
|
|
|
|
if (success) {
|
2019-07-10 14:38:28 -04:00
|
|
|
Log.i(TAG, "Successfully retrieved messages.");
|
2019-05-31 17:12:41 -04:00
|
|
|
jobFinished(params, false);
|
2019-07-03 18:23:26 -04:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Failed to retrieve messages. Scheduling a retry.");
|
2019-05-31 17:12:41 -04:00
|
|
|
jobFinished(params, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onStopJob(JobParameters params) {
|
|
|
|
Log.d(TAG, "onStopJob()");
|
|
|
|
return TextSecurePreferences.getNeedsMessagePull(getApplicationContext());
|
|
|
|
}
|
|
|
|
}
|