[Audiosharing] Impl a base controller for audio sharing page.
The preference controllers attached to audio sharing page will extends this base controller so that their visibility will be updated together depending on the main switch state. Flagged with enable_le_audio_sharing Bug: 305620450 Test: Manual Change-Id: I3d37c87a128098a1b1a38cb8621be9a4d4e8c15e
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2023 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.connecteddevice.audiosharing;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.bluetooth.Utils;
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settings.flags.Flags;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
|
public abstract class AudioSharingBasePreferenceController extends BasePreferenceController {
|
||||||
|
private final LocalBluetoothManager mBtManager;
|
||||||
|
protected final LocalBluetoothLeBroadcast mBroadcast;
|
||||||
|
protected Preference mPreference;
|
||||||
|
|
||||||
|
public AudioSharingBasePreferenceController(Context context, String preferenceKey) {
|
||||||
|
super(context, preferenceKey);
|
||||||
|
mBtManager = Utils.getLocalBtManager(context);
|
||||||
|
mBroadcast =
|
||||||
|
mBtManager == null
|
||||||
|
? null
|
||||||
|
: mBtManager.getProfileManager().getLeAudioBroadcastProfile();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
return mBtManager != null && Flags.enableLeAudioSharing()
|
||||||
|
? AVAILABLE
|
||||||
|
: UNSUPPORTED_ON_DEVICE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
|
super.displayPreference(screen);
|
||||||
|
mPreference = screen.findPreference(getPreferenceKey());
|
||||||
|
updateVisibility(isBroadcasting());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the visibility of the preference.
|
||||||
|
*
|
||||||
|
* @param isVisible the latest visibility state for the preference.
|
||||||
|
*/
|
||||||
|
public void updateVisibility(boolean isVisible) {
|
||||||
|
mPreference.setVisible(isVisible);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isBroadcasting() {
|
||||||
|
return mBroadcast != null && mBroadcast.isEnabled(null);
|
||||||
|
}
|
||||||
|
}
|
@@ -25,11 +25,14 @@ import com.android.settings.SettingsActivity;
|
|||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
import com.android.settings.widget.SettingsMainSwitchBar;
|
import com.android.settings.widget.SettingsMainSwitchBar;
|
||||||
|
|
||||||
public class AudioSharingDashboardFragment extends DashboardFragment {
|
public class AudioSharingDashboardFragment extends DashboardFragment
|
||||||
|
implements AudioSharingSwitchBarController.OnSwitchBarChangedListener {
|
||||||
private static final String TAG = "AudioSharingDashboardFrag";
|
private static final String TAG = "AudioSharingDashboardFrag";
|
||||||
|
|
||||||
SettingsMainSwitchBar mMainSwitchBar;
|
SettingsMainSwitchBar mMainSwitchBar;
|
||||||
private AudioSharingSwitchBarController mSwitchBarController;
|
private AudioSharingSwitchBarController mSwitchBarController;
|
||||||
|
private CallsAndAlarmsPreferenceController mCallsAndAlarmsPreferenceController;
|
||||||
|
private AudioSharingNamePreferenceController mAudioSharingNamePreferenceController;
|
||||||
|
|
||||||
public AudioSharingDashboardFragment() {
|
public AudioSharingDashboardFragment() {
|
||||||
super();
|
super();
|
||||||
@@ -63,7 +66,9 @@ public class AudioSharingDashboardFragment extends DashboardFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
use(CallsAndAlarmsPreferenceController.class).init(this);
|
mCallsAndAlarmsPreferenceController = use(CallsAndAlarmsPreferenceController.class);
|
||||||
|
mCallsAndAlarmsPreferenceController.init(this);
|
||||||
|
mAudioSharingNamePreferenceController = use(AudioSharingNamePreferenceController.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,9 +79,19 @@ public class AudioSharingDashboardFragment extends DashboardFragment {
|
|||||||
final SettingsActivity activity = (SettingsActivity) getActivity();
|
final SettingsActivity activity = (SettingsActivity) getActivity();
|
||||||
mMainSwitchBar = activity.getSwitchBar();
|
mMainSwitchBar = activity.getSwitchBar();
|
||||||
mMainSwitchBar.setTitle(getText(R.string.audio_sharing_switch_title));
|
mMainSwitchBar.setTitle(getText(R.string.audio_sharing_switch_title));
|
||||||
mSwitchBarController = new AudioSharingSwitchBarController(activity, mMainSwitchBar);
|
mSwitchBarController = new AudioSharingSwitchBarController(activity, mMainSwitchBar, this);
|
||||||
mSwitchBarController.init(this);
|
mSwitchBarController.init(this);
|
||||||
getSettingsLifecycle().addObserver(mSwitchBarController);
|
getSettingsLifecycle().addObserver(mSwitchBarController);
|
||||||
mMainSwitchBar.show();
|
mMainSwitchBar.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSwitchBarChanged(boolean newState) {
|
||||||
|
updateVisibilityForAttachedPreferences(newState);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateVisibilityForAttachedPreferences(boolean isVisible) {
|
||||||
|
mCallsAndAlarmsPreferenceController.updateVisibility(isVisible);
|
||||||
|
mAudioSharingNamePreferenceController.updateVisibility(isVisible);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,13 +22,10 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import com.android.settings.core.BasePreferenceController;
|
|
||||||
import com.android.settings.flags.Flags;
|
|
||||||
import com.android.settings.widget.ValidatedEditTextPreference;
|
import com.android.settings.widget.ValidatedEditTextPreference;
|
||||||
|
|
||||||
public class AudioSharingNamePreferenceController extends BasePreferenceController
|
public class AudioSharingNamePreferenceController extends AudioSharingBasePreferenceController
|
||||||
implements ValidatedEditTextPreference.Validator,
|
implements ValidatedEditTextPreference.Validator,
|
||||||
Preference.OnPreferenceChangeListener,
|
Preference.OnPreferenceChangeListener,
|
||||||
DefaultLifecycleObserver {
|
DefaultLifecycleObserver {
|
||||||
@@ -37,8 +34,6 @@ public class AudioSharingNamePreferenceController extends BasePreferenceControll
|
|||||||
|
|
||||||
private static final String PREF_KEY = "audio_sharing_stream_name";
|
private static final String PREF_KEY = "audio_sharing_stream_name";
|
||||||
|
|
||||||
protected Preference mPreference;
|
|
||||||
|
|
||||||
private AudioSharingNameTextValidator mAudioSharingNameTextValidator;
|
private AudioSharingNameTextValidator mAudioSharingNameTextValidator;
|
||||||
|
|
||||||
public AudioSharingNamePreferenceController(Context context) {
|
public AudioSharingNamePreferenceController(Context context) {
|
||||||
@@ -46,11 +41,6 @@ public class AudioSharingNamePreferenceController extends BasePreferenceControll
|
|||||||
mAudioSharingNameTextValidator = new AudioSharingNameTextValidator();
|
mAudioSharingNameTextValidator = new AudioSharingNameTextValidator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getAvailabilityStatus() {
|
|
||||||
return Flags.enableLeAudioSharing() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPreferenceKey() {
|
public String getPreferenceKey() {
|
||||||
return PREF_KEY;
|
return PREF_KEY;
|
||||||
@@ -62,12 +52,6 @@ public class AudioSharingNamePreferenceController extends BasePreferenceControll
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
|
||||||
super.displayPreference(screen);
|
|
||||||
mPreference = screen.findPreference(getPreferenceKey());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isTextValid(String value) {
|
public boolean isTextValid(String value) {
|
||||||
return mAudioSharingNameTextValidator.isTextValid(value);
|
return mAudioSharingNameTextValidator.isTextValid(value);
|
||||||
|
@@ -58,11 +58,17 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
implements DefaultLifecycleObserver, OnCheckedChangeListener {
|
implements DefaultLifecycleObserver, OnCheckedChangeListener {
|
||||||
private static final String TAG = "AudioSharingSwitchBarCtl";
|
private static final String TAG = "AudioSharingSwitchBarCtl";
|
||||||
private static final String PREF_KEY = "audio_sharing_main_switch";
|
private static final String PREF_KEY = "audio_sharing_main_switch";
|
||||||
|
|
||||||
|
interface OnSwitchBarChangedListener {
|
||||||
|
void onSwitchBarChanged(boolean newState);
|
||||||
|
}
|
||||||
|
|
||||||
private final SettingsMainSwitchBar mSwitchBar;
|
private final SettingsMainSwitchBar mSwitchBar;
|
||||||
private final LocalBluetoothManager mBtManager;
|
private final LocalBluetoothManager mBtManager;
|
||||||
private final LocalBluetoothLeBroadcast mBroadcast;
|
private final LocalBluetoothLeBroadcast mBroadcast;
|
||||||
private final LocalBluetoothLeBroadcastAssistant mAssistant;
|
private final LocalBluetoothLeBroadcastAssistant mAssistant;
|
||||||
private final Executor mExecutor;
|
private final Executor mExecutor;
|
||||||
|
private final OnSwitchBarChangedListener mListener;
|
||||||
private DashboardFragment mFragment;
|
private DashboardFragment mFragment;
|
||||||
private List<BluetoothDevice> mTargetSinks = new ArrayList<>();
|
private List<BluetoothDevice> mTargetSinks = new ArrayList<>();
|
||||||
|
|
||||||
@@ -196,9 +202,11 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
BluetoothLeBroadcastReceiveState state) {}
|
BluetoothLeBroadcastReceiveState state) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioSharingSwitchBarController(Context context, SettingsMainSwitchBar switchBar) {
|
AudioSharingSwitchBarController(
|
||||||
|
Context context, SettingsMainSwitchBar switchBar, OnSwitchBarChangedListener listener) {
|
||||||
super(context, PREF_KEY);
|
super(context, PREF_KEY);
|
||||||
mSwitchBar = switchBar;
|
mSwitchBar = switchBar;
|
||||||
|
mListener = listener;
|
||||||
mBtManager = Utils.getLocalBtManager(context);
|
mBtManager = Utils.getLocalBtManager(context);
|
||||||
mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
|
mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
|
||||||
mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
|
mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
|
||||||
@@ -326,8 +334,12 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
private void updateSwitch() {
|
private void updateSwitch() {
|
||||||
ThreadUtils.postOnMainThread(
|
ThreadUtils.postOnMainThread(
|
||||||
() -> {
|
() -> {
|
||||||
mSwitchBar.setChecked(isBroadcasting());
|
boolean isBroadcasting = isBroadcasting();
|
||||||
|
if (mSwitchBar.isChecked() != isBroadcasting) {
|
||||||
|
mSwitchBar.setChecked(isBroadcasting);
|
||||||
|
}
|
||||||
mSwitchBar.setEnabled(true);
|
mSwitchBar.setEnabled(true);
|
||||||
|
mListener.onSwitchBarChanged(isBroadcasting);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -19,32 +19,22 @@ package com.android.settings.connecteddevice.audiosharing;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.core.BasePreferenceController;
|
|
||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
import com.android.settings.flags.Flags;
|
|
||||||
|
|
||||||
/** PreferenceController to control the dialog to choose the active device for calls and alarms */
|
/** PreferenceController to control the dialog to choose the active device for calls and alarms */
|
||||||
public class CallsAndAlarmsPreferenceController extends BasePreferenceController {
|
public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferenceController {
|
||||||
|
|
||||||
private static final String TAG = "CallsAndAlarmsPreferenceController";
|
private static final String TAG = "CallsAndAlarmsPreferenceController";
|
||||||
|
|
||||||
private static final String PREF_KEY = "calls_and_alarms";
|
private static final String PREF_KEY = "calls_and_alarms";
|
||||||
|
|
||||||
private Preference mPreference;
|
|
||||||
private DashboardFragment mFragment;
|
private DashboardFragment mFragment;
|
||||||
|
|
||||||
public CallsAndAlarmsPreferenceController(Context context) {
|
public CallsAndAlarmsPreferenceController(Context context) {
|
||||||
super(context, PREF_KEY);
|
super(context, PREF_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getAvailabilityStatus() {
|
|
||||||
return Flags.enableLeAudioSharing() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPreferenceKey() {
|
public String getPreferenceKey() {
|
||||||
return PREF_KEY;
|
return PREF_KEY;
|
||||||
@@ -53,7 +43,6 @@ public class CallsAndAlarmsPreferenceController extends BasePreferenceController
|
|||||||
@Override
|
@Override
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
mPreference = screen.findPreference(getPreferenceKey());
|
|
||||||
mPreference.setOnPreferenceClickListener(
|
mPreference.setOnPreferenceClickListener(
|
||||||
preference -> {
|
preference -> {
|
||||||
if (mFragment != null) {
|
if (mFragment != null) {
|
||||||
|
Reference in New Issue
Block a user