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

Commit 06df9c9f authored by changbetty's avatar changbetty
Browse files

Add WiFi Enterprise restrictions check for WiFi Configuration Addition

  When UserManager.DISALLOW_ADD_WIFI_CONFIG is set to true.
  - Disable the "Add network" item in the Internet settings.

  - Activity action API for ACTION_WIFI_ADD_NETWORKS should not be
    permitted and the user shouldn’t see a prompt for approval

Bug: 203169077
Test: make RunSettingsRoboTests ROBOTEST_FILTER=NetworkProviderSettingsTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=AddAppNetworksActivityTest
Change-Id: I18d7703b5972bfbc12dca10b6432d756813abace
parent 72f9e8ea
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -13547,6 +13547,8 @@
    <string name="mobile_data_disable_message">You won\’t have access to data or the internet through <xliff:g id="carrier" example="T-Mobile">%s</xliff:g>. Internet will only be available via Wi\u2011Fi.</string>
    <!-- Text used to refer to the user's current carrier in mobile_data_disable_message if the users's mobile network carrier name is not available [CHAR LIMIT=NONE] -->
    <string name="mobile_data_disable_message_default_carrier">your carrier</string>
    <!-- Summary for enterprise restriction not allow to use [CHAR LIMIT=NONE] -->
    <string name="not_allowed_by_ent">Not allowed by your organization</string>
    <!-- Summary for preference when Bedtime mode is on [CHAR LIMIT=NONE] -->
    <string name="aware_summary_when_bedtime_on">Unavailable because bedtime mode is on</string>
+14 −0
Original line number Diff line number Diff line
@@ -26,8 +26,10 @@ import androidx.annotation.DrawableRes;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils;

/**
 * The Preference for users to add Wi-Fi networks in WifiSettings
@@ -37,6 +39,8 @@ public class AddWifiNetworkPreference extends Preference {
    private static final String TAG = "AddWifiNetworkPreference";

    private final Drawable mScanIconDrawable;
    @VisibleForTesting
    boolean mIsAddWifiConfigAllow;

    public AddWifiNetworkPreference(Context context) {
        super(context);
@@ -47,6 +51,8 @@ public class AddWifiNetworkPreference extends Preference {
        setTitle(R.string.wifi_add_network);

        mScanIconDrawable = getDrawable(R.drawable.ic_scan_24dp);
        mIsAddWifiConfigAllow = WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(context);
        updatePreferenceForRestriction();
    }

    @Override
@@ -73,4 +79,12 @@ public class AddWifiNetworkPreference extends Preference {
        }
        return buttonIcon;
    }

    @VisibleForTesting
    void updatePreferenceForRestriction() {
        if (!mIsAddWifiConfigAllow) {
            setEnabled(false);
            setSummary(R.string.not_allowed_by_ent);
        }
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import androidx.fragment.app.FragmentManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin;
import com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils;

/**
 * When apps send a new intent with a WifiConfiguration list extra to Settings APP. Settings APP
@@ -55,6 +56,8 @@ public class AddAppNetworksActivity extends FragmentActivity {
    final Bundle mBundle = new Bundle();
    @VisibleForTesting
    IActivityManager mActivityManager = ActivityManager.getService();
    @VisibleForTesting
    boolean mIsAddWifiConfigAllow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -71,6 +74,8 @@ public class AddAppNetworksActivity extends FragmentActivity {
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);

        mIsAddWifiConfigAllow = WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(this);
    }

    @Override
@@ -85,6 +90,10 @@ public class AddAppNetworksActivity extends FragmentActivity {

    @VisibleForTesting
    protected boolean showAddNetworksFragment() {
        if (!mIsAddWifiConfigAllow) {
            Log.d(TAG, "Not allowed by Enterprise Restriction");
            return false;
        }
        String packageName = getCallingAppPackageName();
        if (TextUtils.isEmpty(packageName)) {
            Log.d(TAG, "Package name is null");
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.wifi;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.os.UserManager;

import androidx.test.core.app.ApplicationProvider;

import com.android.settings.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@RunWith(RobolectricTestRunner.class)
public class AddWifiNetworkPreferenceTest {

    private Context mContext;
    private AddWifiNetworkPreference mPreference;

    @Mock
    private UserManager mUserManager;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mContext = spy(ApplicationProvider.getApplicationContext());
        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
        mPreference = new AddWifiNetworkPreference(mContext);

    }

    @Test
    public void updatePreferenceForRestriction_isAddWifiConfigAllowed_prefIsEnabled() {
        mPreference.mIsAddWifiConfigAllow = true;

        mPreference.updatePreferenceForRestriction();

        assertThat(mPreference.isEnabled()).isTrue();
        assertThat(mPreference.getSummary()).isNull();
    }

    @Test
    public void updatePreferenceForRestriction_isAddWifiConfigNotAllowed_prefIsDisabled() {
        mPreference.mIsAddWifiConfigAllow = false;

        mPreference.updatePreferenceForRestriction();

        assertThat(mPreference.isEnabled()).isFalse();
        assertThat(mPreference.getSummary())
                .isEqualTo(mContext.getString(R.string.not_allowed_by_ent));
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ public class AddAppNetworksActivityTest {

        mActivity = Robolectric.buildActivity(AddAppNetworksActivity.class).create().get();
        mActivity.mActivityManager = mIActivityManager;
        mActivity.mIsAddWifiConfigAllow = true;
    }

    @Test
@@ -77,6 +78,13 @@ public class AddAppNetworksActivityTest {
        assertThat(mActivity.showAddNetworksFragment()).isTrue();
    }

    @Test
    public void showAddNetworksFragment_isAddWifiConfigNotAllow_returnFalse() {
        mActivity.mIsAddWifiConfigAllow = false;

        assertThat(mActivity.showAddNetworksFragment()).isFalse();
    }

    private void fakeCallingPackage(@Nullable String packageName) {
        try {
            when(mIActivityManager.getLaunchedFromPackage(any())).thenReturn(packageName);