Merge "Add vm_safe_mode to dexopt." into lmp-dev
diff --git a/cmds/dumpstate/dumpstate.c b/cmds/dumpstate/dumpstate.c
index 342dc89..ffc8714 100644
--- a/cmds/dumpstate/dumpstate.c
+++ b/cmds/dumpstate/dumpstate.c
@@ -378,8 +378,8 @@
 }
 
 static void sigpipe_handler(int n) {
-    (void)n;
-    exit(EXIT_FAILURE);
+    // don't complain to stderr or stdout
+    _exit(EXIT_FAILURE);
 }
 
 int main(int argc, char *argv[]) {
@@ -404,10 +404,12 @@
     }
     ALOGI("begin\n");
 
+
     memset(&sigact, 0, sizeof(sigact));
     sigact.sa_handler = sigpipe_handler;
     sigaction(SIGPIPE, &sigact, NULL);
 
+
     /* set as high priority, and protect from OOM killer */
     setpriority(PRIO_PROCESS, 0, -20);
     FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
diff --git a/cmds/dumpstate/utils.c b/cmds/dumpstate/utils.c
index a6d9ef6..85c353e 100644
--- a/cmds/dumpstate/utils.c
+++ b/cmds/dumpstate/utils.c
@@ -313,6 +313,12 @@
         /* make sure the child dies when dumpstate dies */
         prctl(PR_SET_PDEATHSIG, SIGKILL);
 
+        /* just ignore SIGPIPE, will go down with parent's */
+        struct sigaction sigact;
+        memset(&sigact, 0, sizeof(sigact));
+        sigact.sa_handler = SIG_IGN;
+        sigaction(SIGPIPE, &sigact, NULL);
+
         va_list ap;
         va_start(ap, command);
         if (title) printf("------ %s (%s", title, command);
diff --git a/include/android/configuration.h b/include/android/configuration.h
index 97d4c42..be00066 100644
--- a/include/android/configuration.h
+++ b/include/android/configuration.h
@@ -45,6 +45,7 @@
     ACONFIGURATION_DENSITY_XHIGH = 320,
     ACONFIGURATION_DENSITY_XXHIGH = 480,
     ACONFIGURATION_DENSITY_XXXHIGH = 640,
+    ACONFIGURATION_DENSITY_ANY = 0xfffe,
     ACONFIGURATION_DENSITY_NONE = 0xffff,
 
     ACONFIGURATION_KEYBOARD_ANY  = 0x0000,
diff --git a/include/android/keycodes.h b/include/android/keycodes.h
index da7302d..813d411 100644
--- a/include/android/keycodes.h
+++ b/include/android/keycodes.h
@@ -273,7 +273,8 @@
     AKEYCODE_11              = 227,
     AKEYCODE_12              = 228,
     AKEYCODE_LAST_CHANNEL    = 229,
-    AKEYCODE_TV_DATA_SERVICE = 230
+    AKEYCODE_TV_DATA_SERVICE = 230,
+    AKEYCODE_VOICE_ASSIST    = 231
 
     // NOTE: If you add a new keycode here you must also add it to several other files.
     //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
diff --git a/include/input/InputEventLabels.h b/include/input/InputEventLabels.h
index 51f7661..279ac40 100644
--- a/include/input/InputEventLabels.h
+++ b/include/input/InputEventLabels.h
@@ -270,6 +270,7 @@
     DEFINE_KEYCODE(12),
     DEFINE_KEYCODE(LAST_CHANNEL),
     DEFINE_KEYCODE(TV_DATA_SERVICE),
+    DEFINE_KEYCODE(VOICE_ASSIST),
 
     { NULL, 0 }
 };
diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp
index 84d3b0f..6c3144b 100644
--- a/services/sensorservice/SensorService.cpp
+++ b/services/sensorservice/SensorService.cpp
@@ -150,14 +150,33 @@
             // debugging sensor list
             mUserSensorListDebug = mSensorList;
 
-            mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;
+            // Check if the device really supports batching by looking at the FIFO event
+            // counts for each sensor.
+            bool batchingSupported = false;
+            for (int i = 0; i < mSensorList.size(); ++i) {
+                if (mSensorList[i].getFifoMaxEventCount() > 0) {
+                    batchingSupported = true;
+                    break;
+                }
+            }
+
+            if (batchingSupported) {
+                // Increase socket buffer size to a max of 100 KB for batching capabilities.
+                mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;
+            } else {
+                mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;
+            }
+
+            // Compare the socketBufferSize value against the system limits and limit
+            // it to maxSystemSocketBufferSize if necessary.
             FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r");
             char line[128];
             if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
                 line[sizeof(line) - 1] = '\0';
