Merge "Replace cutils/log.h and log/logger.h with android/log.h or log/log.h (part deux)"
diff --git a/api/current.txt b/api/current.txt
index 2539699..f1058bf 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -62752,10 +62752,12 @@
     method public java.lang.StringBuffer appendTail(java.lang.StringBuffer);
     method public int end();
     method public int end(int);
+    method public int end(java.lang.String);
     method public boolean find();
     method public boolean find(int);
     method public java.lang.String group();
     method public java.lang.String group(int);
+    method public java.lang.String group(java.lang.String);
     method public int groupCount();
     method public boolean hasAnchoringBounds();
     method public boolean hasTransparentBounds();
@@ -62774,6 +62776,7 @@
     method public java.util.regex.Matcher reset(java.lang.CharSequence);
     method public int start();
     method public int start(int) throws java.lang.IllegalStateException;
+    method public int start(java.lang.String);
     method public java.util.regex.MatchResult toMatchResult();
     method public java.util.regex.Matcher useAnchoringBounds(boolean);
     method public java.util.regex.Matcher usePattern(java.util.regex.Pattern);
diff --git a/api/system-current.txt b/api/system-current.txt
index ecebad5..96b8f1e 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -27039,6 +27039,7 @@
     method public static int calculateSignalLevel(int, int);
     method public void cancelWps(android.net.wifi.WifiManager.WpsCallback);
     method public static int compareSignalLevel(int, int);
+    method public void connect(android.net.wifi.WifiConfiguration, android.net.wifi.WifiManager.ActionListener);
     method public android.net.wifi.WifiManager.MulticastLock createMulticastLock(java.lang.String);
     method public android.net.wifi.WifiManager.WifiLock createWifiLock(int, java.lang.String);
     method public android.net.wifi.WifiManager.WifiLock createWifiLock(java.lang.String);
@@ -27138,6 +27139,11 @@
     field public static final int WPS_WEP_PROHIBITED = 4; // 0x4
   }
 
+  public static abstract interface WifiManager.ActionListener {
+    method public abstract void onFailure(int);
+    method public abstract void onSuccess();
+  }
+
   public class WifiManager.MulticastLock {
     method public void acquire();
     method public boolean isHeld();
@@ -66344,10 +66350,12 @@
     method public java.lang.StringBuffer appendTail(java.lang.StringBuffer);
     method public int end();
     method public int end(int);
+    method public int end(java.lang.String);
     method public boolean find();
     method public boolean find(int);
     method public java.lang.String group();
     method public java.lang.String group(int);
+    method public java.lang.String group(java.lang.String);
     method public int groupCount();
     method public boolean hasAnchoringBounds();
     method public boolean hasTransparentBounds();
@@ -66366,6 +66374,7 @@
     method public java.util.regex.Matcher reset(java.lang.CharSequence);
     method public int start();
     method public int start(int) throws java.lang.IllegalStateException;
+    method public int start(java.lang.String);
     method public java.util.regex.MatchResult toMatchResult();
     method public java.util.regex.Matcher useAnchoringBounds(boolean);
     method public java.util.regex.Matcher usePattern(java.util.regex.Pattern);
diff --git a/api/test-current.txt b/api/test-current.txt
index 490d499..a26672a 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -62843,10 +62843,12 @@
     method public java.lang.StringBuffer appendTail(java.lang.StringBuffer);
     method public int end();
     method public int end(int);
+    method public int end(java.lang.String);
     method public boolean find();
     method public boolean find(int);
     method public java.lang.String group();
     method public java.lang.String group(int);
+    method public java.lang.String group(java.lang.String);
     method public int groupCount();
     method public boolean hasAnchoringBounds();
     method public boolean hasTransparentBounds();
@@ -62865,6 +62867,7 @@
     method public java.util.regex.Matcher reset(java.lang.CharSequence);
     method public int start();
     method public int start(int) throws java.lang.IllegalStateException;
+    method public int start(java.lang.String);
     method public java.util.regex.MatchResult toMatchResult();
     method public java.util.regex.Matcher useAnchoringBounds(boolean);
     method public java.util.regex.Matcher usePattern(java.util.regex.Pattern);
diff --git a/core/jni/android_os_HwBinder.cpp b/core/jni/android_os_HwBinder.cpp
index 20bb885..e653900 100644
--- a/core/jni/android_os_HwBinder.cpp
+++ b/core/jni/android_os_HwBinder.cpp
@@ -26,7 +26,7 @@
 #include <JNIHelp.h>
 #include <android/hidl/manager/1.0/IServiceManager.h>
 #include <android/hidl/base/1.0/IBase.h>
-#include <android/hidl/base/1.0/BpBase.h>
+#include <android/hidl/base/1.0/BpHwBase.h>
 #include <android_runtime/AndroidRuntime.h>
 #include <hidl/ServiceManagement.h>
 #include <hidl/Status.h>
@@ -262,7 +262,7 @@
     sp<hardware::IBinder> binder = JHwBinder::GetNativeContext(env, thiz);
 
     /* TODO(b/33440494) this is not right */
-    sp<hidl::base::V1_0::IBase> base = new hidl::base::V1_0::BpBase(binder);
+    sp<hidl::base::V1_0::IBase> base = new hidl::base::V1_0::BpHwBase(binder);
 
     auto manager = hardware::defaultServiceManager();
 
@@ -331,7 +331,7 @@
     }
 
     sp<hardware::IBinder> service = hardware::toBinder<
