BatteryStatsImpl: Avoid memory corruption due to Parcel Recycling

While processing batterystatsfile, in present code there is a race where a parcel
might be recycled while it is being written to disk in another thread.

The write process is done in 2 steps:
- write into a Parcel (memory block)
- then write this Parcel to disk using Journaled API.
The Journaled API ensures robustness against file-system issues due to power cut.
The writing is done in batch using a thread running commitPendingDataToDisk().
This function writes the parcel to disk but this parcel is a global variable
initialized by writeLocked(). If the writing takes too long time and two write
threads are launched, the parcel is freed by the second writeLocked() call;
which could cause the memory corruption.

Hence, change global Parcel variable by a Parcel parameter to commitPendingDataToDisk()
and only this function will be allowed to recycle the parcel.

Orig-Change-Id: I0e76441399f1ce74b83566edb8cf4f977dce58bf

Change-Id: I1884141d7d9c61601e2c8e07523a6e9aa6709afc
Signed-off-by: Pavan Kumar S <pavan.kumar.s@intel.com>
Signed-off-by: Jian Luo <jian.luo@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
Signed-off-by: Jack Ren <jack.ren@intel.com>
Author-tracking-BZ: 47466
NsdService: Fix retrieving the client id

SparseArray.indexOfValue() doesn't work properly on Integer
objects, because its doing an Integer == Integer comparision.
This works for the first 127 client id's due to the boxing
implementation having static Integer objects from -128->127,
but fails on the 128th.

Walk through the clientInfo.mClientIds array manually instead.

This fixes exceptions in NsdManager of the pattern:
FATAL EXCEPTION: NsdManager
java.lang.NullPointerException
     at android.net.nsd.NsdManager$ServiceHandler.handleMessage(NsdManager.java:367)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:137)
     at android.os.HandlerThread.run(HandlerThread.java:61)

due to the client id/listener being -1.

Change-Id: I02a5392fb81f37f5da32f6e7d4619ace1ea6a0ef