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

Commit 34257d85 authored by Emily Bernier's avatar Emily Bernier Committed by Android (Google) Code Review
Browse files

Merge "Prevent users from mounting USB, if restricted."

parents 12b6bb44 1848d31c
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 {