Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 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; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame^] | 20 | import android.car.settings.CarSettings; |
Ying Zheng | 9fc9940 | 2018-09-19 14:23:59 -0700 | [diff] [blame] | 21 | import android.car.userlib.CarUserManagerHelper; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 26 | import android.location.LocationManager; |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Ying Zheng | 8f90edb | 2018-06-13 12:42:31 -0700 | [diff] [blame] | 28 | import android.os.UserManager; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame^] | 29 | import android.provider.Settings; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
| 32 | import com.android.car.CarServiceBase; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.PrintWriter; |
| 35 | |
| 36 | /** |
| 37 | * User service for cars. Manages users at boot time. Including: |
| 38 | * |
| 39 | * <ol> |
| 40 | * <li> Creates a secondary admin user on first run. |
Ying Zheng | f4339b8 | 2018-06-19 16:01:05 -0700 | [diff] [blame] | 41 | * <li> Log in to the last active user. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 42 | * <ol/> |
| 43 | */ |
| 44 | public class CarUserService extends BroadcastReceiver implements CarServiceBase { |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 45 | private static final String TAG = "CarUserService"; |
| 46 | private final Context mContext; |
| 47 | private final CarUserManagerHelper mCarUserManagerHelper; |
| 48 | |
| 49 | public CarUserService( |
| 50 | @Nullable Context context, @Nullable CarUserManagerHelper carUserManagerHelper) { |
| 51 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 52 | Log.d(TAG, "constructed"); |
| 53 | } |
| 54 | mContext = context; |
| 55 | mCarUserManagerHelper = carUserManagerHelper; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void init() { |
| 60 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 61 | Log.d(TAG, "init"); |
| 62 | } |
| 63 | IntentFilter filter = new IntentFilter(); |
| 64 | filter.addAction(Intent.ACTION_LOCKED_BOOT_COMPLETED); |
Ying Zheng | f4339b8 | 2018-06-19 16:01:05 -0700 | [diff] [blame] | 65 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 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 | |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 90 | if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) { |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame^] | 91 | // We want to set restrictions on system and guest users only once. These are persisted |
| 92 | // onto disk, so it's sufficient to do it once + we minimize the number of disk writes. |
| 93 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 94 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, /* default= */ 0) == 0) { |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 95 | setSystemUserRestrictions(); |
jovanak | 811265f | 2018-06-28 11:22:14 -0700 | [diff] [blame] | 96 | mCarUserManagerHelper.initDefaultGuestRestrictions(); |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame^] | 97 | Settings.Global.putInt(mContext.getContentResolver(), |
| 98 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, 1); |
Ying Zheng | f4339b8 | 2018-06-19 16:01:05 -0700 | [diff] [blame] | 99 | } |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame^] | 100 | |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 101 | } else if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { |
| 102 | // Update last active user if the switched-to user is a persistent, non-system user. |
| 103 | int currentUser = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 104 | if (currentUser > UserHandle.USER_SYSTEM |
| 105 | && mCarUserManagerHelper.isPersistentUser(currentUser)) { |
Ying Zheng | bb1ba2d | 2018-11-08 10:46:16 -0800 | [diff] [blame] | 106 | mCarUserManagerHelper.setLastActiveUser(currentUser); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 110 | |
| 111 | private void setSystemUserRestrictions() { |
| 112 | // Disable adding accounts for system user. |
| 113 | mCarUserManagerHelper.setUserRestriction(mCarUserManagerHelper.getSystemUserInfo(), |
| 114 | UserManager.DISALLOW_MODIFY_ACCOUNTS, /* enable= */ true); |
| 115 | |
| 116 | // Disable Location service for system user. |
| 117 | LocationManager locationManager = |
| 118 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 119 | locationManager.setLocationEnabledForUser( |
| 120 | /* enabled= */ false, UserHandle.of(UserHandle.USER_SYSTEM)); |
| 121 | } |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 122 | } |