/* * Copyright (C) 2017 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. */ /* * Copyright (c) 2015-2017, The Linux Foundation. */ /* * Contributed by: Giesecke & Devrient GmbH. */ package android.se.omapi; import android.annotation.NonNull; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.os.RemoteException; import android.os.ServiceSpecificException; import android.util.Log; import java.io.IOException; /** * Instances of this class represent Secure Element Readers supported to this * device. These Readers can be physical devices or virtual devices. They can be * removable or not. They can contain Secure Element that can or cannot be * removed. * * @see GlobalPlatform Open Mobile API */ public final class Reader { private static final String TAG = "OMAPI.Reader"; private final String mName; private final SEService mService; private ISecureElementReader mReader; private final Object mLock = new Object(); Reader(@NonNull SEService service, @NonNull String name, @NonNull ISecureElementReader reader) { if (reader == null || service == null || name == null) { throw new IllegalArgumentException("Parameters cannot be null"); } mName = name; mService = service; mReader = reader; } /** * Return the name of this reader. *
true
if the SE is present, false
otherwise.
*/
public boolean isSecureElementPresent() {
if (!mService.isConnected()) {
throw new IllegalStateException("service is not connected");
}
try {
return mReader.isSecureElementPresent();
} catch (RemoteException e) {
throw new IllegalStateException("Error in isSecureElementPresent()");
}
}
/**
* Return the Secure Element service this reader is bound to.
*
* @return the SEService object.
*/
public @NonNull SEService getSEService() {
return mService;
}
/**
* Close all the sessions opened on this reader.
* All the channels opened by all these sessions will be closed.
*/
public void closeSessions() {
if (!mService.isConnected()) {
Log.e(TAG, "service is not connected");
return;
}
synchronized (mLock) {
try {
mReader.closeSessions();
} catch (RemoteException ignore) { }
}
}
/**
* Close all the sessions opened on this reader and reset the reader.
* All the channels opened by all these sessions will be closed.
* @return true
if reset success, false
otherwise.
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.SECURE_ELEMENT_PRIVILEGED_OPERATION)
public boolean reset() {
if (!mService.isConnected()) {
Log.e(TAG, "service is not connected");
return false;
}
synchronized (mLock) {
try {
closeSessions();
return mReader.reset();
} catch (RemoteException ignore) {
return false;
}
}
}
}