|
18 | 18 |
|
19 | 19 | import android.app.Activity;
|
20 | 20 | import android.content.Context;
|
21 |
| - |
22 | 21 | import com.yubico.yubikit.android.transport.nfc.NfcConfiguration;
|
23 | 22 | import com.yubico.yubikit.android.transport.nfc.NfcNotAvailable;
|
24 | 23 | import com.yubico.yubikit.android.transport.nfc.NfcYubiKeyDevice;
|
|
27 | 26 | import com.yubico.yubikit.android.transport.usb.UsbYubiKeyDevice;
|
28 | 27 | import com.yubico.yubikit.android.transport.usb.UsbYubiKeyManager;
|
29 | 28 | import com.yubico.yubikit.core.util.Callback;
|
30 |
| - |
31 | 29 | import javax.annotation.Nullable;
|
32 | 30 |
|
33 | 31 | /**
|
34 |
| - * Starting point for YubiKey device discovery over both USB and NFC. |
35 |
| - * Use this class to listen for YubiKeys and get a {@link com.yubico.yubikit.core.YubiKeyDevice} reference. |
| 32 | + * Starting point for YubiKey device discovery over both USB and NFC. Use this class to listen for |
| 33 | + * YubiKeys and get a {@link com.yubico.yubikit.core.YubiKeyDevice} reference. |
36 | 34 | */
|
37 | 35 | public final class YubiKitManager {
|
38 |
| - private final UsbYubiKeyManager usbYubiKeyManager; |
39 |
| - @Nullable |
40 |
| - private final NfcYubiKeyManager nfcYubiKeyManager; |
| 36 | + private final UsbYubiKeyManager usbYubiKeyManager; |
| 37 | + @Nullable private final NfcYubiKeyManager nfcYubiKeyManager; |
41 | 38 |
|
42 |
| - @Nullable |
43 |
| - private static NfcYubiKeyManager buildNfcDeviceManager(Context context) { |
44 |
| - try { |
45 |
| - return new NfcYubiKeyManager(context, null); |
46 |
| - } catch (NfcNotAvailable e) { |
47 |
| - return null; |
48 |
| - } |
| 39 | + @Nullable private static NfcYubiKeyManager buildNfcDeviceManager(Context context) { |
| 40 | + try { |
| 41 | + return new NfcYubiKeyManager(context, null); |
| 42 | + } catch (NfcNotAvailable e) { |
| 43 | + return null; |
49 | 44 | }
|
| 45 | + } |
50 | 46 |
|
51 |
| - /** |
52 |
| - * Initialize instance of {@link YubiKitManager} |
53 |
| - * |
54 |
| - * @param context application context |
55 |
| - */ |
56 |
| - public YubiKitManager(Context context) { |
57 |
| - this(new UsbYubiKeyManager(context.getApplicationContext()), buildNfcDeviceManager(context.getApplicationContext())); |
58 |
| - } |
| 47 | + /** |
| 48 | + * Initialize instance of {@link YubiKitManager} |
| 49 | + * |
| 50 | + * @param context application context |
| 51 | + */ |
| 52 | + public YubiKitManager(Context context) { |
| 53 | + this( |
| 54 | + new UsbYubiKeyManager(context.getApplicationContext()), |
| 55 | + buildNfcDeviceManager(context.getApplicationContext())); |
| 56 | + } |
59 | 57 |
|
60 |
| - /** |
61 |
| - * Initialize an instance of {@link YubiKitManager}, providing the USB and NFC YubiKey managers to use for device discovery. |
62 |
| - * |
63 |
| - * @param usbYubiKeyManager UsbYubiKeyManager instance to use for USB communication |
64 |
| - * @param nfcYubiKeyManager NfcYubiKeyManager instance to use for NFC communication |
65 |
| - */ |
66 |
| - public YubiKitManager(UsbYubiKeyManager usbYubiKeyManager, @Nullable NfcYubiKeyManager nfcYubiKeyManager) { |
67 |
| - this.usbYubiKeyManager = usbYubiKeyManager; |
68 |
| - this.nfcYubiKeyManager = nfcYubiKeyManager; |
69 |
| - } |
| 58 | + /** |
| 59 | + * Initialize an instance of {@link YubiKitManager}, providing the USB and NFC YubiKey managers to |
| 60 | + * use for device discovery. |
| 61 | + * |
| 62 | + * @param usbYubiKeyManager UsbYubiKeyManager instance to use for USB communication |
| 63 | + * @param nfcYubiKeyManager NfcYubiKeyManager instance to use for NFC communication |
| 64 | + */ |
| 65 | + public YubiKitManager( |
| 66 | + UsbYubiKeyManager usbYubiKeyManager, @Nullable NfcYubiKeyManager nfcYubiKeyManager) { |
| 67 | + this.usbYubiKeyManager = usbYubiKeyManager; |
| 68 | + this.nfcYubiKeyManager = nfcYubiKeyManager; |
| 69 | + } |
70 | 70 |
|
| 71 | + /** |
| 72 | + * Subscribe on changes that happen via USB and detect if there any Yubikeys got connected |
| 73 | + * |
| 74 | + * <p>This registers broadcast receivers, to unsubscribe from receiver use {@link |
| 75 | + * YubiKitManager#stopUsbDiscovery()} |
| 76 | + * |
| 77 | + * @param usbConfiguration additional configurations on how USB discovery should be handled |
| 78 | + * @param listener listener that is going to be invoked upon successful discovery of key session |
| 79 | + * or failure to detect any session (lack of permissions) |
| 80 | + */ |
| 81 | + public void startUsbDiscovery( |
| 82 | + final UsbConfiguration usbConfiguration, Callback<? super UsbYubiKeyDevice> listener) { |
| 83 | + usbYubiKeyManager.enable(usbConfiguration, listener); |
| 84 | + } |
71 | 85 |
|
72 |
| - /** |
73 |
| - * Subscribe on changes that happen via USB and detect if there any Yubikeys got connected |
74 |
| - * <p> |
75 |
| - * This registers broadcast receivers, to unsubscribe from receiver use {@link YubiKitManager#stopUsbDiscovery()} |
76 |
| - * |
77 |
| - * @param usbConfiguration additional configurations on how USB discovery should be handled |
78 |
| - * @param listener listener that is going to be invoked upon successful discovery of key session |
79 |
| - * or failure to detect any session (lack of permissions) |
80 |
| - */ |
81 |
| - public void startUsbDiscovery(final UsbConfiguration usbConfiguration, Callback<? super UsbYubiKeyDevice> listener) { |
82 |
| - usbYubiKeyManager.enable(usbConfiguration, listener); |
| 86 | + /** |
| 87 | + * Subscribe on changes that happen via NFC and detect if there any Yubikeys tags got passed |
| 88 | + * |
| 89 | + * <p>This registers broadcast receivers and blocks Ndef tags to be passed to activity, to |
| 90 | + * unsubscribe use {@link YubiKitManager#stopNfcDiscovery(Activity)} |
| 91 | + * |
| 92 | + * @param nfcConfiguration additional configurations on how NFC discovery should be handled |
| 93 | + * @param listener listener that is going to be invoked upon successful discovery of YubiKeys or |
| 94 | + * failure to detect any device (setting if off or no nfc adapter on device) |
| 95 | + * @param activity active (not finished) activity required for nfc foreground dispatch |
| 96 | + * @throws NfcNotAvailable in case if NFC not available on android device |
| 97 | + */ |
| 98 | + public void startNfcDiscovery( |
| 99 | + final NfcConfiguration nfcConfiguration, |
| 100 | + Activity activity, |
| 101 | + Callback<? super NfcYubiKeyDevice> listener) |
| 102 | + throws NfcNotAvailable { |
| 103 | + if (nfcYubiKeyManager == null) { |
| 104 | + throw new NfcNotAvailable("NFC is not available on this device", false); |
83 | 105 | }
|
| 106 | + nfcYubiKeyManager.enable(activity, nfcConfiguration, listener); |
| 107 | + } |
84 | 108 |
|
85 |
| - /** |
86 |
| - * Subscribe on changes that happen via NFC and detect if there any Yubikeys tags got passed |
87 |
| - * <p> |
88 |
| - * This registers broadcast receivers and blocks Ndef tags to be passed to activity, |
89 |
| - * to unsubscribe use {@link YubiKitManager#stopNfcDiscovery(Activity)} |
90 |
| - * |
91 |
| - * @param nfcConfiguration additional configurations on how NFC discovery should be handled |
92 |
| - * @param listener listener that is going to be invoked upon successful discovery of YubiKeys |
93 |
| - * or failure to detect any device (setting if off or no nfc adapter on device) |
94 |
| - * @param activity active (not finished) activity required for nfc foreground dispatch |
95 |
| - * @throws NfcNotAvailable in case if NFC not available on android device |
96 |
| - */ |
97 |
| - public void startNfcDiscovery(final NfcConfiguration nfcConfiguration, Activity activity, Callback<? super NfcYubiKeyDevice> listener) |
98 |
| - throws NfcNotAvailable { |
99 |
| - if (nfcYubiKeyManager == null) { |
100 |
| - throw new NfcNotAvailable("NFC is not available on this device", false); |
101 |
| - } |
102 |
| - nfcYubiKeyManager.enable(activity, nfcConfiguration, listener); |
103 |
| - } |
104 |
| - |
105 |
| - /** |
106 |
| - * Unsubscribe from changes that happen via USB |
107 |
| - */ |
108 |
| - public void stopUsbDiscovery() { |
109 |
| - usbYubiKeyManager.disable(); |
110 |
| - } |
| 109 | + /** Unsubscribe from changes that happen via USB */ |
| 110 | + public void stopUsbDiscovery() { |
| 111 | + usbYubiKeyManager.disable(); |
| 112 | + } |
111 | 113 |
|
112 |
| - /** |
113 |
| - * Unsubscribe from changes that happen via NFC |
114 |
| - * |
115 |
| - * @param activity active (not finished) activity required for nfc foreground dispatch |
116 |
| - */ |
117 |
| - public void stopNfcDiscovery(Activity activity) { |
118 |
| - if (nfcYubiKeyManager != null) { |
119 |
| - nfcYubiKeyManager.disable(activity); |
120 |
| - } |
| 114 | + /** |
| 115 | + * Unsubscribe from changes that happen via NFC |
| 116 | + * |
| 117 | + * @param activity active (not finished) activity required for nfc foreground dispatch |
| 118 | + */ |
| 119 | + public void stopNfcDiscovery(Activity activity) { |
| 120 | + if (nfcYubiKeyManager != null) { |
| 121 | + nfcYubiKeyManager.disable(activity); |
121 | 122 | }
|
| 123 | + } |
122 | 124 | }
|
0 commit comments