They were run over. Or the police are at your doorstep with a warrant. Or you were arrested and the police asked you to unlock your phone. How's it going? Can you refuse? What are your rights? What happens if you say no?
While some of these questions have simple answers, others are not so clear and unfortunately differ a little depending on where they live. Here are some basic principles that should help you decide on your phone with the police.
What are my rights?
The Founding Fathers have incorporated some protection for us directly into the Constitution. The main principles are found in the fourth amendment, which protects against inappropriate search and seizure, and the fifth amendment, which protects against self-incrimination.
How do these principles apply to your phone?
Generally, the police will need a warrant to search your device. There are exceptions: If you agree to a search, if there is a probable cause, and if you are arrested.
If you openly agree to let the police ransack your phone, they do not need a warrant. It is important to know that you can restrict the police's perspective and completely revoke your consent.
There is one catch, however. Everyone can agree to the search, eg. Your roommate or friend or another person. If you disagree with a search, the Electronics Frontier Foundation (EFF) recommends expressly stating this. You have the right to refuse consent. In other words, if the police asks for your phone, you can say no.
Then there is probably a cause. If the police believe that incriminating evidence exists on the device – and that the evidence could be destroyed – they may confiscate the device for search purposes.
If arrested, the police will do the right search for you. This also includes what's in your pockets, which probably means your phone. However, there is a limitation here: the police may look at the physical phone itself, but not unlock the content or data stored on the phone.
What if they have a warrant?
An arrest warrant is a document signed by a judge that grants the police the right to search your device (or anything else). The EFF recommends that the arrest warrant be inspected and reviewed. Warrants often have limits. So it's a good idea to know these limits. Let's assume the warranty covers your device. Here's the fifth change to your friend.
If the police tell you to unlock your phone, whether by PIN, password, pattern, print, iris, or face, you can reject it. In addition, the police can not force you to unlock your device. By grabbing your hand or sliding the phone into your face. A case decided earlier this year added the biometric distinction to this list.
The judge presiding over this particular case ruled that a person's obligation to use their biometric data to unlock a device violated their rights to the fifth amendment. The judge said that "all applications are the same," meaning that there is no difference in what the application looks like. Providing your passcode or otherwise unlocking your phone for the police is a self-incrimination.
The bottom line is that you do not need to unlock your device for the police, even if they have an arrest warrant.
What happens if I refuse? 
The worst thing is, the police have your phone and you told them you will not unlock it. As long as you have this right, you probably have a bad day.
If you have not been arrested, you can go to prison for disrespect. In a case recently uncovered by NBC News, a Florida man refused to divulge his passwords and was sentenced to 44 days in prison for disregard. However, case law on this subject varies and may vary depending on the country in which you live. There are now appeals in Indiana and New Jersey that could potentially reach higher courts and ultimately set a nationwide precedent. NBC News says that the result of refusing your passcode can currently go both ways. The EFF suggests that if a judge forces you to unlock your device, seek legal assistance from the organization immediately.
None of this will stop the police from cracking the code itself. Once the police have a search warrant for your device, they may take it with all necessary funds to unlock.
Two companies, Cellebrite and Grayshift, make devices that can reverse the encryption of mobile phones. With the size of a tablet, the police put your phone in the Gray Key and it does it's job, so law enforcement can finally access everything stored on the device. Not much is known about these devices, but it is worth noting that they are not available in all police departments.
In addition, the police may obtain warranties from your telephone company to obtain specified information such as your location.
What to do?  The EFF says people should always stay calm, state their rights and not interfere in a valid police search. You can refuse to answer questions, refuse to assist the police in their search, and refuse to unlock your phone. You can always ask for a lawyer. If the police carry out an illegal search, anything that is discovered during the search could be thrown off a judge.
Searches conducted at borders are subject to different rules. Read more about it here.
If you have any questions about your rights when you meet the police, the ACLU has a simple guide here.