Circuit for Recharging Battery with a Device
You are asking to provide you with system architecture solution for your project. Although it is a very broad request, industry of mobile devices already has several basic ideas, and there are several companies who compete in the area of "battery power management". Usually the solution is to have a IC that detects the USB source capabilities, an IC that manages battery charging and clean switch (instead of relay) to external power (when available), and an IC that gauges the charge, all under a supervision of a microprocessor, typically over I2C communication.

To start, go Texas Instruments pages, and learn what do they offer in the area of battery PMIC.

This overview from Maxim looks extremely useful.