Code layout cleanup

- Fwd declare where possible
- List .h first in the .cpp to verify proper includes
- Remove hacky -internal.h file and move testBitInRange to a new component

Change-Id: I442248c4b32738c6c2af250f45d4c8822c862e08
diff --git a/modules/input/evdev/Android.mk b/modules/input/evdev/Android.mk
index b12867c..1f5e12f 100644
--- a/modules/input/evdev/Android.mk
+++ b/modules/input/evdev/Android.mk
@@ -18,6 +18,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
+    BitUtils.cpp \
     InputHub.cpp \
     InputDevice.cpp \
     InputDeviceManager.cpp \
diff --git a/modules/input/evdev/BitUtils.cpp b/modules/input/evdev/BitUtils.cpp
new file mode 100644
index 0000000..3434c31
--- /dev/null
+++ b/modules/input/evdev/BitUtils.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "BitUtils"
+//#define LOG_NDEBUG 0
+
+#include "BitUtils.h"
+
+#include <utils/Log.h>
+
+// Enables debug output for hasKeyInRange
+#define DEBUG_KEY_RANGE 0
+
+namespace android {
+
+#if DEBUG_KEY_RANGE
+static const char* bitstrings[16] = {
+    "0000", "0001", "0010", "0011",
+    "0100", "0101", "0110", "0111",
+    "1000", "1001", "1010", "1011",
+    "1100", "1101", "1110", "1111",
+};
+#endif
+
+bool testBitInRange(const uint8_t arr[], size_t start, size_t end) {
+#if DEBUG_KEY_RANGE
+    ALOGD("testBitInRange(%d, %d)", start, end);
+#endif
+    // Invalid range! This is nonsense; just say no.
+    if (end <= start) return false;
+
+    // Find byte array indices. The end is not included in the range, nor is
+    // endIndex. Round up for endIndex.
+    size_t startIndex = start / 8;
+    size_t endIndex = (end + 7) / 8;
+#if DEBUG_KEY_RANGE
+    ALOGD("startIndex=%d, endIndex=%d", startIndex, endIndex);
+#endif
+    for (size_t i = startIndex; i < endIndex; ++i) {
+        uint8_t bits = arr[i];
+        uint8_t mask = 0xff;
+#if DEBUG_KEY_RANGE
+        ALOGD("block %04d: %s%s", i, bitstrings[bits >> 4], bitstrings[bits & 0x0f]);
+#endif
+        if (bits) {
+            // Mask off bits before our start bit
+            if (i == startIndex) {
+                mask &= 0xff << (start % 8);
+            }
+            // Mask off bits after our end bit
+            if (i == endIndex - 1 && (end % 8)) {
+                mask &= 0xff >> (8 - (end % 8));
+            }
+#if DEBUG_KEY_RANGE
+            ALOGD("mask: %s%s", bitstrings[mask >> 4], bitstrings[mask & 0x0f]);
+#endif
+            // Test the index against the mask
+            if (bits & mask) return true;
+        }
+    }
+    return false;
+}
+}  // namespace android
diff --git a/modules/input/evdev/InputHub-internal.h b/modules/input/evdev/BitUtils.h
similarity index 83%
rename from modules/input/evdev/InputHub-internal.h
rename to modules/input/evdev/BitUtils.h
index b4f1297..1aa1f6a 100644
--- a/modules/input/evdev/InputHub-internal.h
+++ b/modules/input/evdev/BitUtils.h
@@ -14,16 +14,16 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_INPUT_HUB_INTERNAL_H_
-#define ANDROID_INPUT_HUB_INTERNAL_H_
+#ifndef ANDROID_BIT_UTILS_H_
+#define ANDROID_BIT_UTILS_H_
+
+#include <cstdint>
 
 namespace android {
-namespace internal {
 
 /** Test whether any bits in the interval [start, end) are set in the array. */
 bool testBitInRange(const uint8_t arr[], size_t start, size_t end);
 
-}  // namespace internal
 }  // namespace android
 
-#endif  // ANDROID_INPUT_HUB_INTERNAL_H_
+#endif  // ANDROID_BIT_UTILS_H_
diff --git a/modules/input/evdev/InputDevice.cpp b/modules/input/evdev/InputDevice.cpp
index 8365386..ea90068 100644
--- a/modules/input/evdev/InputDevice.cpp
+++ b/modules/input/evdev/InputDevice.cpp
@@ -20,6 +20,8 @@
 // Enables debug output for processing input events
 #define DEBUG_INPUT_EVENTS 0
 
+#include "InputDevice.h"
+
 #include <linux/input.h>
 
 #define __STDC_FORMAT_MACROS
@@ -30,9 +32,8 @@
 #include <utils/Log.h>
 #include <utils/Timers.h>
 
+#include "InputHost.h"
 #include "InputHub.h"
-#include "InputDevice.h"
-#include "InputMapper.h"
 #include "SwitchInputMapper.h"
 
 #define MSC_ANDROID_TIME_SEC  0x6
diff --git a/modules/input/evdev/InputDevice.h b/modules/input/evdev/InputDevice.h
index a379d6c..6892778 100644
--- a/modules/input/evdev/InputDevice.h
+++ b/modules/input/evdev/InputDevice.h
@@ -22,12 +22,20 @@
 
 #include <utils/Timers.h>
 
-#include "InputHost.h"
-#include "InputHub.h"
 #include "InputMapper.h"
 
