Subclass Handler to log incoming messages

A new class WifiHandler that overrides handleMessage() method to log
incoming messages and associated unit tests. In order to instantiate
WifiLog, an instance of WifiInjector is required. WifiHandler is
expected to be used by any service in wifi, including WifiP2pService.
Since this service is started before WifiService is started,
WifiInjector which is created in WifiServiceImpl would not be available
to instantiate WifiHandler if is required in the constructor. For now,
we use lazy initialization and invoke WifiInjector.getInstance() once
to get the WifiInjector and then make a WifiLog object the first time
a message is logged. In order to enable testing of this class, a hidden
API is exposed only for testing to set the logging field in the class.

Bug: 33085782
Test: Unit test suite, Sanity tests (power on, Wifi connection)

Change-Id: I9110eacb28a5faea331a818998ef25295aa589a8
diff --git a/service/java/com/android/server/wifi/util/WifiHandler.java b/service/java/com/android/server/wifi/util/WifiHandler.java
new file mode 100644
index 0000000..8750601
--- /dev/null
+++ b/service/java/com/android/server/wifi/util/WifiHandler.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+package com.android.server.wifi.util;
+
+import android.annotation.NonNull;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import com.android.server.wifi.WifiInjector;
+import com.android.server.wifi.WifiLog;
+
+/**
+ * This class subclasses Handler to log incoming messages
+ */
+public class WifiHandler extends Handler {
+    private static final String LOG_TAG = "WifiHandler";
+    private WifiLog mLog;
+    private String mTag;
+
+    public WifiHandler(String tag, Looper looper) {
+        super(looper);
+        mTag = LOG_TAG + "." + tag;
+    }
+
+    @NonNull
+    private WifiLog getOrInitLog() {
+        // Lazy initialization of mLog
+        if (mLog == null) {
+            mLog = WifiInjector.getInstance().makeLog(mTag);
+        }
+        return mLog;
+    }
+
+    @Override
+    public void handleMessage(Message msg) {
+        getOrInitLog().trace("Received message=%d sendingUid=%")
+                .c(msg.what)
+                .c(msg.sendingUid)
+                .flush();
+    }
+
+    /**
+     * @hide
+     */
+    @VisibleForTesting
+    public void setWifiLog(WifiLog wifiLog) {
+        // TODO WifiInjector should be passed as a variable in the constructor
+        // b/33308811 tracks removing lazy initializations of mLog
+        mLog = wifiLog;
+    }
+}
diff --git a/service/tests/wifitests/src/com/android/server/wifi/util/WifiHandlerTest.java b/service/tests/wifitests/src/com/android/server/wifi/util/WifiHandlerTest.java
new file mode 100644
index 0000000..220e6d7
--- /dev/null
+++ b/service/tests/wifitests/src/com/android/server/wifi/util/WifiHandlerTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+package com.android.server.wifi.util;
+
+import android.os.Looper;
+import android.os.Message;
+import android.os.test.TestLooper;
+
+import com.android.server.wifi.FakeWifiLog;
+
+import static org.mockito.Matchers.contains;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
+
+/** Unit tests for {@link WifiHandler}. */
+@RunWith(JUnit4.class)
+public class WifiHandlerTest {
+    private static final String TAG = "WifiHandlerTest";
+    private WifiHandler mCodeUnderTest;
+    @Spy FakeWifiLog mWifiLog;
+    TestLooper mLooper;
+
+    private class WifiHandlerTestClass extends WifiHandler {
+        WifiHandlerTestClass(String tag, Looper looper) {
+            super(tag, looper);
+            super.setWifiLog(mWifiLog);
+        }
+    }
+
+    @Before public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mLooper = new TestLooper();
+        mCodeUnderTest = new WifiHandlerTestClass(TAG, mLooper.getLooper());
+    }
+
+    @Test public void testHandleMessage() {
+        Message msg = Message.obtain();
+        msg.what = 0;
+        msg.sendingUid = 0;
+        mCodeUnderTest.handleMessage(msg);
+        verify(mWifiLog).trace(contains("message"));
+    }
+}