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

Commit 8d0f8c82 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Merge cherrypicks of [14947191, 14947212, 14947132, 14947133, 14947450,...

Merge cherrypicks of [14947191, 14947212, 14947132, 14947133, 14947450, 14947331] into security-aosp-oc-mr1-release

Change-Id: I60fe30214afe6135aa3f5619b1adb22f9eb6ca71
parents b0be3f3f 4611d04c
Loading
Loading
Loading
Loading
+8 −11
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


package com.android.settings.bluetooth;
package com.android.settings.bluetooth;


import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
@@ -29,6 +31,7 @@ import android.view.View;
import android.widget.Button;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TextView;


import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.internal.app.AlertController;
import com.android.settings.R;
import com.android.settings.R;
@@ -36,8 +39,6 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;


import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

/**
/**
 * BluetoothPermissionActivity shows a dialog for accepting incoming
 * BluetoothPermissionActivity shows a dialog for accepting incoming
 * profile connection request from untrusted devices.
 * profile connection request from untrusted devices.
@@ -53,8 +54,6 @@ public class BluetoothPermissionActivity extends AlertActivity implements
    private TextView messageView;
    private TextView messageView;
    private Button mOkButton;
    private Button mOkButton;
    private BluetoothDevice mDevice;
    private BluetoothDevice mDevice;
    private String mReturnPackage = null;
    private String mReturnClass = null;


    private int mRequestType = 0;
    private int mRequestType = 0;
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -91,8 +90,6 @@ public class BluetoothPermissionActivity extends AlertActivity implements
        }
        }


        mDevice = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        mDevice = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        mReturnPackage = i.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
        mReturnClass = i.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
        mRequestType = i.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
        mRequestType = i.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
                                     BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
                                     BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);


@@ -219,14 +216,14 @@ public class BluetoothPermissionActivity extends AlertActivity implements
        sendReplyIntentToReceiver(false, always);
        sendReplyIntentToReceiver(false, always);
    }
    }


    private void sendReplyIntentToReceiver(final boolean allowed, final boolean always) {
    @VisibleForTesting
    void sendReplyIntentToReceiver(final boolean allowed, final boolean always) {
        Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
        Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);


        if (mReturnPackage != null && mReturnClass != null) {
        if (DEBUG) {
            intent.setClassName(mReturnPackage, mReturnClass);
            Log.i(TAG, "sendReplyIntentToReceiver() Request type: " + mRequestType
                    + " mReturnPackage");
        }
        }
        if (DEBUG) Log.i(TAG, "sendReplyIntentToReceiver() Request type: " + mRequestType +
                " mReturnPackage" + mReturnPackage + " mReturnClass" + mReturnClass);


        intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
        intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
                        allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
                        allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
+6 −15
Original line number Original line Diff line number Diff line
@@ -56,8 +56,6 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
    Context mContext;
    Context mContext;
    int mRequestType;
    int mRequestType;
    BluetoothDevice mDevice;
    BluetoothDevice mDevice;
    String mReturnPackage = null;
    String mReturnClass = null;


    @Override
    @Override
    public void onReceive(Context context, Intent intent) {
    public void onReceive(Context context, Intent intent) {
@@ -77,11 +75,10 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
            mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
            mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
                                                 BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION);
                                                 BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION);
            mReturnPackage = intent.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
            mReturnClass = intent.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);


            if (DEBUG) Log.d(TAG, "onReceive request type: " + mRequestType + " return "
            if (DEBUG) {
                    + mReturnPackage + "," + mReturnClass);
                Log.d(TAG, "onReceive request type: " + mRequestType);
            }


            // Even if the user has already made the choice, Bluetooth still may not know that if
            // Even if the user has already made the choice, Bluetooth still may not know that if
            // the user preference data have not been migrated from Settings app's shared
            // the user preference data have not been migrated from Settings app's shared
@@ -110,8 +107,6 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
                                            mRequestType);
                                            mRequestType);
            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_PACKAGE_NAME, mReturnPackage);
            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_CLASS_NAME, mReturnClass);


            String deviceAddress = mDevice != null ? mDevice.getAddress() : null;
            String deviceAddress = mDevice != null ? mDevice.getAddress() : null;
            String deviceName = mDevice != null ? mDevice.getName() : null;
            String deviceName = mDevice != null ? mDevice.getName() : null;
@@ -289,10 +284,6 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
    private void sendReplyIntentToReceiver(final boolean allowed) {
    private void sendReplyIntentToReceiver(final boolean allowed) {
        Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
        Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);


        if (mReturnPackage != null && mReturnClass != null) {
            intent.setClassName(mReturnPackage, mReturnClass);
        }

        intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
        intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
                allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
                allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
                        : BluetoothDevice.CONNECTION_ACCESS_NO);
                        : BluetoothDevice.CONNECTION_ACCESS_NO);
+35 −4
Original line number Original line Diff line number Diff line
@@ -18,13 +18,19 @@ package com.android.settings.bluetooth;


import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;


import android.Manifest;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevicePicker;
import android.bluetooth.BluetoothDevicePicker;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserManager;
import android.os.UserManager;
import android.util.Log;
import android.text.TextUtils;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.VisibleForTesting;
import android.view.Menu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuInflater;
@@ -46,10 +52,16 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {


    @VisibleForTesting
    @VisibleForTesting
    BluetoothProgressCategory mAvailableDevicesCategory;
    BluetoothProgressCategory mAvailableDevicesCategory;
    @VisibleForTesting
    Context mContext;
    @VisibleForTesting
    String mLaunchPackage;
    @VisibleForTesting
    String mLaunchClass;
    @VisibleForTesting
    String mCallingAppPackageName;


    private boolean mNeedAuth;
    private boolean mNeedAuth;
    private String mLaunchPackage;
    private String mLaunchClass;
    private boolean mScanAllowed;
    private boolean mScanAllowed;


    public DevicePickerFragment() {
    public DevicePickerFragment() {
@@ -83,6 +95,12 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
        getActivity().setTitle(getString(R.string.device_picker));
        getActivity().setTitle(getString(R.string.device_picker));
        UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
        UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
        mScanAllowed = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH);
        mScanAllowed = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH);
        mCallingAppPackageName = getCallingAppPackageName(getActivity().getActivityToken());
        if (!TextUtils.equals(mCallingAppPackageName, mLaunchPackage)) {
            Log.w(TAG, "sendDevicePickedIntent() launch package name is not equivalent to"
                    + " calling package name!");
        }
        mContext = getContext();
        setHasOptionsMenu(true);
        setHasOptionsMenu(true);
    }
    }


@@ -183,8 +201,21 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
        Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
        Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        if (mLaunchPackage != null && mLaunchClass != null) {
        if (mLaunchPackage != null && mLaunchClass != null) {
            if (TextUtils.equals(mCallingAppPackageName, mLaunchPackage)) {
                intent.setClassName(mLaunchPackage, mLaunchClass);
                intent.setClassName(mLaunchPackage, mLaunchClass);
            }
            }
        getActivity().sendBroadcast(intent);
        }

        mContext.sendBroadcast(intent, Manifest.permission.BLUETOOTH_ADMIN);
    }

    private String getCallingAppPackageName(IBinder activityToken) {
        String pkg = null;
        try {
            pkg = ActivityManager.getService().getLaunchedFromPackage(activityToken);
        } catch (RemoteException e) {
            Log.v(TAG, "Could not talk to activity manager.", e);
        }
        return pkg;
    }
    }
}
}
+58 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.bluetooth;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.content.Intent;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;

@RunWith(RobolectricTestRunner.class)
public class BluetoothPermissionActivityTest {

    private BluetoothPermissionActivity mActivity;
    private Context mContext;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(RuntimeEnvironment.application);
        mActivity = new BluetoothPermissionActivity();
    }

    @Test
    public void sendBroadcastWithPermission() {
        final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
        ReflectionHelpers.setField(mActivity, "mBase", mContext);

        mActivity.sendReplyIntentToReceiver(true, true);

        verify(mContext).sendBroadcast(intentCaptor.capture(),
                eq("android.permission.BLUETOOTH_ADMIN"));
    }
}
+55 −1
Original line number Original line Diff line number Diff line
@@ -16,7 +16,20 @@


package com.android.settings.bluetooth;
package com.android.settings.bluetooth;


import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;

import com.android.settingslib.bluetooth.CachedBluetoothDevice;


import com.android.settings.TestConfig;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -25,9 +38,12 @@ import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Config;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;


@RunWith(SettingsRobolectricTestRunner.class)
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -36,14 +52,17 @@ public class DevicePickerFragmentTest {
    private LocalBluetoothAdapter mLocalAdapter;
    private LocalBluetoothAdapter mLocalAdapter;
    @Mock
    @Mock
    private BluetoothProgressCategory mAvailableDevicesCategory;
    private BluetoothProgressCategory mAvailableDevicesCategory;

    private DevicePickerFragment mFragment;
    private DevicePickerFragment mFragment;
    private Context mContext;


    @Before
    @Before
    public void setUp() {
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        MockitoAnnotations.initMocks(this);


        mFragment = new DevicePickerFragment();
        mFragment = new DevicePickerFragment();

        mContext = spy(RuntimeEnvironment.application);
        mFragment.mContext = mContext;
        mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
        mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
    }
    }


@@ -55,4 +74,39 @@ public class DevicePickerFragmentTest {


        verify(mAvailableDevicesCategory).setProgress(true);
        verify(mAvailableDevicesCategory).setProgress(true);
    }
    }

    @Test
    public void callingPackageIsEqualToLaunchPackage_sendBroadcastToLaunchPackage() {
        final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
        final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
        final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
        when(cachedDevice.getDevice()).thenReturn(bluetoothDevice);
        mFragment.mSelectedDevice = bluetoothDevice;
        mFragment.mLaunchPackage = "com.android.settings";
        mFragment.mLaunchClass = "com.android.settings.bluetooth.BluetoothPermissionActivity";
        mFragment.mCallingAppPackageName = "com.android.settings";

        mFragment.onDeviceBondStateChanged(cachedDevice, BluetoothDevice.BOND_BONDED);

        verify(mContext).sendBroadcast(intentCaptor.capture(),
                eq("android.permission.BLUETOOTH_ADMIN"));
        assertThat(intentCaptor.getValue().getComponent().getPackageName())
                .isEqualTo(mFragment.mLaunchPackage);
    }

    @Test
    public void callingPackageIsNotEqualToLaunchPackage_broadcastNotSend() {
        final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
        final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
        final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
        when(cachedDevice.getDevice()).thenReturn(bluetoothDevice);
        mFragment.mSelectedDevice = bluetoothDevice;
        mFragment.mLaunchPackage = "com.fake.settings";
        mFragment.mLaunchClass = "com.android.settings.bluetooth.BluetoothPermissionActivity";
        mFragment.mCallingAppPackageName = "com.android.settings";

        mFragment.onDeviceBondStateChanged(cachedDevice, BluetoothDevice.BOND_BONDED);

        verify(mContext, never()).sendBroadcast(intentCaptor.capture());
    }
}
}