Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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.car; |
| 17 | |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 18 | import android.annotation.Nullable; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 19 | import android.app.Service; |
| 20 | import android.car.ICarBluetoothUserService; |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 21 | import android.car.ILocationManagerProxy; |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 22 | import android.car.IPerUserCarService; |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 27 | import android.util.IndentingPrintWriter; |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 28 | |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 29 | import com.android.car.admin.PerUserCarDevicePolicyService; |
Felipe Leme | 701b043 | 2021-03-24 16:25:31 -0700 | [diff] [blame] | 30 | import com.android.server.utils.Slogf; |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 31 | |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 32 | import java.io.FileDescriptor; |
| 33 | import java.io.PrintWriter; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * {@link CarService} process runs as the System User. When logged in as a different user, some |
| 37 | * services do not provide an API to register or bind as a User, hence CarService doesn't receive |
| 38 | * the events from services/processes running as a non-system user. |
| 39 | * |
| 40 | * This Service is run as the Current User on every User Switch and components of CarService can |
| 41 | * use this service to communicate with services/processes running as the current (non-system) user. |
| 42 | */ |
| 43 | public class PerUserCarService extends Service { |
| 44 | private static final boolean DBG = true; |
Mayank Garg | 72c71d2 | 2021-02-03 23:54:45 -0800 | [diff] [blame] | 45 | private static final String TAG = CarLog.tagFor(PerUserCarService.class); |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 46 | |
| 47 | private CarBluetoothUserService mCarBluetoothUserService; |
| 48 | private LocationManagerProxy mLocationManagerProxy; |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 49 | private @Nullable PerUserCarDevicePolicyService mPerUserCarDevicePolicyService; |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 50 | private PerUserCarServiceBinder mPerUserCarServiceBinder; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | public IBinder onBind(Intent intent) { |
Felipe Leme | 701b043 | 2021-03-24 16:25:31 -0700 | [diff] [blame] | 54 | if (DBG) Slogf.d(TAG, "onBind()"); |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 55 | |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 56 | if (mPerUserCarServiceBinder == null) { |
Felipe Leme | 701b043 | 2021-03-24 16:25:31 -0700 | [diff] [blame] | 57 | Slogf.e(TAG, "PerUserCarServiceBinder null"); |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 58 | } |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 59 | return mPerUserCarServiceBinder; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public int onStartCommand(Intent intent, int flags, int startId) { |
Felipe Leme | 701b043 | 2021-03-24 16:25:31 -0700 | [diff] [blame] | 64 | if (DBG) Slogf.d(TAG, "onStart()"); |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 65 | |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 66 | return START_STICKY; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onCreate() { |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 71 | Context context = getApplicationContext(); |
Felipe Leme | 701b043 | 2021-03-24 16:25:31 -0700 | [diff] [blame] | 72 | Slogf.i(TAG, "created for user %d", context.getUserId()); |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 73 | |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 74 | mPerUserCarServiceBinder = new PerUserCarServiceBinder(); |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 75 | mCarBluetoothUserService = new CarBluetoothUserService(this); |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 76 | |
| 77 | if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { |
| 78 | mPerUserCarDevicePolicyService = PerUserCarDevicePolicyService.getInstance(context); |
Felipe Leme | 1ba22f7 | 2021-06-15 11:55:11 -0700 | [diff] [blame] | 79 | mPerUserCarDevicePolicyService.onCreate(); |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 80 | } else if (DBG) { |
Felipe Leme | 701b043 | 2021-03-24 16:25:31 -0700 | [diff] [blame] | 81 | Slogf.d(TAG, "Not setting PerUserCarDevicePolicyService because device doesn't have %s", |
| 82 | PackageManager.FEATURE_DEVICE_ADMIN); |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 85 | mLocationManagerProxy = new LocationManagerProxy(this); |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 86 | super.onCreate(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onDestroy() { |
Felipe Leme | 701b043 | 2021-03-24 16:25:31 -0700 | [diff] [blame] | 91 | Slogf.i(TAG, "destroyed for user %d", getApplicationContext().getUserId()); |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 92 | |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 93 | if (mPerUserCarDevicePolicyService != null) { |
| 94 | mPerUserCarDevicePolicyService.onDestroy(); |
| 95 | } |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 96 | mPerUserCarServiceBinder = null; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 99 | @Override |
| 100 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 101 | try (IndentingPrintWriter pw = new IndentingPrintWriter(writer)) { |
| 102 | pw.println("CarBluetoothUserService"); |
| 103 | pw.increaseIndent(); |
| 104 | mCarBluetoothUserService.dump(pw); |
| 105 | pw.decreaseIndent(); |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 106 | pw.println(); |
| 107 | |
| 108 | if (mPerUserCarDevicePolicyService != null) { |
| 109 | pw.println("PerUserCarDevicePolicyService"); |
| 110 | pw.increaseIndent(); |
| 111 | mPerUserCarDevicePolicyService.dump(pw); |
| 112 | pw.decreaseIndent(); |
| 113 | } else { |
| 114 | pw.println("PerUserCarDevicePolicyService not needed"); |
| 115 | } |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 116 | pw.println(); |
| 117 | |
| 118 | pw.println("LocationManagerProxy"); |
| 119 | pw.increaseIndent(); |
| 120 | mLocationManagerProxy.dump(pw); |
| 121 | pw.decreaseIndent(); |
| 122 | pw.println(); |
Felipe Leme | 83a3de9 | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Other Services in CarService can create their own Binder interface and receive that interface |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 128 | * through this PerUserCarService binder. |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 129 | */ |
Eric Jeong | a84a682 | 2019-09-17 15:46:38 -0700 | [diff] [blame] | 130 | private final class PerUserCarServiceBinder extends IPerUserCarService.Stub { |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 131 | @Override |
| 132 | public ICarBluetoothUserService getBluetoothUserService() { |
| 133 | return mCarBluetoothUserService; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Gregory Clark | 55620c1 | 2019-06-03 17:42:58 -0700 | [diff] [blame] | 137 | public ILocationManagerProxy getLocationManagerProxy() { |
| 138 | return mLocationManagerProxy; |
Ram Periathiruvadi | acb6024 | 2017-04-13 16:19:09 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |