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.analytics;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE;
20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TRIGGER;
22 import static android.app.admin.DevicePolicyManager.PROVISIONING_TRIGGER_QR_CODE;
23 import static android.app.admin.DevicePolicyManager.PROVISIONING_TRIGGER_UNSPECIFIED;
24 import static android.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED;
25 import static android.stats.devicepolicy.DevicePolicyEnums.PROVISIONING_DPC_SETUP_COMPLETED;
26 import static android.stats.devicepolicy.DevicePolicyEnums.PROVISIONING_DPC_SETUP_STARTED;
27 import static android.stats.devicepolicy.DevicePolicyEnums.PROVISIONING_MANAGED_PROFILE_ON_FULLY_MANAGED_DEVICE;
28 
29 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_ACTION;
30 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_CANCELLED;
31 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_DPC_INSTALLED_BY_PACKAGE;
32 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_DPC_PACKAGE_NAME;
33 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_ENTRY_POINT_NFC;
34 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_ENTRY_POINT_QR_CODE;
35 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_ENTRY_POINT_TRUSTED_SOURCE;
36 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_ERROR;
37 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_EXTRA;
38 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_SESSION_COMPLETED;
39 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_SESSION_STARTED;
40 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_TERMS_COUNT;
41 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_TERMS_READ;
42 
43 import android.annotation.IntDef;
44 import android.app.admin.DevicePolicyEventLogger;
45 import android.app.admin.DevicePolicyManager;
46 import android.content.ComponentName;
47 import android.content.Context;
48 import android.content.Intent;
49 import android.stats.devicepolicy.DevicePolicyEnums;
50 
51 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
52 import com.android.managedprovisioning.common.SettingsFacade;
53 import com.android.managedprovisioning.model.ProvisioningParams;
54 import com.android.managedprovisioning.task.AbstractProvisioningTask;
55 
56 import java.util.List;
57 
58 /**
59  * Utility class to log metrics.
60  */
61 public class ProvisioningAnalyticsTracker {
62 
63     private final MetricsLoggerWrapper mMetricsLoggerWrapper = new MetricsLoggerWrapper();
64 
65     // Only add to the end of the list. Do not change or rearrange these values, that will break
66     // historical data. Do not use negative numbers or zero, logger only handles positive
67     // integers.
68     public static final int CANCELLED_BEFORE_PROVISIONING = 1;
69     public static final int CANCELLED_DURING_PROVISIONING = 2;
70     public static final int CANCELLED_DURING_PROVISIONING_PREPARE = 3;
71     private final ManagedProvisioningSharedPreferences mSharedPreferences;
72 
73     @IntDef({
74         CANCELLED_BEFORE_PROVISIONING,
75         CANCELLED_DURING_PROVISIONING,
76         CANCELLED_DURING_PROVISIONING_PREPARE})
77     public @interface CancelState {}
78 
79     private static final int PROVISIONING_FLOW_TYPE_ADMIN_INTEGRATED = 1;
80     private static final int PROVISIONING_FLOW_TYPE_LEGACY = 2;
81 
82     private static final int DPC_SETUP_ACTION_UNKNOWN = 1;
83     private static final int DPC_SETUP_ACTION_PROVISIONING_SUCCESSFUL = 2;
84     private static final int DPC_SETUP_ACTION_ADMIN_POLICY_COMPLIANCE = 3;
85 
86     private final MetricsWriter mMetricsWriter;
87 
ProvisioningAnalyticsTracker(MetricsWriter metricsWriter, ManagedProvisioningSharedPreferences prefs)88     public ProvisioningAnalyticsTracker(MetricsWriter metricsWriter,
89             ManagedProvisioningSharedPreferences prefs) {
90         // Disables instantiation. Use getInstance() instead.
91         mMetricsWriter = metricsWriter;
92         mSharedPreferences = prefs;
93     }
94 
95     /**
96      * Logs some metrics when the provisioning starts.
97      *
98      * @param context Context passed to MetricsLogger
99      * @param params Provisioning params
100      */
logProvisioningStarted(Context context, ProvisioningParams params)101     public void logProvisioningStarted(Context context, ProvisioningParams params) {
102         logDpcPackageInformation(context, params.inferDeviceAdminPackageName());
103         logNetworkType(context);
104     }
105 
106     /**
107      * Logs when provisioning is cancelled.
108      *
109      * @param context Context passed to MetricsLogger
110      * @param cancelState State when provisioning was cancelled
111      */
logProvisioningCancelled(Context context, @CancelState int cancelState)112     public void logProvisioningCancelled(Context context, @CancelState int cancelState) {
113         mMetricsLoggerWrapper.logAction(context, PROVISIONING_CANCELLED, cancelState);
114         mMetricsWriter.write(DevicePolicyEventLogger
115                 .createEvent(DevicePolicyEnums.PROVISIONING_CANCELLED)
116                 .setInt(cancelState)
117                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
118     }
119 
120     /**
121      * Logs error during provisioning tasks.
122      *
123      * @param context Context passed to MetricsLogger
124      * @param task Provisioning task which threw error
125      * @param errorCode Code indicating the type of error that happened.
126      */
logProvisioningError(Context context, AbstractProvisioningTask task, int errorCode)127     public void logProvisioningError(Context context, AbstractProvisioningTask task,
128             int errorCode) {
129         mMetricsLoggerWrapper.logAction(context, PROVISIONING_ERROR,
130                 AnalyticsUtils.getErrorString(task, errorCode));
131         mMetricsWriter.write(DevicePolicyEventLogger
132                 .createEvent(DevicePolicyEnums.PROVISIONING_ERROR)
133                 .setStrings(AnalyticsUtils.getErrorString(task, errorCode))
134                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
135     }
136 
137     /**
138      * Logs error code, when provisioning is not allowed.
139      *
140      * @param context Context passed to MetricsLogger
141      * @param provisioningErrorCode Code indicating why provisioning is not allowed.
142      */
logProvisioningNotAllowed(Context context, int provisioningErrorCode)143     public void logProvisioningNotAllowed(Context context, int provisioningErrorCode) {
144         mMetricsLoggerWrapper.logAction(context, PROVISIONING_ERROR, provisioningErrorCode);
145         mMetricsWriter.write(DevicePolicyEventLogger
146                 .createEvent(DevicePolicyEnums.PROVISIONING_ERROR)
147                 .setStrings(String.valueOf(provisioningErrorCode))
148                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
149     }
150 
151     /**
152      * logs when a provisioning session has started.
153      *
154      * @param context Context passed to MetricsLogger
155      */
logProvisioningSessionStarted(Context context)156     public void logProvisioningSessionStarted(Context context) {
157         mMetricsLoggerWrapper.logAction(context, PROVISIONING_SESSION_STARTED);
158         mMetricsWriter.write(DevicePolicyEventLogger
159                 .createEvent(DevicePolicyEnums.PROVISIONING_SESSION_STARTED)
160                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
161     }
162 
163     /**
164      * logs when a provisioning session has completed.
165      *
166      * @param context Context passed to MetricsLogger
167      */
logProvisioningSessionCompleted(Context context)168     public void logProvisioningSessionCompleted(Context context) {
169         mMetricsLoggerWrapper.logAction(context, PROVISIONING_SESSION_COMPLETED);
170         mMetricsWriter.write(DevicePolicyEventLogger
171                 .createEvent(DevicePolicyEnums.PROVISIONING_SESSION_COMPLETED)
172                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
173     }
174 
175     /**
176      * logs number of terms displayed on the terms screen.
177      *
178      * @param context Context passed to MetricsLogger
179      * @param count Number of terms displayed
180      */
logNumberOfTermsDisplayed(Context context, int count)181     public void logNumberOfTermsDisplayed(Context context, int count) {
182         mMetricsLoggerWrapper.logAction(context, PROVISIONING_TERMS_COUNT, count);
183         mMetricsWriter.write(DevicePolicyEventLogger
184                 .createEvent(DevicePolicyEnums.PROVISIONING_TERMS_COUNT)
185                 .setInt(count)
186                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
187     }
188 
189     /**
190      * logs number of terms read on the terms screen.
191      *
192      * @param context Context passed to MetricsLogger
193      * @param count Number of terms read
194      */
logNumberOfTermsRead(Context context, int count)195     public void logNumberOfTermsRead(Context context, int count) {
196         mMetricsLoggerWrapper.logAction(context, PROVISIONING_TERMS_READ, count);
197         mMetricsWriter.write(DevicePolicyEventLogger
198                 .createEvent(DevicePolicyEnums.PROVISIONING_TERMS_READ)
199                 .setInt(count)
200                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
201     }
202 
203     /**
204      * Logs when the provisioning preparation has started.
205      * <p>The preparation includes network setup, downloading, verifying and installing the
206      * admin app.
207      */
logProvisioningPrepareStarted()208     public void logProvisioningPrepareStarted() {
209         mMetricsWriter.write(DevicePolicyEventLogger
210                 .createEvent(DevicePolicyEnums.PROVISIONING_PREPARE_STARTED)
211                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
212     }
213 
214     /**
215      * Logs when the provisioning preparation has completed.
216      * <p>The preparation includes network setup, downloading, verifying and installing the
217      * admin app.
218      */
logProvisioningPrepareCompleted()219     public void logProvisioningPrepareCompleted() {
220         mMetricsWriter.write(DevicePolicyEventLogger
221                 .createEvent(DevicePolicyEnums.PROVISIONING_PREPARE_COMPLETED)
222                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
223     }
224 
logTimeLoggerEvent(int devicePolicyEvent, int time)225     public void logTimeLoggerEvent(int devicePolicyEvent, int time) {
226         mMetricsWriter.write(DevicePolicyEventLogger
227                 .createEvent(devicePolicyEvent)
228                 .setInt(time)
229                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
230     }
231 
232     /**
233      * Logs the provisioning action.
234      *  @param context Context passed to MetricsLogger
235      * @param provisioningAction Action that triggered provisioning
236      */
logProvisioningAction(Context context, String provisioningAction)237     public void logProvisioningAction(Context context, String provisioningAction) {
238         mMetricsLoggerWrapper.logAction(context, PROVISIONING_ACTION, provisioningAction);
239         mMetricsWriter.write(DevicePolicyEventLogger
240                 .createEvent(DevicePolicyEnums.PROVISIONING_ACTION)
241                 .setStrings(provisioningAction)
242                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
243         maybeLogManagedProfileOnFullyManagedDevice(context, provisioningAction);
244     }
245 
246     /**
247      * Logs organization owned managed profile provisioning.
248      */
logOrganizationOwnedManagedProfileProvisioning()249     public void logOrganizationOwnedManagedProfileProvisioning() {
250         mMetricsWriter.write(DevicePolicyEventLogger
251                 .createEvent(DevicePolicyEnums.PROVISIONING_ORGANIZATION_OWNED_MANAGED_PROFILE)
252                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
253     }
254 
255     /**
256      * Logs when the DPC is started, for the purpose of enterprise setup.  Note that in the admin-
257      * integrated flow, this is when {@link DevicePolicyManager#ACTION_ADMIN_POLICY_COMPLIANCE} is
258      * sent to the DPC, not {@link DevicePolicyManager#ACTION_GET_PROVISIONING_MODE}.
259      *  @param context Context passed to MetricsLogger
260      * @param intentAction Action that was sent to the DPC
261      */
logDpcSetupStarted(Context context, String intentAction)262     public void logDpcSetupStarted(Context context, String intentAction) {
263         mMetricsLoggerWrapper.logAction(context, PROVISIONING_DPC_SETUP_STARTED);
264 
265         int intentActionCode;
266         switch(intentAction) {
267             case DevicePolicyManager.ACTION_PROVISIONING_SUCCESSFUL:
268                 intentActionCode = DPC_SETUP_ACTION_PROVISIONING_SUCCESSFUL;
269                 break;
270             case DevicePolicyManager.ACTION_ADMIN_POLICY_COMPLIANCE:
271                 intentActionCode = DPC_SETUP_ACTION_ADMIN_POLICY_COMPLIANCE;
272                 break;
273             default:
274                 intentActionCode = DPC_SETUP_ACTION_UNKNOWN;
275                 break;
276         }
277 
278         mMetricsWriter.write(DevicePolicyEventLogger
279                 .createEvent(PROVISIONING_DPC_SETUP_STARTED)
280                 .setInt(intentActionCode)
281                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
282     }
283 
284     /**
285      * Logs when the DPC finishes with enterprise setup.  Note that this is only logged when setup
286      * happens inside Setup Wizard; if it happens after Setup Wizard, we never find out when the
287      * DPC finishes.
288      *  @param context Context passed to MetricsLogger
289      * @param resultCode The result code that is returned by the DPC
290      */
logDpcSetupCompleted(Context context, int resultCode)291     public void logDpcSetupCompleted(Context context, int resultCode) {
292         mMetricsLoggerWrapper.logAction(context, PROVISIONING_DPC_SETUP_COMPLETED);
293         mMetricsWriter.write(DevicePolicyEventLogger
294                 .createEvent(PROVISIONING_DPC_SETUP_COMPLETED)
295                 .setInt(resultCode)
296                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
297     }
298 
299     /**
300      * Logs the type of provisioning flow if this is organization owned provisioning.
301      * <p>It would be either admin integrated flow or legacy.
302      *
303      * @param params Used to extract whether this is the admin integrated flow
304      */
logProvisioningFlowType(ProvisioningParams params)305     public void logProvisioningFlowType(ProvisioningParams params) {
306         mMetricsWriter.write(DevicePolicyEventLogger
307                 .createEvent(DevicePolicyEnums.PROVISIONING_FLOW_TYPE)
308                 .setInt(params.flowType == ProvisioningParams.FLOW_TYPE_ADMIN_INTEGRATED
309                         ? PROVISIONING_FLOW_TYPE_ADMIN_INTEGRATED
310                         : PROVISIONING_FLOW_TYPE_LEGACY)
311                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
312     }
313 
314     /**
315      * Logs whether an {@link android.app.Activity} is in landscape mode, along with its name.
316      */
logIsLandscape(boolean isLandscape, String activityName)317     public void logIsLandscape(boolean isLandscape, String activityName) {
318         mMetricsWriter.write(DevicePolicyEventLogger
319                 .createEvent(DevicePolicyEnums.PROVISIONING_IS_LANDSCAPE)
320                 .setBoolean(isLandscape)
321                 .setStrings(activityName)
322                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
323     }
324 
325     /**
326      * Logs whether the app is in night mode.
327      */
logIsNightMode(boolean isNightMode)328     public void logIsNightMode(boolean isNightMode) {
329         mMetricsWriter.write(DevicePolicyEventLogger
330                 .createEvent(DevicePolicyEnums.PROVISIONING_IS_NIGHT_MODE)
331                 .setBoolean(isNightMode)
332                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
333     }
334 
335     /**
336      * Logs all the provisioning extras passed by the dpc.
337      *
338      * @param context Context passed to MetricsLogger
339      * @param intent Intent that started provisioning
340      */
logProvisioningExtras(Context context, Intent intent)341     public void logProvisioningExtras(Context context, Intent intent) {
342         final List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
343         for (String extra : provisioningExtras) {
344             mMetricsLoggerWrapper.logAction(context, PROVISIONING_EXTRA, extra);
345         }
346         mMetricsWriter.write(DevicePolicyEventLogger
347                 .createEvent(DevicePolicyEnums.PROVISIONING_EXTRAS)
348                 .setStrings(provisioningExtras.toArray(new String[0]))
349                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
350     }
351 
352     /**
353      * Logs some entry points to provisioning.
354      *  @param context Context passed to MetricsLogger
355      * @param intent Intent that started provisioning
356      * @param settingsFacade
357      */
logEntryPoint(Context context, Intent intent, SettingsFacade settingsFacade)358     public void logEntryPoint(Context context, Intent intent,
359             SettingsFacade settingsFacade) {
360         if (intent == null || intent.getAction() == null) {
361             return;
362         }
363         switch (intent.getAction()) {
364             case ACTION_NDEF_DISCOVERED:
365                 mMetricsLoggerWrapper.logAction(context, PROVISIONING_ENTRY_POINT_NFC);
366                 mMetricsWriter.write(DevicePolicyEventLogger
367                         .createEvent(DevicePolicyEnums.PROVISIONING_ENTRY_POINT_NFC)
368                         .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
369                 break;
370             case ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE:
371                 logProvisionedFromTrustedSource(context, intent, settingsFacade);
372                 break;
373         }
374     }
375 
logRoleHolderProvisioningStart()376     public void logRoleHolderProvisioningStart() {
377         mMetricsWriter.write(DevicePolicyEventLogger
378                 .createEvent(DevicePolicyEnums.ROLE_HOLDER_PROVISIONING_START)
379                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
380     }
381 
logRoleHolderProvisioningFinish()382     public void logRoleHolderProvisioningFinish() {
383         mMetricsWriter.write(DevicePolicyEventLogger
384                 .createEvent(DevicePolicyEnums.ROLE_HOLDER_PROVISIONING_FINISH)
385                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
386     }
387 
logPlatformRoleHolderUpdateStart()388     public void logPlatformRoleHolderUpdateStart() {
389         mMetricsWriter.write(DevicePolicyEventLogger
390                 .createEvent(DevicePolicyEnums.PLATFORM_ROLE_HOLDER_UPDATE_START)
391                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
392     }
393 
logPlatformRoleHolderUpdateFailed()394     public void logPlatformRoleHolderUpdateFailed() {
395         mMetricsWriter.write(DevicePolicyEventLogger
396                 .createEvent(DevicePolicyEnums.PLATFORM_ROLE_HOLDER_UPDATE_FAILED)
397                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
398     }
399 
logPlatformRoleHolderUpdateFinished(int resultCode)400     public void logPlatformRoleHolderUpdateFinished(int resultCode) {
401         mMetricsWriter.write(DevicePolicyEventLogger
402                 .createEvent(DevicePolicyEnums.PLATFORM_ROLE_HOLDER_UPDATE_FINISHED)
403                         .setInt(resultCode)
404                         .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
405     }
406 
logRoleHolderUpdaterUpdateStart()407     public void logRoleHolderUpdaterUpdateStart() {
408         mMetricsWriter.write(DevicePolicyEventLogger
409                 .createEvent(DevicePolicyEnums.ROLE_HOLDER_UPDATER_UPDATE_START)
410                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
411     }
412 
logRoleHolderUpdaterUpdateFinish(int resultCode)413     public void logRoleHolderUpdaterUpdateFinish(int resultCode) {
414         mMetricsWriter.write(DevicePolicyEventLogger
415                 .createEvent(DevicePolicyEnums.ROLE_HOLDER_UPDATER_UPDATE_FINISH)
416                 .setInt(resultCode)
417                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
418     }
419 
logRoleHolderUpdaterUpdateRetry()420     public void logRoleHolderUpdaterUpdateRetry() {
421         mMetricsWriter.write(DevicePolicyEventLogger
422                 .createEvent(DevicePolicyEnums.ROLE_HOLDER_UPDATER_UPDATE_RETRY)
423                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
424     }
425 
logRoleHolderUpdaterUpdateFailed()426     public void logRoleHolderUpdaterUpdateFailed() {
427         mMetricsWriter.write(DevicePolicyEventLogger
428                 .createEvent(DevicePolicyEnums.ROLE_HOLDER_UPDATER_UPDATE_FAILED)
429                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
430     }
431 
logProvisionedFromTrustedSource(Context context, Intent intent, SettingsFacade settingsFacade)432     private void logProvisionedFromTrustedSource(Context context, Intent intent,
433             SettingsFacade settingsFacade) {
434         mMetricsLoggerWrapper.logAction(context, PROVISIONING_ENTRY_POINT_TRUSTED_SOURCE);
435         final int provisioningTrigger = intent.getIntExtra(EXTRA_PROVISIONING_TRIGGER,
436                 PROVISIONING_TRIGGER_UNSPECIFIED);
437         if (provisioningTrigger == PROVISIONING_TRIGGER_QR_CODE) {
438             mMetricsLoggerWrapper.logAction(context, PROVISIONING_ENTRY_POINT_QR_CODE);
439         }
440         mMetricsWriter.write(DevicePolicyEventLogger
441                 .createEvent(DevicePolicyEnums.PROVISIONING_ENTRY_POINT_TRUSTED_SOURCE)
442                 .setInt(provisioningTrigger)
443                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences))
444                 .setBoolean(settingsFacade.isDuringSetupWizard(context)));
445     }
446 
447     /**
448      * Logs package information of the dpc.
449      *
450      * @param context Context passed to MetricsLogger
451      * @param dpcPackageName Package name of the dpc
452      */
logDpcPackageInformation(Context context, String dpcPackageName)453     private void logDpcPackageInformation(Context context, String dpcPackageName) {
454         // Logs package name of the dpc.
455         mMetricsLoggerWrapper.logAction(context, PROVISIONING_DPC_PACKAGE_NAME, dpcPackageName);
456         mMetricsWriter.write(DevicePolicyEventLogger
457                 .createEvent(DevicePolicyEnums.PROVISIONING_DPC_PACKAGE_NAME)
458                 .setStrings(dpcPackageName)
459                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
460 
461         // Logs package name of the package which installed dpc.
462         final String dpcInstallerPackage =
463                 AnalyticsUtils.getInstallerPackageName(context, dpcPackageName);
464         mMetricsLoggerWrapper.logAction(context, PROVISIONING_DPC_INSTALLED_BY_PACKAGE,
465                 dpcInstallerPackage);
466         mMetricsWriter.write(DevicePolicyEventLogger
467                 .createEvent(DevicePolicyEnums.PROVISIONING_DPC_INSTALLED_BY_PACKAGE)
468                 .setStrings(dpcInstallerPackage)
469                 .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
470     }
471 
472     /**
473      * Logs the network type to which the device is connected.
474      *
475      * @param context Context passed to MetricsLogger
476      */
logNetworkType(Context context)477     private void logNetworkType(Context context) {
478         NetworkTypeLogger networkTypeLogger = new NetworkTypeLogger(context);
479         networkTypeLogger.log();
480     }
481 
maybeLogManagedProfileOnFullyManagedDevice(Context context, String provisioningAction)482     private void maybeLogManagedProfileOnFullyManagedDevice(Context context,
483             String provisioningAction) {
484         final DevicePolicyManager dpm =
485                 (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
486         final ComponentName currentDeviceOwner = dpm.getDeviceOwnerComponentOnAnyUser();
487         if (currentDeviceOwner != null
488                 && ACTION_PROVISION_MANAGED_PROFILE.equals(provisioningAction)) {
489             mMetricsWriter.write(DevicePolicyEventLogger
490                     .createEvent(PROVISIONING_MANAGED_PROFILE_ON_FULLY_MANAGED_DEVICE)
491                     .setTimePeriod(AnalyticsUtils.getProvisioningTime(mSharedPreferences)));
492         }
493     }
494 }
495