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

Commit 4f4f6f83 authored by Pavel Grafov's avatar Pavel Grafov
Browse files

Introduce DISALLOW_BLUETOOTH_SHARING.

When this restriction is enforced Bluetooth sharing option should not be
present when the user tries to share something. Previously this was handled
by explicitly disabling bluetooth sharing activity during managed
provisioning, now this code is to be removed (see topic CLs) and the same
behavior should be achieved by setting this restriction for profile owners
by default.

In Bluetooth:
1) Don't check restrictions on boot, it is invoked anyway through the
  listener during boot.
2) Ignore when the restriction is "changed" from true to true - i think
  it was the initial intent in that condition.
3) Disable the component for a particular user and not always the
  system user. This is something that has to be fixed in O I think since
  currently in secondary user the bluetooth itself gets disabled but the
  sharing thing still shows up.

In DPMS:
1) Now ActiveAdmin for PO also contains a set of restrictions applied by
  default.
2) Now all ActiveAdmins for POs are loaded quite early. That shouldn't
  have huge impact though.

Bug: 36249732
Test: run cts -m CtsDevicePolicyManagerTestCases -t com.android.cts.devicepolicy.ManagedProfileTest#testBluetoothSharingRestriction
Test: run cts -m CtsDevicePolicyManagerTestCases -t com.android.cts.devicepolicy.DeviceOwnerTest#testBluetoothRestriction
Test: runtest --path frameworks/base/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceMigrationTest.java
Change-Id: I78c4ffbd503c4a10138e8c0862a9f206f24c5631
Merged-in: I78c4ffbd503c4a10138e8c0862a9f206f24c5631
(cherry picked from commit 7f4ad752)
parent 424dd39e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -31696,6 +31696,7 @@ package android.os {
    field public static final java.lang.String DISALLOW_APPS_CONTROL = "no_control_apps";
    field public static final java.lang.String DISALLOW_AUTOFILL = "no_autofill";
    field public static final java.lang.String DISALLOW_BLUETOOTH = "no_bluetooth";
    field public static final java.lang.String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
    field public static final java.lang.String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
    field public static final java.lang.String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
    field public static final java.lang.String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
+1 −0
Original line number Diff line number Diff line
@@ -34578,6 +34578,7 @@ package android.os {
    field public static final java.lang.String DISALLOW_APPS_CONTROL = "no_control_apps";
    field public static final java.lang.String DISALLOW_AUTOFILL = "no_autofill";
    field public static final java.lang.String DISALLOW_BLUETOOTH = "no_bluetooth";
    field public static final java.lang.String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
    field public static final java.lang.String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
    field public static final java.lang.String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
    field public static final java.lang.String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
+1 −0
Original line number Diff line number Diff line
@@ -31829,6 +31829,7 @@ package android.os {
    field public static final java.lang.String DISALLOW_APPS_CONTROL = "no_control_apps";
    field public static final java.lang.String DISALLOW_AUTOFILL = "no_autofill";
    field public static final java.lang.String DISALLOW_BLUETOOTH = "no_bluetooth";
    field public static final java.lang.String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
    field public static final java.lang.String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
    field public static final java.lang.String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
    field public static final java.lang.String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
+18 −1
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ import java.util.List;
 */
public class UserManager {

    private static String TAG = "UserManager";
    private static final String TAG = "UserManager";
    private final IUserManager mService;
    private final Context mContext;

@@ -217,6 +217,23 @@ public class UserManager {
     */
    public static final String DISALLOW_BLUETOOTH = "no_bluetooth";

    /**
     * Specifies if outgoing bluetooth sharing is disallowed on the device. Device owner and profile
     * owner can set this restriction. When it is set by device owner, all users on this device will
     * be affected.
     *
     * <p>Default is <code>true</code> for managed profiles and false for otherwise. When a device
     * upgrades to {@link android.os.Build.VERSION_CODES#O}, the system sets it for all existing
     * managed profiles.
     *
     * <p>Key for user restrictions.
     * <p>Type: Boolean
     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
     * @see #getUserRestrictions()
     */
    public static final String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";

    /**
     * Specifies if a user is disallowed from transferring files over
     * USB. This can only be set by device owners and profile owners on the primary user.
+27 −30
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server;

import android.Manifest;
import android.app.ActivityManager;
import android.app.AppGlobals;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetooth;
@@ -37,11 +38,11 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@@ -50,7 +51,6 @@ import android.os.Message;
import android.os.Process;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
@@ -61,15 +61,15 @@ import android.provider.Settings.SettingNotFoundException;
import android.util.Slog;

import com.android.internal.util.DumpUtils;
import com.android.server.pm.PackageManagerService;
import com.android.server.pm.UserRestrictionsUtils;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;


class BluetoothManagerService extends IBluetoothManager.Stub {
@@ -120,7 +120,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    private static final int MESSAGE_ADD_PROXY_DELAYED = 400;
    private static final int MESSAGE_BIND_PROFILE_SERVICE = 401;

    private static final int MAX_SAVE_RETRIES = 3;
    private static final int MAX_ERROR_RESTART_RETRIES = 6;

    // Bluetooth persisted setting is off
@@ -223,22 +222,25 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        @Override
        public void onUserRestrictionsChanged(int userId, Bundle newRestrictions,
                Bundle prevRestrictions) {
            if (!newRestrictions.containsKey(UserManager.DISALLOW_BLUETOOTH)
                    && !prevRestrictions.containsKey(UserManager.DISALLOW_BLUETOOTH)) {
                // The relevant restriction has not changed - do nothing.
                return;
            if (!UserRestrictionsUtils.restrictionsChanged(prevRestrictions, newRestrictions,
                    UserManager.DISALLOW_BLUETOOTH, UserManager.DISALLOW_BLUETOOTH_SHARING)) {
                return; // No relevant changes, nothing to do.
            }
            final boolean bluetoothDisallowed =
                    newRestrictions.getBoolean(UserManager.DISALLOW_BLUETOOTH);
            if ((mEnable || mEnableExternal) && bluetoothDisallowed) {

            final boolean disallowed = newRestrictions.getBoolean(UserManager.DISALLOW_BLUETOOTH);

            // DISALLOW_BLUETOOTH is a global restriction that can only be set by DO or PO on the
            // system user, so we only look at the system user.
            if (userId == UserHandle.USER_SYSTEM && disallowed && (mEnable || mEnableExternal)) {
                try {
                    disable(null, true);
                    disable(null /* packageName */, true /* persist */);
                } catch (RemoteException e) {
                    Slog.w(TAG, "Exception when disabling Bluetooth from UserRestrictionsListener",
                            e);
                    Slog.w(TAG, "Exception when disabling Bluetooth", e);
                }
            }
            updateOppLauncherComponentState(bluetoothDisallowed);
            final boolean sharingDisallowed = disallowed
                    || newRestrictions.getBoolean(UserManager.DISALLOW_BLUETOOTH_SHARING);
            updateOppLauncherComponentState(userId, sharingDisallowed);
        }
    };