-                sscanf(line, "%zu", &mSocketBufferSize);
-                if (mSocketBufferSize > MAX_SOCKET_BUFFER_SIZE_BATCHED) {
-                    mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;
+                size_t maxSystemSocketBufferSize;
+                sscanf(line, "%zu", &maxSystemSocketBufferSize);
+                if (mSocketBufferSize > maxSystemSocketBufferSize) {
+                    mSocketBufferSize = maxSystemSocketBufferSize;
                 }
             }
             if (fp) {
@@ -310,7 +329,7 @@
                     mActiveSensors.valueAt(i)->getNumConnections());
         }
 
-        result.appendFormat("Max Socket Buffer size = %d events\n",
+        result.appendFormat("Socket Buffer size = %d events\n",
                             mSocketBufferSize/sizeof(sensors_event_t));
         result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : "not held");
         result.appendFormat("%zd active connections\n", mActiveConnections.size());
@@ -687,13 +706,18 @@
     status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
                                  maxBatchReportLatencyNs);
 
-    if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT) {
+    // Call flush() before calling activate() on the sensor. Wait for a first flush complete
+    // event before sending events on this connection. Ignore one-shot sensors which don't
+    // support flush(). Also if this sensor isn't already active, don't call flush().
+    if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT &&
+            rec->getNumConnections() > 1) {
+        connection->setFirstFlushPending(handle, true);
         status_t err_flush = sensor->flush(connection.get(), handle);
-        // Flush may return error if the sensor is not activated or the underlying h/w sensor does
-        // not support flush.
+        // Flush may return error if the underlying h/w sensor uses an older HAL.
         if (err_flush == NO_ERROR) {
-            connection->setFirstFlushPending(handle, true);
             rec->addPendingFlushConnection(connection.get());
+        } else {
+            connection->setFirstFlushPending(handle, false);
         }
     }
 
@@ -808,7 +832,6 @@
     return ret;
 }
 
-
 bool SensorService::canAccessSensor(const Sensor& sensor) {
     return (sensor.getRequiredPermission().isEmpty()) ||
             PermissionCache::checkCallingPermission(String16(sensor.getRequiredPermission()));
@@ -916,12 +939,7 @@
     : mService(service), mUid(uid), mWakeLockRefCount(0), mEventCache(NULL), mCacheSize(0),
       mMaxCacheSize(0) {
     const SensorDevice& device(SensorDevice::getInstance());
-    if (device.getHalDeviceVersion() >= SENSORS_DEVICE_API_VERSION_1_1) {
-        // Increase socket buffer size to a max of 100 KB for batching capabilities.
-        mChannel = new BitTube(mService->mSocketBufferSize);
-    } else {
-        mChannel = new BitTube(SOCKET_BUFFER_SIZE_NON_BATCHED);
-    }
+    mChannel = new BitTube(mService->mSocketBufferSize);
 #if DEBUG_CONNECTIONS
     mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
     mTotalAcksNeeded = mTotalAcksReceived = 0;
@@ -1211,13 +1229,14 @@
 }
 
 void SensorService::SensorEventConnection::writeToSocketFromCacheLocked() {
-    // At a time write at most half the size of the receiver buffer in SensorEventQueue.
-    const int maxWriteSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2;
+    // At a time write at most half the size of the receiver buffer in SensorEventQueue OR
+    // half the size of the socket buffer allocated in BitTube whichever is smaller.
+    const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
+            int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
+    // Send pending flush complete events (if any)
     sendPendingFlushEventsLocked();
-    // Write "count" events at a time.
     for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
-        const int numEventsToWrite = (mCacheSize - numEventsSent) < maxWriteSize ?
-                                        mCacheSize - numEventsSent : maxWriteSize;
+        const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize);
         int index_wake_up_event =
                   findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite);
         if (index_wake_up_event >= 0) {
diff --git a/services/sensorservice/SensorService.h b/services/sensorservice/SensorService.h
index 77f6f6a..3717fcb 100644
--- a/services/sensorservice/SensorService.h
+++ b/services/sensorservice/SensorService.h
@@ -218,6 +218,8 @@
     DefaultKeyedVector<int, SensorInterface*> mSensorMap;
     Vector<SensorInterface *> mVirtualSensorList;
     status_t mInitCheck;
+    // Socket buffersize used to initialize BitTube. This size depends on whether batching is
+    // supported or not.
     size_t mSocketBufferSize;
     sp<Looper> mLooper;
 
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.cpp b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
index bd07d24..6302053 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.cpp
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
@@ -524,6 +524,10 @@
             if (!err) {
                 int32_t& events(mDisplayData[disp].events);
                 events = (events & ~eventBit) | newValue;
+
+                char tag[16];
+                snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
+                ATRACE_INT(tag, enabled);
             }
         }
         // error here should not happen -- not sure what we should
@@ -780,6 +784,8 @@
 
 status_t HWComposer::setActiveConfig(int disp, int mode) {
     LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
+    DisplayData& dd(mDisplayData[disp]);
+    dd.currentConfig = mode;
     if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
         return (status_t)mHwc->setActiveConfig(mHwc, disp, mode);
     } else {