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

Commit bc0edcaf authored by Jimmy Chen's avatar Jimmy Chen
Browse files

p2p: add WifiP2pConfig builder for new API

Creating/Joining a group with credentials needs additional
fields in WifiP2pConfig. Using builder to construct corresponding
WifiP2pConfig object

Bug: 64695709
Test: Unit tests - atest frameworks/opt/net/wifi/tests/wifitests
Change-Id: Ib01a7a1313fab3a7e3e9a0c6c9a10e419deed9d4
parent ba1c0174
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -29373,11 +29373,24 @@ package android.net.wifi.p2p {
    method public int describeContents();
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator<android.net.wifi.p2p.WifiP2pConfig> CREATOR;
    field public static final int GROUP_OWNER_BAND_2GHZ = 1; // 0x1
    field public static final int GROUP_OWNER_BAND_5GHZ = 2; // 0x2
    field public static final int GROUP_OWNER_BAND_AUTO = 0; // 0x0
    field public java.lang.String deviceAddress;
    field public int groupOwnerIntent;
    field public android.net.wifi.WpsInfo wps;
  }
  public static final class WifiP2pConfig.Builder {
    ctor public WifiP2pConfig.Builder();
    method public android.net.wifi.p2p.WifiP2pConfig build();
    method public android.net.wifi.p2p.WifiP2pConfig.Builder enablePersistentMode(boolean);
    method public android.net.wifi.p2p.WifiP2pConfig.Builder setDeviceAddress(android.net.MacAddress);
    method public android.net.wifi.p2p.WifiP2pConfig.Builder setGroupOwnerBand(int);
    method public android.net.wifi.p2p.WifiP2pConfig.Builder setNetworkName(java.lang.String);
    method public android.net.wifi.p2p.WifiP2pConfig.Builder setPassphrase(java.lang.String);
  }
  public class WifiP2pDevice implements android.os.Parcelable {
    ctor public WifiP2pDevice();
    ctor public WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice);
+190 −1
Original line number Diff line number Diff line
@@ -16,10 +16,16 @@

package android.net.wifi.p2p;

