MD5 校验值:1036132062d4d223cd1a714f5bbea98b
JobIntentService.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package im.skmzhmurqt.messenger.support; import android.app.Service; import android.app.job.JobInfo; import android.app.job.JobParameters; import android.app.job.JobScheduler; import android.app.job.JobServiceEngine; import android.app.job.JobWorkItem; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Build; import android.os.IBinder; import android.os.PowerManager; import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy; import im.skmzhmurqt.messenger.FileLog; import java.util.ArrayList; import java.util.HashMap; public abstract class JobIntentService extends Service { static final boolean DEBUG = false; static final String TAG = "JobIntentService"; final ArrayList<CompatWorkItem> mCompatQueue; WorkEnqueuer mCompatWorkEnqueuer; CommandProcessor mCurProcessor; CompatJobEngine mJobImpl; static final Object sLock = new Object(); static final HashMap<ComponentName, WorkEnqueuer> sClassWorkEnqueuer = new HashMap<>(); boolean mInterruptIfStopped = false; boolean mStopped = false; boolean mDestroyed = false; public interface CompatJobEngine { IBinder compatGetBinder(); GenericWorkItem dequeueWork(); } public interface GenericWorkItem { void complete(); Intent getIntent(); } protected abstract void onHandleWork(Intent intent); public static abstract class WorkEnqueuer { final ComponentName mComponentName; boolean mHasJobId; int mJobId; abstract void enqueueWork(Intent intent); WorkEnqueuer(Context context, ComponentName cn2) { this.mComponentName = cn2; } void ensureJobId(int jobId) { if (!this.mHasJobId) { this.mHasJobId = true; this.mJobId = jobId; } else if (this.mJobId != jobId) { throw new IllegalArgumentException("Given job ID " + jobId + " is different than previous " + this.mJobId); } } public void serviceStartReceived() { } public void serviceProcessingStarted() { } public void serviceProcessingFinished() { } } public static final class CompatWorkEnqueuer extends WorkEnqueuer { private final Context mContext; private final PowerManager.WakeLock mLaunchWakeLock; boolean mLaunchingService; private final PowerManager.WakeLock mRunWakeLock; boolean mServiceProcessing; CompatWorkEnqueuer(Context context, ComponentName cn2) { super(context, cn2); this.mContext = context.getApplicationContext(); PowerManager pm = (PowerManager) context.getSystemService("power"); PowerManager.WakeLock newWakeLock = pm.newWakeLock(1, cn2.getClassName() + ":launch"); this.mLaunchWakeLock = newWakeLock; newWakeLock.setReferenceCounted(false); PowerManager.WakeLock newWakeLock2 = pm.newWakeLock(1, cn2.getClassName() + ":run"); this.mRunWakeLock = newWakeLock2; newWakeLock2.setReferenceCounted(false); } @Override void enqueueWork(Intent work) { Intent intent = new Intent(work); intent.setComponent(this.mComponentName); if (this.mContext.startService(intent) != null) { synchronized (this) { if (!this.mLaunchingService) { this.mLaunchingService = true; if (!this.mServiceProcessing) { this.mLaunchWakeLock.acquire(DefaultLoadErrorHandlingPolicy.DEFAULT_TRACK_BLACKLIST_MS); } } } } } @Override public void serviceStartReceived() { synchronized (this) { this.mLaunchingService = false; } } @Override public void serviceProcessingStarted() { synchronized (this) { if (!this.mServiceProcessing) { this.mServiceProcessing = true; try { this.mRunWakeLock.acquire(120000L); this.mLaunchWakeLock.release(); } catch (Throwable e) { FileLog.e(e); this.mServiceProcessing = false; } } } } @Override public void serviceProcessingFinished() { synchronized (this) { if (this.mServiceProcessing) { if (this.mLaunchingService) { this.mLaunchWakeLock.acquire(DefaultLoadErrorHandlingPolicy.DEFAULT_TRACK_BLACKLIST_MS); } this.mServiceProcessing = false; this.mRunWakeLock.release(); } } } } static final class JobServiceEngineImpl extends JobServiceEngine implements CompatJobEngine { static final boolean DEBUG = false; static final String TAG = "JobServiceEngineImpl"; final Object mLock; JobParameters mParams; final JobIntentService mService; final class WrapperWorkItem implements GenericWorkItem { final JobWorkItem mJobWork; WrapperWorkItem(JobWorkItem jobWork) { this.mJobWork = jobWork; } @Override public Intent getIntent() { return this.mJobWork.getIntent(); } @Override public void complete() { synchronized (JobServiceEngineImpl.this.mLock) { if (JobServiceEngineImpl.this.mParams != null) { JobServiceEngineImpl.this.mParams.completeWork(this.mJobWork); } } } } JobServiceEngineImpl(JobIntentService service) { super(service); this.mLock = new Object(); this.mService = service; } @Override public IBinder compatGetBinder() { return getBinder(); } @Override public boolean onStartJob(JobParameters params) { this.mParams = params; this.mService.ensureProcessorRunningLocked(false); return true; } @Override public boolean onStopJob(JobParameters params) { boolean result = this.mService.doStopCurrentWork(); synchronized (this.mLock) { this.mParams = null; } return result; } @Override public GenericWorkItem dequeueWork() { JobWorkItem work = null; synchronized (this.mLock) { if (this.mParams == null) { return null; } try { work = this.mParams.dequeueWork(); } catch (Throwable th) { } if (work == null) { return null; } work.getIntent().setExtrasClassLoader(this.mService.getClassLoader()); return new WrapperWorkItem(work); } } } public static final class JobWorkEnqueuer extends WorkEnqueuer { private final JobInfo mJobInfo; private final JobScheduler mJobScheduler; JobWorkEnqueuer(Context context, ComponentName cn2, int jobId) { super(context, cn2); ensureJobId(jobId); JobInfo.Builder b = new JobInfo.Builder(jobId, this.mComponentName); this.mJobInfo = b.setOverrideDeadline(0L).setRequiredNetworkType(1).build(); this.mJobScheduler = (JobScheduler) context.getApplicationContext().getSystemService("jobscheduler"); } @Override void enqueueWork(Intent work) { this.mJobScheduler.enqueue(this.mJobInfo, new JobWorkItem(work)); } } public final class CompatWorkItem implements GenericWorkItem { final Intent mIntent; final int mStartId; CompatWorkItem(Intent intent, int startId) { this.mIntent = intent; this.mStartId = startId; } @Override public Intent getIntent() { return this.mIntent; } @Override public void complete() { JobIntentService.this.stopSelf(this.mStartId); } } public final class CommandProcessor extends AsyncTask<Void, Void, Void> { CommandProcessor() { } @Override public Void doInBackground(Void... params) { while (true) { GenericWorkItem work = JobIntentService.this.dequeueWork(); if (work != null) { JobIntentService.this.onHandleWork(work.getIntent()); try { work.complete(); } catch (Throwable th) { } } else { return null; } } } @Override public void onCancelled(Void aVoid) { JobIntentService.this.processorFinished(); } @Override public void onPostExecute(Void aVoid) { JobIntentService.this.processorFinished(); } } public JobIntentService() { if (Build.VERSION.SDK_INT >= 26) { this.mCompatQueue = null; } else { this.mCompatQueue = new ArrayList<>(); } } @Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= 26) { this.mJobImpl = new JobServiceEngineImpl(this); this.mCompatWorkEnqueuer = null; } else { this.mJobImpl = null; ComponentName cn2 = new ComponentName(this, getClass()); this.mCompatWorkEnqueuer = getWorkEnqueuer(this, cn2, false, 0); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (this.mCompatQueue != null) { this.mCompatWorkEnqueuer.serviceStartReceived(); synchronized (this.mCompatQueue) { this.mCompatQueue.add(new CompatWorkItem(intent != null ? intent : new Intent(), startId)); ensureProcessorRunningLocked(true); } return 3; } return 2; } @Override public IBinder onBind(Intent intent) { CompatJobEngine compatJobEngine = this.mJobImpl; if (compatJobEngine != null) { IBinder engine = compatJobEngine.compatGetBinder(); return engine; } return null; } @Override public void onDestroy() { super.onDestroy(); ArrayList<CompatWorkItem> arrayList = this.mCompatQueue; if (arrayList != null) { synchronized (arrayList) { this.mDestroyed = true; this.mCompatWorkEnqueuer.serviceProcessingFinished(); } } } public static void enqueueWork(Context context, Class cls, int jobId, Intent work) { enqueueWork(context, new ComponentName(context, (Class<?>) cls), jobId, work); } public static void enqueueWork(Context context, ComponentName component, int jobId, Intent work) { if (work == null) { throw new IllegalArgumentException("work must not be null"); } synchronized (sLock) { WorkEnqueuer we = getWorkEnqueuer(context, component, true, jobId); we.ensureJobId(jobId); we.enqueueWork(work); } } static WorkEnqueuer getWorkEnqueuer(Context context, ComponentName cn2, boolean hasJobId, int jobId) { WorkEnqueuer we = sClassWorkEnqueuer.get(cn2); if (we == null) { if (Build.VERSION.SDK_INT >= 26) { if (!hasJobId) { throw new IllegalArgumentException("Can't be here without a job id"); } we = new JobWorkEnqueuer(context, cn2, jobId); } else { we = new CompatWorkEnqueuer(context, cn2); } sClassWorkEnqueuer.put(cn2, we); } return we; } public void setInterruptIfStopped(boolean interruptIfStopped) { this.mInterruptIfStopped = interruptIfStopped; } public boolean isStopped() { return this.mStopped; } public boolean onStopCurrentWork() { return true; } boolean doStopCurrentWork() { CommandProcessor commandProcessor = this.mCurProcessor; if (commandProcessor != null) { commandProcessor.cancel(this.mInterruptIfStopped); } this.mStopped = true; return onStopCurrentWork(); } void ensureProcessorRunningLocked(boolean reportStarted) { if (this.mCurProcessor == null) { this.mCurProcessor = new CommandProcessor(); WorkEnqueuer workEnqueuer = this.mCompatWorkEnqueuer; if (workEnqueuer != null && reportStarted) { workEnqueuer.serviceProcessingStarted(); } this.mCurProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]); } } void processorFinished() { ArrayList<CompatWorkItem> arrayList = this.mCompatQueue; if (arrayList != null) { synchronized (arrayList) { this.mCurProcessor = null; if (this.mCompatQueue != null && this.mCompatQueue.size() > 0) { ensureProcessorRunningLocked(false); } else if (!this.mDestroyed) { this.mCompatWorkEnqueuer.serviceProcessingFinished(); } } } } GenericWorkItem dequeueWork() { CompatJobEngine compatJobEngine = this.mJobImpl; if (compatJobEngine != null) { return compatJobEngine.dequeueWork(); } synchronized (this.mCompatQueue) { if (this.mCompatQueue.size() > 0) { return this.mCompatQueue.remove(0); } return null; } } }