1/*
2 * Copyright 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.managedprovisioning.task;
18
19import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.VIEW_UNKNOWN;
20import static com.android.internal.util.Preconditions.checkNotNull;
21
22import android.content.Context;
23
24import com.android.managedprovisioning.analytics.TimeLogger;
25import com.android.managedprovisioning.model.ProvisioningParams;
26
27/**
28 * Base class for all provisioning tasks.
29 */
30public abstract class AbstractProvisioningTask {
31    protected final Context mContext;
32    protected final ProvisioningParams mProvisioningParams;
33    private final Callback mCallback;
34    private TimeLogger mTimeLogger;
35
36    /**
37     * Constructor for a provisioning task
38     *
39     * @param context {@link Context} object.
40     * @param provisioningParams {@link ProvisioningParams} object for this provisioning process.
41     * @param callback {@link Callback} object to return task results.
42     */
43    AbstractProvisioningTask(
44            Context context,
45            ProvisioningParams provisioningParams,
46            Callback callback) {
47        mContext = checkNotNull(context);
48        mProvisioningParams = provisioningParams;
49        mCallback = checkNotNull(callback);
50
51        mTimeLogger = new TimeLogger(context, getMetricsCategory());
52    }
53
54    protected final void success() {
55        mCallback.onSuccess(this);
56    }
57
58    protected final void error(int resultCode) {
59        mCallback.onError(this, resultCode);
60    }
61
62    protected void startTaskTimer() {
63        mTimeLogger.start();
64    }
65
66    protected void stopTaskTimer() {
67        mTimeLogger.stop();
68    }
69
70    protected int getMetricsCategory() {
71        return VIEW_UNKNOWN;
72    }
73
74    /**
75     * Run the task.
76     *
77     * @param userId the id of the user the action should be performed on.
78     */
79    public abstract void run(int userId);
80
81    /**
82     * @return the resource id of the status message related to the task.
83     */
84    public abstract int getStatusMsgId();
85
86    /**
87     * Callback class for provisioning tasks.
88     *
89     * <p>Every execution of run should result in exactly one of
90     * {@link Callback#onSuccess(AbstractProvisioningTask)} and
91     * {@link Callback#onError(AbstractProvisioningTask, int)} to be called.</p>
92     */
93    public interface Callback {
94
95        /**
96         * Callback indicating that the task has finished successfully.
97         *
98         * @param task the task that finished executing.
99         */
100        void onSuccess(AbstractProvisioningTask task);
101
102        /**
103         * Callback indicating that the task has encountered an error.
104         *
105         * @param task the task that finished executing.
106         * @param errorCode a error code indicating the type of error that happened.
107         */
108        void onError(AbstractProvisioningTask task, int errorCode);
109    }
110}
111