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

Commit 8e16243e authored by Haijie Hong's avatar Haijie Hong Committed by Evelyn Torres
Browse files

Backport BT pairing dialog changes

This is a combination of two commits against b/409868905, merged for
backport convenience.  It updates the string and button style for the BT
pairing dialog.

Bug: 236134583
Bug: 236134775
Test: local tested
Flag: EXEMPT minor style update
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:cdd1489f54c901532984bac346999a6dee1dfcd5
Merged-In: Icd9f509b5363b3ec3900738e21486e54d4e66e4b
Change-Id: Icd9f509b5363b3ec3900738e21486e54d4e66e4b
parent 995cf8e5
Loading
Loading
Loading
Loading
+40 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

@@ -30,6 +31,18 @@
        android:layout_marginTop="@dimen/bluetooth_dialog_padding_top"
        android:orientation="vertical">

        <TextView
            android:id="@+id/pairing_confirmation_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/bluetooth_dialog_padding"
            android:layout_marginBottom="12dp"
            android:layout_marginStart="@dimen/bluetooth_dialog_padding"
            android:layout_marginEnd="@dimen/bluetooth_dialog_padding"
            android:gravity="center_vertical"
            android:visibility="gone"
            android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Body1" />

        <TextView
            android:id="@+id/pairing_caption"
            android:layout_width="wrap_content"
@@ -73,6 +86,32 @@
            android:layout_marginEnd="@dimen/bluetooth_dialog_padding"
            android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Body1" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="@dimen/bluetooth_dialog_padding"
            android:layout_marginEnd="@dimen/bluetooth_dialog_padding">
            <Button
                android:id="@+id/negative_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="@string/bluetooth_pairing_decline"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                style="@style/ActionPrimaryButton"/>
            <Button
                android:id="@+id/positive_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="@string/bluetooth_pairing_accept"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                style="@style/ActionPrimaryButton"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>

</ScrollView>
+26 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

@@ -87,6 +88,30 @@
            android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Body1"
            android:textColor="?android:attr/textColorSecondary"/>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="@dimen/bluetooth_pairing_padding"
            android:layout_marginEnd="@dimen/bluetooth_pairing_padding">
            <Button
                android:id="@+id/negative_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@android:string/cancel"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                style="@style/ActionPrimaryButton"/>
            <Button
                android:id="@+id/positive_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@android:string/ok"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                style="@style/ActionPrimaryButton"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>

</ScrollView>
+6 −0
Original line number Diff line number Diff line
@@ -1839,6 +1839,12 @@
    <!-- Checkbox message in pairing dialogs.  [CHAR LIMIT=NONE] -->
    <string name="bluetooth_pairing_shares_phonebook">Allow access to your contacts and call history</string>
    <!-- Title for the dialog to confirm PIN. [CHAR LIMIT=40] -->
    <string name="bluetooth_pairing_confirmation_title">Check pairing codes</string>
    <!-- Message for the dialog to confirm PIN. [CHAR LIMIT=NONE] -->
    <string name="bluetooth_pairing_confirmation_msg">Check if this code matches the one on <xliff:g id="device_name" example="device_name">%1$s</xliff:g>.\n\nFor your security, do not enter this code anywhere.</string>
    <!-- Title for BT error dialogs. -->
    <string name="bluetooth_error_title"></string>
+42 −23
Original line number Diff line number Diff line
@@ -18,8 +18,6 @@ package com.android.settings.bluetooth;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
@@ -46,7 +44,7 @@ import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
 * for the bluetooth device.
 */
