in niap-cc/Permissions/Tester/app/src/main/java/com/android/certifications/niap/permissions/SignaturePermissionTester.java [2862:2921]
private Runnable getBindRunnable(final String permission) {
return () -> {
final CountDownLatch latch = new CountDownLatch(1);
TestBindService[] service = new TestBindService[1];
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder binder) {
mLogger.logDebug("onServiceConnected: className = " + className);
service[0] = TestBindService.Stub.asInterface(binder);
latch.countDown();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mLogger.logDebug("onServiceDisconnected: arg0 = " + arg0);
}
};
// All of the services exported by the companion app app are named
// Test<PermissionName>Service where <PermissionName> is an upper camel case conversion
// of the snake case permission name. For instance the service for
// BIND_ACCESSIBILITY_SERVICE is TestBindAccessibilityServiceService.
String permissionName = permission.substring(permission.lastIndexOf('.') + 1);
StringBuilder serviceName = new StringBuilder();
serviceName.append("Test");
for (String element : permission.substring(permission.lastIndexOf('.') + 1).split(
"_")) {
serviceName.append(element.substring(0, 1)).append(
element.substring(1).toLowerCase());
}
serviceName.append("Service");
Intent intent = new Intent();
intent.setComponent(new ComponentName(Constants.COMPANION_PACKAGE,
Constants.COMPANION_PACKAGE + ".services." + serviceName.toString()));
mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
boolean connectionSuccessful = false;
try {
connectionSuccessful = latch.await(2000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
mLogger.logError(
"Caught an InterruptedException waiting for the service to connect: ",
e);
}
if (!connectionSuccessful) {
throw new SecurityException(
"Unable to establish a connection to the service guarded by the "
+ permissionName + " permission");
} else {
// Each service implements TestBindService which provides a single #testMethod
// that is used to verify the service connection.
try {
service[0].testMethod();
} catch (RemoteException e) {
throw new UnexpectedPermissionTestFailureException(e);
} finally {
mContext.unbindService(serviceConnection);
}
}
};
}