title: SecurityException: Permission Denial
body: <h3>Background</h3>
  <p>I'm working on an Android app that controls the screen rotation. <a href="https://play.google.com/store/apps/details?id=samuelpalmer.sensorautorotation" rel="nofollow noreferrer">The app is available on the Google Play store</a>. To control the screen rotation, the app disables <a href="http://developer.android.com/reference/android/provider/Settings.System.html#ACCELEROMETER_ROTATION" rel="nofollow noreferrer">system auto rotation</a> and changes the values of <a href="http://developer.android.com/reference/android/provider/Settings.System.html#USER_ROTATION" rel="nofollow noreferrer">USER_ROTATION</a>. <a href="https://bitbucket.org/SamPalmer/android-auto-rotate/src" rel="nofollow noreferrer">The source code is available via Mercurial/hg</a>.</p>
  <h3>Problem</h3>
  <p>While the app works fine on my phone, a rooted Sony Xperia M, it crashes on a friend's Samsung Galaxy S3 running Android 4.3. The crash occurs outside of my code, so I don't get a crash report in the Google Play Store, and the stack trace only shows external code that I don't have access to.</p>
  <blockquote>
  <p>java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
  at android.os.Parcel.readException(Parcel.java:1431)
  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
  at android.content.ContentProviderProxy.call(ContentProviderNative.java:602)
  at android.provider.Settings$NameValueCache.getStringForUser(Settings.java:934)
  at android.provider.Settings$System.getStringForUser(Settings.java:1162)
  at android.provider.Settings$System.getIntForUser(Settings.java:1232)
  at com.android.internal.policy.impl.WindowOrientationListener$ScreenOrientationEventListenerImpl.onSensorChanged(WindowOrientationListener.java:501)
  at android.hardware.SystemSensorManager$SensorE</p>
  </blockquote>
  <p>This stack trace implies that the system auto rotation is still running. It also shows the OS code is trying to read an int system setting as a particular user after receiving a screen orientation change event. So I suspect the problem is related to me disabling <a href="http://developer.android.com/reference/android/provider/Settings.System.html#ACCELEROMETER_ROTATION" rel="nofollow noreferrer">ACCELEROMETER_ROTATION</a> or changing <a href="http://developer.android.com/reference/android/provider/Settings.System.html#USER_ROTATION" rel="nofollow noreferrer">USER_ROTATION</a>, both of which are <a href="http://developer.android.com/reference/android/provider/Settings.System.html#putInt(android.content.ContentResolver,%20java.lang.String,%20int)" rel="nofollow noreferrer"><code>int</code> system settings</a>.</p>
  <h3>Troubleshooting</h3>
  <ul>
  <li>I checked other questions about this error. Most of them only explain what the error means without providing any solution. I couldn't find any with the exact same call stack trace.</li>
  <li>I checked <a href="https://android.googlesource.com/platform/frameworks/base/+/master/policy/src/com/android/internal/policy/impl/WindowOrientationListener.java" rel="nofollow noreferrer">the AOSP code for <code>WindowOrientationListener</code></a>, but it doesn't contain the inner class in which the error occurs, <code>ScreenOrientationEventListenerImpl</code>. The Samsung phone probably uses a custom version of the code, most likely in part because it has a non-AOSP feature, <a href="http://www.samsung.com/us/support/faq/FAQ00053261/62634/SPH-L720ZBASPR" rel="nofollow noreferrer">Smart Rotation</a>.</li>
  <li>I don't think my code is doing any user-specific stuff; it's just using normal Android APIs.</li>
  <li>I tested the app on a non-rooted Sony Xperia M and a Sony Xperia Ray, and it was fine on them.</li>
  <li>I tested the app on 4 different Samsung Galaxy S3s using <a href="http://developer.samsung.com/remotetestlab/rtlDeviceList.action" rel="nofollow noreferrer">Samsung Remote Test Lab</a>.</li>
  <li>I tried to find the source code where the error is occurring. I found <a href="https://github.com/mrimp/G900FXXU1ANCF/blob/master/android.policy.jar.out/smali/com/android/internal/policy/impl/WindowOrientationListener%24ScreenOrientationEventListenerImpl.smali#L240" rel="nofollow noreferrer">a <em>smali</em> file</a>, which at a glance indicates that it's trying to read the setting <code>&quot;intelligent_rotation_mode&quot;</code> as user <code>-0x2</code>, which is interesting since I'm not touching that presumably-Samsung-specific setting. I also downloaded a copy of <a href="http://opensource.samsung.com/reception/receptionSub.do?method=search&amp;searchValue=I9300" rel="nofollow noreferrer">the official source code</a>, but it didn't seem to contain the relevant file.</li>
  </ul>