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

Commit 79c97b80 authored by Alex Johnston's avatar Alex Johnston
Browse files

DPM API to enable or disable USB data signaling

Background
* The admin can disable signaling over USB on
  corporate owned devices

Changes
* Add DevicePolicyManager API to enable and
  disable USB data signaling.
* Add DevicePolicyManager API to test of this
  feature is supported.

Bug: 168301639
Test: atest com.android.server.devicepolicy.DevicePolicyManagerTest
      make RunSettingsLibRoboTests -j ROBOTEST_FILTER=EnableAdbPreferenceControllerTest
Change-Id: I1d2d71ec05a1565f58ffd691a66c68171a4f9b00
parent dc764eb2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -6951,6 +6951,7 @@ package android.app.admin {
    method public void addUserRestriction(@NonNull android.content.ComponentName, String);
    method public boolean bindDeviceAdminServiceAsUser(@NonNull android.content.ComponentName, android.content.Intent, @NonNull android.content.ServiceConnection, int, @NonNull android.os.UserHandle);
    method public boolean canAdminGrantSensorsPermissions();
    method public boolean canUsbDataSignalingBeDisabled();
    method public void clearApplicationUserData(@NonNull android.content.ComponentName, @NonNull String, @NonNull java.util.concurrent.Executor, @NonNull android.app.admin.DevicePolicyManager.OnClearApplicationUserDataListener);
    method public void clearCrossProfileIntentFilters(@NonNull android.content.ComponentName);
    method @Deprecated public void clearDeviceOwnerApp(String);
@@ -7079,6 +7080,7 @@ package android.app.admin {
    method public boolean isSecurityLoggingEnabled(@Nullable android.content.ComponentName);
    method public boolean isUninstallBlocked(@Nullable android.content.ComponentName, String);
    method public boolean isUniqueDeviceAttestationSupported();
    method public boolean isUsbDataSignalingEnabled();
    method public boolean isUsingUnifiedPassword(@NonNull android.content.ComponentName);
    method public void lockNow();
    method public void lockNow(int);
@@ -7181,6 +7183,7 @@ package android.app.admin {
    method public boolean setTimeZone(@NonNull android.content.ComponentName, String);
    method public void setTrustAgentConfiguration(@NonNull android.content.ComponentName, @NonNull android.content.ComponentName, android.os.PersistableBundle);
    method public void setUninstallBlocked(@Nullable android.content.ComponentName, String, boolean);
    method public void setUsbDataSignalingEnabled(boolean);
    method public void setUserControlDisabledPackages(@NonNull android.content.ComponentName, @NonNull java.util.List<java.lang.String>);
    method public void setUserIcon(@NonNull android.content.ComponentName, android.graphics.Bitmap);
    method public int startUserInBackground(@NonNull android.content.ComponentName, @NonNull android.os.UserHandle);
+80 −0
Original line number Diff line number Diff line
@@ -13457,4 +13457,84 @@ public class DevicePolicyManager {
            throw re.rethrowFromSystemServer();
        }
    }
    /**
     * Called by device owner or profile owner of an organization-owned managed profile to
     * enable or disable USB data signaling for the device. When disabled, USB data connections
     * (except from charging functions) are prohibited.
     *
     * <p> This API is not supported on all devices, the caller should call
     * {@link #canUsbDataSignalingBeDisabled()} to check whether enabling or disabling USB data
     * signaling is supported on the device.
     *
     * @param enabled whether USB data signaling should be enabled or not.
     * @throws SecurityException if the caller is not a device owner or a profile owner on
     *         an organization-owned managed profile.
     * @throws IllegalStateException if disabling USB data signaling is not supported or
     *         if USB data signaling fails to be enabled/disabled.
     */
    public void setUsbDataSignalingEnabled(boolean enabled) {
        throwIfParentInstance("setUsbDataSignalingEnabled");
        if (mService != null) {
            try {
                mService.setUsbDataSignalingEnabled(mContext.getPackageName(), enabled);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
    }
    /**
     * Called by device owner or profile owner of an organization-owned managed profile to return
     * whether USB data signaling is currently enabled by the admin.
     *
     * @return {@code true} if USB data signaling is enabled, {@code false} otherwise.
     */
    public boolean isUsbDataSignalingEnabled() {
        throwIfParentInstance("isUsbDataSignalingEnabled");
        if (mService != null) {
            try {
                return mService.isUsbDataSignalingEnabled(mContext.getPackageName());
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
        return true;
    }
    /**
     * Called by the system to check whether USB data signaling is currently enabled for this user.
     *
     * @param userId which user to check for.
     * @return {@code true} if USB data signaling is enabled, {@code false} otherwise.
     * @hide
     */
    public boolean isUsbDataSignalingEnabledForUser(@UserIdInt int userId) {
        throwIfParentInstance("isUsbDataSignalingEnabledForUser");
        if (mService != null) {
            try {
                return mService.isUsbDataSignalingEnabledForUser(userId);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
        return true;
    }
    /**
     * Returns whether enabling or disabling USB data signaling is supported on the device.
     *
     * @return {@code true} if the device supports enabling and disabling USB data signaling.
     */
    public boolean canUsbDataSignalingBeDisabled() {
        throwIfParentInstance("canUsbDataSignalingBeDisabled");
        if (mService != null) {
            try {
                return mService.canUsbDataSignalingBeDisabled();
            } catch (RemoteException re) {
                throw re.rethrowFromSystemServer();
            }
        }
        return false;
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -505,4 +505,9 @@ interface IDevicePolicyManager {

    void resetDefaultCrossProfileIntentFilters(int userId);
    boolean canAdminGrantSensorsPermissionsForUser(int userId);

    void setUsbDataSignalingEnabled(String callerPackage, boolean enabled);
    boolean isUsbDataSignalingEnabled(String callerPackage);
    boolean isUsbDataSignalingEnabledForUser(int userId);
    boolean canUsbDataSignalingBeDisabled();
}
+22 −0
Original line number Diff line number Diff line
@@ -395,6 +395,28 @@ public class RestrictedLockUtilsInternal extends RestrictedLockUtils {
        return getProfileOrDeviceOwner(context, getUserHandleOf(userId));
    }

    /**
     * Check if USB data signaling (except from charging functions) is disabled by the admin.
     * Only a device owner or a profile owner on an organization-owned managed profile can disable
     * USB data signaling.
     *
     * @return EnforcedAdmin Object containing the enforced admin component and admin user details,
     * or {@code null} if USB data signaling is not disabled.
     */
    public static EnforcedAdmin checkIfUsbDataSignalingIsDisabled(Context context, int userId) {
        DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
        if (dpm == null || dpm.isUsbDataSignalingEnabledForUser(userId)) {
            return null;
        } else {
            EnforcedAdmin admin = getProfileOrDeviceOwner(context, getUserHandleOf(userId));
            int managedProfileId = getManagedProfileId(context, userId);
            if (admin == null && managedProfileId != UserHandle.USER_NULL) {
                admin = getProfileOrDeviceOwner(context, getUserHandleOf(managedProfileId));
            }
            return admin;
        }
    }

    /**
     * Check if {@param packageName} is restricted by the profile or device owner from using
     * metered data.
+10 −3
Original line number Diff line number Diff line
@@ -16,10 +16,13 @@

package com.android.settingslib.development;

import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfUsbDataSignalingIsDisabled;

import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.text.TextUtils;
@@ -28,9 +31,9 @@ import androidx.annotation.VisibleForTesting;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.preference.TwoStatePreference;

import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.ConfirmationDialogController;

public abstract class AbstractEnableAdbPreferenceController extends
@@ -44,7 +47,7 @@ public abstract class AbstractEnableAdbPreferenceController extends
    public static final int ADB_SETTING_OFF = 0;


    protected SwitchPreference mPreference;
    protected RestrictedSwitchPreference mPreference;

    public AbstractEnableAdbPreferenceController(Context context) {
        super(context);
@@ -54,7 +57,7 @@ public abstract class AbstractEnableAdbPreferenceController extends
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        if (isAvailable()) {
            mPreference = (SwitchPreference) screen.findPreference(KEY_ENABLE_ADB);
            mPreference = (RestrictedSwitchPreference) screen.findPreference(KEY_ENABLE_ADB);
        }
    }

@@ -77,6 +80,10 @@ public abstract class AbstractEnableAdbPreferenceController extends
    @Override
    public void updateState(Preference preference) {
        ((TwoStatePreference) preference).setChecked(isAdbEnabled());
        if (isAvailable()) {
            ((RestrictedSwitchPreference) preference).setDisabledByAdmin(
                    checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId()));
        }
    }

    public void enablePreference(boolean enabled) {
Loading