@@ -994,11 +996,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                LocalServices.getService(UserManagerInternal.class);
        userManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener);
        final boolean isBluetoothDisallowed = isBluetoothDisallowed();
        PackageManagerService packageManagerService =
                (PackageManagerService) ServiceManager.getService("package");
        if (packageManagerService != null && !packageManagerService.isOnlyCoreApps()) {
            updateOppLauncherComponentState(isBluetoothDisallowed);
        }
        if (isBluetoothDisallowed) {
            return;
        }
@@ -2074,21 +2071,21 @@ class BluetoothManagerService extends IBluetoothManager.Stub {

    /**
     * Disables BluetoothOppLauncherActivity component, so the Bluetooth sharing option is not
     * offered to the user if Bluetooth is disallowed. Puts the component to its default state if
     * Bluetooth is not disallowed.
     * offered to the user if Bluetooth or sharing is disallowed. Puts the component to its default
     * state if Bluetooth is not disallowed.
     *
     * @param bluetoothDisallowed whether the {@link UserManager.DISALLOW_BLUETOOTH} user
     * restriction was set.
     * @param userId user to disable bluetooth sharing for.
     * @param bluetoothSharingDisallowed whether bluetooth sharing is disallowed.
     */
    private void updateOppLauncherComponentState(boolean bluetoothDisallowed) {
    private void updateOppLauncherComponentState(int userId, boolean bluetoothSharingDisallowed) {
        final ComponentName oppLauncherComponent = new ComponentName("com.android.bluetooth",
                "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
        final int newState = bluetoothDisallowed
        final int newState = bluetoothSharingDisallowed
                ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        try {
            mContext.getPackageManager()
                    .setComponentEnabledSetting(oppLauncherComponent, newState, 0);
            final IPackageManager imp = AppGlobals.getPackageManager();
            imp.setComponentEnabledSetting(oppLauncherComponent, newState, 0 /* flags */, userId);
        } catch (Exception e) {
            // The component was not found, do nothing.
        }
Loading