Merge "Show a dialog if bluetooth key is missing when reconnecting" into main

This commit is contained in:
Haijie Hong
2024-09-06 08:18:55 +00:00
committed by Android (Google) Code Review
9 changed files with 596 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2024 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 android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
/** A dialog to ask the user to forget a bluetooth device when the key is missing. */
public class BluetoothKeyMissingDialog extends FragmentActivity {
public static final String FRAGMENT_TAG = "BtKeyMissingFrg";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
Intent intent = getIntent();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device == null) {
finish();
return;
}
BluetoothKeyMissingDialogFragment fragment = new BluetoothKeyMissingDialogFragment(device);
fragment.show(getSupportFragmentManager(), FRAGMENT_TAG);
closeSystemDialogs();
}
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) 2024 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 android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
/**
* A dialogFragment used by {@link BluetoothKeyMissingDialog} to create a dialog for the
* bluetooth device.
*/
public class BluetoothKeyMissingDialogFragment extends InstrumentedDialogFragment
implements OnClickListener {
private static final String TAG = "BTKeyMissingDialogFragment";
private BluetoothDevice mBluetoothDevice;
public BluetoothKeyMissingDialogFragment(@NonNull BluetoothDevice bluetoothDevice) {
mBluetoothDevice = bluetoothDevice;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.bluetooth_key_missing, null);
TextView keyMissingTitle = view.findViewById(R.id.bluetooth_key_missing_title);
keyMissingTitle.setText(
getString(R.string.bluetooth_key_missing_title, mBluetoothDevice.getName()));
builder.setView(view);
builder.setPositiveButton(getString(R.string.bluetooth_key_missing_forget), this);
builder.setNegativeButton(getString(R.string.bluetooth_key_missing_cancel), this);
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
@Override
public void onDestroy() {
super.onDestroy();
if (!getActivity().isFinishing()) {
getActivity().finish();
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
Log.i(
TAG,
"Positive button clicked, remove bond for "
+ mBluetoothDevice.getAnonymizedAddress());
mBluetoothDevice.removeBond();
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
Log.i(TAG, "Negative button clicked for " + mBluetoothDevice.getAnonymizedAddress());
}
if (!getActivity().isFinishing()) {
getActivity().finish();
}
}
@Override
public int getMetricsCategory() {
return SettingsEnums.BLUETOOTH_KEY_MISSING_DIALOG_FRAGMENT;
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright (C) 2024 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 android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.android.settings.R;
import com.android.settings.flags.Flags;
/**
* BluetoothKeyMissingReceiver is a receiver for Bluetooth key missing error when reconnecting to a
* bonded bluetooth device.
*/
public final class BluetoothKeyMissingReceiver extends BroadcastReceiver {
private static final String TAG = "BtKeyMissingReceiver";
private static final String CHANNEL_ID = "bluetooth_notification_channel";
private static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
@Override
public void onReceive(Context context, Intent intent) {
if (!Flags.enableBluetoothKeyMissingDialog()) {
return;
}
String action = intent.getAction();
if (action == null) {
return;
}
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
PowerManager powerManager = context.getSystemService(PowerManager.class);
if (TextUtils.equals(action, BluetoothDevice.ACTION_KEY_MISSING)) {
Log.d(TAG, "Receive ACTION_KEY_MISSING");
if (shouldShowDialog(context, device, powerManager)) {
Intent pairingIntent = getKeyMissingDialogIntent(context, device);
Log.d(TAG, "Show key missing dialog:" + device);
context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
} else {
Log.d(TAG, "Show key missing notification: " + device);
showNotification(context, device);
}
}
}
private Intent getKeyMissingDialogIntent(Context context, BluetoothDevice device) {
Intent pairingIntent = new Intent();
pairingIntent.setClass(context, BluetoothKeyMissingDialog.class);
pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
pairingIntent.setAction(BluetoothDevice.ACTION_KEY_MISSING);
pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return pairingIntent;
}
private boolean shouldShowDialog(
Context context, BluetoothDevice device, PowerManager powerManager) {
return LocalBluetoothPreferences.shouldShowDialogInForeground(context, device)
&& powerManager.isInteractive();
}
private void showNotification(Context context, BluetoothDevice bluetoothDevice) {
NotificationManager nm = context.getSystemService(NotificationManager.class);
NotificationChannel notificationChannel =
new NotificationChannel(
CHANNEL_ID,
context.getString(R.string.bluetooth),
NotificationManager.IMPORTANCE_HIGH);
nm.createNotificationChannel(notificationChannel);
PendingIntent pairIntent =
PendingIntent.getActivity(
context,
0,
getKeyMissingDialogIntent(context, bluetoothDevice),
PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
.setTicker(context.getString(R.string.bluetooth_notif_ticker))
.setLocalOnly(true);
builder.setContentTitle(
context.getString(
R.string.bluetooth_key_missing_title, bluetoothDevice.getName()))
.setContentText(context.getString(R.string.bluetooth_key_missing_message))
.setContentIntent(pairIntent)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setColor(
context.getColor(
com.android.internal.R.color.system_notification_accent_color));
nm.notify(NOTIFICATION_ID, builder.build());
}
}