Update availability for mobile network

This CL updates the availability to behave properly when a
user other than the primary user is accessing it. This also
makes the api return no intent if they don't have permission to
access this because attempting to go to that screen will cause a
crash. Also updates tests related to this change.

Test: robotests still pass
Bug: 64092292
Change-Id: If9913d9ae08ee3e205ff324aaeeadc755ff1d23d
This commit is contained in:
Salvador Martinez
2017-08-07 13:59:16 -07:00
parent 135619d483
commit fe7c35803e
5 changed files with 83 additions and 6 deletions

View File

@@ -32,7 +32,6 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
import static android.os.UserHandle.myUserId; import static android.os.UserHandle.myUserId;
import static android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS; import static android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
import static com.android.settingslib.RestrictedLockUtils.hasBaseUserRestriction;
public class MobileNetworkPreferenceController extends PreferenceController implements public class MobileNetworkPreferenceController extends PreferenceController implements
LifecycleObserver, OnResume, OnPause { LifecycleObserver, OnResume, OnPause {
@@ -41,22 +40,30 @@ public class MobileNetworkPreferenceController extends PreferenceController impl
private final boolean mIsSecondaryUser; private final boolean mIsSecondaryUser;
private final TelephonyManager mTelephonyManager; private final TelephonyManager mTelephonyManager;
private final UserManager mUserManager;
private Preference mPreference; private Preference mPreference;
@VisibleForTesting @VisibleForTesting
PhoneStateListener mPhoneStateListener; PhoneStateListener mPhoneStateListener;
public MobileNetworkPreferenceController(Context context) { public MobileNetworkPreferenceController(Context context) {
super(context); super(context);
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mIsSecondaryUser = !userManager.isAdminUser(); mIsSecondaryUser = !mUserManager.isAdminUser();
} }
@Override @Override
public boolean isAvailable() { public boolean isAvailable() {
return !mIsSecondaryUser return !isUserRestricted() && !Utils.isWifiOnly(mContext);
&& !Utils.isWifiOnly(mContext) }
&& !hasBaseUserRestriction(mContext, DISALLOW_CONFIG_MOBILE_NETWORKS, myUserId());
public boolean isUserRestricted() {
final RestrictedLockUtilsWrapper wrapper = new RestrictedLockUtilsWrapper();
return mIsSecondaryUser ||
wrapper.hasBaseUserRestriction(
mContext,
DISALLOW_CONFIG_MOBILE_NETWORKS,
myUserId());
} }
@Override @Override

View File

@@ -0,0 +1,15 @@
package com.android.settings.network;
import android.content.Context;
import com.android.settingslib.RestrictedLockUtils;
/**
* Wrapper class needed to be able to test classes which use RestrictedLockUtils methods.
* Unfortunately there is no way to deal with this until robolectric is updated due to the fact
* that it is a static method and it uses new API's.
*/
public class RestrictedLockUtilsWrapper {
public boolean hasBaseUserRestriction(Context context, String userRestriction, int userId) {
return RestrictedLockUtils.hasBaseUserRestriction(context, userRestriction, userId);
}
}

View File

@@ -26,6 +26,7 @@ import android.telephony.TelephonyManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsWrapper;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before; import org.junit.Before;
@@ -87,6 +88,7 @@ public class MobileNetworkPreferenceControllerTest {
} }
@Test @Test
@Config(shadows = ShadowRestrictedLockUtilsWrapper.class)
public void wifiOnly_prefIsNotAvailable() { public void wifiOnly_prefIsNotAvailable() {
when(mUserManager.isAdminUser()).thenReturn(true); when(mUserManager.isAdminUser()).thenReturn(true);
when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class))) when(mUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))

View File

@@ -0,0 +1,28 @@
package com.android.settings.testutils.shadow;
import android.content.Context;
import com.android.settings.network.MobileNetworkPreferenceController;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@Implements(MobileNetworkPreferenceController.class)
public class ShadowMobileNetworkPreferenceController {
private static boolean mIsRestricted = false;
public void __constructor__(Context context) {
}
@Implementation
public boolean isAvailable() {
return mIsRestricted ? false : true;
}
@Implementation
public boolean isUserRestricted() {
return mIsRestricted;
}
public static void setRestricted(boolean restricted) {
mIsRestricted = restricted;
}
}

View File

@@ -0,0 +1,25 @@
package com.android.settings.testutils.shadow;
import android.content.Context;
import com.android.settings.network.RestrictedLockUtilsWrapper;
import org.robolectric.annotation.Implements;
/**
* Shadow for the wrapper around RestrictedLockUtils. Should be removed/updated once robolectric is
* updated to allow usage of new UserManager API's. see
* {@link com.android.settingslib.RestrictedLockUtils} and
* {@link com.android.settings.network.RestrictedLockUtilsWrapper}
*/
@Implements(RestrictedLockUtilsWrapper.class)
public class ShadowRestrictedLockUtilsWrapper {
private boolean isRestricted;
public boolean hasBaseUserRestriction(Context context, String userRestriction, int userId) {
return isRestricted;
}
public void setRestricted(boolean restricted) {
isRestricted = restricted;
}
}