public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment implements
        TextWatcher, OnClickListener {
        TextWatcher {

    private static final String TAG = "BTPairingDialogFragment";

@@ -93,21 +91,18 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i
    @Override
    public void afterTextChanged(Editable s) {
        // enable the positive button when we detect potentially valid input
        Button positiveButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        if (positiveButton != null) {
            positiveButton.setEnabled(mPairingController.isPasskeyValid(s));
        }
        mDialog.findViewById(R.id.positive_button).setEnabled(mPairingController.isPasskeyValid(s));
        // notify the controller about user input
        mPairingController.updateUserInput(s.toString());
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
    protected void onAcceptButtonClicked() {
        mPairingController.onDialogPositiveClick(this);
        } else if (which == DialogInterface.BUTTON_NEGATIVE) {
            mPairingController.onDialogNegativeClick(this);
        mPairingDialogActivity.dismiss();
    }

    protected void onDeclineButtonClicked() {
        mPairingController.onDialogNegativeClick(this);
        mPairingDialogActivity.dismiss();
    }

@@ -208,12 +203,10 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i
        mBuilder.setTitle(getString(R.string.bluetooth_pairing_request,
                mPairingController.getDeviceName()));
        mBuilder.setView(createPinEntryView());
        mBuilder.setPositiveButton(getString(android.R.string.ok), this);
        mBuilder.setNegativeButton(getString(android.R.string.cancel), this);
        AlertDialog dialog = mBuilder.create();
        dialog.setOnShowListener(d -> {
            if (TextUtils.isEmpty(getPairingViewText())) {
                mDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
                mDialog.findViewById(R.id.positive_button).setEnabled(false);
            }
            if (mPairingView != null && mPairingView.requestFocus()) {
                InputMethodManager imm = (InputMethodManager)
@@ -277,6 +270,11 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i
        pairingView.setFilters(new InputFilter[]{
                new LengthFilter(maxLength)});

        Button negativeButton = view.findViewById(R.id.negative_button);
        negativeButton.setOnClickListener(v -> onDeclineButtonClicked());
        Button positiveButton = view.findViewById(R.id.positive_button);
        positiveButton.setOnClickListener(v -> onAcceptButtonClicked());

        return view;
    }

@@ -284,11 +282,13 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i
     * Creates a dialog with UI elements that allow the user to confirm a pairing request.
     */
    private AlertDialog createConfirmationDialog() {
        if (mPairingController.hasPairingContent()) {
            mBuilder.setTitle(getString(R.string.bluetooth_pairing_confirmation_title));
        } else {
            mBuilder.setTitle(getString(R.string.bluetooth_pairing_request,
                    mPairingController.getDeviceName()));
        }
        mBuilder.setView(createView());
        mBuilder.setPositiveButton(getString(R.string.bluetooth_pairing_accept), this);
        mBuilder.setNegativeButton(getString(R.string.bluetooth_pairing_decline), this);
        AlertDialog dialog = mBuilder.create();
        return dialog;
    }
@@ -308,7 +308,6 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i
        mBuilder.setTitle(getString(R.string.bluetooth_pairing_request,
                mPairingController.getDeviceName()));
        mBuilder.setView(createView());
        mBuilder.setNegativeButton(getString(android.R.string.cancel), this);
        AlertDialog dialog = mBuilder.create();

        // Tell the controller the dialog has been created.
@@ -323,6 +322,8 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i
     */
    private View createView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.bluetooth_pin_confirm, null);
        TextView pairingConfirmationHint =
                (TextView) view.findViewById(R.id.pairing_confirmation_hint);
        TextView pairingViewCaption = (TextView) view.findViewById(R.id.pairing_caption);
        TextView pairingViewContent = (TextView) view.findViewById(R.id.pairing_subhead);
        TextView messagePairing = (TextView) view.findViewById(R.id.pairing_code_message);
@@ -340,10 +341,28 @@ public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment i
        messagePairing.setVisibility(mPairingController.isDisplayPairingKeyVariant()
                ? View.VISIBLE : View.GONE);
        if (mPairingController.hasPairingContent()) {
            if (mPairingController.isDisplayPairingKeyVariant()) {
                pairingViewCaption.setVisibility(View.VISIBLE);
            } else {
                pairingConfirmationHint.setText(
                        getString(
                                R.string.bluetooth_pairing_confirmation_msg,
                                mPairingController.getDeviceName()));
                pairingConfirmationHint.setVisibility(View.VISIBLE);
            }
            pairingViewContent.setVisibility(View.VISIBLE);
            pairingViewContent.setText(mPairingController.getPairingContent());
        }

        Button negativeButton = view.findViewById(R.id.negative_button);
        negativeButton.setVisibility(View.VISIBLE);
        negativeButton.setOnClickListener(v -> onDeclineButtonClicked());
        if (mPairingController.getDialogType() == BluetoothPairingController.CONFIRMATION_DIALOG) {
            Button positiveButton = view.findViewById(R.id.positive_button);
            positiveButton.setVisibility(View.VISIBLE);
            positiveButton.setOnClickListener(v -> onAcceptButtonClicked());
        }

        return view;
    }

+27 −9
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Dialog;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
@@ -108,7 +107,7 @@ public class BluetoothPairingDialogTest {

        // test that the positive button is enabled when passkey is valid
        frag.afterTextChanged(new SpannableStringBuilder(FILLER));
        View button = frag.getmDialog().getButton(AlertDialog.BUTTON_POSITIVE);
        View button = frag.getmDialog().findViewById(R.id.positive_button);
        assertThat(button).isNotNull();
        assertThat(button.getVisibility()).isEqualTo(View.VISIBLE);
    }
@@ -175,7 +174,7 @@ public class BluetoothPairingDialogTest {
        // get the relevant views
        View messagePairing = frag.getmDialog().findViewById(R.id.pairing_code_message);
        TextView pairingViewContent = frag.getmDialog().findViewById(R.id.pairing_subhead);
        View pairingViewCaption = frag.getmDialog().findViewById(R.id.pairing_caption);
        TextView pairingViewCaption = frag.getmDialog().findViewById(R.id.pairing_caption);

        // check that the relevant views are visible and that the passkey is shown
        assertThat(messagePairing.getVisibility()).isEqualTo(View.VISIBLE);
@@ -207,7 +206,7 @@ public class BluetoothPairingDialogTest {
        BluetoothPairingDialogFragment frag = makeFragment();

        // click the button and verify that the controller hook was called
        frag.onClick(frag.getmDialog(), AlertDialog.BUTTON_POSITIVE);
        frag.onAcceptButtonClicked();
        verify(controller, times(1)).onDialogPositiveClick(any());
    }

@@ -223,7 +222,7 @@ public class BluetoothPairingDialogTest {
        BluetoothPairingDialogFragment frag = makeFragment();

        // click the button and verify that the controller hook was called
        frag.onClick(frag.getmDialog(), AlertDialog.BUTTON_NEGATIVE);
        frag.onDeclineButtonClicked();
        verify(controller, times(1)).onDialogNegativeClick(any());
    }

@@ -268,7 +267,8 @@ public class BluetoothPairingDialogTest {

        // test that the positive button is enabled when passkey is valid
        frag.afterTextChanged(new SpannableStringBuilder(FILLER));
        View button = frag.getmDialog().getButton(AlertDialog.BUTTON_POSITIVE);
        View button = frag.getmDialog().findViewById(R.id.positive_button);

        assertThat(button).isNotNull();
        assertThat(button.isEnabled()).isFalse();
    }
@@ -388,7 +388,7 @@ public class BluetoothPairingDialogTest {
        BluetoothPairingDialogFragment frag = makeFragment();

        // click the button and verify that the controller hook was called
        frag.onClick(frag.getmDialog(), AlertDialog.BUTTON_POSITIVE);
        frag.onAcceptButtonClicked();

        verify(controller, times(1)).onDialogPositiveClick(any());
        verify(dialogActivity, times(1)).dismiss();
@@ -406,12 +406,30 @@ public class BluetoothPairingDialogTest {
        BluetoothPairingDialogFragment frag = makeFragment();

        // click the button and verify that the controller hook was called
        frag.onClick(frag.getmDialog(), AlertDialog.BUTTON_NEGATIVE);
        frag.onDeclineButtonClicked();

        verify(controller, times(1)).onDialogNegativeClick(any());
        verify(dialogActivity, times(1)).dismiss();
    }

    @Test
    public void confirmationDialog_showConfirmationMessage() {
        when(controller.getDialogType()).thenReturn(BluetoothPairingController.CONFIRMATION_DIALOG);
        when(controller.getDeviceName()).thenReturn("Device");
        when(controller.hasPairingContent()).thenReturn(true);
        when(controller.getPairingContent()).thenReturn(FILLER);

        // build the fragment
        BluetoothPairingDialogFragment frag = makeFragment();

        TextView pairingConfirmationHint =
                frag.getmDialog().findViewById(R.id.pairing_confirmation_hint);
        assertThat(pairingConfirmationHint.getText())
                .isEqualTo(frag.getString(R.string.bluetooth_pairing_confirmation_msg, "Device"));
        assertThat(pairingConfirmationHint.getVisibility()).isEqualTo(View.VISIBLE);
    }

    @Ignore
    @Test
    public void rotateDialog_nullPinText_okButtonEnabled() {
        userEntryDialogExistingTextTest(null);
@@ -443,7 +461,7 @@ public class BluetoothPairingDialogTest {
        AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
        assertThat(dialog).isNotNull();
        boolean expected = !TextUtils.isEmpty(existingText);
        assertThat(dialog.getButton(Dialog.BUTTON_POSITIVE).isEnabled()).isEqualTo(expected);
        assertThat(dialog.findViewById(R.id.positive_button).isEnabled()).isEqualTo(expected);
    }

    private void setupFragment(BluetoothPairingDialogFragment frag) {