Fix DialerSettingsActivity so it does not crash pre-M.

UserManager.isSystemUser() is not available pre-M, so we need to copy
over the logic.

Bug: 25776171
Change-Id: I8e7c8e7215a8b55009283ecb137cc54443e61ab8
diff --git a/src/com/android/dialer/compat/UserManagerCompat.java b/src/com/android/dialer/compat/UserManagerCompat.java
new file mode 100644
index 0000000..a1fdcc6
--- /dev/null
+++ b/src/com/android/dialer/compat/UserManagerCompat.java
@@ -0,0 +1,37 @@
+package com.android.dialer.compat;
+
+import android.content.Context;
+import android.os.Process;
+import android.os.UserHandle;
+import android.os.UserManager;
+
+import com.android.contacts.common.compat.CompatUtils;
+
+/**
+ * Compatibility class for {@link UserManager}.
+ */
+public class UserManagerCompat {
+    /**
+     * A user id constant to indicate the "system" user of the device. Copied from
+     * {@link UserHandle}.
+     */
+    private static final int USER_SYSTEM = 0;
+    /**
+     * Range of uids allocated for a user.
+     */
+    private static final int PER_USER_RANGE = 100000;
+
+    /**
+     * Used to check if this process is running under the system user. The system user is the
+     * initial user that is implicitly created on first boot and hosts most of the system services.
+     *
+     * @return whether this process is running under the system user.
+     */
+    public static boolean isSystemUser(UserManager userManager) {
+        if (CompatUtils.isMarshmallowCompatible()) {
+            return userManager.isSystemUser();
+        }
+        // Adapted from {@link UserManager} and {@link UserHandle}.
+        return (Process.myUid() / PER_USER_RANGE) == USER_SYSTEM;
+    }
+}
diff --git a/src/com/android/dialer/settings/DialerSettingsActivity.java b/src/com/android/dialer/settings/DialerSettingsActivity.java
index d7b8e11..13d1965 100644
--- a/src/com/android/dialer/settings/DialerSettingsActivity.java
+++ b/src/com/android/dialer/settings/DialerSettingsActivity.java
@@ -28,9 +28,11 @@
 import android.view.MenuItem;
 import android.widget.Toast;
 
+import com.android.contacts.common.compat.CompatUtils;
 import com.android.contacts.common.compat.SdkVersionOverride;
 import com.android.dialer.R;
 import com.android.dialer.compat.SettingsCompat;
+import com.android.dialer.compat.UserManagerCompat;
 import com.android.dialer.filterednumber.BlockedNumbersSettingsActivity;
 
 import java.util.List;
@@ -71,8 +73,8 @@
         TelephonyManager telephonyManager =
                 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
 
-        // "Call Settings" (full settings) is shown if the current user is primary user and there 
-        // is only one SIM. Before N, "Calling accounts" setting is shown if the current user is 
+        // "Call Settings" (full settings) is shown if the current user is primary user and there
+        // is only one SIM. Before N, "Calling accounts" setting is shown if the current user is
         // primary user and there are multiple SIMs. In N+, "Calling accounts" is shown whenever
         // "Call Settings" is not shown.
         boolean isPrimaryUser = isPrimaryUser();
@@ -158,7 +160,6 @@
      * @return Whether the current user is the primary user.
      */
     private boolean isPrimaryUser() {
-        final UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
-        return userManager.isSystemUser();
+        return UserManagerCompat.isSystemUser((UserManager) getSystemService(Context.USER_SERVICE));
     }
 }