/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.server.job; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import android.app.AppGlobals; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.app.job.JobService; import android.app.job.IJobScheduler; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.IPackageManager; import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; import android.os.BatteryStats; import android.os.Binder; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemClock; import android.os.UserHandle; import android.util.ArraySet; import android.util.Slog; import android.util.SparseArray; import com.android.internal.app.IBatteryStats; import com.android.server.job.controllers.BatteryController; import com.android.server.job.controllers.ConnectivityController; import com.android.server.job.controllers.IdleController; import com.android.server.job.controllers.JobStatus; import com.android.server.job.controllers.StateController; import com.android.server.job.controllers.TimeController; /** * Responsible for taking jobs representing work to be performed by a client app, and determining * based on the criteria specified when that job should be run against the client application's * endpoint. * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing * about constraints, or the state of active jobs. It receives callbacks from the various * controllers and completed jobs and operates accordingly. * * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object. * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}. * @hide */ public class JobSchedulerService extends com.android.server.SystemService implements StateChangedListener, JobCompletedListener { static final boolean DEBUG = false; /** The number of concurrent jobs we run at one time. */ private static final int MAX_JOB_CONTEXTS_COUNT = 3; static final String TAG = "JobSchedulerService"; /** Master list of jobs. */ final JobStore mJobs; static final int MSG_JOB_EXPIRED = 0; static final int MSG_CHECK_JOB = 1; // Policy constants /** * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things * early. */ static final int MIN_IDLE_COUNT = 1; /** * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things * early. */ static final int MIN_CHARGING_COUNT = 1; /** * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule * things early. */ static final int MIN_CONNECTIVITY_COUNT = 2; /** * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running * some work early. * This is correlated with the amount of batching we'll be able to do. */ static final int MIN_READY_JOBS_COUNT = 2; /** * Track Services that have currently active or pending jobs. The index is provided by * {@link JobStatus#getServiceToken()} */ final List mActiveServices = new ArrayList(); /** List of controllers that will notify this service of updates to jobs. */ List mControllers; /** * Queue of pending jobs. The JobServiceContext class will receive jobs from this list * when ready to execute them. */ final ArrayList mPendingJobs = new ArrayList(); final ArrayList mStartedUsers = new ArrayList(); final JobHandler mHandler; final JobSchedulerStub mJobSchedulerStub; IBatteryStats mBatteryStats; /** * Set to true once we are allowed to run third party apps. */ boolean mReadyToRock; /** * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we * still clean up. On reinstall the package will have a new uid. */ private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Slog.d(TAG, "Receieved: " + intent.getAction()); if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { // If this is an outright uninstall rather than the first half of an // app update sequence, cancel the jobs associated with the app. if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1); if (DEBUG) { Slog.d(TAG, "Removing jobs for uid: " + uidRemoved); } cancelJobsForUid(uidRemoved); } } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); if (DEBUG) { Slog.d(TAG, "Removing jobs for user: " + userId); } cancelJobsForUser(userId); } } }; @Override public void onStartUser(int userHandle) { mStartedUsers.add(userHandle); // Let's kick any outstanding jobs for this user. mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); } @Override public void onStopUser(int userHandle) { mStartedUsers.remove(Integer.valueOf(userHandle)); } /** * Entry point from client to schedule the provided job. * This cancels the job if it's already been scheduled, and replaces it with the one provided. * @param job JobInfo object containing execution parameters * @param uId The package identifier of the application this job is for. * @return Result of this operation. See JobScheduler#RESULT_* return codes. */ public int schedule(JobInfo job, int uId) { JobStatus jobStatus = new JobStatus(job, uId); cancelJob(uId, job.getId()); startTrackingJob(jobStatus); mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); return JobScheduler.RESULT_SUCCESS; } public List getPendingJobs(int uid) { ArrayList outList = new ArrayList(); synchronized (mJobs) { ArraySet jobs = mJobs.getJobs(); for (int i=0; i jobsForUser; synchronized (mJobs) { jobsForUser = mJobs.getJobsByUser(userHandle); } for (int i=0; i jobsForUid; synchronized (mJobs) { jobsForUid = mJobs.getJobsByUid(uid); } for (int i=0; i * Subclasses must define a single argument constructor that accepts the context * and passes it to super. *

* * @param context The system server context. */ public JobSchedulerService(Context context) { super(context); // Create the controllers. mControllers = new ArrayList(); mControllers.add(ConnectivityController.get(this)); mControllers.add(TimeController.get(this)); mControllers.add(IdleController.get(this)); mControllers.add(BatteryController.get(this)); mHandler = new JobHandler(context.getMainLooper()); mJobSchedulerStub = new JobSchedulerStub(); mJobs = JobStore.initAndGet(this); } @Override public void onStart() { publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub); } @Override public void onBootPhase(int phase) { if (PHASE_SYSTEM_SERVICES_READY == phase) { // Register br for package removals and user removals. final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); filter.addDataScheme("package"); getContext().registerReceiverAsUser( mBroadcastReceiver, UserHandle.ALL, filter, null, null); final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED); getContext().registerReceiverAsUser( mBroadcastReceiver, UserHandle.ALL, userFilter, null, null); } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { synchronized (mJobs) { // Let's go! mReadyToRock = true; mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( BatteryStats.SERVICE_NAME)); // Create the "runners". for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) { mActiveServices.add( new JobServiceContext(this, mBatteryStats, getContext().getMainLooper())); } // Attach jobs to their controllers. ArraySet jobs = mJobs.getJobs(); for (int i=0; i jobs = mJobs.getJobs(); if (DEBUG) { Slog.d(TAG, "queuing all ready jobs for execution:"); } for (int i=0; i1 of the ready jobs is idle mode we send all of them off * if more than 2 network connectivity jobs are ready we send them all off. * If more than 4 jobs total are ready we send them all off. * TODO: It would be nice to consolidate these sort of high-level policies somewhere. */ private void maybeQueueReadyJobsForExecutionLockedH() { int chargingCount = 0; int idleCount = 0; int backoffCount = 0; int connectivityCount = 0; List runnableJobs = new ArrayList(); ArraySet jobs = mJobs.getJobs(); for (int i=0; i 0) { backoffCount++; } if (job.hasIdleConstraint()) { idleCount++; } if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) { connectivityCount++; } if (job.hasChargingConstraint()) { chargingCount++; } runnableJobs.add(job); } else if (isReadyToBeCancelledLocked(job)) { stopJobOnServiceContextLocked(job); } } if (backoffCount > 0 || idleCount >= MIN_IDLE_COUNT || connectivityCount >= MIN_CONNECTIVITY_COUNT || chargingCount >= MIN_CHARGING_COUNT || runnableJobs.size() >= MIN_READY_JOBS_COUNT) { if (DEBUG) { Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs."); } for (int i=0; i mPersistCache = new SparseArray(); // Enforce that only the app itself (or shared uid participant) can schedule a // job that runs one of the app's services, as well as verifying that the // named service properly requires the BIND_JOB_SERVICE permission private void enforceValidJobRequest(int uid, JobInfo job) { final IPackageManager pm = AppGlobals.getPackageManager(); final ComponentName service = job.getService(); try { ServiceInfo si = pm.getServiceInfo(service, 0, UserHandle.getUserId(uid)); if (si == null) { throw new IllegalArgumentException("No such service " + service); } if (si.applicationInfo.uid != uid) { throw new IllegalArgumentException("uid " + uid + " cannot schedule job in " + service.getPackageName()); } if (!JobService.PERMISSION_BIND.equals(si.permission)) { throw new IllegalArgumentException("Scheduled service " + service + " does not require android.permission.BIND_JOB_SERVICE permission"); } } catch (RemoteException e) { // Can't happen; the Package Manager is in this same process } } private boolean canPersistJobs(int pid, int uid) { // If we get this far we're good to go; all we need to do now is check // whether the app is allowed to persist its scheduled work. final boolean canPersist; synchronized (mPersistCache) { Boolean cached = mPersistCache.get(uid); if (cached != null) { canPersist = cached.booleanValue(); } else { // Persisting jobs is tantamount to running at boot, so we permit // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED // permission int result = getContext().checkPermission( android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid); canPersist = (result == PackageManager.PERMISSION_GRANTED); mPersistCache.put(uid, canPersist); } } return canPersist; } // IJobScheduler implementation @Override public int schedule(JobInfo job) throws RemoteException { if (DEBUG) { Slog.d(TAG, "Scheduling job: " + job.toString()); } final int pid = Binder.getCallingPid(); final int uid = Binder.getCallingUid(); enforceValidJobRequest(uid, job); if (job.isPersisted()) { if (!canPersistJobs(pid, uid)) { throw new IllegalArgumentException("Error: requested job be persisted without" + " holding RECEIVE_BOOT_COMPLETED permission."); } } long ident = Binder.clearCallingIdentity(); try { return JobSchedulerService.this.schedule(job, uid); } finally { Binder.restoreCallingIdentity(ident); } } @Override public List getAllPendingJobs() throws RemoteException { final int uid = Binder.getCallingUid(); long ident = Binder.clearCallingIdentity(); try { return JobSchedulerService.this.getPendingJobs(uid); } finally { Binder.restoreCallingIdentity(ident); } } @Override public void cancelAll() throws RemoteException { final int uid = Binder.getCallingUid(); long ident = Binder.clearCallingIdentity(); try { JobSchedulerService.this.cancelJobsForUid(uid); } finally { Binder.restoreCallingIdentity(ident); } } @Override public void cancel(int jobId) throws RemoteException { final int uid = Binder.getCallingUid(); long ident = Binder.clearCallingIdentity(); try { JobSchedulerService.this.cancelJob(uid, jobId); } finally { Binder.restoreCallingIdentity(ident); } } /** * "dumpsys" infrastructure */ @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); long identityToken = Binder.clearCallingIdentity(); try { JobSchedulerService.this.dumpInternal(pw); } finally { Binder.restoreCallingIdentity(identityToken); } } }; void dumpInternal(PrintWriter pw) { final long now = SystemClock.elapsedRealtime(); synchronized (mJobs) { pw.print("Started users: "); for (int i=0; i 0) { ArraySet jobs = mJobs.getJobs(); for (int i=0; i