Guest User

Untitled

a guest
Jul 6th, 2020
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public class KeepAliveJob extends JobIntentService {
  2.  
  3. private static volatile CountDownLatch countDownLatch;
  4. private static volatile boolean startingJob;
  5. private static final Object sync = new Object();
  6.  
  7. public static void startJob() {
  8. Utilities.globalQueue.postRunnable(new Runnable() {
  9. @Override
  10. public void run() {
  11. if (startingJob || countDownLatch != null) {
  12. return;
  13. }
  14. try {
  15. if (BuildVars.LOGS_ENABLED) {
  16. FileLog.d("starting keep-alive job");
  17. }
  18. synchronized (sync) {
  19. startingJob = true;
  20. }
  21. enqueueWork(ApplicationLoader.applicationContext, KeepAliveJob.class, 1000, new Intent());
  22. } catch (Exception ignore) {
  23.  
  24. }
  25. }
  26. });
  27. }
  28.  
  29. private static void finishJobInternal() {
  30. synchronized (sync) {
  31. if (countDownLatch != null) {
  32. if (BuildVars.LOGS_ENABLED) {
  33. FileLog.d("finish keep-alive job");
  34. }
  35. countDownLatch.countDown();
  36. }
  37. if (startingJob) {
  38. if (BuildVars.LOGS_ENABLED) {
  39. FileLog.d("finish queued keep-alive job");
  40. }
  41. startingJob = false;
  42. }
  43. }
  44. }
  45.  
  46. public static void finishJob() {
  47. Utilities.globalQueue.postRunnable(new Runnable() {
  48. @Override
  49. public void run() {
  50. finishJobInternal();
  51. }
  52. });
  53. }
  54.  
  55. private static Runnable finishJobByTimeoutRunnable = new Runnable() {
  56. @Override
  57. public void run() {
  58. finishJobInternal();
  59. }
  60. };
  61.  
  62. @Override
  63. protected void onHandleWork(Intent intent) {
  64. synchronized (sync) {
  65. if (!startingJob) {
  66. return;
  67. }
  68. countDownLatch = new CountDownLatch(1);
  69. }
  70. if (BuildVars.LOGS_ENABLED) {
  71. FileLog.d("started keep-alive job");
  72. }
  73. Utilities.globalQueue.postRunnable(finishJobByTimeoutRunnable, 60 * 1000);
  74. try {
  75. countDownLatch.await();
  76. } catch (Throwable ignore) {
  77.  
  78. }
  79. Utilities.globalQueue.cancelRunnable(finishJobByTimeoutRunnable);
  80. synchronized (sync) {
  81. countDownLatch = null;
  82. }
  83. if (BuildVars.LOGS_ENABLED) {
  84. FileLog.d("ended keep-alive job");
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment