Skip to content

Best Practices

1. Early Initialization

// Initialize in Application.onCreate() for best performance
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
AlphynSDK.initialize(this);
}
}

2. Proper Listener Management

// Always add listeners before initialization
AlphynSDK.addSecurityListener(securityListener);
// Remove listeners in onDestroy to prevent memory leaks
@Override
protected void onDestroy() {
super.onDestroy();
AlphynSDK.removeSecurityListener(securityListener);
}

3. Thread Safety

// SDK callbacks are delivered on the main thread
public void onSuccess(AlphynCallbackResult result) {
// Safe to update UI directly
updateUI(result);
// For heavy processing, use background thread
if (requiresHeavyProcessing(result)) {
executorService.execute(() -> processResult(result));
}
}

Comprehensive Error Handling Strategy:

public class ByteriaErrorHandler {
private static final int MAX_RETRY_ATTEMPTS = 3;
private static final long RETRY_DELAY_MS = 5000;
public void handleError(AlphynCallbackResult result) {
switch (result.getOriginalEventType()) {
case INIT_FAILED:
handleInitializationError(result);
break;
case NETWORK_ERROR:
handleNetworkError(result);
break;
case KEYSTORE_FAILED:
handleKeystoreError(result);
break;
case SCAN_FAILED:
handleScanError(result);
break;
default:
handleGenericError(result);
break;
}
}
private void handleInitializationError(AlphynCallbackResult result) {
Log.e("Byteria", "Initialization failed: " + result.getMessage());
// Check for common initialization issues
if (result.getMessage().contains("invalid key")) {
notifyInvalidKey();
} else {
scheduleRetryInitialization();
}
}
private void handleNetworkError(AlphynCallbackResult result) {
Log.w("Byteria", "Network error: " + result.getMessage());
// Implement exponential backoff retry
scheduleNetworkRetry();
}
private void scheduleRetryInitialization() {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
// Retry initialization logic
retryInitialization();
}, RETRY_DELAY_MS);
}
}

Memory Management:

public class PerformanceOptimizedListener implements AlphynSecurityListener {
private final WeakReference<Activity> activityRef;
public PerformanceOptimizedListener(Activity activity) {
this.activityRef = new WeakReference<>(activity);
}
@Override
public void onSuccess(AlphynCallbackResult result) {
Activity activity = activityRef.get();
if (activity != null && !activity.isFinishing()) {
// Process result only if activity is still valid
processResult(result);
}
}
// Avoid holding strong references to prevent memory leaks
private void processResult(AlphynCallbackResult result) {
// Efficient result processing
if (result.isSecurityScan()) {
// Process scan results immediately
handleScanResult(result.getScanResult());
} else {
// Queue other results for batch processing
queueForBatchProcessing(result);
}
}
}

Efficient Event Processing:

public class BatchEventProcessor {
private final List<AlphynCallbackResult> pendingEvents = new ArrayList<>();
private final Handler batchHandler = new Handler(Looper.getMainLooper());
private static final long BATCH_DELAY_MS = 100;
public void queueEvent(AlphynCallbackResult result) {
synchronized (pendingEvents) {
pendingEvents.add(result);
scheduleBatchProcessing();
}
}
private void scheduleBatchProcessing() {
batchHandler.removeCallbacks(batchProcessor);
batchHandler.postDelayed(batchProcessor, BATCH_DELAY_MS);
}
private final Runnable batchProcessor = () -> {
synchronized (pendingEvents) {
if (!pendingEvents.isEmpty()) {
processBatch(new ArrayList<>(pendingEvents));
pendingEvents.clear();
}
}
};
}