Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd3d75f3 authored by Irfan Sheriff's avatar Irfan Sheriff
Browse files

Re-use existing thread

Bug: 7377541
Change-Id: I097f2288ebfc2b4a9782cbced19d9c36d0703acb
parent 511d5346
Loading
Loading
Loading
Loading
+16 −5
Original line number Diff line number Diff line
@@ -508,6 +508,10 @@ public class WifiManager {
    private Messenger mWifiServiceMessenger;
    private final CountDownLatch mConnected = new CountDownLatch(1);

    private static Object sThreadRefLock = new Object();
    private static int sThreadRefCount;
    private static HandlerThread sHandlerThread;

    /**
     * Create a new WifiManager instance.
     * Applications will almost always want to use
@@ -1365,9 +1369,14 @@ public class WifiManager {
            return;
        }

        HandlerThread t = new HandlerThread("WifiManager");
        t.start();
        mHandler = new ServiceHandler(t.getLooper());
        synchronized (sThreadRefLock) {
            if (++sThreadRefCount == 1) {
                sHandlerThread = new HandlerThread("WifiManager");
                sHandlerThread.start();
            }
        }

        mHandler = new ServiceHandler(sHandlerThread.getLooper());
        mAsyncChannel.connect(mContext, mHandler, mWifiServiceMessenger);
        try {
            mConnected.await();
@@ -1983,8 +1992,10 @@ public class WifiManager {

    protected void finalize() throws Throwable {
        try {
            if (mHandler != null && mHandler.getLooper() != null) {
                mHandler.getLooper().quit();
            synchronized (sThreadRefLock) {
                if (--sThreadRefCount == 0 && sHandlerThread != null) {
                    sHandlerThread.getLooper().quit();
                }
            }
        } finally {
            super.finalize();