This class has ported and improved from the Android Open Source Project.