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 
18 import android.app.Service;
19 import android.car.ICarBluetoothUserService;
20 import android.car.ICarUserService;
21 import android.car.ILocationManagerProxy;
22 import android.content.Intent;
23 import android.os.IBinder;
24 import android.util.Log;
25 
26 /**
27  * {@link CarService} process runs as the System User. When logged in as a different user, some
28  * services do not provide an API to register or bind as a User, hence CarService doesn't receive
29  * the events from services/processes running as a non-system user.
30  *
31  * This Service is run as the Current User on every User Switch and components of CarService can
32  * use this service to communicate with services/processes running as the current (non-system) user.
33  */
34 public class PerUserCarService extends Service {
35     private static final boolean DBG = true;
36     private static final String TAG = "CarUserService";
37     private volatile CarBluetoothUserService mCarBluetoothUserService;
38     private volatile LocationManagerProxy mLocationManagerProxy;
39     private CarUserServiceBinder mCarUserServiceBinder;
40 
41     @Override
onBind(Intent intent)42     public IBinder onBind(Intent intent) {
43         if (DBG) {
44             Log.d(TAG, "onBind()");
45         }
46         if (mCarUserServiceBinder == null) {
47             Log.e(TAG, "UserSvcBinder null");
48         }
49         return mCarUserServiceBinder;
50     }
51 
52     @Override
onStartCommand(Intent intent, int flags, int startId)53     public int onStartCommand(Intent intent, int flags, int startId) {
54         if (DBG) {
55             Log.d(TAG, "onStart()");
56         }
57         return START_STICKY;
58     }
59 
60     @Override
onCreate()61     public void onCreate() {
62         if (DBG) {
63             Log.d(TAG, "onCreate()");
64         }
65         mCarUserServiceBinder = new CarUserServiceBinder();
66         mCarBluetoothUserService = new CarBluetoothUserService(this);
67         mLocationManagerProxy = new LocationManagerProxy(this);
68         super.onCreate();
69     }
70 
71     @Override
onDestroy()72     public void onDestroy() {
73         if (DBG) {
74             Log.d(TAG, "onDestroy()");
75         }
76         mCarUserServiceBinder = null;
77     }
78 
79     /**
80      * Other Services in CarService can create their own Binder interface and receive that interface
81      * through this CarUserService binder.
82      */
83     private final class CarUserServiceBinder extends ICarUserService.Stub {
84         @Override
getBluetoothUserService()85         public ICarBluetoothUserService getBluetoothUserService() {
86             return mCarBluetoothUserService;
87         }
88 
89         @Override
getLocationManagerProxy()90         public ILocationManagerProxy getLocationManagerProxy() {
91             return mLocationManagerProxy;
92         }
93     }
94 }
95