+struct input_device_handle;
+struct input_device_identifier;
+
 namespace android {
 
+class InputDeviceDefinition;
+class InputDeviceNode;
+class InputHostInterface;
+struct InputEvent;
+using InputDeviceHandle = struct input_device_handle;
+using InputDeviceIdentifier = struct input_device_identifier;
+
 /**
  * InputDeviceInterface represents an input device in the HAL. It processes
  * input events before passing them to the input host.
diff --git a/modules/input/evdev/InputDeviceManager.cpp b/modules/input/evdev/InputDeviceManager.cpp
index 46c9dfb..d50c1ae 100644
--- a/modules/input/evdev/InputDeviceManager.cpp
+++ b/modules/input/evdev/InputDeviceManager.cpp
@@ -17,10 +17,11 @@
 #define LOG_TAG "InputDeviceManager"
 //#define LOG_NDEBUG 0
 
+#include "InputDeviceManager.h"
+
 #include <utils/Log.h>
 
 #include "InputDevice.h"
-#include "InputDeviceManager.h"
 
 namespace android {
 
diff --git a/modules/input/evdev/InputDeviceManager.h b/modules/input/evdev/InputDeviceManager.h
index 7ffec5b..8fbf3ca 100644
--- a/modules/input/evdev/InputDeviceManager.h
+++ b/modules/input/evdev/InputDeviceManager.h
@@ -22,12 +22,13 @@
 
 #include <utils/Timers.h>
 
-#include "InputDevice.h"
-#include "InputHost.h"
 #include "InputHub.h"
 
 namespace android {
 
+class InputDeviceInterface;
+class InputHostInterface;
+
 /**
  * InputDeviceManager keeps the mapping of InputDeviceNodes to
  * InputDeviceInterfaces and handles the callbacks from the InputHub, delegating
diff --git a/modules/input/evdev/InputHub.cpp b/modules/input/evdev/InputHub.cpp
index 8c48345..389955d 100644
--- a/modules/input/evdev/InputHub.cpp
+++ b/modules/input/evdev/InputHub.cpp
@@ -17,8 +17,7 @@
 #define LOG_TAG "InputHub"
 //#define LOG_NDEBUG 0
 
-// Enables debug output for hasKeyInRange
-#define DEBUG_KEY_RANGE 0
+#include "InputHub.h"
 
 #include <dirent.h>
 #include <errno.h>
@@ -36,15 +35,14 @@
 
 #include <vector>
 
-#include "InputHub.h"
-#include "InputHub-internal.h"
-
 #include <android/input.h>
 #include <hardware_legacy/power.h>
 #include <linux/input.h>
 
 #include <utils/Log.h>
 
+#include "BitUtils.h"
+
 namespace android {
 
 static const char WAKE_LOCK_ID[] = "KeyEvents";
@@ -60,57 +58,6 @@
     return (bits + 7) / 8;
 }
 
-namespace internal {
-
-#if DEBUG_KEY_RANGE
-static const char* bitstrings[16] = {
-    "0000", "0001", "0010", "0011",
-    "0100", "0101", "0110", "0111",
-    "1000", "1001", "1010", "1011",
-    "1100", "1101", "1110", "1111",
-};
-#endif
-
-bool testBitInRange(const uint8_t arr[], size_t start, size_t end) {
-#if DEBUG_KEY_RANGE
-    ALOGD("testBitInRange(%d, %d)", start, end);
-#endif
-    // Invalid range! This is nonsense; just say no.
-    if (end <= start) return false;
-
-    // Find byte array indices. The end is not included in the range, nor is
-    // endIndex. Round up for endIndex.
-    size_t startIndex = start / 8;
-    size_t endIndex = (end + 7) / 8;
-#if DEBUG_KEY_RANGE
-    ALOGD("startIndex=%d, endIndex=%d", startIndex, endIndex);
-#endif
-    for (size_t i = startIndex; i < endIndex; ++i) {
-        uint8_t bits = arr[i];
-        uint8_t mask = 0xff;
-#if DEBUG_KEY_RANGE
-        ALOGD("block %04d: %s%s", i, bitstrings[bits >> 4], bitstrings[bits & 0x0f]);
-#endif
-        if (bits) {
-            // Mask off bits before our start bit
-            if (i == startIndex) {
-                mask &= 0xff << (start % 8);
-            }
-            // Mask off bits after our end bit
-            if (i == endIndex - 1 && (end % 8)) {
-                mask &= 0xff >> (8 - (end % 8));
-            }
-#if DEBUG_KEY_RANGE
-            ALOGD("mask: %s%s", bitstrings[mask >> 4], bitstrings[mask & 0x0f]);
-#endif
-            // Test the index against the mask
-            if (bits & mask) return true;
-        }
-    }
-    return false;
-}
-}  // namespace internal
-
 static void getLinuxRelease(int* major, int* minor) {
     struct utsname info;
     if (uname(&info) || sscanf(info.release, "%d.%d", major, minor) <= 0) {
@@ -331,7 +278,7 @@
 }
 
 bool EvdevDeviceNode::hasKeyInRange(int32_t startKey, int32_t endKey) const {
-    return internal::testBitInRange(mKeyBitmask, startKey, endKey);
+    return testBitInRange(mKeyBitmask, startKey, endKey);
 }
 
 bool EvdevDeviceNode::hasRelativeAxis(int axis) const {
diff --git a/modules/input/evdev/InputMapper.cpp b/modules/input/evdev/InputMapper.cpp
index 2353a9a..3893125 100644
--- a/modules/input/evdev/InputMapper.cpp
+++ b/modules/input/evdev/InputMapper.cpp
@@ -16,6 +16,8 @@
 
 #include "InputMapper.h"
 
+#include "InputHost.h"
+
 namespace android {
 
 InputReport* InputMapper::getInputReport() {
diff --git a/modules/input/evdev/InputMapper.h b/modules/input/evdev/InputMapper.h
index b33cb63..5e88d06 100644
--- a/modules/input/evdev/InputMapper.h
+++ b/modules/input/evdev/InputMapper.h
@@ -17,11 +17,16 @@
 #ifndef ANDROID_INPUT_MAPPER_H_
 #define ANDROID_INPUT_MAPPER_H_
 
-#include "InputHost.h"
-#include "InputHub.h"
+struct input_device_handle;
 
 namespace android {
 
+class InputDeviceNode;
+class InputReport;
+class InputReportDefinition;
+struct InputEvent;
+using InputDeviceHandle = struct input_device_handle;
+
 /**
  * An InputMapper processes raw evdev input events and combines them into
  * Android input HAL reports. A given InputMapper will focus on a particular
@@ -31,7 +36,8 @@
  */
 class InputMapper {
 public:
-    virtual ~InputMapper() = default;
+    InputMapper() = default;
+    virtual ~InputMapper() {}
 
     /**
      * If the mapper supports input events from the InputDevice,
diff --git a/tests/input/evdev/Android.mk b/tests/input/evdev/Android.mk
index a79de34..1d8bf43 100644
--- a/tests/input/evdev/Android.mk
+++ b/tests/input/evdev/Android.mk
@@ -5,6 +5,7 @@
 LOCAL_C_INCLUDES += $(TOP)/external/gmock/include
 
 LOCAL_SRC_FILES:= \
+    BitUtils_test.cpp \
     InputDevice_test.cpp \
     InputHub_test.cpp \
     InputMocks.cpp \
diff --git a/tests/input/evdev/BitUtils_test.cpp b/tests/input/evdev/BitUtils_test.cpp
new file mode 100644
index 0000000..76fc8af
--- /dev/null
+++ b/tests/input/evdev/BitUtils_test.cpp
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "BitUtils.h"
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace tests {
+
+TEST(BitInRange, testInvalidRange) {
+    uint8_t arr[2] = { 0xff, 0xff };
+    EXPECT_FALSE(testBitInRange(arr, 0, 0));
+    EXPECT_FALSE(testBitInRange(arr, 1, 0));
+}
+
+TEST(BitInRange, testNoBits) {
+    uint8_t arr[1];
+    arr[0] = 0;
+    EXPECT_FALSE(testBitInRange(arr, 0, 8));
+}
+
+TEST(BitInRange, testOneBit) {
+    uint8_t arr[1];
+    for (int i = 0; i < 8; ++i) {
+        arr[0] = 1 << i;
+        EXPECT_TRUE(testBitInRange(arr, 0, 8));
+    }
+}
+
+TEST(BitInRange, testZeroStart) {
+    uint8_t arr[1] = { 0x10 };
+    for (int i = 0; i < 5; ++i) {
+        EXPECT_FALSE(testBitInRange(arr, 0, i));
+    }
+    for (int i = 5; i <= 8; ++i) {
+        EXPECT_TRUE(testBitInRange(arr, 0, i));
+    }
+}
+
+TEST(BitInRange, testByteBoundaryEnd) {
+    uint8_t arr[1] = { 0x10 };
+    for (int i = 0; i < 5; ++i) {
+        EXPECT_TRUE(testBitInRange(arr, i, 8));
+    }
+    for (int i = 5; i <= 8; ++i) {
+        EXPECT_FALSE(testBitInRange(arr, i, 8));
+    }
+}
+
+TEST(BitInRange, testMultiByteArray) {
+    // bits set: 11 and 16
+    uint8_t arr[3] = { 0x00, 0x08, 0x01 };
+    for (int start = 0; start < 24; ++start) {
+        for (int end = start + 1; end <= 24; ++end) {
+            if (start > 16 || end <= 11 || (start > 11 && end <= 16)) {
+                EXPECT_FALSE(testBitInRange(arr, start, end))
+                    << "range = (" << start << ", " << end << ")";
+            } else {
+                EXPECT_TRUE(testBitInRange(arr, start, end))
+                    << "range = (" << start << ", " << end << ")";
+            }
+        }
+    }
+}
+
+}  // namespace tests
+}  // namespace android
diff --git a/tests/input/evdev/InputDevice_test.cpp b/tests/input/evdev/InputDevice_test.cpp
index bed05b8..8d6aa68 100644
--- a/tests/input/evdev/InputDevice_test.cpp
+++ b/tests/input/evdev/InputDevice_test.cpp
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "InputHub_test"
-//#define LOG_NDEBUG 0
+#include "InputDevice.h"
 
 #include <memory>
 
@@ -25,7 +24,6 @@
 
 #include <utils/Timers.h>
 
-#include "InputDevice.h"
 #include "InputHub.h"
 #include "InputMocks.h"
 #include "MockInputHost.h"
diff --git a/tests/input/evdev/InputHub_test.cpp b/tests/input/evdev/InputHub_test.cpp
index 5864a95..64671ff 100644
--- a/tests/input/evdev/InputHub_test.cpp
+++ b/tests/input/evdev/InputHub_test.cpp
@@ -14,23 +14,19 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "InputHub_test"
-//#define LOG_NDEBUG 0
-
-#include <linux/input.h>
+#include "InputHub.h"
 
 #include <chrono>
 #include <memory>
 #include <mutex>
 
+#include <linux/input.h>
+
 #include <gtest/gtest.h>
 
-#include <utils/Log.h>
 #include <utils/StopWatch.h>
 #include <utils/Timers.h>
 
-#include "InputHub.h"
-#include "InputHub-internal.h"
 #include "TestHelpers.h"
 
 // # of milliseconds to fudge stopwatch measurements
@@ -257,63 +253,5 @@
     EXPECT_TRUE(deviceCallbackFinished);
 }
 
-using internal::testBitInRange;
-
-TEST(BitInRange, testInvalidRange) {
-    uint8_t arr[2] = { 0xff, 0xff };
-    EXPECT_FALSE(testBitInRange(arr, 0, 0));
-    EXPECT_FALSE(testBitInRange(arr, 1, 0));
-}
-
-TEST(BitInRange, testNoBits) {
-    uint8_t arr[1];
-    arr[0] = 0;
-    EXPECT_FALSE(testBitInRange(arr, 0, 8));
-}
-
-TEST(BitInRange, testOneBit) {
-    uint8_t arr[1];
-    for (int i = 0; i < 8; ++i) {
-        arr[0] = 1 << i;
-        EXPECT_TRUE(testBitInRange(arr, 0, 8));
-    }
-}
-
-TEST(BitInRange, testZeroStart) {
-    uint8_t arr[1] = { 0x10 };
-    for (int i = 0; i < 5; ++i) {
-        EXPECT_FALSE(testBitInRange(arr, 0, i));
-    }
-    for (int i = 5; i <= 8; ++i) {
-        EXPECT_TRUE(testBitInRange(arr, 0, i));
-    }
-}
-
-TEST(BitInRange, testByteBoundaryEnd) {
-    uint8_t arr[1] = { 0x10 };
-    for (int i = 0; i < 5; ++i) {
-        EXPECT_TRUE(testBitInRange(arr, i, 8));
-    }
-    for (int i = 5; i <= 8; ++i) {
-        EXPECT_FALSE(testBitInRange(arr, i, 8));
-    }
-}
-
-TEST(BitInRange, testMultiByteArray) {
-    // bits set: 11 and 16
-    uint8_t arr[3] = { 0x00, 0x08, 0x01 };
-    for (int start = 0; start < 24; ++start) {
-        for (int end = start + 1; end <= 24; ++end) {
-            if (start > 16 || end <= 11 || (start > 11 && end <= 16)) {
-                EXPECT_FALSE(testBitInRange(arr, start, end))
-                    << "range = (" << start << ", " << end << ")";
-            } else {
-                EXPECT_TRUE(testBitInRange(arr, start, end))
-                    << "range = (" << start << ", " << end << ")";
-            }
-        }
-    }
-}
-
 }  // namespace tests
 }  // namespace android
diff --git a/tests/input/evdev/InputMocks.cpp b/tests/input/evdev/InputMocks.cpp
index f4a341a..bd09a3d 100644
--- a/tests/input/evdev/InputMocks.cpp
+++ b/tests/input/evdev/InputMocks.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include "InputMocks.h"
 
 namespace android {
diff --git a/tests/input/evdev/MockInputHost.h b/tests/input/evdev/MockInputHost.h
index a91f95c..aae0564 100644
--- a/tests/input/evdev/MockInputHost.h
+++ b/tests/input/evdev/MockInputHost.h
@@ -17,9 +17,10 @@
 #ifndef ANDROID_MOCK_INPUT_HOST_H_
 #define ANDROID_MOCK_INPUT_HOST_H_
 
+#include "InputHost.h"
+
 #include "gmock/gmock.h"
 
-#include "InputHost.h"
 
 namespace android {
 namespace tests {
diff --git a/tests/input/evdev/TestHelpers.cpp b/tests/input/evdev/TestHelpers.cpp
index 63b579e..9898a6f 100644
--- a/tests/input/evdev/TestHelpers.cpp
+++ b/tests/input/evdev/TestHelpers.cpp
@@ -17,6 +17,8 @@
 #define LOG_TAG "TestHelpers"
 #define LOG_NDEBUG 0
 
+#include "TestHelpers.h"
+
 #include <dirent.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -26,8 +28,6 @@
 
 #include <utils/Log.h>
 
-#include "TestHelpers.h"
-
 namespace android {
 
 static const char kTmpDirTemplate[] = "/data/local/tmp/XXXXXX";