Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use spotless gradle plugin for unified formatting #162

Merged
merged 12 commits into from
Jan 14, 2025
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:
- repo: local
hooks:
- id: spotless
name: reformat Java
language: script
entry: scripts/spotless.py
always_run: true
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'com.android.library'
id 'project-convention-spotbugs'
id 'project-convention-logging'
id 'project-convention-spotless'
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,25 @@

package com.yubico.yubikit.android;

import android.content.Context;
import static org.junit.Assert.assertEquals;

import androidx.test.platform.app.InstrumentationRegistry;
import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.yubico.yubikit.android.test", appContext.getPackageName());
}
}
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.yubico.yubikit.android.test", appContext.getPackageName());
}
}
159 changes: 81 additions & 78 deletions android/src/main/java/com/yubico/yubikit/android/YubiKitManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import android.app.Activity;
import android.content.Context;

import com.yubico.yubikit.android.transport.nfc.NfcConfiguration;
import com.yubico.yubikit.android.transport.nfc.NfcNotAvailable;
import com.yubico.yubikit.android.transport.nfc.NfcYubiKeyDevice;
Expand All @@ -27,96 +26,100 @@
import com.yubico.yubikit.android.transport.usb.UsbYubiKeyDevice;
import com.yubico.yubikit.android.transport.usb.UsbYubiKeyManager;
import com.yubico.yubikit.core.util.Callback;

import javax.annotation.Nullable;

