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

Commit 1848d31c authored by Emily Bernier's avatar Emily Bernier
Browse files

Prevent users from mounting USB, if restricted.

Bug: 13585295

Change-Id: I6b7905fad06bf2e1d4dd629a266280ddff9b3121
parent 4143bc5f
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.UEventObserver;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.provider.Settings;
@@ -655,6 +656,17 @@ public class UsbDeviceManager {
                    }
                    break;
                case MSG_USER_SWITCHED: {
                    mCurrentUser = msg.arg1;

                    UserManager userManager =
                            (UserManager) mContext.getSystemService(Context.USER_SERVICE);
                    if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
                        Slog.v(TAG, "Switched to user with DISALLOW_USB_FILE_TRANSFER restriction;"
                                + " disabling USB.");
                        setUsbConfig("none");
                        break;
                    }

                    final boolean mtpActive =
                            containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MTP)
                            || containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_PTP);
@@ -663,7 +675,6 @@ public class UsbDeviceManager {
                        setUsbConfig("none");
                        setUsbConfig(mCurrentFunctions);
                    }
                    mCurrentUser = msg.arg1;
                    break;
                }
            }
+10 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.SparseArray;

import com.android.internal.annotations.GuardedBy;
@@ -248,6 +249,15 @@ public class UsbService extends IUsbManager.Stub {
    @Override
    public void setCurrentFunction(String function, boolean makeDefault) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);

        // If attempt to change USB function while file transfer is restricted, ensure that
        // the current function is set to "none", and return.
        UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
            if (mDeviceManager != null) mDeviceManager.setCurrentFunctions("none", false);
            return;
        }

        if (mDeviceManager != null) {
            mDeviceManager.setCurrentFunctions(function, makeDefault);
        } else {