Is it possible to do DNS resolution based on connection? On my Galaxy Note 2 phone (Android of course), is it possible to edit the hosts file so that it resolves example.com to a public IP BUT resolves it to a local private IP if connected to WiFi with a specific SSID? That is when connected to home WiFi, it should resolve a domain name to a local server on the LAN, but anyplace else it should resolve it to the public IP.

I'm guessing it'd be hard at the very least, if it's even possible, but do I have any options here (either hosts, or some other way)? I've got shell access, and root access, so I can edit pretty much anything that needs to be.
DNS resolver always consults local /etc/hosts file before sending DNS query to configured name server (see details in How does Android OS do DNS name resolution?). So this approach cannot work based on connection.

The possible method is to run a DNS server on that specific network and configure DHCP server (mostly your router) to send local DNS server's IP address in response to DHCP requests. Configure DNS server to always resolve a domain name to a fixed IP. Or you may resolve the domain name to required IP using a VPN app temporarily (it's less hassle than editing hosts file every time). See linked answer for details.