Edit: While reading up on this after answering, stumbled upon the name for this threat- Juice Jacking, defined as (emphasis added)
Juice jacking is a term used to describe a cyber attack where wherein malware might be installed on to, or data surreptitiously copied from, a smart phone, tablet or other computer device using a charging port that doubles as a data connection, typically over USB.
Further, mitigation section of wiki says
Juice jacking is not possible if a device is charged via the AC adapter shipped with the device, a battery backup device, or by utilizing a USB cable that has its data cables removed
The simplest way would be to use a cable that only charges but does not allow data transfer. Such a cable is non-standard, since it does not adhere to standards wherein data connectors / cables also play an important role of negotiating the power requirements (1)
These cables come with a disadvantage- charging speed is very slow (often by a factor of 10x as reported in some forums). A work around is to buy a cable of 20 AWG thickness (2) Since 20 AWG is the thickest cable permitted and by virtue of its thickness offers least resistance , and therefore faster charging. Such cables are available on ebay as an example. While these cables may charge at a reasonable rate, they are not recommended for regular use, as charging current rate is constant and not negotiable (as desired by battery charging logic)
Other Possible Methods
- adb commands/ tweaking your system OS: These may or may not work since it is highly dependent on the way device OS is designed. To get a flavor, see my answer , which is the opposite of what you want to achieve.
Edit: Thanks to Firelord's answer, found a way that hasn't been documented anywhere AFAIK.
You can run this command:
adb shell svc usb setFunctions
This should reset the USB mode/configuration to charging only.
DIY methods modifying the cable or end connectors: Not recommended unless you have a good knowledge and are adept at such tinkering. For example, How-to-make-a-USB-no-data-charger-cable- Google search will give you many more
USB Condoms: I didn't make that up :). These are implementation of DIY methods, sold as ready for use. SyncStop/ USB condom is one such product- many are available
Note: All above methods result in sub optimal charging and IMO not recommended for regular use since charging power can't be negotiated
Interestingly, there is a product Charging Safely over USB marketed last year (currently out of stock as per website). This recognises that doing away with data connectors is a bad idea, and permits power negotiation to happen (although slowly) while denying data transfer by using capacitors to limit bandwidth only for power negotiation
Bottom Line Use 20 AWG cable recommended right on top, and as sparingly as possible
(1): USB charging specifications in conjunction with Battery Charging Revision 1.2 lays down " standard for establishing the proper way to charge a battery from a USB port "
(2): American Wire Gauge (AWG) is a reference for thickness of cables. In this system, larger numbers indicate thinner wires. USB specification above makes reference to AWG numbers of 20, 22, 24, 26 and 28