Skip to content

Commit

Permalink
Merge branch 'main' into client-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pankinkun authored Oct 18, 2024
2 parents 2ea2c00 + 9ac59c9 commit 8eb0b23
Showing 1 changed file with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package net.discdd.bundleclient;

import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -21,11 +25,19 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import net.discdd.client.bundletransmission.BundleTransmission;
import net.discdd.model.ADU;
import net.discdd.model.BundleDTO;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UsbFragment extends Fragment {
Expand All @@ -38,6 +50,8 @@ public class UsbFragment extends Fragment {
private static final Logger logger = Logger.getLogger(UsbFragment.class.getName());
private static final String usbDirName = "/DDD_transport";
private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
private BundleTransmission bundleTransmission;
private File usbDirectory;

@Override
public View onCreateView(
Expand All @@ -63,7 +77,32 @@ public void onReceive(Context context, Intent intent) {
getActivity().registerReceiver(mUsbReceiver, filter);

// Check initial USB connection
checkUsbConnection(1);
checkUsbConnection(2);

usbExchangeButton.setOnClickListener(v -> {
logger.log(Level.INFO, "usbExchangeButton clicked");
if (usbConnected) {
//Bundle we want to send to usb.
scheduledExecutor.execute(() -> {
try {
bundleCreation();
logger.log(INFO, "Starting bundle creation");
BundleDTO bundleDTO = bundleTransmission.generateBundleForTransmission();
File bundleFile = bundleDTO.getBundle().getSource();
File targetFile = new File(usbDirectory, bundleFile.getName());
Files.copy(bundleFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
logger.log(INFO, "Bundle creation and transfer successful");
updateUsbStatus(false, "Bundle created and transferred to USB", Color.GREEN);
} catch (Exception e) {
e.printStackTrace();
updateUsbStatus(false, "Error creating or transferring bundle", Color.RED);
}
});
} else {
Toast.makeText(getActivity(), "No bundle created as usb device not connected", Toast.LENGTH_SHORT)
.show();
}
});

return view;
}
Expand All @@ -81,11 +120,11 @@ private void handleUsbBroadcast(Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
updateUsbStatus(false, getString(R.string.no_usb_connection_detected), Color.RED);
UsbFragment.usbConnected = false;
usbConnected = false;
showUsbDetachedToast();
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
updateUsbStatus(false, getString(R.string.usb_device_attached_checking_for_storage_volumes), Color.BLUE);
scheduledExecutor.schedule(() -> checkUsbConnection(1), 1, TimeUnit.SECONDS);
scheduledExecutor.schedule(() -> checkUsbConnection(2), 1, TimeUnit.SECONDS);
showUsbAttachedToast();
}
}
Expand Down Expand Up @@ -113,9 +152,11 @@ private void checkUsbConnection(int tries) {

//Method to update USB status
public void updateUsbStatus(boolean isConnected, String statusText, int color) {
usbExchangeButton.setEnabled(isConnected);
usbConnectionText.setText(statusText);
usbConnectionText.setTextColor(color);
getActivity().runOnUiThread(() -> {
usbExchangeButton.setEnabled(isConnected);
usbConnectionText.setText(statusText);
usbConnectionText.setTextColor(color);
});
}

private void showUsbAttachedToast() {
Expand All @@ -127,8 +168,8 @@ private boolean usbDirExists() {
List<StorageVolume> storageVolumeList = storageManager.getStorageVolumes();
for (StorageVolume storageVolume : storageVolumeList) {
if (storageVolume.isRemovable()) {
File fileUsb = new File(storageVolume.getDirectory().getPath() + usbDirName);
if (fileUsb.exists()) {
usbDirectory = new File(storageVolume.getDirectory().getPath() + usbDirName);
if (usbDirectory.exists()) {
return true;
}
}
Expand All @@ -139,4 +180,21 @@ private boolean usbDirExists() {
private void showUsbDetachedToast() {
Toast.makeText(getActivity(), getString(R.string.usb_device_detached), Toast.LENGTH_SHORT).show();
}

private void bundleCreation() {
try {
bundleTransmission = new BundleTransmission(getActivity().getApplicationContext().getDataDir().toPath(),
this::processIncomingADU);
} catch (Exception e) {
e.printStackTrace();
updateUsbStatus(false, "Error creating or transferring bundle in bundleCreation", Color.RED);
}
}

private void processIncomingADU(ADU adu) {
Intent intent = new Intent("android.intent.dtn.DATA_RECEIVED");
intent.setPackage(adu.getAppId());
intent.setType("text/plain");
getActivity().getApplicationContext().sendBroadcast(intent);
}
}

0 comments on commit 8eb0b23

Please sign in to comment.