At the point when the police started to search the car he was under arrest, suspected of being under the influence of an illegal drug. The officer was trained in how to identify people as being under the influence of drugs. The officer, in his report, thought the suspect was under the influence of an illegal stimulant. The urine test for the suspect came back positive only for marijuana and opiates. If you know anything about drugs, those two are not stimulants and their effects, and symptoms, are quite different from that of stimulants. The court's judgement did not go into detail about the drug test results, in particular, the levels detected. Low levels in the urine can still be detected corresponding to an amount that would have little effect on the body and mind. He could have ingested marijuana two weeks previously or taken opiates at any time in the last few days. The result of the drug test clearly showed the officer didn't know what he was talking about, was just guessing, or took the behavior of a person nervous because of a police stop as indicative of illegal drug use in order to allow a search of the car.
The appeals court affirmed that a search of the phone's contents was not justified by an inventory search prior to impounding the car. The police then shifted their justification to being a search incident to arrest. This, according to the appeals court, justified the search of the phones contents. Apart from the issue of 4th amendment rights, what I find bogus is the arrest itself. If a "trained" officer can interpret the behavior of someone who has just been stopped by the police and is understandably nervous, as the abnormal symptoms of someone under the influence of an illegal drug, then drug detection becomes just a ruse to justify a fishing expedition by searching for evidence of further crimes.
The other inconsistency by the officer was in deciding to impound the car when the suspect was a half mile from home. He was suspected of being under the influence of an illegal drug but not impaired enough to be a DUI. Otherwise the sheriff would have been forced to transfer the stop to a CHP officer. So the sheriff is trying to justify this in court, saying he couldn't allow him to drive because the suspect couldn't drive safely yet he wasn't impaired. The sheriff's office probably used this ruse to allow a search of the entire car while a search incident to arrest would be limited the passenger compartment.