Skip to content

Commit

Permalink
Client usb tab visibility (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
03fernanda-palacios authored Nov 23, 2024
1 parent ea565a8 commit 17658dd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
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.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -38,6 +42,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class BundleClientActivity extends AppCompatActivity {
Expand All @@ -60,6 +65,9 @@ public class BundleClientActivity extends AppCompatActivity {
private ViewPager2 viewPager;
private TabLayout tabLayout;
private TabLayoutMediator tabLayoutMediator;
PermissionsViewModel permissionsViewModel;
private BroadcastReceiver mUsbReceiver;
private boolean usbExists;

public BundleClientActivity() {
connection = new ServiceConnection() {
Expand Down Expand Up @@ -97,7 +105,23 @@ protected void onCreate(Bundle savedInstanceState) {
var intent = new Intent(this, BundleClientWifiDirectService.class);
var svc = bindService(intent, connection, Context.BIND_AUTO_CREATE);

PermissionsViewModel permissionsViewModel = new ViewModelProvider(this).get(PermissionsViewModel.class);
mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
handleUsbBroadcast(intent);
}
};
try {
//Register USB broadcast receiver
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
registerReceiver(mUsbReceiver, filter);
} catch (Exception e) {
logger.log(WARNING, "Failed to register usb broadcast", e);
}

permissionsViewModel = new ViewModelProvider(this).get(PermissionsViewModel.class);
permissionsFragment = new PermissionsFragment(permissionsViewModel);
homeFragment = new BundleClientWifiDirectFragment();
usbFragment = new UsbFragment();
Expand Down Expand Up @@ -130,9 +154,11 @@ private void updateTabs(Boolean satisfied) {
if (satisfied) {
logger.log(INFO, "ALL TABS BEING SHOWN");
newFragments.add(new FragmentWithTitle(homeFragment, getString(R.string.home_tab)));
newFragments.add(new FragmentWithTitle(usbFragment, getString(R.string.usb_tab)));
newFragments.add(new FragmentWithTitle(serverFragment, getString(R.string.server_tab)));
newFragments.add(new FragmentWithTitle(logFragment, getString(R.string.logs_tab)));
if (usbExists) {
newFragments.add(new FragmentWithTitle(usbFragment, getString(R.string.usb_tab)));
}
} else {
logger.log(INFO, "ONLY PERMISSIONS TAB IS BEING SHOWN");
newFragments.add(new FragmentWithTitle(permissionsFragment, getString(R.string.permissions_tab)));
Expand Down Expand Up @@ -173,6 +199,9 @@ public void onDestroy() {
BundleClientWifiDirectService.NET_DISCDD_BUNDLECLIENT_SETTING_BACKGROUND_EXCHANGE, false)) {
stopService(new Intent(this, BundleClientWifiDirectService.class));
}
if (mUsbReceiver != null) {
unregisterReceiver(mUsbReceiver);
}
unbindService(connection);
super.onDestroy();
}
Expand All @@ -197,4 +226,19 @@ public int getItemCount() {
return fragmentsWithTitles.size();
}
}

private void handleUsbBroadcast(Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
updateUsbExists(false);
permissionsViewModel.getPermissionSatisfied().observe(this, this::updateTabs);
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
updateUsbExists(true);
permissionsViewModel.getPermissionSatisfied().observe(this, this::updateTabs);
}
}

public void updateUsbExists(boolean result) {
usbExists = result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class UsbFragment extends Fragment {
private Button usbExchangeButton;
private TextView usbConnectionText;
private UsbManager usbManager;
private BroadcastReceiver mUsbReceiver;
public static boolean usbConnected = false;
private StorageManager storageManager;
private static final Logger logger = Logger.getLogger(UsbFragment.class.getName());
Expand All @@ -64,20 +63,8 @@ public View onCreateView(
usbConnectionText = view.findViewById(R.id.usbconnection_response_text);

usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
handleUsbBroadcast(intent);
}
};
storageManager = (StorageManager) getActivity().getSystemService(Context.STORAGE_SERVICE);

//Register USB broadcast receiver
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
getActivity().registerReceiver(mUsbReceiver, filter);

// Check initial USB connection
checkUsbConnection(2);

Expand Down Expand Up @@ -111,39 +98,18 @@ public void onReceive(Context context, Intent intent) {
@Override
public void onDestroyView() {
super.onDestroyView();
if (mUsbReceiver != null) {
getActivity().unregisterReceiver(mUsbReceiver);
}
}

//Method to handle USB broadcasts
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);
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(2), 1, TimeUnit.SECONDS);
showUsbAttachedToast();
}
}

//Method to check initial USB connection
private void checkUsbConnection(int tries) {
usbConnected = !usbManager.getDeviceList().isEmpty() && usbDirExists();
usbConnected = usbDirExists();
getActivity().getMainExecutor().execute(() -> {
if (!usbManager.getDeviceList().isEmpty()) {
if (usbDirExists()) {
updateUsbStatus(true, getString(R.string.usb_connection_detected), Color.GREEN);
} else {
if (!usbDirExists()) {
updateUsbStatus(false,
getString(R.string.usb_was_connected_but_ddd_transport_directory_was_not_detected),
Color.RED);
}
} else {
updateUsbStatus(false, getString(R.string.usb_device_not_connected), Color.RED);
}
});
if (tries > 0 && !usbConnected) {
Expand Down

0 comments on commit 17658dd

Please sign in to comment.