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)} on the callback given in the 85 * constructor. 86 */ error(int resultCode)87 protected final void error(int resultCode) { 88 mCallback.onError(this, resultCode); 89 } 90 startTaskTimer()91 protected void startTaskTimer() { 92 mTimeLogger.start(); 93 } 94 stopTaskTimer()95 protected void stopTaskTimer() { 96 mTimeLogger.stop(); 97 } 98 getMetricsCategory()99 protected int getMetricsCategory() { 100 return VIEW_UNKNOWN; 101 } 102 103 /** 104 * Run the task. 105 * 106 * @param userId the id of the user the action should be performed on. 107 */ run(int userId)108 public abstract void run(int userId); 109 110 /** 111 * Callback class for provisioning tasks. 112 * 113 * <p>Every execution of run should result in exactly one of 114 * {@link Callback#onSuccess(AbstractProvisioningTask)} and 115 * {@link Callback#onError(AbstractProvisioningTask, int)} to be called.</p> 116 */ 117 public interface Callback { 118 119 /** 120 * Callback indicating that the task has finished successfully. 121 * 122 * @param task the task that finished executing. 123 */ onSuccess(AbstractProvisioningTask task)124 void onSuccess(AbstractProvisioningTask task); 125 126 /** 127 * Callback indicating that the task has encountered an error. 128 * 129 * @param task the task that finished executing. 130 * @param errorCode a error code indicating the type of error that happened. 131 */ onError(AbstractProvisioningTask task, int errorCode)132 void onError(AbstractProvisioningTask task, int errorCode); 133 } 134 } 135