More complete DelegateWorker

This commit is contained in:
topjohnwu 2019-01-28 16:12:59 -05:00
parent 976c299657
commit 5df426380d
2 changed files with 87 additions and 1 deletions

View File

@ -10,7 +10,7 @@ import androidx.annotation.NonNull;
import androidx.work.Worker; import androidx.work.Worker;
import androidx.work.WorkerParameters; import androidx.work.WorkerParameters;
public class w<T extends DelegateWorker> extends Worker { public abstract class w<T extends DelegateWorker> extends Worker {
/* Wrapper class to workaround Proguard -keep class * extends Worker */ /* Wrapper class to workaround Proguard -keep class * extends Worker */
@ -22,6 +22,7 @@ public class w<T extends DelegateWorker> extends Worker {
try { try {
base = ((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) base = ((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0]).newInstance(); .getActualTypeArguments()[0]).newInstance();
base.setActualWorker(this);
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
@ -32,4 +33,10 @@ public class w<T extends DelegateWorker> extends Worker {
return Result.failure(); return Result.failure();
return base.doWork(); return base.doWork();
} }
@Override
public void onStopped() {
if (base != null)
base.onStopped();
}
} }

View File

@ -1,9 +1,88 @@
package com.topjohnwu.magisk.services; package com.topjohnwu.magisk.services;
import android.content.Context;
import android.net.Network;
import android.net.Uri;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executor;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.work.Data;
import androidx.work.ListenableWorker; import androidx.work.ListenableWorker;
import androidx.work.WorkerFactory;
import androidx.work.impl.utils.taskexecutor.TaskExecutor;
public abstract class DelegateWorker { public abstract class DelegateWorker {
private ListenableWorker worker;
@NonNull @NonNull
public abstract ListenableWorker.Result doWork(); public abstract ListenableWorker.Result doWork();
public void onStopped() {}
public void setActualWorker(ListenableWorker w) {
worker = w;
}
@NonNull
public Context getApplicationContext() {
return worker.getApplicationContext();
}
@NonNull
public UUID getId() {
return worker.getId();
}
@NonNull
public Data getInputData() {
return worker.getInputData();
}
@NonNull
public Set<String> getTags() {
return worker.getTags();
}
@NonNull
@RequiresApi(24)
public List<Uri> getTriggeredContentUris() {
return worker.getTriggeredContentUris();
}
@NonNull
@RequiresApi(24)
public List<String> getTriggeredContentAuthorities() {
return worker.getTriggeredContentAuthorities();
}
@Nullable
@RequiresApi(28)
public Network getNetwork() {
return worker.getNetwork();
}
public int getRunAttemptCount() {
return worker.getRunAttemptCount();
}
@NonNull
@MainThread
public ListenableFuture<ListenableWorker.Result> startWork() {
return worker.startWork();
}
public boolean isStopped() {
return worker.isStopped();
}
} }