Removing TrustAgent prototype code.
Trusted Device is a supported feature only from Q. Deleting the
TrustAgent and CarTrustAgentClientApp code that was done when
this feature was prototyped in P.
Bug: 144143366
Test: Target builds successfully.
Change-Id: I75dd7739c264697a2d5b5856bc31e183db3d72c2
(cherry picked from commit f83c5727c75ea84fe9f6691a5fbef7817ec7c063)
diff --git a/tests/CarTrustAgentClientApp/Android.mk b/tests/CarTrustAgentClientApp/Android.mk
deleted file mode 100644
index 3504ff7..0000000
--- a/tests/CarTrustAgentClientApp/Android.mk
+++ /dev/null
@@ -1,25 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_PACKAGE_NAME := CarTrustAgentClient
-
-LOCAL_USE_AAPT2 := true
-LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
-
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-
-LOCAL_STATIC_ANDROID_LIBRARIES := \
- androidx.appcompat_appcompat \
- androidx-constraintlayout_constraintlayout \
- androidx.legacy_legacy-support-v4
-
-LOCAL_CERTIFICATE := platform
-LOCAL_MODULE_TAGS := optional
-LOCAL_MIN_SDK_VERSION := 23
-LOCAL_SDK_VERSION := current
-
-LOCAL_PROGUARD_ENABLED := disabled
-
-LOCAL_DEX_PREOPT := false
-
-include $(BUILD_PACKAGE)
diff --git a/tests/CarTrustAgentClientApp/AndroidManifest.xml b/tests/CarTrustAgentClientApp/AndroidManifest.xml
deleted file mode 100644
index e76485f..0000000
--- a/tests/CarTrustAgentClientApp/AndroidManifest.xml
+++ /dev/null
@@ -1,53 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- ~ Copyright (C) 2018 The Android Open Source Project
- ~
- ~ Licensed under the Apache License, Version 2.0 (the "License");
- ~ you may not use this file except in compliance with the License.
- ~ You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing, software
- ~ distributed under the License is distributed on an "AS IS" BASIS,
- ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ~ See the License for the specific language governing permissions and
- ~ limitations under the License.
- -->
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.car.trust.client">
-
- <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23"/>
-
- <!-- Need Bluetooth LE -->
- <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
-
- <uses-permission android:name="android.permission.BLUETOOTH" />
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
-
- <!-- Needed to unlock user -->
- <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
- <uses-permission android:name="android.permission.MANAGE_USERS" />
- <uses-permission android:name="android.permission.CONTROL_KEYGUARD" />
- <uses-permission android:name="android.permission.PROVIDE_TRUST_AGENT" />
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
-
- <application
- android:label="@string/app_name"
- android:theme="@style/Theme.AppCompat">
-
- <activity
- android:name=".PhoneEnrolmentActivity"
- android:label="@string/app_name"
- android:exported="true"
- android:screenOrientation="portrait"
- android:launchMode="singleInstance">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-</manifest>
diff --git a/tests/CarTrustAgentClientApp/README.txt b/tests/CarTrustAgentClientApp/README.txt
deleted file mode 100644
index bf6c444..0000000
--- a/tests/CarTrustAgentClientApp/README.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-IMPORTANT NOTE: This is a reference app to smart unlock paired HU during development.
-Consider moving the functionality to a more proper place.
diff --git a/tests/CarTrustAgentClientApp/res/layout/phone_enrolment_activity.xml b/tests/CarTrustAgentClientApp/res/layout/phone_enrolment_activity.xml
deleted file mode 100644
index 7237dfa..0000000
--- a/tests/CarTrustAgentClientApp/res/layout/phone_enrolment_activity.xml
+++ /dev/null
@@ -1,70 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- ~ Copyright (C) 2018 The Android Open Source Project
- ~
- ~ Licensed under the Apache License, Version 2.0 (the "License");
- ~ you may not use this file except in compliance with the License.
- ~ You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing, software
- ~ distributed under the License is distributed on an "AS IS" BASIS,
- ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ~ See the License for the specific language governing permissions and
- ~ limitations under the License.
- -->
-<LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:weightSum="1">
- <ScrollView
- android:id="@+id/scroll"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:scrollbars="vertical"
- android:layout_weight="0.80">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/output"/>
- </ScrollView>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="0.10"
- android:orientation="horizontal">
- <Button
- android:id="@+id/enroll_scan"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="2"
- android:text="@string/enroll_scan"/>
- <Button
- android:id="@+id/enroll_button"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="3"
- android:text="@string/enroll_button"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="0.10"
- android:orientation="horizontal">
- <Button
- android:id="@+id/unlock_scan"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="2"
- android:text="@string/unlock_scan"/>
- <Button
- android:id="@+id/unlock_button"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="3"
- android:text="@string/unlock_button"/>
- </LinearLayout>
-</LinearLayout>
diff --git a/tests/CarTrustAgentClientApp/res/values/strings.xml b/tests/CarTrustAgentClientApp/res/values/strings.xml
deleted file mode 100644
index 6e33a81..0000000
--- a/tests/CarTrustAgentClientApp/res/values/strings.xml
+++ /dev/null
@@ -1,37 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- ~ Copyright (C) 2018 The Android Open Source Project
- ~
- ~ Licensed under the Apache License, Version 2.0 (the "License");
- ~ you may not use this file except in compliance with the License.
- ~ You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing, software
- ~ distributed under the License is distributed on an "AS IS" BASIS,
- ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ~ See the License for the specific language governing permissions and
- ~ limitations under the License.
- -->
-<resources>
- <string name="app_name" translatable="false">CarTrustAgentClient</string>
-
- <!-- service/characteristics uuid for unlocking a device -->
- <string name="unlock_service_uuid" translatable="false">5e2a68a1-27be-43f9-8d1e-4546976fabd7</string>
- <string name="unlock_escrow_token_uiid" translatable="false">5e2a68a2-27be-43f9-8d1e-4546976fabd7</string>
- <string name="unlock_handle_uiid" translatable="false">5e2a68a3-27be-43f9-8d1e-4546976fabd7</string>
-
- <!-- service/characteristics uuid for adding new escrow token -->
- <string name="enrollment_service_uuid" translatable="false">5e2a68a4-27be-43f9-8d1e-4546976fabd7</string>
- <string name="enrollment_handle_uuid" translatable="false">5e2a68a5-27be-43f9-8d1e-4546976fabd7</string>
- <string name="enrollment_token_uuid" translatable="false">5e2a68a6-27be-43f9-8d1e-4546976fabd7</string>
-
- <string name="pref_key_token_handle" translatable="false">token-handle-key</string>
- <string name="pref_key_escrow_token" translatable="false">escrow-token-key</string>
-
- <string name="enroll_button" translatable="false">Enroll new token</string>
- <string name="enroll_scan" translatable="false">Scan to enroll</string>
- <string name="unlock_button" translatable="false">Unlock</string>
- <string name="unlock_scan" translatable="false">Scan to unlock</string>
-</resources>
diff --git a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/BluetoothUtils.java b/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/BluetoothUtils.java
deleted file mode 100644
index 77ed7bb..0000000
--- a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/BluetoothUtils.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.car.trust.client;
-
-import android.bluetooth.BluetoothGattCharacteristic;
-import android.bluetooth.BluetoothGattService;
-import android.content.Context;
-
-import androidx.annotation.Nullable;
-import androidx.annotation.StringRes;
-
-import java.util.UUID;
-
-/**
- * A utility class holding methods related to Bluetooth.
- */
-public class BluetoothUtils {
- private BluetoothUtils() {}
-
- /**
- * Returns a characteristic off the given {@link BluetoothGattService} mapped to the jUUID
- * specified. If the given service has multiple characteristics of the same UUID, then the
- * first instance is returned.
- *
- * @param uuidRes The unique identifier for the characteristic.
- * @param service The {@link BluetoothGattService} that contains the characteristic.
- * @param context The current {@link Context}.
- * @return A {@link BluetoothGattCharacteristic} with a UUID matching {@code uuidRes} or
- * {@code null} if none exists.
- *
- * @see BluetoothGattService#getCharacteristic(UUID)
- */
- @Nullable
- public static BluetoothGattCharacteristic getCharacteristic(@StringRes int uuidRes,
- BluetoothGattService service, Context context) {
- return service.getCharacteristic(UUID.fromString(context.getString(uuidRes)));
- }
-}
diff --git a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneEnrolmentActivity.java b/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneEnrolmentActivity.java
deleted file mode 100644
index fd29624..0000000
--- a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneEnrolmentActivity.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.car.trust.client;
-
-import android.Manifest;
-import android.content.pm.PackageManager;
-import android.os.Bundle;
-
-import androidx.fragment.app.FragmentActivity;
-
-/**
- * Activity to allow the user to add an escrow token to a remote device. <p/>
- *
- * For this to work properly, the correct permissions must be set in the system config. In AOSP,
- * this config is in frameworks/base/core/res/res/values/config.xml <p/>
- *
- * The config must set config_allowEscrowTokenForTrustAgent to true. For the desired car
- * experience, the config should also set config_strongAuthRequiredOnBoot to false.
- */
-public class PhoneEnrolmentActivity extends FragmentActivity {
-
- private static final int FINE_LOCATION_REQUEST_CODE = 42;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.phone_enrolment_activity);
-
- PhoneEnrolmentController enrolmentController = new PhoneEnrolmentController(this);
- enrolmentController.bind(findViewById(R.id.output), findViewById(R.id.enroll_scan),
- findViewById(R.id.enroll_button));
-
- PhoneUnlockController unlockController = new PhoneUnlockController(this);
- unlockController.bind(findViewById(R.id.output), findViewById(R.id.unlock_scan),
- findViewById(R.id.unlock_button));
- }
-
- @Override
- protected void onResume() {
- super.onResume();
-
- if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- requestPermissions(
- new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION },
- FINE_LOCATION_REQUEST_CODE);
- }
- }
-}
diff --git a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneEnrolmentController.java b/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneEnrolmentController.java
deleted file mode 100644
index 1d3f672..0000000
--- a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneEnrolmentController.java
+++ /dev/null
@@ -1,197 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.car.trust.client;
-
-import android.bluetooth.BluetoothDevice;
-import android.bluetooth.BluetoothGatt;
-import android.bluetooth.BluetoothGattCharacteristic;
-import android.bluetooth.BluetoothGattService;
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.os.Handler;
-import android.os.ParcelUuid;
-import android.preference.PreferenceManager;
-import android.util.Base64;
-import android.util.Log;
-import android.widget.Button;
-import android.widget.TextView;
-
-import java.nio.ByteBuffer;
-import java.util.Random;
-import java.util.UUID;
-
-/**
- * A controller that sets up a {@link SimpleBleClient} to connect to the BLE enrollment service.
- * It also binds the UI components to control the enrollment process.
- */
-public class PhoneEnrolmentController {
- private static final String TAG = "PhoneEnrollmentCltr";
-
- private final String mTokenHandleKey;
- private final String mEscrowTokenKey;
-
- private final ParcelUuid mEnrolmentServiceUuid;
-
- private final SimpleBleClient mClient;
- private final Context mContext;
- private final Handler mHandler;
-
- // BLE characteristics associated with the enrollment/add escrow token service.
- private BluetoothGattCharacteristic mEnrolmentTokenHandle;
- private BluetoothGattCharacteristic mEnrolmentEscrowToken;
-
- private TextView mTextView;
- private Button mEnrolButton;
-
- public PhoneEnrolmentController(Context context) {
- mContext = context;
-
- mTokenHandleKey = context.getString(R.string.pref_key_token_handle);
- mEscrowTokenKey = context.getString(R.string.pref_key_escrow_token);
-
- mClient = new SimpleBleClient(context);
- mEnrolmentServiceUuid = new ParcelUuid(
- UUID.fromString(mContext.getString(R.string.enrollment_service_uuid)));
- mClient.addCallback(mCallback /* callback */);
-
- mHandler = new Handler(mContext.getMainLooper());
- }
-
- /**
- * Binds the views to the actions that can be performed by this controller.
- *
- * @param textView A text view used to display results from various BLE actions
- * @param scanButton Button used to start scanning for available BLE devices.
- * @param enrolButton Button used to send new escrow token to remote device.
- */
- public void bind(TextView textView, Button scanButton, Button enrolButton) {
- mTextView = textView;
- mEnrolButton = enrolButton;
-
- scanButton.setOnClickListener(v -> mClient.start(mEnrolmentServiceUuid));
-
- mEnrolButton.setEnabled(false);
- mEnrolButton.setAlpha(0.3f);
- mEnrolButton.setOnClickListener(v -> {
- appendOutputText("Sending new escrow token to remote device");
-
- byte[] token = generateEscrowToken();
- sendEnrolmentRequest(token);
-
- // WARNING: Store the token so it can be used later for unlocking. This token
- // should NEVER be stored on the device that is being unlocked. It should
- // always be securely stored on a remote device that will trigger the unlock.
- storeToken(token);
- });
- }
-
- /**
- * @return A random byte array that is used as the escrow token for remote device unlock.
- */
- private byte[] generateEscrowToken() {
- Random random = new Random();
- ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
- buffer.putLong(0, random.nextLong());
- return buffer.array();
- }
-
- private void sendEnrolmentRequest(byte[] token) {
- mEnrolmentEscrowToken.setValue(token);
- mClient.writeCharacteristic(mEnrolmentEscrowToken);
- storeToken(token);
- }
-
- private void storeHandle(long handle) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
- prefs.edit().putLong(mTokenHandleKey, handle).apply();
- }
-
- private void storeToken(byte[] token) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
- String byteArray = Base64.encodeToString(token, Base64.DEFAULT);
- prefs.edit().putString(mEscrowTokenKey, byteArray).apply();
- }
-
- private void appendOutputText(final String text) {
- mHandler.post(() -> mTextView.append("\n" + text));
- }
-
- private final SimpleBleClient.ClientCallback mCallback = new SimpleBleClient.ClientCallback() {
- @Override
- public void onDeviceConnected(BluetoothDevice device) {
- appendOutputText("Device connected: " + device.getName()
- + " addr: " + device.getAddress());
- }
-
- @Override
- public void onDeviceDisconnected() {
- appendOutputText("Device disconnected");
- }
-
- @Override
- public void onCharacteristicChanged(BluetoothGatt gatt,
- BluetoothGattCharacteristic characteristic) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "onCharacteristicChanged: "
- + convertToLong(characteristic.getValue()));
- }
-
- if (characteristic.getUuid().equals(mEnrolmentTokenHandle.getUuid())) {
- // Store the new token handle that the BLE server is sending us. This required
- // to unlock the device.
- long handle = convertToLong(characteristic.getValue());
- storeHandle(handle);
- appendOutputText("Token handle received: " + handle);
- }
- }
-
- @Override
- public void onServiceDiscovered(BluetoothGattService service) {
- if (!service.getUuid().equals(mEnrolmentServiceUuid.getUuid())) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Service UUID: " + service.getUuid()
- + " does not match Enrolment UUID " + mEnrolmentServiceUuid.getUuid());
- }
- return;
- }
-
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Enrolment Service # characteristics: "
- + service.getCharacteristics().size());
- }
-
- mEnrolmentEscrowToken = BluetoothUtils.getCharacteristic(
- R.string.enrollment_token_uuid, service, mContext);
- mEnrolmentTokenHandle = BluetoothUtils.getCharacteristic(
- R.string.enrollment_handle_uuid, service, mContext);
- mClient.setCharacteristicNotification(mEnrolmentTokenHandle, true /* enable */);
- appendOutputText("Enrolment BLE client successfully connected");
-
- mHandler.post(() -> {
- // Services are now set up, allow users to enrol new escrow tokens.
- mEnrolButton.setEnabled(true);
- mEnrolButton.setAlpha(1.0f);
- });
- }
-
- private long convertToLong(byte[] bytes) {
- ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
- buffer.put(bytes);
- buffer.flip();
- return buffer.getLong();
- }
- };
-}
diff --git a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneUnlockController.java b/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneUnlockController.java
deleted file mode 100644
index 1296529..0000000
--- a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/PhoneUnlockController.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.car.trust.client;
-
-import android.bluetooth.BluetoothDevice;
-import android.bluetooth.BluetoothGatt;
-import android.bluetooth.BluetoothGattCharacteristic;
-import android.bluetooth.BluetoothGattService;
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.os.Handler;
-import android.os.ParcelUuid;
-import android.preference.PreferenceManager;
-import android.util.Base64;
-import android.util.Log;
-import android.widget.Button;
-import android.widget.TextView;
-
-import java.nio.ByteBuffer;
-import java.util.UUID;
-
-/**
- * A controller that sets up a {@link SimpleBleClient} to connect to the BLE unlock service.
- */
-public class PhoneUnlockController {
- private static final String TAG = "PhoneUnlockController";
-
- private final String mTokenHandleKey;
- private final String mEscrowTokenKey;
-
- // BLE characteristics associated with the enrolment/add escrow token service.
- private BluetoothGattCharacteristic mUnlockTokenHandle;
- private BluetoothGattCharacteristic mUnlockEscrowToken;
-
- private final ParcelUuid mUnlockServiceUuid;
-
- private final SimpleBleClient mClient;
- private final Context mContext;
- private final Handler mHandler;
-
- private TextView mTextView;
- private Button mUnlockButton;
-
- public PhoneUnlockController(Context context) {
- mContext = context;
-
- mTokenHandleKey = context.getString(R.string.pref_key_token_handle);
- mEscrowTokenKey = context.getString(R.string.pref_key_escrow_token);
-
- mClient = new SimpleBleClient(context);
- mUnlockServiceUuid = new ParcelUuid(
- UUID.fromString(mContext.getString(R.string.unlock_service_uuid)));
- mClient.addCallback(mCallback /* callback */);
-
- mHandler = new Handler(mContext.getMainLooper());
- }
-
- /**
- * Binds the views to the actions that can be performed by this controller.
- *
- * @param textView A text view used to display results from various BLE actions
- * @param scanButton Button used to start scanning for available BLE devices.
- * @param enrolButton Button used to send new escrow token to remote device.
- */
- public void bind(TextView textView, Button scanButton, Button enrolButton) {
- mTextView = textView;
- mUnlockButton = enrolButton;
-
- scanButton.setOnClickListener(v -> mClient.start(mUnlockServiceUuid));
-
- mUnlockButton.setEnabled(false);
- mUnlockButton.setAlpha(0.3f);
- mUnlockButton.setOnClickListener(v -> {
- appendOutputText("Sending unlock token and handle to remote device");
- sendUnlockRequest();
- });
- }
-
- private void sendUnlockRequest() {
- // Retrieve stored token and handle and write to remote device.
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
- long handle = prefs.getLong(mTokenHandleKey, -1);
- byte[] token = Base64.decode(prefs.getString(mEscrowTokenKey, null), Base64.DEFAULT);
-
- mUnlockEscrowToken.setValue(token);
- mUnlockTokenHandle.setValue(convertToBytes(handle));
-
- mClient.writeCharacteristic(mUnlockEscrowToken);
- mClient.writeCharacteristic(mUnlockTokenHandle);
- }
-
- private void appendOutputText(String text) {
- mHandler.post(() -> mTextView.append("\n" + text));
- }
-
- private static byte[] convertToBytes(long l) {
- ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
- buffer.putLong(0, l);
- return buffer.array();
- }
-
- private final SimpleBleClient.ClientCallback mCallback = new SimpleBleClient.ClientCallback() {
- @Override
- public void onDeviceConnected(BluetoothDevice device) {
- appendOutputText("Device connected: " + device.getName()
- + " addr: " + device.getAddress());
- }
-
- @Override
- public void onDeviceDisconnected() {
- appendOutputText("Device disconnected");
- }
-
- @Override
- public void onCharacteristicChanged(BluetoothGatt gatt,
- BluetoothGattCharacteristic characteristic) {
- // Not expecting any characteristics changes for the unlocking client.
- }
-
- @Override
- public void onServiceDiscovered(BluetoothGattService service) {
- if (!service.getUuid().equals(mUnlockServiceUuid.getUuid())) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Service UUID: " + service.getUuid()
- + " does not match Enrolment UUID " + mUnlockServiceUuid.getUuid());
- }
- return;
- }
-
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Unlock Service # characteristics: "
- + service.getCharacteristics().size());
- }
-
- mUnlockEscrowToken = BluetoothUtils.getCharacteristic(
- R.string.unlock_escrow_token_uiid, service, mContext);
- mUnlockTokenHandle = BluetoothUtils.getCharacteristic(
- R.string.unlock_handle_uiid, service, mContext);
- appendOutputText("Unlock BLE client successfully connected");
-
- mHandler.post(() -> {
- // Services are now set up, allow users to enrol new escrow tokens.
- mUnlockButton.setEnabled(true);
- mUnlockButton.setAlpha(1.0f);
- });
- }
- };
-}
diff --git a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/SimpleBleClient.java b/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/SimpleBleClient.java
deleted file mode 100644
index 3cce775..0000000
--- a/tests/CarTrustAgentClientApp/src/com/android/car/trust/client/SimpleBleClient.java
+++ /dev/null
@@ -1,405 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.car.trust.client;
-
-import android.bluetooth.BluetoothDevice;
-import android.bluetooth.BluetoothGatt;
-import android.bluetooth.BluetoothGattCallback;
-import android.bluetooth.BluetoothGattCharacteristic;
-import android.bluetooth.BluetoothGattService;
-import android.bluetooth.BluetoothManager;
-import android.bluetooth.BluetoothProfile;
-import android.bluetooth.le.BluetoothLeScanner;
-import android.bluetooth.le.ScanCallback;
-import android.bluetooth.le.ScanFilter;
-import android.bluetooth.le.ScanResult;
-import android.bluetooth.le.ScanSettings;
-import android.content.Context;
-import android.os.Handler;
-import android.os.ParcelUuid;
-import android.util.Log;
-
-import androidx.annotation.IntDef;
-import androidx.annotation.Nullable;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Queue;
-import java.util.concurrent.ConcurrentLinkedQueue;
-import java.util.concurrent.TimeUnit;
-
-/**
- * A simple client that supports the scanning and connecting to available BLE devices. Should be
- * used along with {@link SimpleBleServer}.
- */
-public class SimpleBleClient {
- private static final String TAG = "SimpleBleClient";
- private static final long SCAN_TIME_MS = TimeUnit.SECONDS.toMillis(10);
-
- private final Queue<BleAction> mBleActionQueue = new ConcurrentLinkedQueue<BleAction>();
- private final List<ClientCallback> mCallbacks = new ArrayList<>();
- private final Context mContext;
- private final BluetoothLeScanner mScanner;
-
- private BluetoothGatt mBtGatt;
- private ParcelUuid mServiceUuid;
-
- public SimpleBleClient(Context context) {
- mContext = context;
- BluetoothManager btManager = (BluetoothManager) mContext.getSystemService(
- Context.BLUETOOTH_SERVICE);
- mScanner = btManager.getAdapter().getBluetoothLeScanner();
- }
-
- /**
- * Start scanning for a BLE devices with the specified service uuid.
- *
- * @param parcelUuid {@link ParcelUuid} used to identify the device that should be used for
- * this client. This uuid should be the same as the one that is set in the
- * {@link android.bluetooth.le.AdvertiseData.Builder} by the advertising
- * device.
- */
- public void start(ParcelUuid parcelUuid) {
- mServiceUuid = parcelUuid;
-
- // We only want to scan for devices that have the correct uuid set in its advertise data.
- List<ScanFilter> filters = new ArrayList<ScanFilter>();
- ScanFilter.Builder serviceFilter = new ScanFilter.Builder();
- serviceFilter.setServiceUuid(mServiceUuid);
- filters.add(serviceFilter.build());
-
- ScanSettings.Builder settings = new ScanSettings.Builder();
- settings.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
-
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Start scanning for uuid: " + mServiceUuid.getUuid());
- }
-
- mScanner.startScan(filters, settings.build(), mScanCallback);
-
- Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Stopping Scanner");
- }
- mScanner.stopScan(mScanCallback);
- }
- }, SCAN_TIME_MS);
- }
-
- private boolean hasServiceUuid(ScanResult result) {
- if (result.getScanRecord() == null
- || result.getScanRecord().getServiceUuids() == null
- || result.getScanRecord().getServiceUuids().size() == 0) {
- return false;
- }
- return true;
- }
-
- /**
- * Writes to a {@link BluetoothGattCharacteristic} if possible, or queues the action until
- * other actions are complete.
- *
- * @param characteristic {@link BluetoothGattCharacteristic} to be written
- */
- public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
- processAction(new BleAction(characteristic, BleAction.ACTION_WRITE));
- }
-
- /**
- * Reads a {@link BluetoothGattCharacteristic} if possible, or queues the read action until
- * other actions are complete.
- *
- * @param characteristic {@link BluetoothGattCharacteristic} to be read.
- */
- public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
- processAction(new BleAction(characteristic, BleAction.ACTION_READ));
- }
-
- /**
- * Enable or disable notification for specified {@link BluetoothGattCharacteristic}.
- *
- * @param characteristic The {@link BluetoothGattCharacteristic} for which to enable
- * notifications.
- * @param enabled True if notifications should be enabled, false otherwise.
- */
- public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
- boolean enabled) {
- mBtGatt.setCharacteristicNotification(characteristic, enabled);
- }
-
- /**
- * Add a {@link ClientCallback} to listen for updates from BLE components
- */
- public void addCallback(ClientCallback callback) {
- mCallbacks.add(callback);
- }
-
- public void removeCallback(ClientCallback callback) {
- mCallbacks.remove(callback);
- }
-
- private void processAction(BleAction action) {
- // Only execute actions if the queue is empty.
- if (mBleActionQueue.size() > 0) {
- mBleActionQueue.add(action);
- return;
- }
-
- mBleActionQueue.add(action);
- executeAction(mBleActionQueue.peek());
- }
-
- private void processNextAction() {
- mBleActionQueue.poll();
- executeAction(mBleActionQueue.peek());
- }
-
- private void executeAction(@Nullable BleAction action) {
- if (action == null) {
- return;
- }
-
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Executing BLE Action type: " + action.getAction());
- }
-
- switch (action.getAction()) {
- case BleAction.ACTION_WRITE:
- mBtGatt.writeCharacteristic(action.getCharacteristic());
- break;
- case BleAction.ACTION_READ:
- mBtGatt.readCharacteristic(action.getCharacteristic());
- break;
- default:
- Log.e(TAG, "Encountered unknown BlueAction: " + action.getAction());
- }
- }
-
- private String getStatus(int status) {
- switch (status) {
- case BluetoothGatt.GATT_FAILURE:
- return "Failure";
- case BluetoothGatt.GATT_SUCCESS:
- return "GATT_SUCCESS";
- case BluetoothGatt.GATT_READ_NOT_PERMITTED:
- return "GATT_READ_NOT_PERMITTED";
- case BluetoothGatt.GATT_WRITE_NOT_PERMITTED:
- return "GATT_WRITE_NOT_PERMITTED";
- case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
- return "GATT_INSUFFICIENT_AUTHENTICATION";
- case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED:
- return "GATT_REQUEST_NOT_SUPPORTED";
- case BluetoothGatt.GATT_INVALID_OFFSET:
- return "GATT_INVALID_OFFSET";
- case BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH:
- return "GATT_INVALID_ATTRIBUTE_LENGTH";
- case BluetoothGatt.GATT_CONNECTION_CONGESTED:
- return "GATT_CONNECTION_CONGESTED";
- default:
- return "unknown";
- }
- }
-
- private ScanCallback mScanCallback = new ScanCallback() {
- @Override
- public void onScanResult(int callbackType, ScanResult result) {
- BluetoothDevice device = result.getDevice();
-
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Scan result found: " + result.getScanRecord().getServiceUuids());
- }
-
- if (!hasServiceUuid(result)) {
- return;
- }
-
- for (ParcelUuid uuid : result.getScanRecord().getServiceUuids()) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Scan result UUID: " + uuid);
- }
-
- if (uuid.equals(mServiceUuid)) {
- // This client only supports connecting to one service.
- // Once we find one, stop scanning and open a GATT connection to the device.
- mScanner.stopScan(mScanCallback);
- mBtGatt = device.connectGatt(mContext, /* autoConnect= */ false, mGattCallback);
- return;
- }
- }
- }
-
- @Override
- public void onBatchScanResults(List<ScanResult> results) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- for (ScanResult r : results) {
- Log.d(TAG, "Batch scanResult: " + r.getDevice().getName()
- + " " + r.getDevice().getAddress());
- }
- }
- }
-
- @Override
- public void onScanFailed(int errorCode) {
- Log.e(TAG, "Scan failed: " + errorCode);
- }
- };
-
- private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
- @Override
- public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
- super.onConnectionStateChange(gatt, status, newState);
-
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Gatt connection status: " + getStatus(status)
- + " newState: " + newState);
- }
-
- switch (newState) {
- case BluetoothProfile.STATE_CONNECTED:
- mBtGatt.discoverServices();
- for (ClientCallback callback : mCallbacks) {
- callback.onDeviceConnected(gatt.getDevice());
- }
- break;
-
- case BluetoothProfile.STATE_DISCONNECTED:
- for (ClientCallback callback : mCallbacks) {
- callback.onDeviceDisconnected();
- }
- break;
-
- default:
- // Do nothing.
- }
- }
-
- @Override
- public void onServicesDiscovered(BluetoothGatt gatt, int status) {
- super.onServicesDiscovered(gatt, status);
-
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "onServicesDiscovered: " + status);
- }
-
- List<BluetoothGattService> services = gatt.getServices();
- if (services == null || services.size() <= 0) {
- return;
- }
-
- // Notify clients of newly discovered services.
- for (BluetoothGattService service : mBtGatt.getServices()) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "Found service: " + service.getUuid() + " notifying clients");
- }
-
- for (ClientCallback callback : mCallbacks) {
- callback.onServiceDiscovered(service);
- }
- }
- }
-
- @Override
- public void onCharacteristicWrite(BluetoothGatt gatt,
- BluetoothGattCharacteristic characteristic, int status) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "onCharacteristicWrite: " + status);
- }
-
- processNextAction();
- }
-
- @Override
- public void onCharacteristicRead(BluetoothGatt gatt,
- BluetoothGattCharacteristic characteristic, int status) {
- if (Log.isLoggable(TAG, Log.DEBUG)) {
- Log.d(TAG, "onCharacteristicRead:" + new String(characteristic.getValue()));
- }
-
- processNextAction();
- }
-
- @Override
- public void onCharacteristicChanged(BluetoothGatt gatt,
- BluetoothGattCharacteristic characteristic) {
- for (ClientCallback callback : mCallbacks) {
- callback.onCharacteristicChanged(gatt, characteristic);
- }
- processNextAction();
- }
- };
-
- /**
- * Wrapper class to allow queuing of BLE actions. The BLE stack allows only one action to be
- * executed at a time.
- */
- private static class BleAction {
- public static final int ACTION_WRITE = 0;
- public static final int ACTION_READ = 1;
-
- @IntDef({ ACTION_WRITE, ACTION_READ })
- public @interface ActionType {}
-
- private final int mAction;
- private final BluetoothGattCharacteristic mCharacteristic;
-
- BleAction(BluetoothGattCharacteristic characteristic, @ActionType int action) {
- mAction = action;
- mCharacteristic = characteristic;
- }
-
- @ActionType
- public int getAction() {
- return mAction;
- }
-
- public BluetoothGattCharacteristic getCharacteristic() {
- return mCharacteristic;
- }
- }
-
- /**
- * Callback for classes that wish to be notified of BLE updates.
- */
- public interface ClientCallback {
- /**
- * Called when a device that has a matching service UUID is found.
- **/
- void onDeviceConnected(BluetoothDevice device);
-
- /** Called when the currently connected device has been disconnected. */
- void onDeviceDisconnected();
-
- /**
- * Called when a characteristic has been changed.
- *
- * @param gatt The GATT client the characteristic is associated with.
- * @param characteristic The characteristic that has been changed.
- */
- void onCharacteristicChanged(BluetoothGatt gatt,
- BluetoothGattCharacteristic characteristic);
-
- /**
- * Called for each {@link BluetoothGattService} that is discovered on the
- * {@link BluetoothDevice} after a matching scan result and connection.
- *
- * @param service {@link BluetoothGattService} that has been discovered.
- */
- void onServiceDiscovered(BluetoothGattService service);
- }
-}