DO NOT MERGE - Create a car user service. Protected by flag.

Test: Unit test
Bug: 79536548
Change-Id: I8f8a42a5811e38c8c03f0f5d4e9665870a44674d
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
new file mode 100644
index 0000000..74ff78d
--- /dev/null
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.user;
+
+import android.annotation.Nullable;
+import android.car.settings.CarSettings;
+import android.car.user.CarUserManagerHelper;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.UserInfo;
+import android.util.Log;
+
+import com.android.car.CarServiceBase;
+import com.android.internal.annotations.VisibleForTesting;
+
+import java.io.PrintWriter;
+
+/**
+ * User service for cars. Manages users at boot time. Including:
+ *
+ * <ol>
+ *   <li> Creates a secondary admin user on first run.
+ *   <li> Log in to a default user.
+ * <ol/>
+ */
+public class CarUserService extends BroadcastReceiver implements CarServiceBase {
+    // Place holder for user name of the first user created.
+    @VisibleForTesting
+    static final String OWNER_NAME = "Owner";
+    private static final String TAG = "CarUserService";
+    private final Context mContext;
+    private final CarUserManagerHelper mCarUserManagerHelper;
+
+    public CarUserService(
+                @Nullable Context context, @Nullable CarUserManagerHelper carUserManagerHelper) {
+        if (Log.isLoggable(TAG, Log.DEBUG)) {
+            Log.d(TAG, "constructed");
+        }
+        mContext = context;
+        mCarUserManagerHelper = carUserManagerHelper;
+    }
+
+    @Override
+    public void init() {
+        if (Log.isLoggable(TAG, Log.DEBUG)) {
+            Log.d(TAG, "init");
+        }
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(Intent.ACTION_LOCKED_BOOT_COMPLETED);
+
+        mContext.registerReceiver(this, filter);
+    }
+
+    @Override
+    public void release() {
+        if (Log.isLoggable(TAG, Log.DEBUG)) {
+            Log.d(TAG, "release");
+        }
+        mContext.unregisterReceiver(this);
+    }
+
+    @Override
+    public void dump(PrintWriter writer) {
+        writer.println(TAG);
+        writer.println("Context: " + mContext);
+    }
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (Log.isLoggable(TAG, Log.DEBUG)) {
+            Log.d(TAG, "onReceive " + intent);
+        }
+
+        if (intent.getAction() == Intent.ACTION_LOCKED_BOOT_COMPLETED) {
+            if (mCarUserManagerHelper.getAllUsers().size() == 0) {
+                UserInfo admin = mCarUserManagerHelper.createNewAdminUser(OWNER_NAME);
+                mCarUserManagerHelper.switchToUser(admin);
+            } else {
+                mCarUserManagerHelper.switchToUserId(CarSettings.DEFAULT_USER_ID_TO_BOOT_INTO);
+            }
+        }
+    }
+}