diff options
author | Brad Pitcher <bradpitcher@gmail.com> | 2019-04-10 08:21:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-10 08:21:55 -0700 |
commit | f2af5192e68d756380556f8420caff2d5095ae97 (patch) | |
tree | 66b79c10506df9fafb64e7a68792ee9913ddf005 /core/src/main/java/android | |
parent | 3019c4a9be26239b193537264fbfaca3f8221a1e (diff) | |
parent | d8a010ad487d186afedc85f61a8726813b72a82e (diff) | |
download | AntennaPod-f2af5192e68d756380556f8420caff2d5095ae97.zip |
Merge branch 'develop' into fix-2359
Diffstat (limited to 'core/src/main/java/android')
-rw-r--r-- | core/src/main/java/android/support/v4/app/SafeJobIntentService.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/core/src/main/java/android/support/v4/app/SafeJobIntentService.java b/core/src/main/java/android/support/v4/app/SafeJobIntentService.java new file mode 100644 index 000000000..c07c409ee --- /dev/null +++ b/core/src/main/java/android/support/v4/app/SafeJobIntentService.java @@ -0,0 +1,118 @@ +package android.support.v4.app; + +import android.app.job.JobParameters; +import android.app.job.JobServiceEngine; +import android.app.job.JobWorkItem; +import android.content.Intent; +import android.os.Build; +import android.os.IBinder; +import android.support.annotation.RequiresApi; +import android.util.Log; + + +public abstract class SafeJobIntentService extends JobIntentService { + + @Override + public void onCreate() { + super.onCreate(); + if (Build.VERSION.SDK_INT >= 26) { + mJobImpl = new SafeJobServiceEngineImpl(this); + } + } + + /** + * Implementation of a safe JobServiceEngine for interaction with JobIntentService. + */ + @RequiresApi(26) + static final class SafeJobServiceEngineImpl extends JobServiceEngine + implements JobIntentService.CompatJobEngine { + static final String TAG = "JobServiceEngineImpl"; + + static final boolean DEBUG = false; + + final JobIntentService mService; + final Object mLock = new Object(); + JobParameters mParams; + + final class WrapperWorkItem implements JobIntentService.GenericWorkItem { + final JobWorkItem mJobWork; + + WrapperWorkItem(JobWorkItem jobWork) { + mJobWork = jobWork; + } + + @Override + public Intent getIntent() { + return mJobWork.getIntent(); + } + + @Override + public void complete() { + synchronized (mLock) { + if (mParams != null) { + try { + mParams.completeWork(mJobWork); + } catch (SecurityException e) { + Log.e(TAG, Log.getStackTraceString(e)); + } + } + } + } + } + + SafeJobServiceEngineImpl(JobIntentService service) { + super(service); + mService = service; + } + + @Override + public IBinder compatGetBinder() { + return getBinder(); + } + + @Override + public boolean onStartJob(JobParameters params) { + if (DEBUG) Log.d(TAG, "onStartJob: " + params); + mParams = params; + // We can now start dequeuing work! + mService.ensureProcessorRunningLocked(false); + return true; + } + + @Override + public boolean onStopJob(JobParameters params) { + if (DEBUG) Log.d(TAG, "onStartJob: " + params); + boolean result = mService.doStopCurrentWork(); + synchronized (mLock) { + // Once we return, the job is stopped, so its JobParameters are no + // longer valid and we should not be doing anything with them. + mParams = null; + } + return result; + } + + /** + * Dequeue some work. + */ + @Override + public JobIntentService.GenericWorkItem dequeueWork() { + JobWorkItem work = null; + synchronized (mLock) { + if (mParams == null) { + return null; + } + try { + work = mParams.dequeueWork(); + } catch (SecurityException e) { + Log.e(TAG, Log.getStackTraceString(e)); + } + } + if (work != null) { + work.getIntent().setExtrasClassLoader(mService.getClassLoader()); + return new WrapperWorkItem(work); + } else { + return null; + } + } + } +} |