aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbConnectionManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbConnectionManager.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbConnectionManager.java118
1 files changed, 12 insertions, 106 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbConnectionManager.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbConnectionManager.java
index c98d5d43f..8a6971fe6 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbConnectionManager.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbConnectionManager.java
@@ -1,7 +1,6 @@
package org.sufficientlysecure.keychain.smartcard;
import android.app.Activity;
-import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -10,79 +9,29 @@ import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import org.sufficientlysecure.keychain.Constants;
+import org.sufficientlysecure.keychain.ui.UsbEventReceiverActivity;
import org.sufficientlysecure.keychain.util.Log;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.atomic.AtomicBoolean;
-
public class UsbConnectionManager {
- private static final String LOG_TAG = UsbConnectionManager.class.getName();
- private static final String ACTION_USB_PERMISSION = Constants.PACKAGE_NAME + ".USB_PERMITSSION";
- private final Semaphore mRunning = new Semaphore(1);
- private final Set<UsbDevice> mProcessedDevices = Collections.newSetFromMap(new ConcurrentHashMap<UsbDevice, Boolean>());
- private final AtomicBoolean mStopped = new AtomicBoolean(false);
private Activity mActivity;
- private final Thread mWatchThread = new Thread() {
- @Override
- public void run() {
- final UsbManager usbManager = (UsbManager) mActivity.getSystemService(Context.USB_SERVICE);
-
- while (!mStopped.get()) {
- try {
- mRunning.acquire();
- } catch (InterruptedException e) {
- }
- mRunning.release();
- if (mStopped.get()) return;
-
- //
- final UsbDevice device = getDevice(usbManager);
- if (device != null && !mProcessedDevices.contains(device)) {
- mProcessedDevices.add(device);
-
- final Intent intent = new Intent(ACTION_USB_PERMISSION);
-
- IntentFilter filter = new IntentFilter();
- filter.addAction(ACTION_USB_PERMISSION);
- mActivity.registerReceiver(mUsbReceiver, filter);
- Log.d(LOG_TAG, "Requesting permission for " + device.getDeviceName());
- usbManager.requestPermission(device, PendingIntent.getBroadcast(mActivity, 0, intent, 0));
- }
-
- try {
- sleep(1000);
- } catch (InterruptedException ignored) {
- }
- }
- }
- };
private OnDiscoveredUsbDeviceListener mListener;
/**
- * Receives broadcast when a supported USB device is attached, detached or
- * when a permission to communicate to the device has been granted.
+ * Receives broadcast when a supported USB device get permission.
*/
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
- UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- String deviceName = usbDevice.getDeviceName();
- if (ACTION_USB_PERMISSION.equals(action)) {
+ if (UsbEventReceiverActivity.ACTION_USB_PERMISSION.equals(action)) {
+ UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,
false);
- Log.d(LOG_TAG, "ACTION_USB_PERMISSION: " + permission + " Device: " + deviceName);
-
if (permission) {
- interceptIntent(intent);
+ Log.d(Constants.TAG, "Got permission for " + usbDevice.getDeviceName());
+ mListener.usbDeviceDiscovered(usbDevice);
}
-
- context.unregisterReceiver(mUsbReceiver);
}
}
};
@@ -90,59 +39,16 @@ public class UsbConnectionManager {
public UsbConnectionManager(final Activity activity, final OnDiscoveredUsbDeviceListener listener) {
this.mActivity = activity;
this.mListener = listener;
- mRunning.acquireUninterruptibly();
- mWatchThread.start();
- }
-
- private static UsbDevice getDevice(UsbManager manager) {
- HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
- for (UsbDevice device : deviceList.values()) {
- if (device.getVendorId() == 0x1050 && (device.getProductId() == 0x0112 || device.getProductId() == 0x0115)) {
- return device;
- }
- }
- return null;
}
- public void startListeningForDevices() {
- mRunning.release();
- }
-
- public void stopListeningForDevices() {
- mRunning.acquireUninterruptibly();
- }
+ public void onStart() {
+ final IntentFilter intentFilter = new IntentFilter();
+ intentFilter.addAction(UsbEventReceiverActivity.ACTION_USB_PERMISSION);
- public void interceptIntent(final Intent intent) {
- if (intent == null || intent.getAction() == null) return;
- switch (intent.getAction()) {
- /*case UsbManager.ACTION_USB_DEVICE_ATTACHED: {
- final UsbManager usbManager = (UsbManager) mActivity.getSystemService(Context.USB_SERVICE);
- final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- Intent usbI = new Intent(mActivity, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- usbI.setAction(ACTION_USB_PERMISSION);
- usbI.putExtra(UsbManager.EXTRA_DEVICE, device);
- PendingIntent pi = PendingIntent.getActivity(mActivity, 0, usbI, PendingIntent.FLAG_CANCEL_CURRENT);
- usbManager.requestPermission(device, pi);
- break;
- }*/
- case ACTION_USB_PERMISSION: {
- UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (device != null)
- mListener.usbDeviceDiscovered(device);
- break;
- }
- default:
- break;
- }
+ mActivity.registerReceiver(mUsbReceiver, intentFilter);
}
- public void onDestroy() {
- mStopped.set(true);
- mRunning.release();
- try {
- mActivity.unregisterReceiver(mUsbReceiver);
- } catch (IllegalArgumentException ignore) {
- }
- mActivity = null;
+ public void onStop() {
+ mActivity.unregisterReceiver(mUsbReceiver);
}
}