NekoX/TMessagesProj/src/main/java/org/telegram/messenger/KeepAliveJob.java

91 lines
2.7 KiB
Java
Raw Normal View History

2018-07-30 04:07:02 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2018-07-30 04:07:02 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2018.
*/
package org.telegram.messenger;
import android.content.Intent;
import org.telegram.messenger.support.JobIntentService;
import java.util.concurrent.CountDownLatch;
public class KeepAliveJob extends JobIntentService {
private static volatile CountDownLatch countDownLatch;
private static volatile boolean startingJob;
private static final Object sync = new Object();
public static void startJob() {
2021-09-20 07:54:41 +02:00
Utilities.globalQueue.postRunnable(() -> {
if (startingJob || countDownLatch != null) {
return;
}
try {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("starting keep-alive job");
2018-07-30 04:07:02 +02:00
}
2021-09-20 07:54:41 +02:00
synchronized (sync) {
startingJob = true;
2018-07-30 04:07:02 +02:00
}
2021-09-20 07:54:41 +02:00
enqueueWork(ApplicationLoader.applicationContext, KeepAliveJob.class, 1000, new Intent());
} catch (Exception ignore) {
2018-07-30 04:07:02 +02:00
}
});
}
private static void finishJobInternal() {
synchronized (sync) {
if (countDownLatch != null) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("finish keep-alive job");
}
countDownLatch.countDown();
}
if (startingJob) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("finish queued keep-alive job");
}
startingJob = false;
}
}
}
public static void finishJob() {
2021-09-20 07:54:41 +02:00
Utilities.globalQueue.postRunnable(KeepAliveJob::finishJobInternal);
2018-07-30 04:07:02 +02:00
}
2021-09-20 07:54:41 +02:00
private static Runnable finishJobByTimeoutRunnable = KeepAliveJob::finishJobInternal;
2018-07-30 04:07:02 +02:00
@Override
protected void onHandleWork(Intent intent) {
synchronized (sync) {
if (!startingJob) {
return;
}
countDownLatch = new CountDownLatch(1);
}
if (BuildVars.LOGS_ENABLED) {
FileLog.d("started keep-alive job");
}
Utilities.globalQueue.postRunnable(finishJobByTimeoutRunnable, 60 * 1000);
try {
countDownLatch.await();
} catch (Throwable ignore) {
}
Utilities.globalQueue.cancelRunnable(finishJobByTimeoutRunnable);
synchronized (sync) {
countDownLatch = null;
}
if (BuildVars.LOGS_ENABLED) {
FileLog.d("ended keep-alive job");
}
}
}