Skip to content

Commit

Permalink
merge handle crashes when client rotate (#380)
Browse files Browse the repository at this point in the history
Co-authored-by: ben reed <[email protected]>
Co-authored-by: Benjamin Reed <[email protected]>
  • Loading branch information
3 people authored Nov 26, 2024
1 parent 1d12762 commit b76c211
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.discdd.bundleclient;

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

import android.content.BroadcastReceiver;
Expand Down Expand Up @@ -33,13 +32,7 @@
import net.discdd.android.fragments.PermissionsFragment;
import net.discdd.android.fragments.PermissionsViewModel;
import net.discdd.client.bundlerouting.ClientWindow;
import net.discdd.client.bundlesecurity.BundleSecurity;
import net.discdd.pathutils.ClientPaths;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -104,7 +97,7 @@ protected void onCreate(Bundle savedInstanceState) {

var intent = new Intent(this, BundleClientWifiDirectService.class);
var svc = bindService(intent, connection, Context.BIND_AUTO_CREATE);

mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -122,11 +115,11 @@ public void onReceive(Context context, Intent intent) {
}

permissionsViewModel = new ViewModelProvider(this).get(PermissionsViewModel.class);
permissionsFragment = new PermissionsFragment(permissionsViewModel);
homeFragment = new BundleClientWifiDirectFragment();
usbFragment = new UsbFragment();
serverFragment = new ServerFragment();
logFragment = new LogFragment();
permissionsFragment = PermissionsFragment.newInstance();
homeFragment = BundleClientWifiDirectFragment.newInstance();
usbFragment = UsbFragment.newInstance();
serverFragment = ServerFragment.newInstance();
logFragment = LogFragment.newInstance();
fragmentsWithTitles.add(new FragmentWithTitle(permissionsFragment, getString(R.string.permissions_tab)));
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public class BundleClientWifiDirectFragment extends Fragment {
private SharedPreferences preferences;
private TextView deliveryStatus;

BundleClientWifiDirectFragment() {
public BundleClientWifiDirectFragment() {
intentFilter.addAction(BundleClientWifiDirectService.NET_DISCDD_BUNDLECLIENT_WIFI_EVENT_ACTION);
intentFilter.addAction(BundleClientWifiDirectService.NET_DISCDD_BUNDLECLIENT_LOG_ACTION);
}

public static BundleClientWifiDirectFragment newInstance() { return new BundleClientWifiDirectFragment(); }

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class ServerFragment extends Fragment {
private EditText portInput;
private SharedPreferences sharedPref;

public static ServerFragment newInstance() { return new ServerFragment(); }

@Nullable
@Override
public View onCreateView(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
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;
import android.content.IntentFilter;
import android.graphics.Color;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.storage.StorageManager;
Expand All @@ -28,10 +25,8 @@
import net.discdd.client.bundletransmission.BundleTransmission;
import net.discdd.model.ADU;
import net.discdd.model.BundleDTO;
import net.discdd.pathutils.ClientPaths;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
Expand All @@ -53,11 +48,22 @@ public class UsbFragment extends Fragment {
private BundleTransmission bundleTransmission;
private File usbDirectory;

public static UsbFragment newInstance() { return new UsbFragment(); }

@Override
public View onCreateView(
@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.usb_fragment, container, false);
bundleTransmission = ((BundleClientActivity) getActivity()).wifiBgService.getBundleTransmission();
BundleClientActivity activity = (BundleClientActivity) getActivity();
if (activity != null && activity.wifiBgService != null) {
try {
bundleTransmission = activity.wifiBgService.getBundleTransmission();
} catch (Exception e) {
logger.log(Level.WARNING, "Error getting bundle transmission: ", e);
}
} else {
logger.log(Level.INFO, "BundleClientActivity or wifiBgService is null");
}

usbExchangeButton = view.findViewById(R.id.usb_exchange_button);
usbConnectionText = view.findViewById(R.id.usbconnection_response_text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.Network;
Expand All @@ -34,21 +33,19 @@

import net.discdd.android.fragments.LogFragment;
import net.discdd.android.fragments.PermissionsFragment;
import net.discdd.pathutils.TransportPaths;
import net.discdd.android.fragments.PermissionsViewModel;
import net.discdd.pathutils.TransportPaths;
import net.discdd.transport.TransportSecurity;

import org.whispersystems.libsignal.InvalidKeyException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.SubmissionPublisher;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class BundleTransportActivity extends AppCompatActivity {
Expand All @@ -61,13 +58,13 @@ public class BundleTransportActivity extends AppCompatActivity {
private TitledFragment usbFrag;
private TitledFragment logFragment;
private TitledFragment permissionsTitledFragment;
private PermissionsViewModel permissionsViewModel;

record ConnectivityEvent(boolean internetAvailable) {}

private final SubmissionPublisher<ConnectivityEvent> connectivityEventPublisher = new SubmissionPublisher<>();
private ViewPager2 viewPager2;
private FragmentStateAdapter viewPager2Adapter;
private PermissionsViewModel permissionsViewModel;
private PermissionsFragment permissionsFragment;
private TabLayout tabLayout;
private TabLayoutMediator mediator;
Expand Down Expand Up @@ -125,18 +122,19 @@ public void onReceive(Context context, Intent intent) {
logger.log(SEVERE, "[SEC]: Failed to initialize Server Keys", e);
}

ServerUploadFragment serverFrag = ServerUploadFragment.newInstance(transportSecurity.getTransportID(),
transportPaths, connectivityEventPublisher);
serverUploadFragment = new TitledFragment(getString(R.string.upload),
new ServerUploadFragment(connectivityEventPublisher,
transportSecurity.getTransportID(),
this.transportPaths));
serverFrag);
TransportWifiDirectFragment transportFrag = TransportWifiDirectFragment.newInstance(transportPaths);
transportWifiFragment = new TitledFragment(getString(R.string.local_wifi),
new TransportWifiDirectFragment(this.transportPaths));
storageFragment = new TitledFragment("Storage Settings", new StorageFragment());
usbFrag = new TitledFragment("USB", new UsbFragment(transportPaths));
logFragment = new TitledFragment(getString(R.string.logs), new LogFragment());
transportFrag);
storageFragment = new TitledFragment("Storage Settings", StorageFragment.newInstance());
usbFrag = new TitledFragment("USB", UsbFragment.newInstance(transportPaths));
logFragment = new TitledFragment(getString(R.string.logs), LogFragment.newInstance());

permissionsViewModel = new ViewModelProvider(this).get(PermissionsViewModel.class);
permissionsFragment = new PermissionsFragment(permissionsViewModel);
permissionsFragment = PermissionsFragment.newInstance();
permissionsTitledFragment = new TitledFragment("Permissions", permissionsFragment);
fragments.add(permissionsTitledFragment);

Expand Down Expand Up @@ -358,4 +356,4 @@ public void onServiceDisconnected(ComponentName arg0) {
btService = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
import androidx.fragment.app.Fragment;

import net.discdd.pathutils.TransportPaths;
import net.discdd.transport.TransportToBundleServerManager;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SubmissionPublisher;
import java.util.logging.Logger;

import net.discdd.transport.TransportToBundleServerManager;

/**
* A Fragment to manage server uploads
*/
Expand All @@ -45,16 +43,32 @@ public class ServerUploadFragment extends Fragment {
private TextView numberBundlestoServer;
private Button reloadButton;

public ServerUploadFragment(SubmissionPublisher<BundleTransportActivity.ConnectivityEvent> connectivityFlow,
String transportID, TransportPaths transportPaths) {
public static ServerUploadFragment newInstance(String transportID, TransportPaths transportPaths, SubmissionPublisher<BundleTransportActivity.ConnectivityEvent> connectivityFlow) {
ServerUploadFragment fragment = new ServerUploadFragment();
fragment.setTransportPaths(transportPaths);
fragment.setConnectivityFlow(connectivityFlow);
fragment.setTransportID(transportID);
return fragment;
}

public void setConnectivityFlow(SubmissionPublisher<BundleTransportActivity.ConnectivityEvent> connectivityFlow) {
this.connectivityFlow = connectivityFlow;
this.transportID = transportID;
}

public void setTransportPaths(TransportPaths transportPaths) {
this.transportPaths = transportPaths;
}

public void setTransportID(String transportID) {
this.transportID = transportID;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
transportID = getArguments().getString("transportID");
}
}

@Override
Expand All @@ -67,7 +81,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
saveDomainAndPortBtn = mainView.findViewById(R.id.save_domain_port);
restoreDomainAndPortBtn = mainView.findViewById(R.id.restore_domain_port);
connectServerBtn.setOnClickListener(view -> connectToServer());
connectivityFlow.consume(event -> connectServerBtn.setEnabled(event.internetAvailable()));
if (connectivityFlow != null) { connectivityFlow.consume(event -> connectServerBtn.setEnabled(event.internetAvailable())); }
else { logger.warning("connectivityFlow is null"); }
serverConnnectedStatus = mainView.findViewById(R.id.server_upload_status);

// save the domain and port inputs
Expand All @@ -79,11 +94,21 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
numberBundlestoClient = mainView.findViewById(R.id.numberBundlestoClient);
numberBundlestoServer = mainView.findViewById(R.id.numberBundlestoServer);
reloadButton = mainView.findViewById(R.id.reloadCounts); // Assuming this ID is for the reload button

// Set click listener for the Reload button
reloadButton.setOnClickListener(view -> {
numberBundlestoClient.setText(String.valueOf(transportPaths.toClientPath.toFile().list().length));
numberBundlestoServer.setText(String.valueOf(transportPaths.toServerPath.toFile().list().length));
if (transportPaths != null &&
transportPaths.toClientPath != null &&
transportPaths.toServerPath != null) {
String[] clientFiles = transportPaths.toClientPath.toFile().list();
String[] serverFiles = transportPaths.toServerPath.toFile().list();

int clientCount = (clientFiles != null) ? clientFiles.length : 0;
int serverCount = (serverFiles != null) ? serverFiles.length : 0;

numberBundlestoClient.setText(String.valueOf(clientCount));
numberBundlestoServer.setText(String.valueOf(serverCount));
} else {
logger.warning("transportPaths or its paths are null when attempting to reload counts");
}
});

// set saved domain and port to inputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class StorageFragment extends Fragment {
private Toast currentToast;
private int currentValue;

public static StorageFragment newInstance() { return new StorageFragment(); }

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.fragment_storage_preferences, container, false);
storageManager = new StorageManager(requireActivity().getExternalFilesDir(null).toPath(), retrievePreference());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public class TransportWifiDirectFragment extends Fragment {
private TransportWifiDirectService btService;
private TransportPaths transportPaths;

public TransportWifiDirectFragment(TransportPaths transportPaths) {
intentFilter.addAction(TransportWifiDirectService.NET_DISCDD_BUNDLETRANSPORT_WIFI_EVENT_ACTION);
intentFilter.addAction(TransportWifiDirectService.NET_DISCDD_BUNDLETRANSPORT_CLIENT_LOG_ACTION);
public static TransportWifiDirectFragment newInstance(TransportPaths transportPaths) {
TransportWifiDirectFragment fragment = new TransportWifiDirectFragment();
fragment.setTransportPaths(transportPaths);
return fragment;
}

public void setTransportPaths(TransportPaths transportPaths) {
this.transportPaths = transportPaths;
}

Expand Down Expand Up @@ -72,6 +76,8 @@ private void processDeviceInfoChange() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intentFilter.addAction(TransportWifiDirectService.NET_DISCDD_BUNDLETRANSPORT_WIFI_EVENT_ACTION);
intentFilter.addAction(TransportWifiDirectService.NET_DISCDD_BUNDLETRANSPORT_CLIENT_LOG_ACTION);
sharedPreferences = requireActivity().getSharedPreferences(TransportWifiDirectService.WIFI_DIRECT_PREFERENCES,
Context.MODE_PRIVATE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class UsbFragment extends Fragment {
private UsbFileManager usbFileManager;
private final TransportPaths transportPaths;
private TransportPaths transportPaths;
private Button toSettingsButton;
private Button reloadButton;
private TextView toSettingstext;
Expand All @@ -47,7 +47,13 @@ public class UsbFragment extends Fragment {
private static final Logger logger = Logger.getLogger(UsbFragment.class.getName());
private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();

public UsbFragment(TransportPaths transportPaths) {
public static UsbFragment newInstance(TransportPaths transportPaths) {
UsbFragment fragment = new UsbFragment();
fragment.setTransportPaths(transportPaths);
return fragment;
}

public void setTransportPaths (TransportPaths transportPaths) {
this.transportPaths = transportPaths;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class LogFragment extends Fragment {
private TextView logMsgs;
private static LinkedList<String> logRecords;

public static LogFragment newInstance() { return new LogFragment(); }

static {
LogFragment.registerLoggerHandler();
}
Expand Down
Loading

0 comments on commit b76c211

Please sign in to comment.