From a0628cc521614835933db74ba5a2536a936629c5 Mon Sep 17 00:00:00 2001 From: Tony Wickham Date: Wed, 14 Oct 2015 15:23:04 -0700 Subject: Fix "The specified child already has a parent" IllegalStateException. The problem was due to a race condition between removing a prebound widget view from the drag layer and adding the same view to the workspace upon dropping it; if you let go of the widget immediately after picking it up, the latter happened before the former. Specifically, the flow was: long-click a widget --> drop --> remove the view from the drag layer if it's not null (it is, so nothing happens) --> the view is finally bound/inflated and added to the drag layer --> add the view to the workspace --> already has a parent. There are actually 2 problems here: one is that the bind/inflate is asynchronous, and can therefore happen after dropping the widget view being inflated, and the other is that the view is added to the workspace even though the transition has barely started (we usually ignore drops if the transition is less than half complete). It turns out that this second problem was also due to a race condition, this time between dropping a widget or app onto the workspace and calling LauncherStateTransitionAnimation.dispatchOnLauncherTransitionStart(). If the drop happened before the dispatch, as in the case of the crash, then the drop was accepted because the transition progress was still 1.0 from the previous transition. I fixed the first problem by removing the drag layer widget view in Launcher where it is potentially used instead of Workspace. And I fixed the second problem by setting mTransitionProgress to 0 in Workspace.onLauncherTransitionPrepare(). I also added some debugging logs. Bug: 23896857 Change-Id: I66944e6d3f23b70dea15f7fb01af0763a1bfcbda --- .../android/launcher3/widget/WidgetsContainerView.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/com/android/launcher3/widget/WidgetsContainerView.java') diff --git a/src/com/android/launcher3/widget/WidgetsContainerView.java b/src/com/android/launcher3/widget/WidgetsContainerView.java index 64d33aa09..b780f590b 100644 --- a/src/com/android/launcher3/widget/WidgetsContainerView.java +++ b/src/com/android/launcher3/widget/WidgetsContainerView.java @@ -52,10 +52,9 @@ import com.android.launcher3.util.Thunk; * The widgets list view container. */ public class WidgetsContainerView extends BaseContainerView - implements View.OnLongClickListener, View.OnClickListener, DragSource{ - + implements View.OnLongClickListener, View.OnClickListener, DragSource { private static final String TAG = "WidgetsContainerView"; - private static final boolean DEBUG = false; + private static final boolean LOGD = false; /* Coefficient multiplied to the screen height for preloading widgets. */ private static final int PRELOAD_SCREEN_HEIGHT_MULTIPLE = 1; @@ -92,13 +91,14 @@ public class WidgetsContainerView extends BaseContainerView mDragController = mLauncher.getDragController(); mAdapter = new WidgetsListAdapter(context, this, this, mLauncher); mIconCache = (LauncherAppState.getInstance()).getIconCache(); - if (DEBUG) { + if (LOGD) { Log.d(TAG, "WidgetsContainerView constructor"); } } @Override protected void onFinishInflate() { + super.onFinishInflate(); mContent = findViewById(R.id.content); mView = (WidgetsRecyclerView) findViewById(R.id.widgets_list_view); mView.setAdapter(mAdapter); @@ -158,7 +158,7 @@ public class WidgetsContainerView extends BaseContainerView @Override public boolean onLongClick(View v) { - if (DEBUG) { + if (LOGD) { Log.d(TAG, String.format("onLonglick [v=%s]", v)); } // Return early if this is not initiated from a touch @@ -173,7 +173,7 @@ public class WidgetsContainerView extends BaseContainerView if (status && v.getTag() instanceof PendingAddWidgetInfo) { WidgetHostViewLoader hostLoader = new WidgetHostViewLoader(mLauncher, v); boolean preloadStatus = hostLoader.preloadWidget(); - if (DEBUG) { + if (LOGD) { Log.d(TAG, String.format("preloading widget [status=%s]", preloadStatus)); } mLauncher.getDragController().addDragListener(hostLoader); @@ -302,6 +302,9 @@ public class WidgetsContainerView extends BaseContainerView @Override public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete, boolean success) { + if (LOGD) { + Log.d(TAG, "onDropCompleted"); + } if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() && !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) { // Exit spring loaded mode if we have not successfully dropped or have not handled the -- cgit v1.2.3