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