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

Commit a1dff771 authored by Etan Cohen's avatar Etan Cohen
Browse files

[P2P] Public API to clean-up resources of P2P

1. Add public API to close a Channel and allow configuration to be
   cleaned-up. Actual clean-up will only happen when the last p2p
   client executes close or terminates (binder death is triggered
   for the service).
2. Add Close Guard to verify that API is called - issue warning
   otherwise. Note that to actually get the warning an app needs
   to use the StrictMode policy:

   StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
       .detectLeakedClosableObjects().build());

Bug: 37443149
Test: (new) unit tests, CtsVerifier, Settings/WFD app
Change-Id: I9590101ef7f7ba0a90812634ac1b931d1482fe72
parent 520caf4a
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -27070,7 +27070,8 @@ package android.net.wifi.p2p {
    method public abstract void onSuccess();
    method public abstract void onSuccess();
  }
  }
  public static class WifiP2pManager.Channel {
  public static class WifiP2pManager.Channel implements java.lang.AutoCloseable {
    method public void close();
  }
  }
  public static abstract interface WifiP2pManager.ChannelListener {
  public static abstract interface WifiP2pManager.ChannelListener {
+2 −1
Original line number Original line Diff line number Diff line
@@ -29866,7 +29866,8 @@ package android.net.wifi.p2p {
    method public abstract void onSuccess();
    method public abstract void onSuccess();
  }
  }
  public static class WifiP2pManager.Channel {
  public static class WifiP2pManager.Channel implements java.lang.AutoCloseable {
    method public void close();
  }
  }
  public static abstract interface WifiP2pManager.ChannelListener {
  public static abstract interface WifiP2pManager.ChannelListener {
+2 −1
Original line number Original line Diff line number Diff line
@@ -27181,7 +27181,8 @@ package android.net.wifi.p2p {
    method public abstract void onSuccess();
    method public abstract void onSuccess();
  }
  }
  public static class WifiP2pManager.Channel {
  public static class WifiP2pManager.Channel implements java.lang.AutoCloseable {
    method public void close();
  }
  }
  public static abstract interface WifiP2pManager.ChannelListener {
  public static abstract interface WifiP2pManager.ChannelListener {
+47 −21
Original line number Original line Diff line number Diff line
@@ -41,6 +41,8 @@ import android.util.Log;
import com.android.internal.util.AsyncChannel;
import com.android.internal.util.AsyncChannel;
import com.android.internal.util.Protocol;
import com.android.internal.util.Protocol;


import dalvik.system.CloseGuard;

import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Map;
@@ -668,15 +670,21 @@ public class WifiP2pManager {
     * Most p2p operations require a Channel as an argument. An instance of Channel is obtained
     * Most p2p operations require a Channel as an argument. An instance of Channel is obtained
     * by doing a call on {@link #initialize}
     * by doing a call on {@link #initialize}
     */
     */
    public static class Channel {
    public static class Channel implements AutoCloseable {
        Channel(Context context, Looper looper, ChannelListener l, Binder binder) {
        /** @hide */
        public Channel(Context context, Looper looper, ChannelListener l, Binder binder,
                WifiP2pManager p2pManager) {
            mAsyncChannel = new AsyncChannel();
            mAsyncChannel = new AsyncChannel();
            mHandler = new P2pHandler(looper);
            mHandler = new P2pHandler(looper);
            mChannelListener = l;
            mChannelListener = l;
            mContext = context;
            mContext = context;
            mBinder = binder;
            mBinder = binder;
            mP2pManager = p2pManager;

            mCloseGuard.open("close");
        }
        }
        private final static int INVALID_LISTENER_KEY = 0;
        private final static int INVALID_LISTENER_KEY = 0;
        private final WifiP2pManager mP2pManager;
        private ChannelListener mChannelListener;
        private ChannelListener mChannelListener;
        private ServiceResponseListener mServRspListener;
        private ServiceResponseListener mServRspListener;
        private DnsSdServiceResponseListener mDnsSdServRspListener;
        private DnsSdServiceResponseListener mDnsSdServRspListener;
@@ -686,6 +694,41 @@ public class WifiP2pManager {
        private final Object mListenerMapLock = new Object();
        private final Object mListenerMapLock = new Object();
        private int mListenerKey = 0;
        private int mListenerKey = 0;


        private final CloseGuard mCloseGuard = CloseGuard.get();

        /**
         * Close the current P2P connection and indicate to the P2P service that connections
         * created by the app can be removed.
         */
        public void close() {
            if (mP2pManager == null) {
                Log.w(TAG, "Channel.close(): Null mP2pManager!?");
            } else {
                try {
                    mP2pManager.mService.close(mBinder);
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }

            mAsyncChannel.disconnect();
            mCloseGuard.close();
        }

        /** @hide */
        @Override
        protected void finalize() throws Throwable {
            try {
                if (mCloseGuard != null) {
                    mCloseGuard.warnIfOpen();
                }

                close();
            } finally {
                super.finalize();
            }
        }

        /* package */ final Binder mBinder;
        /* package */ final Binder mBinder;


        private AsyncChannel mAsyncChannel;
        private AsyncChannel mAsyncChannel;
@@ -913,11 +956,12 @@ public class WifiP2pManager {
                                     Messenger messenger, Binder binder) {
                                     Messenger messenger, Binder binder) {
        if (messenger == null) return null;
        if (messenger == null) return null;


        Channel c = new Channel(srcContext, srcLooper, listener, binder);
        Channel c = new Channel(srcContext, srcLooper, listener, binder, this);
        if (c.mAsyncChannel.connectSync(srcContext, c.mHandler, messenger)
        if (c.mAsyncChannel.connectSync(srcContext, c.mHandler, messenger)
                == AsyncChannel.STATUS_SUCCESSFUL) {
                == AsyncChannel.STATUS_SUCCESSFUL) {
            return c;
            return c;
        } else {
        } else {
            c.close();
            return null;
            return null;
        }
        }
    }
    }
@@ -1421,24 +1465,6 @@ public class WifiP2pManager {
        }
        }
    }
    }


    /**
     * Close the current P2P connection and clean-up any configuration requested by the
     * current app. Takes same action as taken when the app dies.
     *
     * @param c is the channel created at {@link #initialize}
     *
     * @hide
     */
    public void close(Channel c) {
        try {
            if (c != null) {
                mService.close(c.mBinder);
            }
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
    /**
     * Get a handover request message for use in WFA NFC Handover transfer.
     * Get a handover request message for use in WFA NFC Handover transfer.
     * @hide
     * @hide
+1 −0
Original line number Original line Diff line number Diff line
@@ -50,6 +50,7 @@ LOCAL_JACK_COVERAGE_EXCLUDE_FILTER := $(jacoco_exclude)


LOCAL_STATIC_JAVA_LIBRARIES := \
LOCAL_STATIC_JAVA_LIBRARIES := \
	android-support-test \
	android-support-test \
	core-test-rules \
	guava \
	guava \
	mockito-target-minus-junit4 \
	mockito-target-minus-junit4 \
	frameworks-base-testutils \
	frameworks-base-testutils \
Loading