Android Device State & Security Snippets
1. Active USB Connection
val usbManager = context.getSystemService(Context.USB_SERVICE) as UsbManager
// 1. Android as Host: Checks if external USB devices are plugged INTO the phone
val isUsbDeviceConnected = usbManager.deviceList.isNotEmpty()
// 2. Android as Accessory: Checks if the phone is plugged into a USB accessory (like a car dock)
val isUsbAccessoryConnected = usbManager.accessoryList?.isNotEmpty() == true2. Work Profile Detection
val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
// Returns true if the app is currently running inside a managed profile
val isRunningInWorkProfile = userManager.isManagedProfile3. Device Encryption Status
4. Unknown Sources Enabled
5. Bootloader Unlock Status
6. Last Security Patch Date
Last updated
