62 lines
2.2 KiB
Java
62 lines
2.2 KiB
Java
package androidx.work.impl.background.systemalarm;
|
|
|
|
import android.content.Intent;
|
|
import androidx.annotation.MainThread;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.lifecycle.LifecycleService;
|
|
import androidx.work.Logger;
|
|
import androidx.work.impl.background.systemalarm.SystemAlarmDispatcher;
|
|
import androidx.work.impl.utils.WakeLocks;
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public class SystemAlarmService extends LifecycleService implements SystemAlarmDispatcher.CommandsCompletedListener {
|
|
private static final String TAG = Logger.tagWithPrefix("SystemAlarmService");
|
|
private SystemAlarmDispatcher mDispatcher;
|
|
private boolean mIsShutdown;
|
|
|
|
@MainThread
|
|
private void initializeDispatcher() {
|
|
SystemAlarmDispatcher systemAlarmDispatcher = new SystemAlarmDispatcher(this);
|
|
this.mDispatcher = systemAlarmDispatcher;
|
|
systemAlarmDispatcher.setCompletedListener(this);
|
|
}
|
|
|
|
@Override // androidx.work.impl.background.systemalarm.SystemAlarmDispatcher.CommandsCompletedListener
|
|
@MainThread
|
|
public void onAllCommandsCompleted() {
|
|
this.mIsShutdown = true;
|
|
Logger.get().debug(TAG, "All commands completed in dispatcher", new Throwable[0]);
|
|
WakeLocks.checkWakeLocks();
|
|
stopSelf();
|
|
}
|
|
|
|
@Override // androidx.lifecycle.LifecycleService, android.app.Service
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
initializeDispatcher();
|
|
this.mIsShutdown = false;
|
|
}
|
|
|
|
@Override // androidx.lifecycle.LifecycleService, android.app.Service
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
this.mIsShutdown = true;
|
|
this.mDispatcher.onDestroy();
|
|
}
|
|
|
|
@Override // androidx.lifecycle.LifecycleService, android.app.Service
|
|
public int onStartCommand(Intent intent, int i, int i2) {
|
|
super.onStartCommand(intent, i, i2);
|
|
if (this.mIsShutdown) {
|
|
Logger.get().info(TAG, "Re-initializing SystemAlarmDispatcher after a request to shut-down.", new Throwable[0]);
|
|
this.mDispatcher.onDestroy();
|
|
initializeDispatcher();
|
|
this.mIsShutdown = false;
|
|
}
|
|
if (intent == null) {
|
|
return 3;
|
|
}
|
|
this.mDispatcher.add(intent, i2);
|
|
return 3;
|
|
}
|
|
}
|