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 
17 package com.android.managedprovisioning.task;
18 
19 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.VIEW_UNKNOWN;
20 import static com.android.internal.util.Preconditions.checkNotNull;
21 
22 import android.content.Context;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.managedprovisioning.analytics.AnalyticsUtils;
26 import com.android.managedprovisioning.analytics.MetricsLoggerWrapper;
27 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
28 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
29 import com.android.managedprovisioning.analytics.TimeLogger;
30 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
31 import com.android.managedprovisioning.common.SettingsFacade;
32 import com.android.managedprovisioning.model.ProvisioningParams;
33 
34 /**
35  * Base class for all provisioning tasks.
36  */
37 public abstract class AbstractProvisioningTask {
38     protected final Context mContext;
39     protected final ProvisioningParams mProvisioningParams;
40     private final Callback mCallback;
41     private TimeLogger mTimeLogger;
42 
43     /**
44      * Constructor for a provisioning task
45      *
46      * @param context {@link Context} object.
47      * @param provisioningParams {@link ProvisioningParams} object for this provisioning process.
48      * @param callback {@link Callback} object to return task results.
49      */
AbstractProvisioningTask( Context context, ProvisioningParams provisioningParams, Callback callback)50     AbstractProvisioningTask(
51             Context context,
52             ProvisioningParams provisioningParams,
53             Callback callback) {
54         this(context, provisioningParams, callback,
55                 new ProvisioningAnalyticsTracker(
56                         MetricsWriterFactory.getMetricsWriter(context, new SettingsFacade()),
57                         new ManagedProvisioningSharedPreferences(context)));
58     }
59 
60     @VisibleForTesting
AbstractProvisioningTask( Context context, ProvisioningParams provisioningParams, Callback callback, ProvisioningAnalyticsTracker provisioningAnalyticsTracker)61     AbstractProvisioningTask(
62             Context context,
63             ProvisioningParams provisioningParams,
64             Callback callback,
65             ProvisioningAnalyticsTracker provisioningAnalyticsTracker) {
66         mContext = checkNotNull(context);
67         mProvisioningParams = provisioningParams;
68         mCallback = checkNotNull(callback);
69 
70         mTimeLogger = new TimeLogger(context, getMetricsCategory(), new MetricsLoggerWrapper(),
71                 new AnalyticsUtils(),
72                 checkNotNull(provisioningAnalyticsTracker));
73     }
74 
75     /**
76      * Calls {@link Callback#onSuccess(AbstractProvisioningTask)} on the callback given in the
77      * constructor.
78      */
success()79     protected final void success() {
80         mCallback.onSuccess(this);
81     }
82 
83     /**
84      * Calls {@link Callback#onError(AbstractProvisioningTask, int, String)} on the callback given
85      * in the constructor.
86      */
error(int resultCode)87     protected final void error(int resultCode) {
88         mCallback.onError(this, resultCode, /* errorMessageRes= */ null);
89     }
90 
91     /**
92      * Calls {@link Callback#onError(AbstractProvisioningTask, int, String)} on the callback given
93      * in the constructor.
94      */
error(int resultCode, String errorMessage)95     protected final void error(int resultCode, String errorMessage) {
96         mCallback.onError(this, resultCode, errorMessage);
97     }
98 
startTaskTimer()99     protected void startTaskTimer() {
100         mTimeLogger.start();
101     }
102 
stopTaskTimer()103     protected void stopTaskTimer() {
104         mTimeLogger.stop();
105     }
106 
getMetricsCategory()107     protected int getMetricsCategory() {
108         return VIEW_UNKNOWN;
109     }
110 
111     /**
112      * Run the task.
113      *
114      * @param userId the id of the user the action should be performed on.
115      */
run(int userId)116     public abstract void run(int userId);
117 
118     /**
119      * Callback class for provisioning tasks.
120      *
121      * <p>Every execution of run should result in exactly one of
122      * {@link Callback#onSuccess(AbstractProvisioningTask)} and
123      * {@link Callback#onError(AbstractProvisioningTask, int, String)} to be called.</p>
124      */
125     public interface Callback {
126 
127         /**
128          * Callback indicating that the task has finished successfully.
129          *
130          * @param task the task that finished executing.
131          */
onSuccess(AbstractProvisioningTask task)132         void onSuccess(AbstractProvisioningTask task);
133 
134         /**
135          * Callback indicating that the task has encountered an error.
136          * @param task the task that finished executing.
137          * @param errorCode a error code indicating the type of error that happened.
138          * @param errorMessage a user-visible message; if {@code null}, a generic message will
139          *                          be shown instead
140          */
onError(AbstractProvisioningTask task, int errorCode, String errorMessage)141         void onError(AbstractProvisioningTask task, int errorCode, String errorMessage);
142     }
143 }
144