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 static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.DUMP_INFO; 19 20 import android.car.ICarBluetoothUserService; 21 import android.car.ILocationManagerProxy; 22 import android.car.IPerUserCarService; 23 import android.car.builtin.util.Slogf; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.IBinder; 27 28 import com.android.car.bluetooth.CarBluetoothUserService; 29 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 30 import com.android.car.internal.ProxiedService; 31 import com.android.car.internal.util.IndentingPrintWriter; 32 33 import java.io.FileDescriptor; 34 import java.io.PrintWriter; 35 36 /** 37 * {@link CarServiceImpl} process runs as the System User. When logged in as a different user, some 38 * services do not provide an API to register or bind as a User, hence CarService doesn't receive 39 * the events from services/processes running as a non-system user. 40 * 41 * This Service is run as the Current User on every User Switch and components of CarService can 42 * use this service to communicate with services/processes running as the current (non-system) user. 43 */ 44 public class PerUserCarServiceImpl extends ProxiedService { 45 private static final boolean DBG = true; 46 private static final String TAG = CarLog.tagFor(PerUserCarServiceImpl.class); 47 48 private CarBluetoothUserService mCarBluetoothUserService; 49 private LocationManagerProxy mLocationManagerProxy; 50 51 private PerUserCarServiceBinder mPerUserCarServiceBinder; 52 53 @Override onBind(Intent intent)54 public IBinder onBind(Intent intent) { 55 if (DBG) Slogf.d(TAG, "onBind()"); 56 57 if (mPerUserCarServiceBinder == null) { 58 Slogf.e(TAG, "PerUserCarServiceBinder null"); 59 } 60 return mPerUserCarServiceBinder; 61 } 62 63 @Override onStartCommand(Intent intent, int flags, int startId)64 public int onStartCommand(Intent intent, int flags, int startId) { 65 if (DBG) Slogf.d(TAG, "onStart()"); 66 67 return START_STICKY; 68 } 69 70 @Override onCreate()71 public void onCreate() { 72 Context context = getApplicationContext(); 73 Slogf.i(TAG, "created for user %s", context.getUser()); 74 75 mPerUserCarServiceBinder = new PerUserCarServiceBinder(); 76 mCarBluetoothUserService = new CarBluetoothUserService(this); 77 mLocationManagerProxy = new LocationManagerProxy(this); 78 super.onCreate(); 79 } 80 81 @Override onDestroy()82 public void onDestroy() { 83 Slogf.i(TAG, "destroyed for user %s", getApplicationContext().getUser()); 84 85 mPerUserCarServiceBinder = null; 86 } 87 88 @Override 89 @ExcludeFromCodeCoverageGeneratedReport(reason = DUMP_INFO) dump(FileDescriptor fd, PrintWriter writer, String[] args)90 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 91 try (IndentingPrintWriter pw = new IndentingPrintWriter(writer)) { 92 pw.println("CarBluetoothUserService"); 93 pw.increaseIndent(); 94 mCarBluetoothUserService.dump(pw); 95 pw.decreaseIndent(); 96 pw.println(); 97 pw.println("LocationManagerProxy"); 98 pw.increaseIndent(); 99 mLocationManagerProxy.dump(pw); 100 pw.decreaseIndent(); 101 pw.println(); 102 } 103 } 104 105 /** 106 * Other Services in CarService can create their own Binder interface and receive that interface 107 * through this PerUserCarService binder. 108 */ 109 private final class PerUserCarServiceBinder extends IPerUserCarService.Stub { 110 @Override getBluetoothUserService()111 public ICarBluetoothUserService getBluetoothUserService() { 112 return mCarBluetoothUserService; 113 } 114 115 @Override getLocationManagerProxy()116 public ILocationManagerProxy getLocationManagerProxy() { 117 return mLocationManagerProxy; 118 } 119 } 120 } 121