• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.car.user;
18 
19 import android.annotation.Nullable;
20 import android.car.settings.CarSettings;
21 import android.car.user.CarUserManagerHelper;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.pm.UserInfo;
27 import android.util.Log;
28 
29 import com.android.car.CarServiceBase;
30 import com.android.internal.annotations.VisibleForTesting;
31 
32 import 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  */
42 public 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 
CarUserService( @ullable Context context, @Nullable CarUserManagerHelper carUserManagerHelper)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
init()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
release()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
dump(PrintWriter writer)79     public void dump(PrintWriter writer) {
80         writer.println(TAG);
81         writer.println("Context: " + mContext);
82     }
83 
84     @Override
onReceive(Context context, Intent intent)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 }
100