blob: 6a9291a8509fd0887924e86f473d4ec882e3f28d [file] [log] [blame]
Ram Periathiruvadiacb60242017-04-13 16:19:09 -07001/*
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 */
16package com.android.car;
17
Felipe Leme8725a2f2020-12-09 16:04:51 -080018import android.annotation.Nullable;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070019import android.app.Service;
20import android.car.ICarBluetoothUserService;
Gregory Clark55620c12019-06-03 17:42:58 -070021import android.car.ILocationManagerProxy;
Eric Jeonga84a6822019-09-17 15:46:38 -070022import android.car.IPerUserCarService;
Felipe Leme8725a2f2020-12-09 16:04:51 -080023import android.content.Context;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070024import android.content.Intent;
Felipe Leme8725a2f2020-12-09 16:04:51 -080025import android.content.pm.PackageManager;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070026import android.os.IBinder;
Felipe Leme83a3de92020-12-09 16:04:51 -080027import android.util.IndentingPrintWriter;
Felipe Leme83a3de92020-12-09 16:04:51 -080028
Felipe Leme8725a2f2020-12-09 16:04:51 -080029import com.android.car.admin.PerUserCarDevicePolicyService;
Felipe Leme701b0432021-03-24 16:25:31 -070030import com.android.server.utils.Slogf;
Felipe Leme8725a2f2020-12-09 16:04:51 -080031
Felipe Leme83a3de92020-12-09 16:04:51 -080032import java.io.FileDescriptor;
33import java.io.PrintWriter;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070034
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 */
43public class PerUserCarService extends Service {
44 private static final boolean DBG = true;
Mayank Garg72c71d22021-02-03 23:54:45 -080045 private static final String TAG = CarLog.tagFor(PerUserCarService.class);
Felipe Leme83a3de92020-12-09 16:04:51 -080046
47 private CarBluetoothUserService mCarBluetoothUserService;
48 private LocationManagerProxy mLocationManagerProxy;
Felipe Leme8725a2f2020-12-09 16:04:51 -080049 private @Nullable PerUserCarDevicePolicyService mPerUserCarDevicePolicyService;
Eric Jeonga84a6822019-09-17 15:46:38 -070050 private PerUserCarServiceBinder mPerUserCarServiceBinder;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070051
52 @Override
53 public IBinder onBind(Intent intent) {
Felipe Leme701b0432021-03-24 16:25:31 -070054 if (DBG) Slogf.d(TAG, "onBind()");
Felipe Leme83a3de92020-12-09 16:04:51 -080055
Eric Jeonga84a6822019-09-17 15:46:38 -070056 if (mPerUserCarServiceBinder == null) {
Felipe Leme701b0432021-03-24 16:25:31 -070057 Slogf.e(TAG, "PerUserCarServiceBinder null");
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070058 }
Eric Jeonga84a6822019-09-17 15:46:38 -070059 return mPerUserCarServiceBinder;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070060 }
61
62 @Override
63 public int onStartCommand(Intent intent, int flags, int startId) {
Felipe Leme701b0432021-03-24 16:25:31 -070064 if (DBG) Slogf.d(TAG, "onStart()");
Felipe Leme83a3de92020-12-09 16:04:51 -080065
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070066 return START_STICKY;
67 }
68
69 @Override
70 public void onCreate() {
Felipe Leme8725a2f2020-12-09 16:04:51 -080071 Context context = getApplicationContext();
Felipe Leme701b0432021-03-24 16:25:31 -070072 Slogf.i(TAG, "created for user %d", context.getUserId());
Felipe Leme83a3de92020-12-09 16:04:51 -080073
Eric Jeonga84a6822019-09-17 15:46:38 -070074 mPerUserCarServiceBinder = new PerUserCarServiceBinder();
Gregory Clark55620c12019-06-03 17:42:58 -070075 mCarBluetoothUserService = new CarBluetoothUserService(this);
Felipe Leme8725a2f2020-12-09 16:04:51 -080076
77 if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) {
78 mPerUserCarDevicePolicyService = PerUserCarDevicePolicyService.getInstance(context);
Felipe Leme1ba22f72021-06-15 11:55:11 -070079 mPerUserCarDevicePolicyService.onCreate();
Felipe Leme8725a2f2020-12-09 16:04:51 -080080 } else if (DBG) {
Felipe Leme701b0432021-03-24 16:25:31 -070081 Slogf.d(TAG, "Not setting PerUserCarDevicePolicyService because device doesn't have %s",
82 PackageManager.FEATURE_DEVICE_ADMIN);
Felipe Leme8725a2f2020-12-09 16:04:51 -080083 }
84
Gregory Clark55620c12019-06-03 17:42:58 -070085 mLocationManagerProxy = new LocationManagerProxy(this);
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070086 super.onCreate();
87 }
88
89 @Override
90 public void onDestroy() {
Felipe Leme701b0432021-03-24 16:25:31 -070091 Slogf.i(TAG, "destroyed for user %d", getApplicationContext().getUserId());
Felipe Leme83a3de92020-12-09 16:04:51 -080092
Felipe Leme8725a2f2020-12-09 16:04:51 -080093 if (mPerUserCarDevicePolicyService != null) {
94 mPerUserCarDevicePolicyService.onDestroy();
95 }
Eric Jeonga84a6822019-09-17 15:46:38 -070096 mPerUserCarServiceBinder = null;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070097 }
98
Felipe Leme83a3de92020-12-09 16:04:51 -080099 @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 Leme8725a2f2020-12-09 16:04:51 -0800106 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 Leme176a5fd2021-01-20 15:48:33 -0800116 pw.println();
117
118 pw.println("LocationManagerProxy");
119 pw.increaseIndent();
120 mLocationManagerProxy.dump(pw);
121 pw.decreaseIndent();
122 pw.println();
Felipe Leme83a3de92020-12-09 16:04:51 -0800123 }
124 }
125
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700126 /**
127 * Other Services in CarService can create their own Binder interface and receive that interface
Eric Jeonga84a6822019-09-17 15:46:38 -0700128 * through this PerUserCarService binder.
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700129 */
Eric Jeonga84a6822019-09-17 15:46:38 -0700130 private final class PerUserCarServiceBinder extends IPerUserCarService.Stub {
Gregory Clark55620c12019-06-03 17:42:58 -0700131 @Override
132 public ICarBluetoothUserService getBluetoothUserService() {
133 return mCarBluetoothUserService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700134 }
135
136 @Override
Gregory Clark55620c12019-06-03 17:42:58 -0700137 public ILocationManagerProxy getLocationManagerProxy() {
138 return mLocationManagerProxy;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700139 }
140 }
141}