Rewiring for doorbell, is idea up to code?
Wow, now that I see your picture, this thing is a hatchet job.  

No, no.  That transformer cannot just be stuffed loose in a plastic (!!) Box.  There is no separation between mains and low voltage.  What a fiasco.  

I would extract the doorbell wiring from the box and bring it out to the wall through a hole outside the box.  Now, with only mains wiring in the box, I would install 2 standard receptacles (path of least challenge) with 2-gang cover plate, and fit up a plug-in "wall wart" transformer for the doorbell.  Use another socket for any bog standard USB supply from the local gas station. 



It's all about separation

There should be a metal curtain between your mains and low-voltage wiring.  This is where we get into mains wiring methods.  This separation is easy with doorbell transformers; they make transformers that mount on a 1/2" knockout or a  junction box cover plate. The mains wiring is contained; the 24V comes out to open screw terminals. 

This concept of "metal curtain" must be respected. The single best way to achieve that is to install a common receptacle and plug a quality USB power supply (Apple iPad is my pick) in the receptacle.  


Install any common 1-gang deep "handy-box" steel junction box right next to the doorbell box.  That holds the receptacle.  Assuming the power supply cable going into the doorbell box is full-current-rated, reroute it into the handy-box instead, remembering minimum slack (6" of wires free inside the box).
Connect from the handy-box into the old doorbell box.  I'm a conduit person so I would go into a standard knockout via a steel conduit nipple to the handy-box, and use THHN wire between; this also carries ground to the metal doorbell box.  But you could use a few inches of appropriate Romex (most places sell it by the foot, remember 6" tail on each end).  Or alternately, just install a power cord (with strain relief) on the doorbell box. 
Wire it up in the usual, Code-legal way, so both the receptacle and doorbell box get power. 


In all things mains, hook up safety ground first always, and never disconnect it, not even for troubleshooting (it's never the problem). 

Plug the USB power supply into one socket, and (if needed) the doorbell box's new line cord into the other.