Make bluetooth switch not discoverable via SliceDeepLinkTrampoline

Bug: 244423101
Test: make RunSettingsRoboTests ROBOTEST_FILTER=BluetoothSwitchPreferenceControllerTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=BluetoothDashboardFragmentTest
Test: manual test by test apk

Change-Id: I13562d227e06627fac33239a9d21fd405a18d012
(cherry picked from commit c706aa6108)
Merged-In: I13562d227e06627fac33239a9d21fd405a18d012
This commit is contained in:
changbetty
2022-11-07 07:58:14 +00:00
committed by Android Build Coastguard Worker
parent 53da1703af
commit 9d2b2ca5d8
4 changed files with 125 additions and 5 deletions

View File

@@ -45,6 +45,7 @@ public class BluetoothSwitchPreferenceController
private SwitchWidgetController mSwitch;
private Context mContext;
private FooterPreference mFooterPreference;
private boolean mIsAlwaysDiscoverable;
@VisibleForTesting
AlwaysDiscoverable mAlwaysDiscoverable;
@@ -78,7 +79,9 @@ public class BluetoothSwitchPreferenceController
@Override
public void onStart() {
mBluetoothEnabler.resume(mContext);
mAlwaysDiscoverable.start();
if (mIsAlwaysDiscoverable) {
mAlwaysDiscoverable.start();
}
if (mSwitch != null) {
updateText(mSwitch.isChecked());
}
@@ -87,7 +90,19 @@ public class BluetoothSwitchPreferenceController
@Override
public void onStop() {
mBluetoothEnabler.pause();
mAlwaysDiscoverable.stop();
if (mIsAlwaysDiscoverable) {
mAlwaysDiscoverable.stop();
}
}
/**
* Set whether the device can be discovered. By default the value will be {@code false}.
*
* @param isAlwaysDiscoverable {@code true} if the device can be discovered,
* otherwise {@code false}
*/
public void setAlwaysDiscoverable(boolean isAlwaysDiscoverable) {
mIsAlwaysDiscoverable = isAlwaysDiscoverable;
}
@Override

View File

@@ -18,12 +18,17 @@ package com.android.settings.connecteddevice;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController;
import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.password.PasswordUtils;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.MainSwitchBarController;
import com.android.settings.widget.SettingsMainSwitchBar;
@@ -40,6 +45,10 @@ public class BluetoothDashboardFragment extends DashboardFragment {
private static final String TAG = "BluetoothDashboardFrag";
private static final String KEY_BLUETOOTH_SCREEN_FOOTER = "bluetooth_screen_footer";
private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui";
private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private FooterPreference mFooterPreference;
private SettingsMainSwitchBar mSwitchBar;
@@ -80,17 +89,33 @@ public class BluetoothDashboardFragment extends DashboardFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String callingAppPackageName = PasswordUtils.getCallingAppPackageName(
getActivity().getActivityToken());
String action = getIntent() != null ? getIntent().getAction() : "";
if (DEBUG) {
Log.d(TAG, "onActivityCreated() calling package name is : " + callingAppPackageName
+ ", action : " + action);
}
SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
mSwitchBar.setTitle(getContext().getString(R.string.bluetooth_main_switch_title));
mController = new BluetoothSwitchPreferenceController(activity,
new MainSwitchBarController(mSwitchBar), mFooterPreference);
mController.setAlwaysDiscoverable(isAlwaysDiscoverable(callingAppPackageName, action));
Lifecycle lifecycle = getSettingsLifecycle();
if (lifecycle != null) {
lifecycle.addObserver(mController);
}
}
@VisibleForTesting
boolean isAlwaysDiscoverable(String callingAppPackageName, String action) {
return TextUtils.equals(SLICE_ACTION, action) ? false
: TextUtils.equals(SETTINGS_PACKAGE_NAME, callingAppPackageName)
|| TextUtils.equals(SYSTEMUI_PACKAGE_NAME, callingAppPackageName);
}
/**
* For Search.
*/