Voltage Detection via Analog Comparator
I would simply treat the 5V line as GPIO. Connect it via a divider so it does not go above MCU Vcc.

The "bottom" resistor will make sure that GPIO reads as zero when there is no voltage.

GPIO reads low = you're on battery power

GPIO reads high = you're on USB power