This patch adds synchronization inside LegacyTypeTracker so that
getNetworkForType() can safely run concurrently with remove().
Without synchronization if remove() removes the last network for a
given type while getNetworkForType() runs for the same type, it is
possible that getNetworkForType tries to access the head of an empty
list, resulting in a runtime exception.
This issue was found by zoran.jovanovic@sonymobile.com who proposed a
fix in AOSP (Change-Id: Ia963662edb9d643790e8d9439e4dbdcac4c2187b).
This patch differs from the fix proposed by the bug reporter and tries
instead to do the minimum amount of locking to make getNetworkForType
safe.
Bug: 29030387
(cherry picked from commit 78caa25870391f676e1edbd448b5ff3e12a99a1e)
Change-Id: I915aac527fc8828b32bf35fee870add2dfb11d8d