1 /* 2 * Copyright (C) 2019 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 17 package com.android.car.trust; 18 19 import android.app.ActivityManager; 20 import android.bluetooth.BluetoothDevice; 21 import android.car.trust.TrustedDeviceInfo; 22 import android.content.Context; 23 import android.util.Log; 24 25 import com.android.car.CarServiceBase; 26 import com.android.car.trust.CarTrustAgentBleManager.BleEventCallback; 27 28 import java.io.PrintWriter; 29 import java.util.List; 30 31 /** 32 * The part of the Car service that enables the Trusted device feature. Trusted Device is a feature 33 * where a remote device is enrolled as a trusted device that can authorize an Android user in lieu 34 * of the user entering a password or PIN. 35 * <p> 36 * It is comprised of the {@link CarTrustAgentEnrollmentService} for handling enrollment and 37 * {@link CarTrustAgentUnlockService} for handling unlock/auth. 38 * 39 * @deprecated Enrolling a trusted device is no longer a supported feature of car service and these 40 * APIs will be removed in the next Android release. 41 */ 42 @Deprecated 43 public class CarTrustedDeviceService implements CarServiceBase, BleEventCallback { 44 private static final String TAG = CarTrustedDeviceService.class.getSimpleName(); 45 46 private final Context mContext; 47 private CarTrustAgentEnrollmentService mCarTrustAgentEnrollmentService; 48 private CarTrustAgentUnlockService mCarTrustAgentUnlockService; 49 private CarTrustAgentBleManager mCarTrustAgentBleManager; 50 CarTrustedDeviceService(Context context)51 public CarTrustedDeviceService(Context context) { 52 mContext = context; 53 BlePeripheralManager blePeripheralManager = new BlePeripheralManager(context); 54 mCarTrustAgentBleManager = new CarTrustAgentBleManager(context, blePeripheralManager); 55 mCarTrustAgentEnrollmentService = new CarTrustAgentEnrollmentService(mContext, this, 56 mCarTrustAgentBleManager); 57 mCarTrustAgentUnlockService = new CarTrustAgentUnlockService(mContext, this, 58 mCarTrustAgentBleManager); 59 } 60 61 @Override init()62 public synchronized void init() { 63 mCarTrustAgentEnrollmentService.init(); 64 mCarTrustAgentUnlockService.init(); 65 mCarTrustAgentBleManager.addBleEventCallback(this); 66 } 67 68 @Override release()69 public synchronized void release() { 70 mCarTrustAgentBleManager.cleanup(); 71 mCarTrustAgentEnrollmentService.release(); 72 mCarTrustAgentUnlockService.release(); 73 } 74 75 /** 76 * Returns the internal {@link CarTrustAgentEnrollmentService} instance. 77 */ getCarTrustAgentEnrollmentService()78 public CarTrustAgentEnrollmentService getCarTrustAgentEnrollmentService() { 79 return mCarTrustAgentEnrollmentService; 80 } 81 82 /** 83 * Returns the internal {@link CarTrustAgentUnlockService} instance. 84 */ getCarTrustAgentUnlockService()85 public CarTrustAgentUnlockService getCarTrustAgentUnlockService() { 86 return mCarTrustAgentUnlockService; 87 } 88 89 /** 90 * Called when a remote device is connected 91 * 92 * @param device the remote device 93 */ 94 @Override onRemoteDeviceConnected(BluetoothDevice device)95 public void onRemoteDeviceConnected(BluetoothDevice device) { 96 mCarTrustAgentEnrollmentService.onRemoteDeviceConnected(device); 97 mCarTrustAgentUnlockService.onRemoteDeviceConnected(device); 98 } 99 100 @Override onRemoteDeviceDisconnected(BluetoothDevice device)101 public void onRemoteDeviceDisconnected(BluetoothDevice device) { 102 mCarTrustAgentEnrollmentService.onRemoteDeviceDisconnected(device); 103 mCarTrustAgentUnlockService.onRemoteDeviceDisconnected(device); 104 } 105 106 @Override onClientDeviceNameRetrieved(String deviceName)107 public void onClientDeviceNameRetrieved(String deviceName) { 108 mCarTrustAgentEnrollmentService.onClientDeviceNameRetrieved(deviceName); 109 } 110 cleanupBleService()111 void cleanupBleService() { 112 if (Log.isLoggable(TAG, Log.DEBUG)) { 113 Log.d(TAG, "cleanupBleService"); 114 } 115 mCarTrustAgentBleManager.stopGattServer(); 116 mCarTrustAgentBleManager.stopEnrollmentAdvertising(); 117 mCarTrustAgentBleManager.stopUnlockAdvertising(); 118 } 119 120 @Override dump(PrintWriter writer)121 public void dump(PrintWriter writer) { 122 writer.println("*CarTrustedDeviceService*"); 123 int uid = ActivityManager.getCurrentUser(); 124 writer.println("current user id: " + uid); 125 List<TrustedDeviceInfo> deviceInfos = mCarTrustAgentEnrollmentService 126 .getEnrolledDeviceInfosForUser(uid); 127 writer.println(getDeviceInfoListString(uid, deviceInfos)); 128 mCarTrustAgentEnrollmentService.dump(writer); 129 mCarTrustAgentUnlockService.dump(writer); 130 } 131 getDeviceInfoListString(int uid, List<TrustedDeviceInfo> deviceInfos)132 private static String getDeviceInfoListString(int uid, List<TrustedDeviceInfo> deviceInfos) { 133 StringBuilder sb = new StringBuilder(); 134 sb.append("device list of (user : ").append(uid).append("):"); 135 if (deviceInfos != null && deviceInfos.size() > 0) { 136 for (int i = 0; i < deviceInfos.size(); i++) { 137 sb.append("\n\tdevice# ").append(i + 1).append(" : ") 138 .append(deviceInfos.get(i).toString()); 139 } 140 } else { 141 sb.append("\n\tno device listed"); 142 } 143 return sb.toString(); 144 } 145 146 } 147