1 /*
2  * Copyright 2017, 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 package com.android.managedprovisioning.manageduser;
17 
18 import android.app.admin.DevicePolicyManager;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.UserHandle;
23 
24 import com.android.managedprovisioning.common.ProvisionLogger;
25 
26 /**
27  * This receiver is invoked after a managed user is created.
28  */
29 // TODO(b/178711424): move this into the framework.
30 public class ManagedUserCreationListener extends BroadcastReceiver {
31 
32     @Override
onReceive(Context context, Intent intent)33     public void onReceive(Context context, Intent intent) {
34         if (DevicePolicyManager.ACTION_MANAGED_USER_CREATED.equals(intent.getAction())) {
35             final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
36             final boolean leaveAllSystemAppsEnabled = intent.getBooleanExtra(
37                     DevicePolicyManager.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED, false);
38             ProvisionLogger.logd("ACTION_MANAGED_USER_CREATED received for user " + userId);
39             final PendingResult result = goAsync();
40             Thread thread = new Thread(() -> {
41                 new ManagedUserCreationController(userId, leaveAllSystemAppsEnabled, context).run();
42                 result.finish();
43             });
44             thread.setPriority(Thread.MAX_PRIORITY);
45             thread.start();
46         } else {
47             ProvisionLogger.logw("Unexpected intent action: " + intent.getAction());
48         }
49     }
50 }
51