/**
* Starting point for YubiKey device discovery over both USB and NFC.
* Use this class to listen for YubiKeys and get a {@link com.yubico.yubikit.core.YubiKeyDevice} reference.
* Starting point for YubiKey device discovery over both USB and NFC. Use this class to listen for
* YubiKeys and get a {@link com.yubico.yubikit.core.YubiKeyDevice} reference.
*/
public final class YubiKitManager {
private final UsbYubiKeyManager usbYubiKeyManager;
@Nullable
private final NfcYubiKeyManager nfcYubiKeyManager;
private final UsbYubiKeyManager usbYubiKeyManager;
@Nullable private final NfcYubiKeyManager nfcYubiKeyManager;

@Nullable
private static NfcYubiKeyManager buildNfcDeviceManager(Context context) {
try {
return new NfcYubiKeyManager(context, null);
} catch (NfcNotAvailable e) {
return null;
}
@Nullable
private static NfcYubiKeyManager buildNfcDeviceManager(Context context) {
try {
return new NfcYubiKeyManager(context, null);
} catch (NfcNotAvailable e) {
return null;
}
}

/**
* Initialize instance of {@link YubiKitManager}
*
* @param context application context
*/
public YubiKitManager(Context context) {
this(new UsbYubiKeyManager(context.getApplicationContext()), buildNfcDeviceManager(context.getApplicationContext()));
}
/**
* Initialize instance of {@link YubiKitManager}
*
* @param context application context
*/
public YubiKitManager(Context context) {
this(
new UsbYubiKeyManager(context.getApplicationContext()),
buildNfcDeviceManager(context.getApplicationContext()));
}

/**
* Initialize an instance of {@link YubiKitManager}, providing the USB and NFC YubiKey managers to use for device discovery.
*
* @param usbYubiKeyManager UsbYubiKeyManager instance to use for USB communication
* @param nfcYubiKeyManager NfcYubiKeyManager instance to use for NFC communication
*/
public YubiKitManager(UsbYubiKeyManager usbYubiKeyManager, @Nullable NfcYubiKeyManager nfcYubiKeyManager) {
this.usbYubiKeyManager = usbYubiKeyManager;
this.nfcYubiKeyManager = nfcYubiKeyManager;
}
/**
* Initialize an instance of {@link YubiKitManager}, providing the USB and NFC YubiKey managers to
* use for device discovery.
*
* @param usbYubiKeyManager UsbYubiKeyManager instance to use for USB communication
* @param nfcYubiKeyManager NfcYubiKeyManager instance to use for NFC communication
*/
public YubiKitManager(
UsbYubiKeyManager usbYubiKeyManager, @Nullable NfcYubiKeyManager nfcYubiKeyManager) {
this.usbYubiKeyManager = usbYubiKeyManager;
this.nfcYubiKeyManager = nfcYubiKeyManager;
}

/**
* Subscribe on changes that happen via USB and detect if there any Yubikeys got connected
*
* <p>This registers broadcast receivers, to unsubscribe from receiver use {@link
* YubiKitManager#stopUsbDiscovery()}
*
* @param usbConfiguration additional configurations on how USB discovery should be handled
* @param listener listener that is going to be invoked upon successful discovery of key session
* or failure to detect any session (lack of permissions)
*/
public void startUsbDiscovery(
final UsbConfiguration usbConfiguration, Callback<? super UsbYubiKeyDevice> listener) {
usbYubiKeyManager.enable(usbConfiguration, listener);
}

/**
* Subscribe on changes that happen via USB and detect if there any Yubikeys got connected
* <p>
* This registers broadcast receivers, to unsubscribe from receiver use {@link YubiKitManager#stopUsbDiscovery()}
*
* @param usbConfiguration additional configurations on how USB discovery should be handled
* @param listener listener that is going to be invoked upon successful discovery of key session
* or failure to detect any session (lack of permissions)
*/
public void startUsbDiscovery(final UsbConfiguration usbConfiguration, Callback<? super UsbYubiKeyDevice> listener) {
usbYubiKeyManager.enable(usbConfiguration, listener);
/**
* Subscribe on changes that happen via NFC and detect if there any Yubikeys tags got passed
*
* <p>This registers broadcast receivers and blocks Ndef tags to be passed to activity, to
* unsubscribe use {@link YubiKitManager#stopNfcDiscovery(Activity)}
*
* @param nfcConfiguration additional configurations on how NFC discovery should be handled
* @param listener listener that is going to be invoked upon successful discovery of YubiKeys or
* failure to detect any device (setting if off or no nfc adapter on device)
* @param activity active (not finished) activity required for nfc foreground dispatch
* @throws NfcNotAvailable in case if NFC not available on android device
*/
public void startNfcDiscovery(
final NfcConfiguration nfcConfiguration,
Activity activity,
Callback<? super NfcYubiKeyDevice> listener)
throws NfcNotAvailable {
if (nfcYubiKeyManager == null) {
throw new NfcNotAvailable("NFC is not available on this device", false);
}
nfcYubiKeyManager.enable(activity, nfcConfiguration, listener);
}

/**
* Subscribe on changes that happen via NFC and detect if there any Yubikeys tags got passed
* <p>
* This registers broadcast receivers and blocks Ndef tags to be passed to activity,
* to unsubscribe use {@link YubiKitManager#stopNfcDiscovery(Activity)}
*
* @param nfcConfiguration additional configurations on how NFC discovery should be handled
* @param listener listener that is going to be invoked upon successful discovery of YubiKeys
* or failure to detect any device (setting if off or no nfc adapter on device)
* @param activity active (not finished) activity required for nfc foreground dispatch
* @throws NfcNotAvailable in case if NFC not available on android device
*/
public void startNfcDiscovery(final NfcConfiguration nfcConfiguration, Activity activity, Callback<? super NfcYubiKeyDevice> listener)
throws NfcNotAvailable {
if (nfcYubiKeyManager == null) {
throw new NfcNotAvailable("NFC is not available on this device", false);
}
nfcYubiKeyManager.enable(activity, nfcConfiguration, listener);
}

/**
* Unsubscribe from changes that happen via USB
*/
public void stopUsbDiscovery() {
usbYubiKeyManager.disable();
}
/** Unsubscribe from changes that happen via USB */
public void stopUsbDiscovery() {
usbYubiKeyManager.disable();
}

/**
* Unsubscribe from changes that happen via NFC
*
* @param activity active (not finished) activity required for nfc foreground dispatch
*/
public void stopNfcDiscovery(Activity activity) {
if (nfcYubiKeyManager != null) {
nfcYubiKeyManager.disable(activity);
}
/**
* Unsubscribe from changes that happen via NFC
*
* @param activity active (not finished) activity required for nfc foreground dispatch
*/
public void stopNfcDiscovery(Activity activity) {
if (nfcYubiKeyManager != null) {
nfcYubiKeyManager.disable(activity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@

public class Base64CodecImpl implements com.yubico.yubikit.core.internal.codec.Base64Codec {

@Override
public String toUrlSafeString(byte[] data) {
return Base64.encodeToString(data, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
}

@Override
public String toString(byte[] data) {
return Base64.encodeToString(data, Base64.NO_WRAP | Base64.NO_PADDING);
}

@Override
public byte[] fromUrlSafeString(String data) {
return Base64.decode(data, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
}

@Override
public byte[] fromString(String data) {
return Base64.decode(data, Base64.NO_WRAP | Base64.NO_PADDING);
}
@Override
public String toUrlSafeString(byte[] data) {
return Base64.encodeToString(data, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
}

@Override
public String toString(byte[] data) {
return Base64.encodeToString(data, Base64.NO_WRAP | Base64.NO_PADDING);
}

@Override
public byte[] fromUrlSafeString(String data) {
return Base64.decode(data, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
}

@Override
public byte[] fromString(String data) {
return Base64.decode(data, Base64.NO_WRAP | Base64.NO_PADDING);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
@PackageNonnullByDefault
package com.yubico.yubikit.android.internal;

import com.yubico.yubikit.core.PackageNonnullByDefault;
import com.yubico.yubikit.core.PackageNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
@PackageNonnullByDefault
package com.yubico.yubikit.android;

import com.yubico.yubikit.core.PackageNonnullByDefault;
import com.yubico.yubikit.core.PackageNonnullByDefault;
Loading
Loading