AI 148368: Decrease CPU usage by throttling touch events
  We are decreasing CPU usage at the cost of event latency. Events are queued up
  and released to the app at the specified rate. You can
  experiment with different values via:
  adb shell setprop windowsmgr.max_events_per_sec 35
  The new value is picked up when you let go and retouch the screen. No
  reboot needed.
  Also the following changes were made after profiling:
  - In WindowManagerService, limit the call to userActivity() when we
  have a flood touch events.
  - In PowerManagerService, skip checking of permission if the caller
  is system user.
  - In PowerManagerService, integrated the functionality of gatherState()
  into reactivateWakeLocksLocked(). They loop through the same data
  structure and are called back to back.
  BUG=1692771

Automated import of CL 148368
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index 9c6e9dc..c5ea5fa9 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -496,8 +496,10 @@
     }
 
     public void acquireWakeLock(int flags, IBinder lock, String tag) {
-        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
         int uid = Binder.getCallingUid();
+        if (uid != Process.myUid()) {
+            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
+        }
         long ident = Binder.clearCallingIdentity();
         try {
             synchronized (mLocks) {
@@ -554,14 +556,14 @@
             // by the current state so we never turn it more on than
             // it already is.
             if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
-                reactivateWakeLocksLocked();
+                int oldWakeLockState = mWakeLockState;
+                mWakeLockState = mLocks.reactivateScreenLocksLocked();
                 if (mSpew) {
                     Log.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
-                            + " mLocks.gatherState()=0x"
-                            + Integer.toHexString(mLocks.gatherState())
-                            + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
+                            + " mWakeLockState=0x"
+                            + Integer.toHexString(mWakeLockState)
+                            + " previous wakeLockState=0x" + Integer.toHexString(oldWakeLockState));
                 }
-                mWakeLockState = mLocks.gatherState();
             } else {
                 if (mSpew) {
                     Log.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
@@ -598,7 +600,10 @@
     }
 
     public void releaseWakeLock(IBinder lock) {
-        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
+        int uid = Binder.getCallingUid();
+        if (uid != Process.myUid()) {
+            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
+        }
 
         synchronized (mLocks) {
             releaseWakeLockLocked(lock, false);
@@ -653,17 +658,6 @@
         }
     }
 
-    private void reactivateWakeLocksLocked()
-    {
-        int N = mLocks.size();
-        for (int i=0; i<N; i++) {
-            WakeLock wl = mLocks.get(i);
-            if (isScreenLock(wl.flags)) {
-                mLocks.get(i).activated = true;
-            }
-        }
-    }
-
     private class PokeLock implements IBinder.DeathRecipient
     {
         PokeLock(int p, IBinder b, String t) {
@@ -1752,8 +1746,7 @@
                         Binder.restoreCallingIdentity(ident);
                     }
                     
-                    reactivateWakeLocksLocked();
-                    mWakeLockState = mLocks.gatherState();
+                    mWakeLockState = mLocks.reactivateScreenLocksLocked();
                     setPowerState(mUserState | mWakeLockState, noChangeLights, true);
                     setTimeoutLocked(time, SCREEN_BRIGHT);
                 }
@@ -1944,6 +1937,20 @@
             }
             return result;
         }
+        
+        int reactivateScreenLocksLocked()
+        {
+            int result = 0;
+            int N = this.size();
+            for (int i=0; i<N; i++) {
+                WakeLock wl = this.get(i);
+                if (isScreenLock(wl.flags)) {
+                    wl.activated = true;
+                    result |= wl.minState;
+                }
+            }
+            return result;
+        }
     }
 
     void setPolicy(WindowManagerPolicy p) {