Categories
android java usermanager

Proper use cases for Android UserManager.isUserAGoat()?

3916

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?

7

1947

+50

Android R Update:

From Android R, this method always returns false. Google says that this is done “to protect goat privacy”:

/**
 * Used to determine whether the user making this call is subject to
 * teleportations.
 *
 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
 * now automatically identify goats using advanced goat recognition technology.</p>
 *
 * <p>As of {@link android.os.Build.VERSION_CODES#R}, this method always returns
 * {@code false} in order to protect goat privacy.</p>
 *
 * @return Returns whether the user making this call is a goat.
 */
public boolean isUserAGoat() {
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R) {
        return false;
    }
    return mContext.getPackageManager()
            .isPackageAvailable("com.coffeestainstudios.goatsimulator");
}

Previous answer:

From their source, the method used to return false until it was changed in API 21.

/**
 * Used to determine whether the user making this call is subject to
 * teleportations.
 * @return whether the user making this call is a goat 
 */
public boolean isUserAGoat() {
    return false;
}

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

/**
 * Used to determine whether the user making this call is subject to
 * teleportations.
 *
 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
 * now automatically identify goats using advanced goat recognition technology.</p>
 *
 * @return Returns true if the user making this call is a goat.
 */
public boolean isUserAGoat() {
    return mContext.getPackageManager()
            .isPackageAvailable("com.coffeestainstudios.goatsimulator");
}

Here is the source and the change.

12

  • 111

    Theory: Goats are found in all kinds of precarious places, like sheer cliffs, possibly bucking location trends they get from regular non-goat users. Might explain “teleportations”, which could be goats appearing to wildly relocate as the software tries to determine their location with algos designed for non-goats.

    – tedders

    Nov 14, 2012 at 12:58

  • 12

    @LarsH – the source for one.

    – djechlin

    Nov 14, 2012 at 20:07

  • 61

    This is a history lesson, not an answer. Even if it is an Easter egg, is there a valid use case for it? Being an Easter egg doesn’t preclude it from being useful, and you’ve not shown whether it is or is not useful.

    – casperOne

    Nov 14, 2012 at 20:26

  • 43

    That’s a real app.

    Feb 21, 2015 at 8:07

  • 226

    I feel bad for anyone who cheekily used this function, expecting it to return false forever.

    Apr 27, 2015 at 5:11

1022

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):

while (1 == 2) { // Note that "if" is treated differently
    System.out.println("Unreachable code");
}

However this is legal:

while (isUserAGoat()) {
    System.out.println("Unreachable but determined at runtime, not at compile time");
}

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 for if, 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.

10

  • 11

    @djechlin I do not know if this is android only, in standard java ` if(false){…}` compiles just fine (open jdk javac 1.6 and 1.7)

    – josefx

    Nov 14, 2012 at 20:24


  • 10

    @PeterOlson – if you are specifically trying to test a code branch where an if goes false or true, this is the most precise and obvious way to simulate that – commenting out the block could be less accurate if you don’t match to the proper end of the block (perhaps you didn’t notice a discreet if/else block immediately following), it will lead to a compile error if you’re forcing to the following else block, etc.

    – djechlin

    Nov 15, 2012 at 2:01

  • 189

    “provided the implementation doesn’t change” – and there’s the problem. This is a disastrous way to implement a warning-free “if (false)” because the implementation can and does change. See my answer to this question: as of API21 isUserAGoat() returns true if a user has a specific app installed. Good luck diagnosing that when your code suddenly starts behaving oddly on “random” devices!

    Nov 5, 2014 at 12:29

  • 18

    @MarkWhitaker Or if goats start using devices.

    Feb 18, 2016 at 1:51

  • 13

    @thedayturns If goats start using Android devices this will be the least of our problems.

    Feb 29, 2016 at 13:16

795

This appears to be an inside joke at Google. It’s also featured in the Google Chrome task manager. It has no purpose, other than some engineers finding it amusing. Which is a purpose by itself, if you will.

  1. In Chrome, open the Task Manager with Shift+Esc.
  2. Right click to add the Goats Teleported column.
  3. Wonder.

There is even a huge Chromium bug report about too many teleported goats.

chrome

The following Chromium source code snippet is stolen from the HN comments.

int TaskManagerModel::GetGoatsTeleported(int index) const {
  int seed = goat_salt_ * (index + 1);
  return (seed >> 16) & 255;
}

1

  • 54

    Windows XP + latest Chrome = it isn’t there, on Ubuntu I see it.

    – auselen

    Nov 14, 2012 at 9:47