[Audiosharing] Fix IllegalStateException at getChildFragmentManager
Fix: 349300412 Test: atest Flag: com.android.settingslib.flags.enable_le_audio_sharing Change-Id: I33a22ce12ae4c602be9aafdbd9cf9fdcee82bdde
This commit is contained in:
@@ -19,6 +19,7 @@ package com.android.settings.connecteddevice.audiosharing;
|
||||
import android.app.Dialog;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -66,7 +67,13 @@ public class AudioSharingCallAudioDialogFragment extends InstrumentedDialogFragm
|
||||
@NonNull List<AudioSharingDeviceItem> deviceItems,
|
||||
@NonNull DialogEventListener listener) {
|
||||
if (!AudioSharingUtils.isFeatureEnabled()) return;
|
||||
final FragmentManager manager = host.getChildFragmentManager();
|
||||
final FragmentManager manager;
|
||||
try {
|
||||
manager = host.getChildFragmentManager();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to show dialog: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
sListener = listener;
|
||||
if (manager.findFragmentByTag(TAG) == null) {
|
||||
final Bundle bundle = new Bundle();
|
||||
@@ -79,10 +86,18 @@ public class AudioSharingCallAudioDialogFragment extends InstrumentedDialogFragm
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
Bundle arguments = requireArguments();
|
||||
List<AudioSharingDeviceItem> deviceItems =
|
||||
arguments.getParcelable(BUNDLE_KEY_DEVICE_ITEMS, List.class);
|
||||
AlertDialog.Builder builder =
|
||||
new AlertDialog.Builder(getActivity())
|
||||
.setTitle(R.string.audio_sharing_call_audio_title);
|
||||
if (deviceItems == null) {
|
||||
Log.d(TAG, "Create dialog error: null deviceItems");
|
||||
return builder.create();
|
||||
}
|
||||
int checkedItem = -1;
|
||||
for (AudioSharingDeviceItem item : deviceItems) {
|
||||
int fallbackActiveGroupId = AudioSharingUtils.getFallbackActiveGroupId(getContext());
|
||||
@@ -92,17 +107,14 @@ public class AudioSharingCallAudioDialogFragment extends InstrumentedDialogFragm
|
||||
}
|
||||
String[] choices =
|
||||
deviceItems.stream().map(AudioSharingDeviceItem::getName).toArray(String[]::new);
|
||||
AlertDialog.Builder builder =
|
||||
new AlertDialog.Builder(getActivity())
|
||||
.setTitle(R.string.audio_sharing_call_audio_title)
|
||||
.setSingleChoiceItems(
|
||||
choices,
|
||||
checkedItem,
|
||||
(dialog, which) -> {
|
||||
if (sListener != null) {
|
||||
sListener.onItemClick(deviceItems.get(which));
|
||||
}
|
||||
});
|
||||
builder.setSingleChoiceItems(
|
||||
choices,
|
||||
checkedItem,
|
||||
(dialog, which) -> {
|
||||
if (sListener != null) {
|
||||
sListener.onItemClick(deviceItems.get(which));
|
||||
}
|
||||
});
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ import android.app.settings.SettingsEnums;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
@@ -44,7 +45,13 @@ public class AudioSharingConfirmDialogFragment extends InstrumentedDialogFragmen
|
||||
*/
|
||||
public static void show(Fragment host) {
|
||||
if (!AudioSharingUtils.isFeatureEnabled()) return;
|
||||
FragmentManager manager = host.getChildFragmentManager();
|
||||
final FragmentManager manager;
|
||||
try {
|
||||
manager = host.getChildFragmentManager();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to show dialog: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, TAG);
|
||||
if (dialog != null) {
|
||||
Log.d(TAG, "Dialog is showing, return.");
|
||||
@@ -56,6 +63,7 @@ public class AudioSharingConfirmDialogFragment extends InstrumentedDialogFragmen
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
AlertDialog dialog =
|
||||
AudioSharingDialogFactory.newBuilder(getActivity())
|
||||
|
@@ -77,7 +77,13 @@ public class AudioSharingDialogFragment extends InstrumentedDialogFragment {
|
||||
@NonNull DialogEventListener listener,
|
||||
@NonNull Pair<Integer, Object>[] eventData) {
|
||||
if (!AudioSharingUtils.isFeatureEnabled()) return;
|
||||
final FragmentManager manager = host.getChildFragmentManager();
|
||||
final FragmentManager manager;
|
||||
try {
|
||||
manager = host.getChildFragmentManager();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to show dialog: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
sListener = listener;
|
||||
sEventData = eventData;
|
||||
AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, TAG);
|
||||
|
@@ -414,7 +414,13 @@ public class AudioSharingDialogHandler {
|
||||
|
||||
private void closeOpeningDialogsOtherThan(String tag) {
|
||||
if (mHostFragment == null) return;
|
||||
List<Fragment> fragments = mHostFragment.getChildFragmentManager().getFragments();
|
||||
List<Fragment> fragments;
|
||||
try {
|
||||
fragments = mHostFragment.getChildFragmentManager().getFragments();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to closeOpeningDialogsOtherThan " + tag + ": " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
for (Fragment fragment : fragments) {
|
||||
if (fragment instanceof DialogFragment
|
||||
&& fragment.getTag() != null
|
||||
@@ -430,7 +436,13 @@ public class AudioSharingDialogHandler {
|
||||
public void closeOpeningDialogsForLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
|
||||
if (mHostFragment == null) return;
|
||||
int groupId = AudioSharingUtils.getGroupId(cachedDevice);
|
||||
List<Fragment> fragments = mHostFragment.getChildFragmentManager().getFragments();
|
||||
List<Fragment> fragments;
|
||||
try {
|
||||
fragments = mHostFragment.getChildFragmentManager().getFragments();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to closeOpeningDialogsForLeaDevice: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
for (Fragment fragment : fragments) {
|
||||
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
|
||||
if (device != null
|
||||
@@ -447,7 +459,13 @@ public class AudioSharingDialogHandler {
|
||||
public void closeOpeningDialogsForNonLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
|
||||
if (mHostFragment == null) return;
|
||||
String address = cachedDevice.getAddress();
|
||||
List<Fragment> fragments = mHostFragment.getChildFragmentManager().getFragments();
|
||||
List<Fragment> fragments;
|
||||
try {
|
||||
fragments = mHostFragment.getChildFragmentManager().getFragments();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to closeOpeningDialogsForNonLeaDevice: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
for (Fragment fragment : fragments) {
|
||||
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
|
||||
if (device != null && address != null && address.equals(device.getAddress())) {
|
||||
|
@@ -51,12 +51,13 @@ public class AudioSharingDialogHelper {
|
||||
public static AlertDialog getDialogIfShowing(
|
||||
@NonNull FragmentManager manager, @NonNull String tag) {
|
||||
Fragment dialog = manager.findFragmentByTag(tag);
|
||||
return dialog != null
|
||||
&& dialog instanceof DialogFragment
|
||||
&& ((DialogFragment) dialog).getDialog() != null
|
||||
&& ((DialogFragment) dialog).getDialog().isShowing()
|
||||
&& ((DialogFragment) dialog).getDialog() instanceof AlertDialog
|
||||
return dialog instanceof DialogFragment
|
||||
&& ((DialogFragment) dialog).getDialog() != null
|
||||
&& ((DialogFragment) dialog).getDialog().isShowing()
|
||||
&& ((DialogFragment) dialog).getDialog() instanceof AlertDialog
|
||||
? (AlertDialog) ((DialogFragment) dialog).getDialog()
|
||||
: null;
|
||||
}
|
||||
|
||||
private AudioSharingDialogHelper() {}
|
||||
}
|
||||
|
@@ -84,7 +84,13 @@ public class AudioSharingDisconnectDialogFragment extends InstrumentedDialogFrag
|
||||
@NonNull DialogEventListener listener,
|
||||
@NonNull Pair<Integer, Object>[] eventData) {
|
||||
if (!AudioSharingUtils.isFeatureEnabled()) return;
|
||||
FragmentManager manager = host.getChildFragmentManager();
|
||||
final FragmentManager manager;
|
||||
try {
|
||||
manager = host.getChildFragmentManager();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to show dialog: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, TAG);
|
||||
if (dialog != null) {
|
||||
int newGroupId = AudioSharingUtils.getGroupId(newDevice);
|
||||
|
@@ -81,7 +81,13 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
|
||||
@NonNull DialogEventListener listener,
|
||||
@NonNull Pair<Integer, Object>[] eventData) {
|
||||
if (!AudioSharingUtils.isFeatureEnabled()) return;
|
||||
final FragmentManager manager = host.getChildFragmentManager();
|
||||
final FragmentManager manager;
|
||||
try {
|
||||
manager = host.getChildFragmentManager();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to show dialog: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
sListener = listener;
|
||||
sNewDevice = newDevice;
|
||||
sEventData = eventData;
|
||||
|
@@ -81,7 +81,13 @@ public class AudioSharingStopDialogFragment extends InstrumentedDialogFragment {
|
||||
@NonNull DialogEventListener listener,
|
||||
@NonNull Pair<Integer, Object>[] eventData) {
|
||||
if (!AudioSharingUtils.isFeatureEnabled()) return;
|
||||
final FragmentManager manager = host.getChildFragmentManager();
|
||||
final FragmentManager manager;
|
||||
try {
|
||||
manager = host.getChildFragmentManager();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.d(TAG, "Fail to show dialog: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, TAG);
|
||||
if (dialog != null) {
|
||||
int newGroupId = AudioSharingUtils.getGroupId(newDevice);
|
||||
|
Reference in New Issue
Block a user