-            hidl::base::V1_0::IBase, hidl::base::V1_0::BpBase>(ret);
+            hidl::base::V1_0::IBase, hidl::base::V1_0::BpHwBase>(ret);
 
     env->ReleaseStringUTFChars(ifaceNameObj, ifaceName);
     ifaceName = NULL;
diff --git a/core/jni/android_os_HwParcel.cpp b/core/jni/android_os_HwParcel.cpp
index b9d810a..aefdc84 100644
--- a/core/jni/android_os_HwParcel.cpp
+++ b/core/jni/android_os_HwParcel.cpp
@@ -568,15 +568,16 @@
 
     size_t parentHandle;
 
-    const hidl_string *s = static_cast<const hidl_string *>(
-            parcel->readBuffer(&parentHandle));
+    const hidl_string *s;
+    status_t err = parcel->readBuffer(&parentHandle,
+            reinterpret_cast<const void**>(&s));
 
-    if (s == NULL) {
-        signalExceptionForError(env, UNKNOWN_ERROR);
+    if (err != OK) {
+        signalExceptionForError(env, err);
         return NULL;
     }
 
-    status_t err = ::android::hardware::readEmbeddedFromParcel(
+    err = ::android::hardware::readEmbeddedFromParcel(
             const_cast<hidl_string *>(s),
             *parcel, parentHandle, 0 /* parentOffset */);
 
@@ -593,20 +594,20 @@
         JNIEnv *env, jobject thiz) {                                           \
     hardware::Parcel *parcel =                                                 \
         JHwParcel::GetNativeContext(env, thiz)->getParcel();                   \
-                                                                               \
     size_t parentHandle;                                                       \
                                                                                \
-    const hidl_vec<Type> *vec =                                                \
-        (const hidl_vec<Type> *)parcel->readBuffer(&parentHandle);             \
+    const hidl_vec<Type> *vec;                                                 \
+    status_t err = parcel->readBuffer(&parentHandle,                           \
+            reinterpret_cast<const void**>(&vec));                             \
                                                                                \
-    if (vec == NULL) {                                                         \
-        signalExceptionForError(env, UNKNOWN_ERROR);                           \
+    if (err != OK) {                                                           \
+        signalExceptionForError(env, err);                                     \
         return NULL;                                                           \
     }                                                                          \
                                                                                \
     size_t childHandle;                                                        \
                                                                                \
-    status_t err = ::android::hardware::readEmbeddedFromParcel(                \
+    err = ::android::hardware::readEmbeddedFromParcel(                         \
                 const_cast<hidl_vec<Type> *>(vec),                             \
                 *parcel,                                                       \
                 parentHandle,                                                  \
@@ -638,17 +639,18 @@
 
     size_t parentHandle;
 
-    const hidl_vec<bool> *vec =
-        (const hidl_vec<bool> *)parcel->readBuffer(&parentHandle);
+    const hidl_vec<bool> *vec;
+    status_t err = parcel->readBuffer(&parentHandle,
+            reinterpret_cast<const void**>(&vec));
 
-    if (vec == NULL) {
-        signalExceptionForError(env, UNKNOWN_ERROR);
+    if (err != OK) {
+        signalExceptionForError(env, err);
         return NULL;
     }
 
     size_t childHandle;
 
-    status_t err = ::android::hardware::readEmbeddedFromParcel(
+    err = ::android::hardware::readEmbeddedFromParcel(
                 const_cast<hidl_vec<bool> *>(vec),
                 *parcel,
                 parentHandle,
@@ -701,16 +703,17 @@
 
     size_t parentHandle;
 
-    const string_vec *vec=
-        (const string_vec *)parcel->readBuffer(&parentHandle);
+    const string_vec *vec;
+    status_t err = parcel->readBuffer(&parentHandle,
+            reinterpret_cast<const void **>(&vec));
 
-    if (vec == NULL) {
-        signalExceptionForError(env, UNKNOWN_ERROR);
+    if (err != OK) {
+        signalExceptionForError(env, err);
         return NULL;
     }
 
     size_t childHandle;
-    status_t err = ::android::hardware::readEmbeddedFromParcel(
+    err = ::android::hardware::readEmbeddedFromParcel(
             const_cast<string_vec *>(vec),
             *parcel, parentHandle, 0 /* parentOffset */, &childHandle);
 
@@ -807,9 +810,10 @@
         JHwParcel::GetNativeContext(env, thiz)->getParcel();
 
     size_t handle;
-    const void *ptr = parcel->readBuffer(&handle);
+    const void *ptr;
+    status_t status = parcel->readBuffer(&handle, &ptr);
 
-    if (ptr == nullptr) {
+    if (status != OK) {
         jniThrowException(env, "java/util/NoSuchElementException", NULL);
         return nullptr;
     }
@@ -824,10 +828,11 @@
 
     size_t childHandle;
 
-    const void *ptr =
-        parcel->readEmbeddedBuffer(&childHandle, parentHandle, offset);
+    const void *ptr;
+    status_t status =
+        parcel->readEmbeddedBuffer(&childHandle, parentHandle, offset, &ptr);
 
-    if (ptr == nullptr) {
+    if (status != OK) {
         jniThrowException(env, "java/util/NoSuchElementException", NULL);
         return 0;
     }
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index 79567d5..e336283 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -1077,15 +1077,11 @@
         }
 
         public void stop() {
-            if (mDefaultNetworkCallback != null) {
-                cm().unregisterNetworkCallback(mDefaultNetworkCallback);
-                mDefaultNetworkCallback = null;
-            }
+            releaseCallback(mDefaultNetworkCallback);
+            mDefaultNetworkCallback = null;
 
-            if (mDunTetheringCallback != null) {
-                cm().unregisterNetworkCallback(mDunTetheringCallback);
-                mDunTetheringCallback = null;
-            }
+            releaseCallback(mDunTetheringCallback);
+            mDunTetheringCallback = null;
 
             mNetworkMap.clear();
         }
@@ -1094,88 +1090,87 @@
             return (network != null) ? mNetworkMap.get(network) : null;
         }
 
-        public NetworkState processCallback(int arg1, Object obj) {
-            switch (arg1) {
-                case EVENT_ON_AVAILABLE: {
-                    final Network network = (Network) obj;
-                    if (VDBG) {
-                        Log.d(TAG, "EVENT_ON_AVAILABLE for " + network);
-                    }
-                    if (!mNetworkMap.containsKey(network)) {
-                        mNetworkMap.put(network,
-                                new NetworkState(null, null, null, network, null, null));
-                    }
-
-                    final ConnectivityManager cm = cm();
-
-                    if (mDefaultNetworkCallback != null) {
-                        cm.requestNetworkCapabilities(mDefaultNetworkCallback);
-                        cm.requestLinkProperties(mDefaultNetworkCallback);
-                    }
-
-                    // Requesting updates for mDunTetheringCallback is not
-                    // necessary. Because it's a listen, it will already have
-                    // heard all NetworkCapabilities and LinkProperties updates
-                    // since UpstreamNetworkMonitor was started. Because we
-                    // start UpstreamNetworkMonitor before chooseUpstreamType()
-                    // is ever invoked (it can register a DUN request) this is
-                    // mostly safe. However, if a DUN network is already up for
-                    // some reason (unlikely, because DUN is restricted and,
-                    // unless the DUN network is shared with another APN, only
-                    // the system can request it and this is the only part of
-                    // the system that requests it) we won't know its
-                    // LinkProperties or NetworkCapabilities.
-
-                    return mNetworkMap.get(network);
-                }
-                case EVENT_ON_CAPABILITIES: {
-                    final NetworkState ns = (NetworkState) obj;
-                    if (!mNetworkMap.containsKey(ns.network)) {
-                        // Ignore updates for networks for which we have not yet
-                        // received onAvailable() - which should never happen -
-                        // or for which we have already received onLost().
-                        return null;
-                    }
-                    if (VDBG) {
-                        Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s",
-                                ns.network, ns.networkCapabilities));
-                    }
-
-                    final NetworkState prev = mNetworkMap.get(ns.network);
-                    mNetworkMap.put(ns.network,
-                            new NetworkState(null, prev.linkProperties, ns.networkCapabilities,
-                                             ns.network, null, null));
-                    return mNetworkMap.get(ns.network);
-                }
-                case EVENT_ON_LINKPROPERTIES: {
-                    final NetworkState ns = (NetworkState) obj;
-                    if (!mNetworkMap.containsKey(ns.network)) {
-                        // Ignore updates for networks for which we have not yet
-                        // received onAvailable() - which should never happen -
-                        // or for which we have already received onLost().
-                        return null;
-                    }
-                    if (VDBG) {
-                        Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s",
-                                ns.network, ns.linkProperties));
-                    }
-
-                    final NetworkState prev = mNetworkMap.get(ns.network);
-                    mNetworkMap.put(ns.network,
-                            new NetworkState(null, ns.linkProperties, prev.networkCapabilities,
-                                             ns.network, null, null));
-                    return mNetworkMap.get(ns.network);
-                }
-                case EVENT_ON_LOST: {
-                    final Network network = (Network) obj;
-                    if (VDBG) {
-                        Log.d(TAG, "EVENT_ON_LOST for " + network);
-                    }
-                    return mNetworkMap.remove(network);
-                }
-                default:
-                    return null;
+        private void handleAvailable(Network network) {
+            if (VDBG) {
+                Log.d(TAG, "EVENT_ON_AVAILABLE for " + network);
             }
+            if (!mNetworkMap.containsKey(network)) {
+                mNetworkMap.put(network,
+                        new NetworkState(null, null, null, network, null, null));
+            }
+
+            final ConnectivityManager cm = cm();
+
+            if (mDefaultNetworkCallback != null) {
+                cm.requestNetworkCapabilities(mDefaultNetworkCallback);
+                cm.requestLinkProperties(mDefaultNetworkCallback);
+            }
+
+            // Requesting updates for mDunTetheringCallback is not
+            // necessary. Because it's a listen, it will already have
+            // heard all NetworkCapabilities and LinkProperties updates
+            // since UpstreamNetworkMonitor was started. Because we
+            // start UpstreamNetworkMonitor before chooseUpstreamType()
+            // is ever invoked (it can register a DUN request) this is
+            // mostly safe. However, if a DUN network is already up for
+            // some reason (unlikely, because DUN is restricted and,
+            // unless the DUN network is shared with another APN, only
+            // the system can request it and this is the only part of
+            // the system that requests it) we won't know its
+            // LinkProperties or NetworkCapabilities.
+
+            notifyTarget(EVENT_ON_AVAILABLE, network);
+        }
+
+        private void handleNetCap(Network network, NetworkCapabilities newNc) {
+            if (!mNetworkMap.containsKey(network)) {
+                // Ignore updates for networks for which we have not yet
+                // received onAvailable() - which should never happen -
+                // or for which we have already received onLost().
+                return;
+            }
+            if (VDBG) {
+                Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s",
+                        network, newNc));
+            }
+
+            final NetworkState prev = mNetworkMap.get(network);
+            mNetworkMap.put(network,
+                    new NetworkState(null, prev.linkProperties, newNc,
+                                     network, null, null));
+            notifyTarget(EVENT_ON_CAPABILITIES, network);
+        }
+
+        private void handleLinkProp(Network network, LinkProperties newLp) {
+            if (!mNetworkMap.containsKey(network)) {
+                // Ignore updates for networks for which we have not yet
+                // received onAvailable() - which should never happen -
+                // or for which we have already received onLost().
+                return;
+            }
+            if (VDBG) {
+                Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s",
+                        network, newLp));
+            }
+
+            final NetworkState prev = mNetworkMap.get(network);
+            mNetworkMap.put(network,
+                    new NetworkState(null, newLp, prev.networkCapabilities,
+                                     network, null, null));
+            notifyTarget(EVENT_ON_LINKPROPERTIES, network);
+        }
+
+        private void handleLost(Network network) {
+            if (!mNetworkMap.containsKey(network)) {
+                // Ignore updates for networks for which we have not yet
+                // received onAvailable() - which should never happen -
+                // or for which we have already received onLost().
+                return;
+            }
+            if (VDBG) {
+                Log.d(TAG, "EVENT_ON_LOST for " + network);
+            }
+            notifyTarget(EVENT_ON_LOST, mNetworkMap.remove(network));
         }
 
         // Fetch (and cache) a ConnectivityManager only if and when we need one.
@@ -1193,26 +1188,36 @@
         private class UpstreamNetworkCallback extends NetworkCallback {
             @Override
             public void onAvailable(Network network) {
-                mTarget.sendMessage(mWhat, EVENT_ON_AVAILABLE, 0, network);
+                mTarget.getHandler().post(() -> handleAvailable(network));
             }
 
             @Override
             public void onCapabilitiesChanged(Network network, NetworkCapabilities newNc) {
-                mTarget.sendMessage(mWhat, EVENT_ON_CAPABILITIES, 0,
-                        new NetworkState(null, null, newNc, network, null, null));
+                mTarget.getHandler().post(() -> handleNetCap(network, newNc));
             }
 
             @Override
             public void onLinkPropertiesChanged(Network network, LinkProperties newLp) {
-                mTarget.sendMessage(mWhat, EVENT_ON_LINKPROPERTIES, 0,
-                        new NetworkState(null, newLp, null, network, null, null));
+                mTarget.getHandler().post(() -> handleLinkProp(network, newLp));
             }
 
             @Override
             public void onLost(Network network) {
-                mTarget.sendMessage(mWhat, EVENT_ON_LOST, 0, network);
+                mTarget.getHandler().post(() -> handleLost(network));
             }
         }
+
+        private void releaseCallback(NetworkCallback cb) {
+            if (cb != null) cm().unregisterNetworkCallback(cb);
+        }
+
+        private void notifyTarget(int which, Network network) {
+            notifyTarget(which, mNetworkMap.get(network));
+        }
+
+        private void notifyTarget(int which, NetworkState netstate) {
+            mTarget.sendMessage(mWhat, which, 0, netstate);
+        }
     }
 
     // Needed because the canonical source of upstream truth is just the
@@ -1729,9 +1734,7 @@
                         mTryCell = !mTryCell;
                         break;
                     case EVENT_UPSTREAM_CALLBACK: {
-                        // First: always update local state about every network.
-                        final NetworkState ns = mUpstreamNetworkMonitor.processCallback(
-                                message.arg1, message.obj);
+                        final NetworkState ns = (NetworkState) message.obj;
 
                         if (ns == null || !pertainsToCurrentUpstream(ns)) {
                             // TODO: In future, this is where upstream evaluation and selection
diff --git a/services/tests/servicestests/Android.mk b/services/tests/servicestests/Android.mk
index 2187c57..336351e 100644
--- a/services/tests/servicestests/Android.mk
+++ b/services/tests/servicestests/Android.mk
@@ -30,7 +30,7 @@
 LOCAL_CERTIFICATE := platform
 
 # These are not normally accessible from apps so they must be explicitly included.
-LOCAL_JNI_SHARED_LIBRARIES := libservicestestsjni \
+LOCAL_JNI_SHARED_LIBRARIES := \
     libbacktrace \
     libbase \
     libbinder \
@@ -47,36 +47,3 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
 include $(BUILD_PACKAGE)
-
-#########################################################################
-# Build JNI Shared Library
-#########################################################################
-
-LOCAL_PATH:= $(LOCAL_PATH)/jni
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE_TAGS := tests
-
-LOCAL_CFLAGS := -Wall -Wextra -Werror
-
-LOCAL_C_INCLUDES := \
-  libpcap \
-  hardware/google/apf
-
-LOCAL_SRC_FILES := $(call all-cpp-files-under)
-
-LOCAL_SHARED_LIBRARIES := \
-  libbinder \
-  liblog \
-  libcutils \
-  libnativehelper \
-  libnetdaidl
-
-LOCAL_STATIC_LIBRARIES := \
-  libpcap \
-  libapf
-
-LOCAL_MODULE := libservicestestsjni
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/wifi/java/android/net/wifi/WifiManager.java b/wifi/java/android/net/wifi/WifiManager.java
index 618a719..88820cd 100644
--- a/wifi/java/android/net/wifi/WifiManager.java
+++ b/wifi/java/android/net/wifi/WifiManager.java
@@ -1765,6 +1765,7 @@
      * Interface for callback invocation on an application action
      * @hide
      */
+    @SystemApi
     public interface ActionListener {
         /** The operation succeeded */
         public void onSuccess();
@@ -1976,6 +1977,7 @@
      *
      * @hide
      */
+    @SystemApi
     public void connect(WifiConfiguration config, ActionListener listener) {
         if (config == null) throw new IllegalArgumentException("config cannot be null");
         // Use INVALID_NETWORK_ID for arg1 when passing a config object