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

Commit 907ce63e authored by Etan Cohen's avatar Etan Cohen Committed by Android Partner Code Review
Browse files

Merge "[NAN] Update finalize to check and warn user if resources not freed" into mm-wireless-dev

parents a3264c4e 9d556496
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ import android.util.SparseArray;

import com.android.internal.annotations.GuardedBy;

import dalvik.system.CloseGuard;

import libcore.util.HexEncoding;

import org.json.JSONException;
@@ -221,6 +223,7 @@ public class WifiNanManager {
    public static final int WIFI_NAN_DATA_PATH_ROLE_RESPONDER = 1;

    private final IWifiNanManager mService;
    private final CloseGuard mCloseGuard = CloseGuard.get();

    private final Object mLock = new Object(); // lock access to the following vars

@@ -332,6 +335,8 @@ public class WifiNanManager {
                e.rethrowAsRuntimeException();
            }
        }

        mCloseGuard.open("disconnect");
    }

    /**
@@ -361,6 +366,7 @@ public class WifiNanManager {
            mClientId = INVALID_CLIENT_ID;
        }

        mCloseGuard.close();
        try {
            mService.disconnect(clientId, binder);
        } catch (RemoteException e) {
@@ -370,9 +376,13 @@ public class WifiNanManager {

    @Override
    protected void finalize() throws Throwable {
        try {
            mCloseGuard.warnIfOpen();
            disconnect();
        } finally {
            super.finalize();
        }
    }

    /**
     * Request a NAN publish session. The actual publish session is provided by
+15 −6
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.annotation.SystemApi;
import android.net.wifi.RttManager;
import android.util.Log;

import dalvik.system.CloseGuard;

import java.lang.ref.WeakReference;

/**
@@ -52,6 +54,8 @@ public class WifiNanSession {
     */
    protected boolean mTerminated = false;

    private final CloseGuard mCloseGuard = CloseGuard.get();

    /**
     * {@hide}
     */
@@ -60,6 +64,8 @@ public class WifiNanSession {

        mMgr = new WeakReference<>(manager);
        mSessionId = sessionId;

        mCloseGuard.open("terminate");
    }

    /**
@@ -77,6 +83,7 @@ public class WifiNanSession {
        mgr.terminateSession(mSessionId);
        mTerminated = true;
        mMgr.clear();
        mCloseGuard.close();
    }

    /**
@@ -92,18 +99,20 @@ public class WifiNanSession {
        }
        mTerminated = true;
        mMgr.clear();
        mCloseGuard.close();
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            if (!mTerminated) {
            Log.w(TAG, "WifiNanSession mSessionId=" + mSessionId
                    + " was not explicitly terminated. The session may use resources until "
                    + "terminated so step should be done explicitly");
                mCloseGuard.warnIfOpen();
                terminate();
            }
        } finally {
            super.finalize();
        }
    }

    /**
     * Sends a message to the specified destination. Message transmission is part of the current