Don't know if still relevant, but you can just power it off if you see it was plugged in.
I attach a basic code here, and you can tweak it to your needs of course.
Be aware that you might need to use a small delay before M5.Axp.GetVBusVoltage() will give you a result you can trust.
//function which reads the voltage coming out of the USB, and if it's above 4.1 I know the device is being charged.
float charge = M5.Axp.GetVBusVoltage();
return charge > 4.1;
//check here of anywhere you want in the code if the device is being charged, and power it off.