I was looking at the new APIs introduced in Android 4.2.
While looking at the UserManager
class I came across the following method:
public boolean isUserAGoat()
Used to determine whether the user making this call is subject to teleportations.
Returns whether the user making this call is a goat.
How and when should this be used?
From their source, the method used to return
false
until it was changed in API 21.It looks like the method has no real use for us as developers. Someone has previously stated that it might be an Easter egg.
In API 21 the implementation was changed to check if there is an installed app with the package
com.coffeestainstudios.goatsimulator
Here is the source link
Please see the below source code :
I don't know if this was "the" official use case, but the following produces a warning in Java (that can further produce compile errors if mixed with
return
statements, leading to unreachable code):However this is legal:
So I often find myself writing a silly utility method for the quickest way to dummy out a code block, then in completing debugging find all calls to it, so provided the implementation doesn't change this can be used for that.
JLS points out
if (false)
does not trigger "unreachable code" for the specific reason that this would break support for debug flags, i.e., basically this use case (h/t @auselen). (static final boolean DEBUG = false;
for instance).I replaced
while
forif
, producing a more obscure use case. I believe you can trip up your IDE, like Eclipse, with this behavior, but this edit is 4 years into the future, and I don't have an Eclipse environment to play with.As of API 21 (the first Android 5.0/Lollipop SDK), this detects whether the Goat Simulator app is installed:
This should make it clear that djechlin's suggestion of using it as a warning-free
if (false)
is a potentially disastrous strategy. What previously returnedfalse
for every device now returns a seemingly random value: if this was buried deep enough in your code it could take a long time to figure out where your new bugs are coming from.Bottom line: if you don't control the implementation of a method and decide to use it for purposes other than stated in the API documentation, you're heading for trouble.
There is a similar call,
isUserAMonkey()
, that returns true if the MonkeyRunner tool is being used. The SDK explanation is just as curious as this one.Here is the source.
I expect that this was added in anticipation of a new SDK tool named something with a goat and will actually be functional to test for the presence of that tool.
Also see a similar question, Strange function in ActivityManager: isUserAMonkey. What does this mean, what is its use?.
In the most remote mountains of the planet, lies an advanced species of goat that appear to be currently able to use phones, just like us humans!
Leaked footage: youtu.be/YJwZMUn7GdQ
Google must have spotted this and decided to provide support for them, in an attempt to stay in the frontline of technology advancement.