Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6de79e2b authored by Matthew Williams's avatar Matthew Williams
Browse files

First implementation of the TaskManager internals.

Sketching out the TaskManagerService, and provide an implementation
of the Connectivity & Time controllers to use as a basis for how the
IdleMode controller will function.

Change-Id: I9163b8b9df1f8a027354720b469a0fb4c8e37194
parent cae68731
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -7613,7 +7613,7 @@ package android.content {
    method public long getMaxExecutionDelayMillis();
    method public long getMinLatencyMillis();
    method public int getNetworkCapabilities();
    method public java.lang.String getServiceClassName();
    method public android.content.ComponentName getService();
    method public int getTaskId();
    method public boolean isPeriodic();
    method public boolean isRequireCharging();
@@ -7628,7 +7628,7 @@ package android.content {
  }
  public final class Task.Builder {
    ctor public Task.Builder(int, java.lang.Class<android.app.task.TaskService>);
    ctor public Task.Builder(int, android.content.ComponentName);
    method public android.content.Task build();
    method public android.content.Task.Builder setBackoffCriteria(long, int);
    method public android.content.Task.Builder setExtras(android.os.Bundle);
+12 −7
Original line number Diff line number Diff line
@@ -29,7 +29,14 @@ public class TaskParams implements Parcelable {

    private final int taskId;
    private final Bundle extras;
    private final IBinder mCallback;
    private final IBinder callback;

    /** @hide */
    public TaskParams(int taskId, Bundle extras, IBinder callback) {
        this.taskId = taskId;
        this.extras = extras;
        this.callback = callback;
    }

    /**
     * @return The unique id of this task, specified at creation time.
@@ -47,17 +54,15 @@ public class TaskParams implements Parcelable {
        return extras;
    }

    /**
     * @hide
     */
    /** @hide */
    public ITaskCallback getCallback() {
        return ITaskCallback.Stub.asInterface(mCallback);
        return ITaskCallback.Stub.asInterface(callback);
    }

    private TaskParams(Parcel in) {
        taskId = in.readInt();
        extras = in.readBundle();
        mCallback = in.readStrongBinder();
        callback = in.readStrongBinder();
    }

    @Override
@@ -69,7 +74,7 @@ public class TaskParams implements Parcelable {
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(taskId);
        dest.writeBundle(extras);
        dest.writeStrongBinder(mCallback);
        dest.writeStrongBinder(callback);
    }

    public static final Creator<TaskParams> CREATOR = new Creator<TaskParams>() {
+24 −24
Original line number Diff line number Diff line
@@ -42,6 +42,20 @@ public class Task implements Parcelable {
        public final int EXPONENTIAL = 1;
    }

    private final int taskId;
    // TODO: Change this to use PersistableBundle when that lands in master.
    private final Bundle extras;
    private final ComponentName service;
    private final boolean requireCharging;
    private final boolean requireDeviceIdle;
    private final int networkCapabilities;
    private final long minLatencyMillis;
    private final long maxExecutionDelayMillis;
    private final boolean isPeriodic;
    private final long intervalMillis;
    private final long initialBackoffMillis;
    private final int backoffPolicy;

    /**
     * Unique task id associated with this class. This is assigned to your task by the scheduler.
     */
@@ -59,8 +73,8 @@ public class Task implements Parcelable {
    /**
     * Name of the service endpoint that will be called back into by the TaskManager.
     */
    public String getServiceClassName() {
        return serviceClassName;
    public ComponentName getService() {
        return service;
    }

    /**
@@ -132,24 +146,10 @@ public class Task implements Parcelable {
        return backoffPolicy;
    }

    private final int taskId;
    // TODO: Change this to use PersistableBundle when that lands in master.
    private final Bundle extras;
    private final String serviceClassName;
    private final boolean requireCharging;
    private final boolean requireDeviceIdle;
    private final int networkCapabilities;
    private final long minLatencyMillis;
    private final long maxExecutionDelayMillis;
    private final boolean isPeriodic;
    private final long intervalMillis;
    private final long initialBackoffMillis;
    private final int backoffPolicy;

    private Task(Parcel in) {
        taskId = in.readInt();
        extras = in.readBundle();
        serviceClassName = in.readString();
        service = ComponentName.readFromParcel(in);
        requireCharging = in.readInt() == 1;
        requireDeviceIdle = in.readInt() == 1;
        networkCapabilities = in.readInt();
@@ -164,7 +164,7 @@ public class Task implements Parcelable {
    private Task(Task.Builder b) {
        taskId = b.mTaskId;
        extras = new Bundle(b.mExtras);
        serviceClassName = b.mTaskServiceClassName;
        service = b.mTaskService;
        requireCharging = b.mRequiresCharging;
        requireDeviceIdle = b.mRequiresDeviceIdle;
        networkCapabilities = b.mNetworkCapabilities;
@@ -185,7 +185,7 @@ public class Task implements Parcelable {
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(taskId);
        out.writeBundle(extras);
        out.writeString(serviceClassName);
        ComponentName.writeToParcel(service, out);
        out.writeInt(requireCharging ? 1 : 0);
        out.writeInt(requireDeviceIdle ? 1 : 0);
        out.writeInt(networkCapabilities);
@@ -215,7 +215,7 @@ public class Task implements Parcelable {
    public final class Builder {
        private int mTaskId;
        private Bundle mExtras;
        private String mTaskServiceClassName;
        private ComponentName mTaskService;
        // Requirements.
        private boolean mRequiresCharging;
        private boolean mRequiresDeviceIdle;
@@ -236,11 +236,11 @@ public class Task implements Parcelable {
         * @param taskId Application-provided id for this task. Subsequent calls to cancel, or
         *               tasks created with the same taskId, will update the pre-existing task with
         *               the same id.
         * @param cls The endpoint that you implement that will receive the callback from the
         * @param taskService The endpoint that you implement that will receive the callback from the
         *            TaskManager.
         */
        public Builder(int taskId, Class<TaskService> cls) {
            mTaskServiceClassName = cls.getClass().getName();
        public Builder(int taskId, ComponentName taskService) {
            mTaskService = taskService;
            mTaskId = taskId;
        }

@@ -296,7 +296,7 @@ public class Task implements Parcelable {
         * period. You have no control over when within this interval this task will be executed,
         * only the guarantee that it will be executed at most once within this interval.
         * A periodic task will be repeated until the phone is turned off, however it will only be
         * persisted if the client app has declared the
         * persisted beyond boot if the client app has declared the
         * {@link android.Manifest.permission#RECEIVE_BOOT_COMPLETED} permission. You can schedule
         * periodic tasks without this permission, they simply will cease to exist after the phone
         * restarts.
+40 −0
Original line number Diff line number Diff line
/*
 * 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.task;

import com.android.server.task.controllers.TaskStatus;

/**
 * Interface through which a {@link StateController} informs the
 * {@link com.android.server.task.TaskManagerService} that there are some tasks potentially ready
 * to be run.
 */
public interface StateChangedListener {
    /**
     * Called by the controller to notify the TaskManager that it should check on the state of a
     * task.
     * @param taskStatus The state of the task which has changed.
     */
    public void onTaskStateChanged(TaskStatus taskStatus);

    /**
     * Called by the controller to notify the TaskManager that regardless of the state of the task,
     * it must be run immediately.
     * @param taskStatus The state of the task which is to be run immediately.
     */
    public void onTaskDeadlineExpired(TaskStatus taskStatus);
}
+78 −0
Original line number Diff line number Diff line
/*
 * 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.task;

import android.content.ComponentName;
import android.content.Task;

import com.android.server.task.controllers.TaskStatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Maintain a list of classes, and accessor methods/logic for these tasks.
 * This class offers the following functionality:
 *     - When a task is added, it will determine if the task requirements have changed (update) and
 *       whether the controllers need to be updated.
 *     - Persists Tasks, figures out when to to rewrite the Task to disk.
 *     - Is threadsafe.
 *     - Handles rescheduling of tasks.
 *       - When a periodic task is executed and must be re-added.
 *       - When a task fails and the client requests that it be retried with backoff.
 */
public class TaskList {

    final List<TaskStatus> mTasks;

    TaskList() {
        mTasks = intialiseTaskMapFromDisk();
    }

    /**
     * Add a task to the master list, persisting it if necessary.
     * @param task Task to add.
     * @param persistable true if the TaskQueue should persist this task to the disk.
     * @return true if this operation was successful. If false, this task was neither added nor
     * persisted.
     */
    // TODO: implement this when i decide whether i want to key by TaskStatus
    public boolean add(Task task, boolean persistable) {
        return true;
    }

    /**
     * Remove the provided task. Will also delete the task if it was persisted. Note that this
     * function does not return the validity of the operation, as we assume a delete will always
     * succeed.
     * @param task Task to remove.
     */
    public void remove(Task task) {

    }

    /**
     *
     * @return
     */
    // TODO: Implement this.
    private List<TaskStatus> intialiseTaskMapFromDisk() {
        return new ArrayList<TaskStatus>();
    }
}
Loading