Merge "Revert "Fix bluetooth settings will broadcast to anywhere when some cases"" into sc-dev
This commit is contained in:
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.android.settings.bluetooth;
|
||||
|
||||
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
@@ -25,7 +23,6 @@ import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
@@ -33,14 +30,11 @@ import android.widget.TextView;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.internal.app.AlertActivity;
|
||||
import com.android.internal.app.AlertController;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.password.PasswordUtils;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
|
||||
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
|
||||
|
||||
/**
|
||||
* BluetoothPermissionActivity shows a dialog for accepting incoming
|
||||
@@ -57,13 +51,8 @@ public class BluetoothPermissionActivity extends AlertActivity implements
|
||||
private TextView messageView;
|
||||
private Button mOkButton;
|
||||
private BluetoothDevice mDevice;
|
||||
|
||||
@VisibleForTesting
|
||||
String mReturnPackage = null;
|
||||
@VisibleForTesting
|
||||
String mReturnClass = null;
|
||||
@VisibleForTesting
|
||||
String mCallingAppPackageName;
|
||||
private String mReturnPackage = null;
|
||||
private String mReturnClass = null;
|
||||
|
||||
private int mRequestType = 0;
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@@ -91,7 +80,6 @@ public class BluetoothPermissionActivity extends AlertActivity implements
|
||||
|
||||
getWindow().addPrivateFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
|
||||
Intent i = getIntent();
|
||||
mCallingAppPackageName = PasswordUtils.getCallingAppPackageName(getActivityToken());
|
||||
String action = i.getAction();
|
||||
if (!action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
|
||||
Log.e(TAG, "Error: this activity may be started only with intent "
|
||||
@@ -106,22 +94,6 @@ public class BluetoothPermissionActivity extends AlertActivity implements
|
||||
mRequestType = i.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
|
||||
BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
|
||||
|
||||
// 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
|
||||
// preferences to Bluetooth app's. In that case, Bluetooth app broadcasts an
|
||||
// ACTION_CONNECTION_ACCESS_REQUEST intent to ask to Settings app.
|
||||
//
|
||||
// If that happens, 'checkUserChoice()' here will do migration because it finds or
|
||||
// creates a 'CachedBluetoothDevice' object for the device.
|
||||
//
|
||||
// After migration is done, 'checkUserChoice()' replies to the request by sending an
|
||||
// ACTION_CONNECTION_ACCESS_REPLY intent. And we don't need to start permission activity
|
||||
// dialog or notification.
|
||||
if (checkUserChoice()) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
if(DEBUG) Log.i(TAG, "onCreate() Request type: " + mRequestType);
|
||||
|
||||
if (mRequestType == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
|
||||
@@ -230,14 +202,7 @@ public class BluetoothPermissionActivity extends AlertActivity implements
|
||||
sendReplyIntentToReceiver(false, true);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void sendReplyIntentToReceiver(final boolean allowed, final boolean always) {
|
||||
if (!TextUtils.equals(mCallingAppPackageName, mReturnPackage)) {
|
||||
Log.w(TAG, "sendReplyIntentToReceiver() return package name is not equivalent"
|
||||
+ " to calling package name!");
|
||||
return;
|
||||
}
|
||||
|
||||
private void sendReplyIntentToReceiver(final boolean allowed, final boolean always) {
|
||||
Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
|
||||
|
||||
if (mReturnPackage != null && mReturnClass != null) {
|
||||
@@ -281,76 +246,4 @@ public class BluetoothPermissionActivity extends AlertActivity implements
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true user had made a choice, this method replies to the request according
|
||||
* to user's previous decision
|
||||
* false user hadnot made any choice on this device
|
||||
*/
|
||||
private boolean checkUserChoice() {
|
||||
boolean processed = false;
|
||||
|
||||
// ignore if it is something else than phonebook/message settings it wants us to remember
|
||||
if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS
|
||||
&& mRequestType != BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS
|
||||
&& mRequestType != BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
|
||||
Log.d(TAG, "checkUserChoice(): Unknown RequestType " + mRequestType);
|
||||
return processed;
|
||||
}
|
||||
|
||||
final LocalBluetoothManager bluetoothManager = Utils.getLocalBtManager(this);
|
||||
final CachedBluetoothDeviceManager cachedDeviceManager =
|
||||
bluetoothManager.getCachedDeviceManager();
|
||||
CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
|
||||
if (cachedDevice == null) {
|
||||
cachedDevice = cachedDeviceManager.addDevice(mDevice);
|
||||
}
|
||||
|
||||
if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
|
||||
final int phonebookPermission = mDevice.getPhonebookAccessPermission();
|
||||
|
||||
if (phonebookPermission == BluetoothDevice.ACCESS_UNKNOWN) {
|
||||
// Leave 'processed' as false.
|
||||
} else if (phonebookPermission == BluetoothDevice.ACCESS_ALLOWED) {
|
||||
sendReplyIntentToReceiver(true, true);
|
||||
processed = true;
|
||||
} else if (phonebookPermission == BluetoothDevice.ACCESS_REJECTED) {
|
||||
sendReplyIntentToReceiver(false, true);
|
||||
processed = true;
|
||||
} else {
|
||||
Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission);
|
||||
}
|
||||
} else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
|
||||
final int messagePermission = mDevice.getMessageAccessPermission();
|
||||
|
||||
if (messagePermission == BluetoothDevice.ACCESS_UNKNOWN) {
|
||||
// Leave 'processed' as false.
|
||||
} else if (messagePermission == BluetoothDevice.ACCESS_ALLOWED) {
|
||||
sendReplyIntentToReceiver(true, true);
|
||||
processed = true;
|
||||
} else if (messagePermission == BluetoothDevice.ACCESS_REJECTED) {
|
||||
sendReplyIntentToReceiver(false, true);
|
||||
processed = true;
|
||||
} else {
|
||||
Log.e(TAG, "Bad messagePermission: " + messagePermission);
|
||||
}
|
||||
} else if (mRequestType == BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
|
||||
final int simPermission = mDevice.getSimAccessPermission();
|
||||
|
||||
if (simPermission == BluetoothDevice.ACCESS_UNKNOWN) {
|
||||
// Leave 'processed' as false.
|
||||
} else if (simPermission == BluetoothDevice.ACCESS_ALLOWED) {
|
||||
sendReplyIntentToReceiver(true, true);
|
||||
processed = true;
|
||||
} else if (simPermission == BluetoothDevice.ACCESS_REJECTED) {
|
||||
sendReplyIntentToReceiver(false, true);
|
||||
processed = true;
|
||||
} else {
|
||||
Log.e(TAG, "Bad simPermission: " + simPermission);
|
||||
}
|
||||
}
|
||||
|
||||
Log.d(TAG, "checkUserChoice(): returning " + processed);
|
||||
return processed;
|
||||
}
|
||||
}
|
||||
|
@@ -29,6 +29,9 @@ import android.os.UserManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
|
||||
/**
|
||||
* BluetoothPermissionRequest is a receiver to receive Bluetooth connection
|
||||
@@ -80,6 +83,21 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
|
||||
if (DEBUG) Log.d(TAG, "onReceive request type: " + mRequestType + " return "
|
||||
+ mReturnPackage + "," + mReturnClass);
|
||||
|
||||
// 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
|
||||
// preferences to Bluetooth app's. In that case, Bluetooth app broadcasts an
|
||||
// ACTION_CONNECTION_ACCESS_REQUEST intent to ask to Settings app.
|
||||
//
|
||||
// If that happens, 'checkUserChoice()' here will do migration because it finds or
|
||||
// creates a 'CachedBluetoothDevice' object for the device.
|
||||
//
|
||||
// After migration is done, 'checkUserChoice()' replies to the request by sending an
|
||||
// ACTION_CONNECTION_ACCESS_REPLY intent. And we don't need to start permission activity
|
||||
// dialog or notification.
|
||||
if (checkUserChoice()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent connectionAccessIntent = new Intent(action);
|
||||
connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class);
|
||||
// We use the FLAG_ACTIVITY_MULTIPLE_TASK since we can have multiple concurrent access
|
||||
@@ -194,4 +212,92 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true user had made a choice, this method replies to the request according
|
||||
* to user's previous decision
|
||||
* false user hadnot made any choice on this device
|
||||
*/
|
||||
private boolean checkUserChoice() {
|
||||
boolean processed = false;
|
||||
|
||||
// ignore if it is something else than phonebook/message settings it wants us to remember
|
||||
if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS
|
||||
&& mRequestType != BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS
|
||||
&& mRequestType != BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
|
||||
if (DEBUG) Log.d(TAG, "checkUserChoice(): Unknown RequestType " + mRequestType);
|
||||
return processed;
|
||||
}
|
||||
|
||||
LocalBluetoothManager bluetoothManager = Utils.getLocalBtManager(mContext);
|
||||
CachedBluetoothDeviceManager cachedDeviceManager =
|
||||
bluetoothManager.getCachedDeviceManager();
|
||||
CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
|
||||
if (cachedDevice == null) {
|
||||
cachedDevice = cachedDeviceManager.addDevice(mDevice);
|
||||
}
|
||||
|
||||
String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
|
||||
|
||||
if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
|
||||
int phonebookPermission = mDevice.getPhonebookAccessPermission();
|
||||
|
||||
if (phonebookPermission == BluetoothDevice.ACCESS_UNKNOWN) {
|
||||
// Leave 'processed' as false.
|
||||
} else if (phonebookPermission == BluetoothDevice.ACCESS_ALLOWED) {
|
||||
sendReplyIntentToReceiver(true);
|
||||
processed = true;
|
||||
} else if (phonebookPermission == BluetoothDevice.ACCESS_REJECTED) {
|
||||
sendReplyIntentToReceiver(false);
|
||||
processed = true;
|
||||
} else {
|
||||
Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission);
|
||||
}
|
||||
} else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
|
||||
int messagePermission = mDevice.getMessageAccessPermission();
|
||||
|
||||
if (messagePermission == BluetoothDevice.ACCESS_UNKNOWN) {
|
||||
// Leave 'processed' as false.
|
||||
} else if (messagePermission == BluetoothDevice.ACCESS_ALLOWED) {
|
||||
sendReplyIntentToReceiver(true);
|
||||
processed = true;
|
||||
} else if (messagePermission == BluetoothDevice.ACCESS_REJECTED) {
|
||||
sendReplyIntentToReceiver(false);
|
||||
processed = true;
|
||||
} else {
|
||||
Log.e(TAG, "Bad messagePermission: " + messagePermission);
|
||||
}
|
||||
} else if(mRequestType == BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
|
||||
int simPermission = mDevice.getSimAccessPermission();
|
||||
|
||||
if (simPermission == BluetoothDevice.ACCESS_UNKNOWN) {
|
||||
// Leave 'processed' as false.
|
||||
} else if (simPermission == BluetoothDevice.ACCESS_ALLOWED) {
|
||||
sendReplyIntentToReceiver(true);
|
||||
processed = true;
|
||||
} else if (simPermission == BluetoothDevice.ACCESS_REJECTED) {
|
||||
sendReplyIntentToReceiver(false);
|
||||
processed = true;
|
||||
} else {
|
||||
Log.e(TAG, "Bad simPermission: " + simPermission);
|
||||
}
|
||||
}
|
||||
if (DEBUG) Log.d(TAG,"checkUserChoice(): returning " + processed);
|
||||
return processed;
|
||||
}
|
||||
|
||||
private void sendReplyIntentToReceiver(final boolean allowed) {
|
||||
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,
|
||||
allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
|
||||
: BluetoothDevice.CONNECTION_ACCESS_NO);
|
||||
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
|
||||
intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
|
||||
mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
|
||||
}
|
||||
}
|
||||
|
@@ -27,15 +27,12 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.password.PasswordUtils;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
@@ -51,16 +48,10 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothProgressCategory mAvailableDevicesCategory;
|
||||
@VisibleForTesting
|
||||
String mLaunchPackage;
|
||||
@VisibleForTesting
|
||||
String mLaunchClass;
|
||||
@VisibleForTesting
|
||||
String mCallingAppPackageName;
|
||||
@VisibleForTesting
|
||||
Context mContext;
|
||||
|
||||
private boolean mNeedAuth;
|
||||
private String mLaunchPackage;
|
||||
private String mLaunchClass;
|
||||
private boolean mScanAllowed;
|
||||
|
||||
public DevicePickerFragment() {
|
||||
@@ -94,9 +85,6 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
|
||||
getActivity().setTitle(getString(R.string.device_picker));
|
||||
UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
|
||||
mScanAllowed = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH);
|
||||
mCallingAppPackageName = PasswordUtils.getCallingAppPackageName(
|
||||
getActivity().getActivityToken());
|
||||
mContext = getContext();
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@@ -200,17 +188,11 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
|
||||
}
|
||||
|
||||
private void sendDevicePickedIntent(BluetoothDevice device) {
|
||||
if (!TextUtils.equals(mCallingAppPackageName, mLaunchPackage)) {
|
||||
Log.w(TAG, "sendDevicePickedIntent() launch package name is not equivalent to"
|
||||
+ " calling package name!");
|
||||
return;
|
||||
}
|
||||
|
||||
Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
|
||||
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
|
||||
if (mLaunchPackage != null && mLaunchClass != null) {
|
||||
intent.setClassName(mLaunchPackage, mLaunchClass);
|
||||
}
|
||||
mContext.sendBroadcast(intent, Manifest.permission.BLUETOOTH_ADMIN);
|
||||
getActivity().sendBroadcast(intent, Manifest.permission.BLUETOOTH_ADMIN);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user