From 6a8a45fc4f698364d03b5902c1818d2acc79db22 Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Wed, 20 Mar 2019 12:26:30 -0700 Subject: [PATCH] Have the AccessibilityBridge attach/detach itself to the (#8229) PlatformViewsDelegate. Since onDetachedFromWindow can be called after the activity was destroyed, the previous call to detach the accessibility bridge could have crash as the NativeFlutterView was already null. --- .../PlatformViewsAccessibilityDelegate.java | 18 +++++++++++++++++ .../platform/PlatformViewsController.java | 13 ++---------- .../io/flutter/view/AccessibilityBridge.java | 20 ++++++++++++++++++- .../android/io/flutter/view/FlutterView.java | 2 -- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java index 8f07f6b0c5b..a48a7d59ff6 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java @@ -4,10 +4,28 @@ package io.flutter.plugin.platform; +import io.flutter.view.AccessibilityBridge; + /** * Facilitates interaction between the accessibility bridge and embedded platform views. */ public interface PlatformViewsAccessibilityDelegate { // TODO(amirh): add a View getViewById(int id) here. // not filing a tracking issue as this is going to be done in the next PR. + + /** + * Attaches an accessibility bridge for this platform views accessibility delegate. + * + * Accessibility events originating in platform views belonging to this delegate will be delegated + * to this accessibility bridge. + */ + void attachAccessibilityBridge(AccessibilityBridge accessibilityBridge); + + /** + * Detaches the current accessibility bridge. + * + * Any accessibility events sent by platform views belonging to this delegate will be ignored until + * a new accessibility bridge is attached. + */ + void detachAccessibiltyBridge(); } diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index bd2ba001e22..55a5d10f473 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -98,21 +98,12 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler, mTextureRegistry = null; } - /** - * Attaches an accessibility bridge for this platform views controller. - * - * Accessibility events sent by platform views that belonging to this controller will be - * dispatched to this accessibility bridge. - */ + @Override public void attachAccessibilityBridge(AccessibilityBridge accessibilityBridge) { this.accessibilityBridge = accessibilityBridge; } - /** - * Detaches the current accessibility bridge. - * - * Any accessibility events sent by platform views belonging to this controller will be ignored. - */ + @Override public void detachAccessibiltyBridge() { this.accessibilityBridge = null; } diff --git a/shell/platform/android/io/flutter/view/AccessibilityBridge.java b/shell/platform/android/io/flutter/view/AccessibilityBridge.java index f18616b1578..7a94b330359 100644 --- a/shell/platform/android/io/flutter/view/AccessibilityBridge.java +++ b/shell/platform/android/io/flutter/view/AccessibilityBridge.java @@ -312,7 +312,10 @@ public class AccessibilityBridge extends AccessibilityNodeProvider { @NonNull AccessibilityChannel accessibilityChannel, @NonNull AccessibilityManager accessibilityManager, @NonNull ContentResolver contentResolver, - @NonNull PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate + // This should be @NonNull once the plumbing for io.flutter.embedding.engine.android.FlutterView is done. + // TODO(mattcarrol): Add the annotation once the plumbing is done. + // https://github.com/flutter/flutter/issues/29618 + PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate ) { this.rootAccessibilityView = rootAccessibilityView; this.accessibilityChannel = accessibilityChannel; @@ -362,6 +365,14 @@ public class AccessibilityBridge extends AccessibilityNodeProvider { Uri transitionUri = Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE); this.contentResolver.registerContentObserver(transitionUri, false, animationScaleObserver); } + + // platformViewsAccessibilityDelegate should be @NonNull once the plumbing + // for io.flutter.embedding.engine.android.FlutterView is done. + // TODO(mattcarrol): Remove the null check once the plumbing is done. + // https://github.com/flutter/flutter/issues/29618 + if (platformViewsAccessibilityDelegate != null) { + platformViewsAccessibilityDelegate.attachAccessibilityBridge(this); + } } /** @@ -372,6 +383,13 @@ public class AccessibilityBridge extends AccessibilityNodeProvider { * on this {@code AccessibilityBridge} after invoking {@code release()} is undefined. */ public void release() { + // platformViewsAccessibilityDelegate should be @NonNull once the plumbing + // for io.flutter.embedding.engine.android.FlutterView is done. + // TODO(mattcarrol): Remove the null check once the plumbing is done. + // https://github.com/flutter/flutter/issues/29618 + if (platformViewsAccessibilityDelegate != null) { + platformViewsAccessibilityDelegate.detachAccessibiltyBridge(); + } setOnAccessibilityChangeListener(null); accessibilityManager.removeAccessibilityStateChangeListener(accessibilityStateChangeListener); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { diff --git a/shell/platform/android/io/flutter/view/FlutterView.java b/shell/platform/android/io/flutter/view/FlutterView.java index 4faef7d518b..f8f38abbd48 100644 --- a/shell/platform/android/io/flutter/view/FlutterView.java +++ b/shell/platform/android/io/flutter/view/FlutterView.java @@ -668,7 +668,6 @@ public class FlutterView extends SurfaceView implements BinaryMessenger, Texture getContext().getContentResolver(), platformViewsController ); - platformViewsController.attachAccessibilityBridge(mAccessibilityNodeProvider); mAccessibilityNodeProvider.setOnAccessibilityChangeListener(onAccessibilityChangeListener); resetWillNotDraw( @@ -681,7 +680,6 @@ public class FlutterView extends SurfaceView implements BinaryMessenger, Texture protected void onDetachedFromWindow() { super.onDetachedFromWindow(); - getPluginRegistry().getPlatformViewsController().detachAccessibiltyBridge(); mAccessibilityNodeProvider.release(); mAccessibilityNodeProvider = null; }