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