blob: 74ff78d4b9b88125937054be1cbd420e4b5f34a7 [file] [log] [blame]
Ying Zhengd3cb98e2018-05-11 11:42:48 -07001/*
2 * Copyright (C) 2018 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
17package com.android.car.user;
18
19import android.annotation.Nullable;
20import android.car.settings.CarSettings;
21import android.car.user.CarUserManagerHelper;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.pm.UserInfo;
27import android.util.Log;
28
29import com.android.car.CarServiceBase;
30import com.android.internal.annotations.VisibleForTesting;
31
32import java.io.PrintWriter;
33
34/**
35 * User service for cars. Manages users at boot time. Including:
36 *
37 * <ol>
38 * <li> Creates a secondary admin user on first run.
39 * <li> Log in to a default user.
40 * <ol/>
41 */
42public class CarUserService extends BroadcastReceiver implements CarServiceBase {
43 // Place holder for user name of the first user created.
44 @VisibleForTesting
45 static final String OWNER_NAME = "Owner";
46 private static final String TAG = "CarUserService";
47 private final Context mContext;
48 private final CarUserManagerHelper mCarUserManagerHelper;
49
50 public CarUserService(
51 @Nullable Context context, @Nullable CarUserManagerHelper carUserManagerHelper) {
52 if (Log.isLoggable(TAG, Log.DEBUG)) {
53 Log.d(TAG, "constructed");
54 }
55 mContext = context;
56 mCarUserManagerHelper = carUserManagerHelper;
57 }
58
59 @Override
60 public void init() {
61 if (Log.isLoggable(TAG, Log.DEBUG)) {
62 Log.d(TAG, "init");
63 }
64 IntentFilter filter = new IntentFilter();
65 filter.addAction(Intent.ACTION_LOCKED_BOOT_COMPLETED);
66
67 mContext.registerReceiver(this, filter);
68 }
69
70 @Override
71 public void release() {
72 if (Log.isLoggable(TAG, Log.DEBUG)) {
73 Log.d(TAG, "release");
74 }
75 mContext.unregisterReceiver(this);
76 }
77
78 @Override
79 public void dump(PrintWriter writer) {
80 writer.println(TAG);
81 writer.println("Context: " + mContext);
82 }
83
84 @Override
85 public void onReceive(Context context, Intent intent) {
86 if (Log.isLoggable(TAG, Log.DEBUG)) {
87 Log.d(TAG, "onReceive " + intent);
88 }
89
90 if (intent.getAction() == Intent.ACTION_LOCKED_BOOT_COMPLETED) {
91 if (mCarUserManagerHelper.getAllUsers().size() == 0) {
92 UserInfo admin = mCarUserManagerHelper.createNewAdminUser(OWNER_NAME);
93 mCarUserManagerHelper.switchToUser(admin);
94 } else {
95 mCarUserManagerHelper.switchToUserId(CarSettings.DEFAULT_USER_ID_TO_BOOT_INTO);
96 }
97 }
98 }
99}