Algorithms and Models for Optimal Power Management on Smartphones