import android.annotation.IntDef;
import android.annotation.UnsupportedAppUsage;
import android.net.MacAddress;
import android.net.wifi.WpsInfo;
import android.os.Parcelable;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * A class representing a Wi-Fi P2p configuration for setting up a connection
@@ -38,12 +44,46 @@ public class WifiP2pConfig implements Parcelable {
     */
    public WpsInfo wps;

    /**
     * The network name of a group, should be configured by helper method
     */
    /** @hide */
    public String networkName = "";

    /**
     * The passphrase of a group, should be configured by helper method
     */
    /** @hide */
    public String passphrase = "";

    /**
     * The required band for Group Owner
     */
    /** @hide */
    public int groupOwnerBand = GROUP_OWNER_BAND_AUTO;

    /** @hide */
    public static final int MAX_GROUP_OWNER_INTENT   =   15;
    /** @hide */
    @UnsupportedAppUsage
    public static final int MIN_GROUP_OWNER_INTENT   =   0;

    /** @hide */
    @IntDef(flag = false, prefix = { "GROUP_OWNER_BAND_" }, value = {
        GROUP_OWNER_BAND_AUTO,
        GROUP_OWNER_BAND_2GHZ,
        GROUP_OWNER_BAND_5GHZ
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface GroupOwnerBandType {}

    /**
     * Recognized Group Owner required band.
     */
    public static final int GROUP_OWNER_BAND_AUTO = 0;
    public static final int GROUP_OWNER_BAND_2GHZ = 1;
    public static final int GROUP_OWNER_BAND_5GHZ = 2;

    /**
     * This is an integer value between 0 and 15 where 0 indicates the least
     * inclination to be a group owner and 15 indicates the highest inclination
@@ -115,6 +155,10 @@ public class WifiP2pConfig implements Parcelable {
        sbuf.append("\n wps: ").append(wps);
        sbuf.append("\n groupOwnerIntent: ").append(groupOwnerIntent);
        sbuf.append("\n persist: ").append(netId);
        sbuf.append("\n networkName: ").append(networkName);
        sbuf.append("\n passphrase: ").append(
                TextUtils.isEmpty(passphrase) ? "<empty>" : "<non-empty>");
        sbuf.append("\n groupOwnerBand: ").append(groupOwnerBand);
        return sbuf.toString();
    }

@@ -130,6 +174,9 @@ public class WifiP2pConfig implements Parcelable {
            wps = new WpsInfo(source.wps);
            groupOwnerIntent = source.groupOwnerIntent;
            netId = source.netId;
            networkName = source.networkName;
            passphrase = source.passphrase;
            groupOwnerBand = source.groupOwnerBand;
        }
    }

@@ -139,6 +186,9 @@ public class WifiP2pConfig implements Parcelable {
        dest.writeParcelable(wps, flags);
        dest.writeInt(groupOwnerIntent);
        dest.writeInt(netId);
        dest.writeString(networkName);
        dest.writeString(passphrase);
        dest.writeInt(groupOwnerBand);
    }

    /** Implement the Parcelable interface */
@@ -150,6 +200,9 @@ public class WifiP2pConfig implements Parcelable {
                config.wps = (WpsInfo) in.readParcelable(null);
                config.groupOwnerIntent = in.readInt();
                config.netId = in.readInt();
                config.networkName = in.readString();
                config.passphrase = in.readString();
                config.groupOwnerBand = in.readInt();
                return config;
            }

@@ -157,4 +210,140 @@ public class WifiP2pConfig implements Parcelable {
                return new WifiP2pConfig[size];
            }
        };

    /**
     * Builder used to build {@link WifiP2pConfig} objects for
     * creating or joining a group.
     */
    public static final class Builder {

        private static final MacAddress MAC_ANY_ADDRESS =
                MacAddress.fromString("00:00:00:00:00:00");

        private MacAddress mDeviceAddress = MAC_ANY_ADDRESS;
        private String mNetworkName = "";
        private String mPassphrase = "";
        private int mGroupOwnerBand = GROUP_OWNER_BAND_AUTO;
        private int mNetId = WifiP2pGroup.TEMPORARY_NET_ID;

        /**
         * Specify the peer's MAC address. If not set, the device will
         * try to find a peer whose SSID matches the network name as
         * specified by {@link #setNetworkName(String)}. Specifying null will
         * reset the peer's MAC address to "00:00:00:00:00:00".
         * <p>
         *     Optional. "00:00:00:00:00:00" by default.
         *
         * @param deviceAddress the peer's MAC address.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder setDeviceAddress(MacAddress deviceAddress) {
            if (deviceAddress == null) {
                mDeviceAddress = MAC_ANY_ADDRESS;
            } else {
                mDeviceAddress = deviceAddress;
            }
            return this;
        }

        /**
         * Specify the network name, a.k.a. group name,
         * for creating or joining a group.
         * <p>
         *     Must be called - an empty network name is not valid.
         *
         * @param networkName network name of a group.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder setNetworkName(String networkName) {
            if (TextUtils.isEmpty(networkName)) {
                throw new IllegalArgumentException(
                        "network name must be non-empty.");
            }
            mNetworkName = networkName;
            return this;
        }

        /**
         * Specify the passphrase for creating or joining a group.
         * <p>
         *     Must be called - an empty passphrase is not valid.
         *
         * @param passphrase the passphrase of a group.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder setPassphrase(String passphrase) {
            if (TextUtils.isEmpty(passphrase)) {
                throw new IllegalArgumentException(
                        "passphrase must be non-empty.");
            }
            mPassphrase = passphrase;
            return this;
        }

        /**
         * Specify the band to use for creating the group. This method only applies when
         * creating a group as Group Owner using {@link WifiP2pManager#createGroup}.
         * The band should be {@link #GROUP_OWNER_BAND_2GHZ} or {@link #GROUP_OWNER_BAND_5GHZ},
         * or allow the system to pick the band by specifying {@link #GROUP_OWNER_BAND_AUTO}.
         * If the Group Owner cannot create a group in the specified band, the operation will fail.
         * <p>
         *     Optional. {@link #GROUP_OWNER_BAND_AUTO} by default.
         *
         * @param band the required band of group owner.
         *             This should be one of {@link #GROUP_OWNER_BAND_AUTO},
         *             {@link #GROUP_OWNER_BAND_2GHZ}, {@link #GROUP_OWNER_BAND_5GHZ}.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder setGroupOwnerBand(int band) {
            mGroupOwnerBand = band;
            return this;
        }

        /**
         * Specify that the group configuration be persisted (i.e. saved).
         * By default the group configuration will not be saved.
         * <p>
         *     Optional. false by default.
         *
         * @param persistent is this group persistent group.
         * @return The builder to facilitate chaining
         *         {@code builder.setXXX(..).setXXX(..)}.
         */
        public Builder enablePersistentMode(boolean persistent) {
            if (persistent) {
                mNetId = WifiP2pGroup.PERSISTENT_NET_ID;
            } else {
                mNetId = WifiP2pGroup.TEMPORARY_NET_ID;
            }
            return this;
        }

        /**
         * Build {@link WifiP2pConfig} given the current requests made on the builder.
         * @return {@link WifiP2pConfig} constructed based on builder method calls.
         */
        public WifiP2pConfig build() {
            if (TextUtils.isEmpty(mNetworkName)) {
                throw new IllegalStateException(
                        "network name must be non-empty.");
            }
            if (TextUtils.isEmpty(mPassphrase)) {
                throw new IllegalStateException(
                        "passphrase must be non-empty.");
            }

            WifiP2pConfig config = new WifiP2pConfig();
            config.deviceAddress = mDeviceAddress.toString();
            config.networkName = mNetworkName;
            config.passphrase = mPassphrase;
            config.groupOwnerBand = mGroupOwnerBand;
            config.netId = mNetId;
            return config;
        }
    }
}