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

Commit e0888a1e authored by Nikhil Nayunigari's avatar Nikhil Nayunigari
Browse files

Locking the edit icon based on network ownership

This change locks the edit icon on the network details page and shows a
popup message based on the network ownership.

Bug: 409845361

Flag: com.android.settings.connectivity.wifi_multiuser

Test: Manual testing
Change-Id: Ib34b854a8faa35db93ba92961bf0491910a82e2f
parent 8a887616
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2626,6 +2626,7 @@
    <!-- Label for the share wifi network summary. [CHAR LIMIT=NONE] -->
    <string name="share_wifi_network_summary">All users on this device can view and connect to a shared network. Administrators can delete a shared network. Learn more</string>
    <string name="edit_wifi_network_configuration">Allow other users to edit network configuration</string>
    <string name="edit_wifi_network_non_owner_message">Contact the admin of this device to make changes to this network.</string>
    <!-- Dialog text to tell the user that the selected network does not have Internet access. -->
    <string name="no_internet_access_text">This network has no internet access. Stay connected?</string>
+7 −2
Original line number Diff line number Diff line
@@ -215,6 +215,11 @@ public class WifiNetworkDetailsFragment extends RestrictedDashboardFragment impl
            MenuItem item = menu.add(0, Menu.FIRST, 0, R.string.wifi_modify);
            item.setIcon(com.android.internal.R.drawable.ic_mode_edit);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
            if (com.android.settings.connectivity.Flags.wifiMultiuser()
                    && !mWifiDetailPreferenceController2.canModifyNetwork()) {
                item.setTooltipText(
                        getContext().getString(R.string.edit_wifi_network_non_owner_message));
            }
        }
        super.onCreateOptionsMenu(menu, inflater);
    }
@@ -223,7 +228,7 @@ public class WifiNetworkDetailsFragment extends RestrictedDashboardFragment impl
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case Menu.FIRST:
                if (!mWifiDetailPreferenceController2.canModifyNetwork()) {
                if (mWifiDetailPreferenceController2.isNetworkAdminLocked()) {
                    EnforcedAdmin admin = RestrictedLockUtilsInternal.getDeviceOwner(getContext());
                    if (admin == null) {
                        final DevicePolicyManager dpm = (DevicePolicyManager)
@@ -238,7 +243,7 @@ public class WifiNetworkDetailsFragment extends RestrictedDashboardFragment impl
                        }
                    }
                    RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), admin);
                } else {
                } else if (mWifiDetailPreferenceController2.canModifyNetwork()) {
                    showDialog(WIFI_DIALOG_ID);
                }
                return true;
+10 −2
Original line number Diff line number Diff line
@@ -890,9 +890,17 @@ public class WifiDetailPreferenceController2 extends AbstractPreferenceControlle
    /**
     * Returns whether the network represented by this preference can be modified.
     */
    public boolean canModifyNetwork() {
    public boolean isNetworkAdminLocked() {
        return mWifiEntry.isSaved()
                && !WifiUtils.isNetworkLockedDown(mContext, mWifiEntry.getWifiConfiguration());
                && WifiUtils.isNetworkLockedDown(mContext, mWifiEntry.getWifiConfiguration());
    }

    /**
     * Returns true if the current user owns the network or if there is only a single user
     * on the device.
     */
    public boolean canModifyNetwork() {
        return WifiUtils.isNetworkEditable(mWifiEntry, mContext);
    }

    /**
+52 −4
Original line number Diff line number Diff line
@@ -1256,21 +1256,22 @@ public class WifiDetailPreferenceController2Test {
    }

    @Test
    public void canModifyNetwork_savedNetwork_returnTrue() {
    public void isNetworkAdminLocked_savedNetwork_returnFalse() {
        setUpForConnectedNetwork();
        setUpSpyController();
        when(mMockWifiEntry.isSaved()).thenReturn(true);

        assertThat(mController.canModifyNetwork()).isTrue();
        assertThat(mController.isNetworkAdminLocked()).isFalse();
    }

    @Test
    public void canModifyNetwork_lockedDown() {
    public void isNetworkAdminLocked_lockedDown() {
        setUpForConnectedNetwork();
        setUpSpyController();
        lockDownNetwork();
        when(mMockWifiEntry.isSaved()).thenReturn(true);

        assertThat(mController.canModifyNetwork()).isFalse();
        assertThat(mController.isNetworkAdminLocked()).isTrue();
    }

    /**
@@ -1296,6 +1297,53 @@ public class WifiDetailPreferenceController2Test {
                Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 1);
    }

    @Test
    public void canModifyNetwork_featureDisabled_returnTrue() {
        setUpForConnectedNetwork();
        setUpSpyController();

        assertThat(mController.canModifyNetwork()).isTrue();
    }

    @Test
    @EnableFlags(Flags.FLAG_WIFI_MULTIUSER)
    public void canModifyNetwork_networkOwned_returnTrue() {
        setUpForConnectedNetwork();
        setUpSpyController();
        final WifiConfiguration mockWifiConfiguration = mock(WifiConfiguration.class);
        when(mMockWifiEntry.getWifiConfiguration()).thenReturn(mockWifiConfiguration);
        mockWifiConfiguration.creatorUid = 1;

        assertThat(mController.canModifyNetwork()).isTrue();
    }

    @Test
    @EnableFlags(Flags.FLAG_WIFI_MULTIUSER)
    public void canModifyNetwork_networkNotOwned_returnFalse() {
        setUpForConnectedNetwork();
        setUpSpyController();
        final WifiConfiguration mockWifiConfiguration = mock(WifiConfiguration.class);
        when(mMockWifiEntry.getWifiConfiguration()).thenReturn(mockWifiConfiguration);
        mockWifiConfiguration.creatorUid = Integer.MAX_VALUE;
        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager);
        when(mMockUserManager.getUserCount()).thenReturn(3);

        assertThat(mController.canModifyNetwork()).isFalse();
    }

    @Test
    @EnableFlags(Flags.FLAG_WIFI_MULTIUSER)
    public void canModifyNetwork_networkNotOwned_singleUser_returnTrue() {
        setUpForConnectedNetwork();
        setUpSpyController();
        final WifiConfiguration mockWifiConfiguration = mock(WifiConfiguration.class);
        when(mMockWifiEntry.getWifiConfiguration()).thenReturn(mockWifiConfiguration);
        mockWifiConfiguration.creatorUid = Integer.MAX_VALUE;
        when(mMockUserManager.getUserCount()).thenReturn(1);

        assertThat(mController.canModifyNetwork()).isTrue();
    }

    @Test
    public void forgetNetwork_activityGone_ignoreFinish() {
        setUpForConnectedNetwork();