Skip to content

Commit

Permalink
Have the AccessibilityBridge attach/detach itself to the (flutter#8229)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amirh authored Mar 20, 2019
1 parent 45b19e4 commit 6a8a45f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,12 @@ public void detach() {
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;
}
Expand Down
20 changes: 19 additions & 1 deletion shell/platform/android/io/flutter/view/AccessibilityBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ public AccessibilityBridge(
@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;
Expand Down Expand Up @@ -362,6 +365,14 @@ public void onTouchExplorationStateChanged(boolean isTouchExplorationEnabled) {
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);
}
}

/**
Expand All @@ -372,6 +383,13 @@ public void onTouchExplorationStateChanged(boolean isTouchExplorationEnabled) {
* 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) {
Expand Down
2 changes: 0 additions & 2 deletions shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,6 @@ protected void onAttachedToWindow() {
getContext().getContentResolver(),
platformViewsController
);
platformViewsController.attachAccessibilityBridge(mAccessibilityNodeProvider);
mAccessibilityNodeProvider.setOnAccessibilityChangeListener(onAccessibilityChangeListener);

resetWillNotDraw(
Expand All @@ -681,7 +680,6 @@ protected void onAttachedToWindow() {
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();

getPluginRegistry().getPlatformViewsController().detachAccessibiltyBridge();
mAccessibilityNodeProvider.release();
mAccessibilityNodeProvider = null;
}
Expand Down

0 comments on commit 6a8a45f

Please sign in to comment.