diff --git a/res/xml/bluetooth_audio_sharing.xml b/res/xml/bluetooth_audio_sharing.xml index 9ffa2b27798..dc577f6af9d 100644 --- a/res/xml/bluetooth_audio_sharing.xml +++ b/res/xml/bluetooth_audio_sharing.xml @@ -38,11 +38,17 @@ android:title="Play a test sound" settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingPlaySoundPreferenceController" /> - + + + + { + if (mPreference == null) { + Log.w( + TAG, + "Skip BT state change due to mPreference " + + "is null"); + } else { + mPreference.setVisible( + adapterState == BluetoothAdapter.STATE_ON); + } + }); + } + }; + + public StreamSettingsCategoryController(Context context, String key) { + super(context, key); + mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); + } + + @Override + public void onStart(@NonNull LifecycleOwner owner) { + mContext.registerReceiver(mReceiver, mIntentFilter, Context.RECEIVER_EXPORTED_UNAUDITED); + } + + @Override + public void onStop(@NonNull LifecycleOwner owner) { + mContext.unregisterReceiver(mReceiver); + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = screen.findPreference(getPreferenceKey()); + if (mPreference != null) { + mPreference.setVisible(isBluetoothStateOn()); + } + } + + @Override + public int getAvailabilityStatus() { + return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; + } + + private boolean isBluetoothStateOn() { + return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled(); + } +}