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

Commit 8b5662b5 authored by Nate Jiang's avatar Nate Jiang Committed by Android (Google) Code Review
Browse files

Merge "[NetworkSuggestion] add API to allow app share credential with user"

parents 8b542e36 f3b8e8e0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -30246,6 +30246,7 @@ package android.net.wifi {
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsEnhancedOpen(boolean);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsHiddenSsid(boolean);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsMetered(boolean);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsUserAllowedToManuallyConnect(boolean);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setIsUserInteractionRequired(boolean);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setPasspointConfig(@NonNull android.net.wifi.hotspot2.PasspointConfiguration);
    method @NonNull public android.net.wifi.WifiNetworkSuggestion.Builder setPriority(@IntRange(from=0) int);
+54 −0
Original line number Diff line number Diff line
@@ -116,6 +116,16 @@ public final class WifiNetworkSuggestion implements Parcelable {
         */
        private int mCarrierId;

        /**
         * Whether this network is shared credential with user to allow user manually connect.
         */
        private boolean mIsUserAllowed;

        /**
         * Whether the setIsUserAllowedToManuallyConnect have been called.
         */
        private boolean mIsUserAllowedBeenSet;

        public Builder() {
            mSsid = null;
            mBssid =  null;
@@ -129,6 +139,8 @@ public final class WifiNetworkSuggestion implements Parcelable {
            mIsAppInteractionRequired = false;
            mIsUserInteractionRequired = false;
            mIsMetered = false;
            mIsUserAllowed = true;
            mIsUserAllowedBeenSet = false;
            mPriority = UNASSIGNED_PRIORITY;
            mCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
        }
@@ -365,6 +377,27 @@ public final class WifiNetworkSuggestion implements Parcelable {
            return this;
        }

        /**
         * Specifies whether the network credentials provided with this suggestion can be used by
         * the user to explicitly (manually) connect to this network. If true this network will
         * appear in the Wi-Fi Picker (in Settings) and the user will be able to select and connect
         * to it with the provided credentials. If false, the user will need to enter network
         * credentials and the resulting configuration will become a user saved network.
         * <p>
         * <li>Note: Only valid for secure (non-open) networks.
         * <li>If not set, defaults to true (i.e. allow user to manually connect) for secure
         * networks and false for open networks.</li>
         *
         * @param isAllowed {@code true} to indicate that the credentials may be used by the user to
         *                              manually connect to the network, {@code false} otherwise.
         * @return Instance of {@link Builder} to enable chaining of the builder method.
         */
        public @NonNull Builder setIsUserAllowedToManuallyConnect(boolean isAllowed) {
            mIsUserAllowed = isAllowed;
            mIsUserAllowedBeenSet = true;
            return this;
        }

        private void setSecurityParamsInWifiConfiguration(
                @NonNull WifiConfiguration configuration) {
            if (!TextUtils.isEmpty(mWpa2PskPassphrase)) { // WPA-PSK network.
@@ -516,6 +549,13 @@ public final class WifiNetworkSuggestion implements Parcelable {
                    throw new IllegalStateException("invalid bssid for suggestion");
                }
                wifiConfiguration = buildWifiConfiguration();
                if (wifiConfiguration.isOpenNetwork()) {
                    if (mIsUserAllowedBeenSet && mIsUserAllowed) {
                        throw new IllegalStateException("Open network should not be "
                                + "setIsUserAllowedToManuallyConnect to true");
                    }
                    mIsUserAllowed = false;
                }
            }

            return new WifiNetworkSuggestion(
@@ -523,6 +563,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
                    mPasspointConfiguration,
                    mIsAppInteractionRequired,
                    mIsUserInteractionRequired,
                    mIsUserAllowed,
                    Process.myUid(),
                    ActivityThread.currentApplication().getApplicationContext().getOpPackageName());
        }
@@ -564,12 +605,20 @@ public final class WifiNetworkSuggestion implements Parcelable {
     */
    public final String suggestorPackageName;

    /**
     * Whether app share credential with the user, allow user use provided credential to
     * connect network manually.
     * @hide
     */
    public final boolean isUserAllowedToManuallyConnect;

    /** @hide */
    public WifiNetworkSuggestion() {
        this.wifiConfiguration = null;
        this.passpointConfiguration = null;
        this.isAppInteractionRequired = false;
        this.isUserInteractionRequired = false;
        this.isUserAllowedToManuallyConnect = true;
        this.suggestorUid = -1;
        this.suggestorPackageName = null;
    }
@@ -579,6 +628,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
                                 @Nullable PasspointConfiguration passpointConfiguration,
                                 boolean isAppInteractionRequired,
                                 boolean isUserInteractionRequired,
                                 boolean isUserAllowedToManuallyConnect,
                                 int suggestorUid, @NonNull String suggestorPackageName) {
        checkNotNull(networkConfiguration);
        checkNotNull(suggestorPackageName);
@@ -587,6 +637,7 @@ public final class WifiNetworkSuggestion implements Parcelable {

        this.isAppInteractionRequired = isAppInteractionRequired;
        this.isUserInteractionRequired = isUserInteractionRequired;
        this.isUserAllowedToManuallyConnect = isUserAllowedToManuallyConnect;
        this.suggestorUid = suggestorUid;
        this.suggestorPackageName = suggestorPackageName;
    }
@@ -600,6 +651,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
                            in.readParcelable(null), // PasspointConfiguration
                            in.readBoolean(), // isAppInteractionRequired
                            in.readBoolean(), // isUserInteractionRequired
                            in.readBoolean(), // isSharedCredentialWithUser
                            in.readInt(), // suggestorUid
                            in.readString() // suggestorPackageName
                    );
@@ -622,6 +674,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
        dest.writeParcelable(passpointConfiguration, flags);
        dest.writeBoolean(isAppInteractionRequired);
        dest.writeBoolean(isUserInteractionRequired);
        dest.writeBoolean(isUserAllowedToManuallyConnect);
        dest.writeInt(suggestorUid);
        dest.writeString(suggestorPackageName);
    }
@@ -666,6 +719,7 @@ public final class WifiNetworkSuggestion implements Parcelable {
                .append(", FQDN=").append(wifiConfiguration.FQDN)
                .append(", isAppInteractionRequired=").append(isAppInteractionRequired)
                .append(", isUserInteractionRequired=").append(isUserInteractionRequired)
                .append(", isUserAllowedToManuallyConnect=").append(isUserAllowedToManuallyConnect)
                .append(", suggestorUid=").append(suggestorUid)
                .append(", suggestorPackageName=").append(suggestorPackageName)
                .append("]");
+36 −14
Original line number Diff line number Diff line
@@ -64,12 +64,13 @@ public class WifiNetworkSuggestionTest {
        assertEquals(WifiConfiguration.METERED_OVERRIDE_NONE,
                suggestion.wifiConfiguration.meteredOverride);
        assertEquals(-1, suggestion.wifiConfiguration.priority);
        assertEquals(false, suggestion.isUserAllowedToManuallyConnect);
    }

    /**
     * Validate correctness of WifiNetworkSuggestion object created by
     * {@link WifiNetworkSuggestion.Builder#build()} for WPA_EAP network which requires
     * app interaction and has a priority of zero set.
     * app interaction, not share credential and has a priority of zero set.
     */
    @Test
    public void
@@ -78,6 +79,7 @@ public class WifiNetworkSuggestionTest {
                .setSsid(TEST_SSID)
                .setWpa2Passphrase(TEST_PRESHARED_KEY)
                .setIsAppInteractionRequired(true)
                .setIsUserAllowedToManuallyConnect(false)
                .setPriority(0)
                .build();

@@ -91,6 +93,7 @@ public class WifiNetworkSuggestionTest {
        assertEquals(WifiConfiguration.METERED_OVERRIDE_NONE,
                suggestion.wifiConfiguration.meteredOverride);
        assertEquals(0, suggestion.wifiConfiguration.priority);
        assertEquals(false, suggestion.isUserAllowedToManuallyConnect);
    }

    /**
@@ -118,6 +121,7 @@ public class WifiNetworkSuggestionTest {
        assertEquals(WifiConfiguration.METERED_OVERRIDE_METERED,
                suggestion.wifiConfiguration.meteredOverride);
        assertEquals(-1, suggestion.wifiConfiguration.priority);
        assertTrue(suggestion.isUserAllowedToManuallyConnect);
    }

    /**
@@ -138,6 +142,7 @@ public class WifiNetworkSuggestionTest {
                .get(WifiConfiguration.KeyMgmt.OWE));
        assertNull(suggestion.wifiConfiguration.preSharedKey);
        assertTrue(suggestion.wifiConfiguration.requirePMF);
        assertFalse(suggestion.isUserAllowedToManuallyConnect);
    }

    /**
@@ -149,6 +154,7 @@ public class WifiNetworkSuggestionTest {
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
                .setSsid(TEST_SSID)
                .setWpa3Passphrase(TEST_PRESHARED_KEY)
                .setIsUserAllowedToManuallyConnect(true)
                .build();

        assertEquals("\"" + TEST_SSID + "\"", suggestion.wifiConfiguration.SSID);
@@ -157,6 +163,7 @@ public class WifiNetworkSuggestionTest {
        assertEquals("\"" + TEST_PRESHARED_KEY + "\"",
                suggestion.wifiConfiguration.preSharedKey);
        assertTrue(suggestion.wifiConfiguration.requirePMF);
        assertTrue(suggestion.isUserAllowedToManuallyConnect);
    }


@@ -186,6 +193,7 @@ public class WifiNetworkSuggestionTest {
        assertNull(suggestion.wifiConfiguration.preSharedKey);
        // allowedSuiteBCiphers are set according to the loaded certificate and cannot be tested
        // here.
        assertTrue(suggestion.isUserAllowedToManuallyConnect);
    }

    /**
@@ -205,6 +213,7 @@ public class WifiNetworkSuggestionTest {
        assertTrue(suggestion.isAppInteractionRequired);
        assertEquals(suggestion.wifiConfiguration.meteredOverride,
                WifiConfiguration.METERED_OVERRIDE_METERED);
        assertTrue(suggestion.isUserAllowedToManuallyConnect);
    }

    /**
@@ -439,7 +448,7 @@ public class WifiNetworkSuggestionTest {
        configuration.BSSID = TEST_BSSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion(
                configuration, null, false, true, TEST_UID, TEST_PACKAGE_NAME);
                configuration, null, false, true, true, TEST_UID, TEST_PACKAGE_NAME);

        Parcel parcelW = Parcel.obtain();
        suggestion.writeToParcel(parcelW, 0);
@@ -506,7 +515,7 @@ public class WifiNetworkSuggestionTest {
        configuration.BSSID = TEST_BSSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null, true, false, TEST_UID,
                new WifiNetworkSuggestion(configuration, null, true, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        WifiConfiguration configuration1 = new WifiConfiguration();
@@ -514,7 +523,7 @@ public class WifiNetworkSuggestionTest {
        configuration1.BSSID = TEST_BSSID;
        configuration1.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, true, TEST_UID,
                new WifiNetworkSuggestion(configuration1, null, false, true, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        assertEquals(suggestion, suggestion1);
@@ -531,14 +540,14 @@ public class WifiNetworkSuggestionTest {
        configuration.SSID = TEST_SSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null, false, false, TEST_UID,
                new WifiNetworkSuggestion(configuration, null, false, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        WifiConfiguration configuration1 = new WifiConfiguration();
        configuration1.SSID = TEST_SSID_1;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, false, TEST_UID,
                new WifiNetworkSuggestion(configuration1, null, false, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        assertNotEquals(suggestion, suggestion1);
@@ -555,14 +564,14 @@ public class WifiNetworkSuggestionTest {
        configuration.BSSID = TEST_BSSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null,  false, false, TEST_UID,
                new WifiNetworkSuggestion(configuration, null,  false, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        WifiConfiguration configuration1 = new WifiConfiguration();
        configuration1.SSID = TEST_SSID;
        configuration1.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, false, TEST_UID,
                new WifiNetworkSuggestion(configuration1, null, false, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        assertNotEquals(suggestion, suggestion1);
@@ -578,14 +587,14 @@ public class WifiNetworkSuggestionTest {
        configuration.SSID = TEST_SSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null, false, false, TEST_UID,
                new WifiNetworkSuggestion(configuration, null, false, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        WifiConfiguration configuration1 = new WifiConfiguration();
        configuration1.SSID = TEST_SSID;
        configuration1.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration1, null, false, false, TEST_UID,
                new WifiNetworkSuggestion(configuration1, null, false, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        assertNotEquals(suggestion, suggestion1);
@@ -601,11 +610,11 @@ public class WifiNetworkSuggestionTest {
        configuration.SSID = TEST_SSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion =
                new WifiNetworkSuggestion(configuration, null, false, false, TEST_UID,
                new WifiNetworkSuggestion(configuration, null, false, false, true, TEST_UID,
                        TEST_PACKAGE_NAME);

        WifiNetworkSuggestion suggestion1 =
                new WifiNetworkSuggestion(configuration, null, false, false, TEST_UID_OTHER,
                new WifiNetworkSuggestion(configuration, null, false, false, true, TEST_UID_OTHER,
                        TEST_PACKAGE_NAME);

        assertNotEquals(suggestion, suggestion1);
@@ -621,10 +630,10 @@ public class WifiNetworkSuggestionTest {
        configuration.SSID = TEST_SSID;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion(
                configuration, null, false, false, TEST_UID, TEST_PACKAGE_NAME);
                configuration, null, false, false, true, TEST_UID, TEST_PACKAGE_NAME);

        WifiNetworkSuggestion suggestion1 = new WifiNetworkSuggestion(
                configuration, null, false, false, TEST_UID, TEST_PACKAGE_NAME_OTHER);
                configuration, null, false, false, true, TEST_UID, TEST_PACKAGE_NAME_OTHER);

        assertNotEquals(suggestion, suggestion1);
    }
@@ -664,4 +673,17 @@ public class WifiNetworkSuggestionTest {
                .build();
        assertNotEquals(suggestion, suggestion1);
    }

    /**
     * Ensure {@link WifiNetworkSuggestion.Builder#build()} throws an exception
     * when {@link WifiNetworkSuggestion.Builder#setIsUserAllowedToManuallyConnect(boolean)} to
     * true on a open network suggestion.
     */
    @Test(expected = IllegalStateException.class)
    public void testSetIsUserAllowedToManuallyConnectToWithOpenNetwork() {
        WifiNetworkSuggestion suggestion = new WifiNetworkSuggestion.Builder()
                .setSsid(TEST_SSID)
                .setIsUserAllowedToManuallyConnect(true)
                .build();
    }
}