Unify and simplify the logic, and add unit test. Bug: 293845605 Test: manual - on Mobile Settings Test: unit test Change-Id: I5467b92baa8e47fbd400c7a21fd39fd5ec90ed3a