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.annotation.Nullable; 19 import android.app.Service; 20 import android.car.ICarBluetoothUserService; 21 import android.car.ILocationManagerProxy; 22 import android.car.IPerUserCarService; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.os.IBinder; 27 import android.util.IndentingPrintWriter; 28 29 import com.android.car.admin.PerUserCarDevicePolicyService; 30 import com.android.server.utils.Slogf; 31 32 import java.io.FileDescriptor; 33 import java.io.PrintWriter; 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; 45 private static final String TAG = CarLog.tagFor(PerUserCarService.class); 46 47 private CarBluetoothUserService mCarBluetoothUserService; 48 private LocationManagerProxy mLocationManagerProxy; 49 private @Nullable PerUserCarDevicePolicyService mPerUserCarDevicePolicyService; 50 private PerUserCarServiceBinder mPerUserCarServiceBinder; 51 52 @Override onBind(Intent intent)53 public IBinder onBind(Intent intent) { 54 if (DBG) Slogf.d(TAG, "onBind()"); 55 56 if (mPerUserCarServiceBinder == null) { 57 Slogf.e(TAG, "PerUserCarServiceBinder null"); 58 } 59 return mPerUserCarServiceBinder; 60 } 61 62 @Override onStartCommand(Intent intent, int flags, int startId)63 public int onStartCommand(Intent intent, int flags, int startId) { 64 if (DBG) Slogf.d(TAG, "onStart()"); 65 66 return START_STICKY; 67 } 68 69 @Override onCreate()70 public void onCreate() { 71 Context context = getApplicationContext(); 72 Slogf.i(TAG, "created for user %d", context.getUserId()); 73 74 mPerUserCarServiceBinder = new PerUserCarServiceBinder(); 75 mCarBluetoothUserService = new CarBluetoothUserService(this); 76 77 if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { 78 mPerUserCarDevicePolicyService = PerUserCarDevicePolicyService.getInstance(context); 79 mPerUserCarDevicePolicyService.onCreate(); 80 } else if (DBG) { 81 Slogf.d(TAG, "Not setting PerUserCarDevicePolicyService because device doesn't have %s", 82 PackageManager.FEATURE_DEVICE_ADMIN); 83 } 84 85 mLocationManagerProxy = new LocationManagerProxy(this); 86 super.onCreate(); 87 } 88 89 @Override onDestroy()90 public void onDestroy() { 91 Slogf.i(TAG, "destroyed for user %d", getApplicationContext().getUserId()); 92 93 if (mPerUserCarDevicePolicyService != null) { 94 mPerUserCarDevicePolicyService.onDestroy(); 95 } 96 mPerUserCarServiceBinder = null; 97 } 98 99 @Override dump(FileDescriptor fd, PrintWriter writer, String[] args)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(); 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 } 116 pw.println(); 117 118 pw.println("LocationManagerProxy"); 119 pw.increaseIndent(); 120 mLocationManagerProxy.dump(pw); 121 pw.decreaseIndent(); 122 pw.println(); 123 } 124 } 125 126 /** 127 * Other Services in CarService can create their own Binder interface and receive that interface 128 * through this PerUserCarService binder. 129 */ 130 private final class PerUserCarServiceBinder extends IPerUserCarService.Stub { 131 @Override getBluetoothUserService()132 public ICarBluetoothUserService getBluetoothUserService() { 133 return mCarBluetoothUserService; 134 } 135 136 @Override getLocationManagerProxy()137 public ILocationManagerProxy getLocationManagerProxy() { 138 return mLocationManagerProxy; 139 } 140 } 141 } 142