| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.job; |
| 18 |
|
| 19 |
import android.app.Service; |
| 20 |
import android.content.Intent; |
| 21 |
import android.os.Handler; |
| 22 |
import android.os.IBinder; |
| 23 |
import android.os.Looper; |
| 24 |
import android.os.Message; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.util.Log; |
| 27 |
|
| 28 |
import java.lang.ref.WeakReference; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public abstract class JobServiceEngine { |
| 39 |
private static final String TAG = "JobServiceEngine"; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private static final int MSG_EXECUTE_JOB = 0; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private static final int MSG_STOP_JOB = 1; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private static final int MSG_JOB_FINISHED = 2; |
| 54 |
|
| 55 |
private final IJobService mBinder; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
JobHandler mHandler; |
| 62 |
|
| 63 |
static final class JobInterface extends IJobService.Stub { |
| 64 |
final WeakReference<JobServiceEngine> mService; |
| 65 |
|
| 66 |
JobInterface(JobServiceEngine service) { |
| 67 |
mService = new WeakReference<>(service); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void startJob(JobParameters jobParams) throws RemoteException { |
| 72 |
JobServiceEngine service = mService.get(); |
| 73 |
if (service != null) { |
| 74 |
Message m = Message.obtain(service.mHandler, MSG_EXECUTE_JOB, jobParams); |
| 75 |
m.sendToTarget(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void stopJob(JobParameters jobParams) throws RemoteException { |
| 81 |
JobServiceEngine service = mService.get(); |
| 82 |
if (service != null) { |
| 83 |
Message m = Message.obtain(service.mHandler, MSG_STOP_JOB, jobParams); |
| 84 |
m.sendToTarget(); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
class JobHandler extends Handler { |
| 95 |
JobHandler(Looper looper) { |
| 96 |
super(looper); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void handleMessage(Message msg) { |
| 101 |
final JobParameters params = (JobParameters) msg.obj; |
| 102 |
switch (msg.what) { |
| 103 |
case MSG_EXECUTE_JOB: |
| 104 |
try { |
| 105 |
boolean workOngoing = JobServiceEngine.this.onStartJob(params); |
| 106 |
ackStartMessage(params, workOngoing); |
| 107 |
} catch (Exception e) { |
| 108 |
Log.e(TAG, "Error while executing job: " + params.getJobId()); |
| 109 |
throw new RuntimeException(e); |
| 110 |
} |
| 111 |
break; |
| 112 |
case MSG_STOP_JOB: |
| 113 |
try { |
| 114 |
boolean ret = JobServiceEngine.this.onStopJob(params); |
| 115 |
ackStopMessage(params, ret); |
| 116 |
} catch (Exception e) { |
| 117 |
Log.e(TAG, "Application unable to handle onStopJob.", e); |
| 118 |
throw new RuntimeException(e); |
| 119 |
} |
| 120 |
break; |
| 121 |
case MSG_JOB_FINISHED: |
| 122 |
final boolean needsReschedule = (msg.arg2 == 1); |
| 123 |
IJobCallback callback = params.getCallback(); |
| 124 |
if (callback != null) { |
| 125 |
try { |
| 126 |
callback.jobFinished(params.getJobId(), needsReschedule); |
| 127 |
} catch (RemoteException e) { |
| 128 |
Log.e(TAG, "Error reporting job finish to system: binder has gone" + |
| 129 |
"away."); |
| 130 |
} |
| 131 |
} else { |
| 132 |
Log.e(TAG, "finishJob() called for a nonexistent job id."); |
| 133 |
} |
| 134 |
break; |
| 135 |
default: |
| 136 |
Log.e(TAG, "Unrecognised message received."); |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private void ackStartMessage(JobParameters params, boolean workOngoing) { |
| 142 |
final IJobCallback callback = params.getCallback(); |
| 143 |
final int jobId = params.getJobId(); |
| 144 |
if (callback != null) { |
| 145 |
try { |
| 146 |
callback.acknowledgeStartMessage(jobId, workOngoing); |
| 147 |
} catch(RemoteException e) { |
| 148 |
Log.e(TAG, "System unreachable for starting job."); |
| 149 |
} |
| 150 |
} else { |
| 151 |
if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 152 |
Log.d(TAG, "Attempting to ack a job that has already been processed."); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
private void ackStopMessage(JobParameters params, boolean reschedule) { |
| 158 |
final IJobCallback callback = params.getCallback(); |
| 159 |
final int jobId = params.getJobId(); |
| 160 |
if (callback != null) { |
| 161 |
try { |
| 162 |
callback.acknowledgeStopMessage(jobId, reschedule); |
| 163 |
} catch(RemoteException e) { |
| 164 |
Log.e(TAG, "System unreachable for stopping job."); |
| 165 |
} |
| 166 |
} else { |
| 167 |
if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 168 |
Log.d(TAG, "Attempting to ack a job that has already been processed."); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public JobServiceEngine(Service service) { |
| 180 |
mBinder = new JobInterface(this); |
| 181 |
mHandler = new JobHandler(service.getMainLooper()); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public final IBinder getBinder() { |
| 189 |
return mBinder.asBinder(); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public abstract boolean onStartJob(JobParameters params); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public abstract boolean onStopJob(JobParameters params); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public void jobFinished(JobParameters params, boolean needsReschedule) { |
| 209 |
if (params == null) { |
| 210 |
throw new NullPointerException("params"); |
| 211 |
} |
| 212 |
Message m = Message.obtain(mHandler, MSG_JOB_FINISHED, params); |
| 213 |
m.arg2 = needsReschedule ? 1 : 0; |
| 214 |
m.sendToTarget(); |
| 215 |
} |
| 216 |
} |