AccessibilityBridge.java 22.9 KB
Newer Older
H
Hixie 已提交
1 2 3 4
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
package io.flutter.view;
H
Hixie 已提交
6 7 8

import android.graphics.Rect;
import android.opengl.Matrix;
H
Hixie 已提交
9
import android.os.Bundle;
10
import android.util.Log;
H
Hixie 已提交
11
import android.view.View;
12
import android.view.accessibility.AccessibilityEvent;
H
Hixie 已提交
13 14
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeProvider;
15
import io.flutter.plugin.common.BasicMessageChannel;
16
import io.flutter.plugin.common.StandardMessageCodec;
H
Hixie 已提交
17

18
import java.nio.ByteBuffer;
H
Hixie 已提交
19
import java.util.ArrayList;
20
import java.util.Arrays;
21 22
import java.util.Collections;
import java.util.Comparator;
H
Hixie 已提交
23
import java.util.HashMap;
24
import java.util.HashSet;
25
import java.util.Iterator;
H
Hixie 已提交
26 27
import java.util.List;
import java.util.Map;
28
import java.util.Set;
H
Hixie 已提交
29

30
class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMessageChannel.MessageHandler<Object> {
31 32 33
    private static final String TAG = "FlutterView";

    private Map<Integer, SemanticsObject> mObjects;
34
    private FlutterView mOwner;
35
    private boolean mAccessibilityEnabled = false;
36 37 38
    private SemanticsObject mFocusedObject;
    private SemanticsObject mHoveredObject;

39 40
    private final BasicMessageChannel<Object> mFlutterAccessibilityChannel;

41 42 43 44 45 46 47 48
    private static final int SEMANTICS_ACTION_TAP = 1 << 0;
    private static final int SEMANTICS_ACTION_LONG_PRESS = 1 << 1;
    private static final int SEMANTICS_ACTION_SCROLL_LEFT = 1 << 2;
    private static final int SEMANTICS_ACTION_SCROLL_RIGHT = 1 << 3;
    private static final int SEMANTICS_ACTION_SCROLL_UP = 1 << 4;
    private static final int SEMANTICS_ACTION_SCROLL_DOWN = 1 << 5;
    private static final int SEMANTICS_ACTION_INCREASE = 1 << 6;
    private static final int SEMANTICS_ACTION_DECREASE = 1 << 7;
49
    private static final int SEMANTICS_ACTION_SHOW_ON_SCREEN = 1 << 8;
50 51 52 53 54 55 56 57

    private static final int SEMANTICS_ACTION_SCROLLABLE = SEMANTICS_ACTION_SCROLL_LEFT |
                                                           SEMANTICS_ACTION_SCROLL_RIGHT |
                                                           SEMANTICS_ACTION_SCROLL_UP |
                                                           SEMANTICS_ACTION_SCROLL_DOWN;

    private static final int SEMANTICS_FLAG_HAS_CHECKED_STATE = 1 << 0;
    private static final int SEMANTICS_FLAG_IS_CHECKED = 1 << 1;
58
    private static final int SEMANTICS_FLAG_IS_SELECTED = 1 << 2;
59 60

    AccessibilityBridge(FlutterView owner) {
H
Hixie 已提交
61 62
        assert owner != null;
        mOwner = owner;
63
        mObjects = new HashMap<Integer, SemanticsObject>();
64
        mFlutterAccessibilityChannel = new BasicMessageChannel<>(owner, "flutter/accessibility",
65
            StandardMessageCodec.INSTANCE);
H
Hixie 已提交
66 67
    }

68
    void setAccessibilityEnabled(boolean accessibilityEnabled) {
69
        mAccessibilityEnabled = accessibilityEnabled;
70 71 72 73 74
        if (accessibilityEnabled) {
            mFlutterAccessibilityChannel.setMessageHandler(this);
        } else {
            mFlutterAccessibilityChannel.setMessageHandler(null);
        }
H
Hixie 已提交
75 76
    }

H
Hixie 已提交
77
    @Override
78
    @SuppressWarnings("deprecation")
H
Hixie 已提交
79 80 81 82
    public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
        if (virtualViewId == View.NO_ID) {
            AccessibilityNodeInfo result = AccessibilityNodeInfo.obtain(mOwner);
            mOwner.onInitializeAccessibilityNodeInfo(result);
83
            if (mObjects.containsKey(0))
H
Hixie 已提交
84 85 86 87
                result.addChild(mOwner, 0);
            return result;
        }

88
        SemanticsObject object = mObjects.get(virtualViewId);
89
        if (object == null)
H
Hixie 已提交
90 91 92 93
            return null;

        AccessibilityNodeInfo result = AccessibilityNodeInfo.obtain(mOwner, virtualViewId);
        result.setPackageName(mOwner.getContext().getPackageName());
94
        result.setClassName("Flutter"); // TODO(goderbauer): Set proper class names
H
Hixie 已提交
95
        result.setSource(mOwner, virtualViewId);
96
        result.setFocusable(object.isFocusable());
H
Hixie 已提交
97

98 99 100
        if (object.parent != null) {
            assert object.id > 0;
            result.setParent(mOwner, object.parent.id);
H
Hixie 已提交
101
        } else {
102
            assert object.id == 0;
H
Hixie 已提交
103 104 105
            result.setParent(mOwner);
        }

106 107 108
        Rect bounds = object.getGlobalRect();
        if (object.parent != null) {
            Rect parentBounds = object.parent.getGlobalRect();
H
Hixie 已提交
109 110 111 112 113 114 115 116
            Rect boundsInParent = new Rect(bounds);
            boundsInParent.offset(-parentBounds.left, -parentBounds.top);
            result.setBoundsInParent(boundsInParent);
        } else {
            result.setBoundsInParent(bounds);
        }
        result.setBoundsInScreen(bounds);
        result.setVisibleToUser(true);
H
Hixie 已提交
117
        result.setEnabled(true); // TODO(ianh): Expose disabled subtrees
H
Hixie 已提交
118

119
        if ((object.actions & SEMANTICS_ACTION_TAP) != 0) {
120
            result.addAction(AccessibilityNodeInfo.ACTION_CLICK);
H
Hixie 已提交
121 122
            result.setClickable(true);
        }
123
        if ((object.actions & SEMANTICS_ACTION_LONG_PRESS) != 0) {
124
            result.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
H
Hixie 已提交
125 126
            result.setLongClickable(true);
        }
127
        if ((object.actions & SEMANTICS_ACTION_SCROLLABLE) != 0) {
H
Hixie 已提交
128
            result.setScrollable(true);
129 130 131
            // This tells Android's a11y to send scroll events when reaching the end of
            // the visible viewport of a scrollable.
            result.setClassName("android.widget.ScrollView");
132 133 134 135 136 137 138 139 140 141 142
            // TODO(ianh): Once we're on SDK v23+, call addAction to
            // expose AccessibilityAction.ACTION_SCROLL_LEFT, _RIGHT,
            // _UP, and _DOWN when appropriate.
            if ((object.actions & SEMANTICS_ACTION_SCROLL_RIGHT) != 0
                    || (object.actions & SEMANTICS_ACTION_SCROLL_UP) != 0) {
                result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
            }
            if ((object.actions & SEMANTICS_ACTION_SCROLL_LEFT) != 0
                    || (object.actions & SEMANTICS_ACTION_SCROLL_DOWN) != 0) {
                result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
            }
H
Hixie 已提交
143
        }
144 145
        if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0
                || (object.actions & SEMANTICS_ACTION_DECREASE) != 0 ) {
146 147 148 149 150 151 152 153
            result.setClassName("android.widget.SeekBar");
            if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0) {
                result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
            }
            if ((object.actions & SEMANTICS_ACTION_DECREASE) != 0) {
                result.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
            }
        }
H
Hixie 已提交
154

155 156
        result.setCheckable((object.flags & SEMANTICS_FLAG_HAS_CHECKED_STATE) != 0);
        result.setChecked((object.flags & SEMANTICS_FLAG_IS_CHECKED) != 0);
157
        result.setSelected((object.flags & SEMANTICS_FLAG_IS_SELECTED) != 0);
158
        result.setText(object.label);
H
Hixie 已提交
159

H
Hixie 已提交
160
        // Accessibility Focus
161
        if (mFocusedObject != null && mFocusedObject.id == virtualViewId) {
162
            result.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
H
Hixie 已提交
163
        } else {
164
            result.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
H
Hixie 已提交
165 166
        }

167
        if (object.children != null) {
168 169 170 171 172 173 174 175 176 177
            List<SemanticsObject> childrenInTraversalOrder =
                new ArrayList<SemanticsObject>(object.children);
            Collections.sort(childrenInTraversalOrder, new Comparator<SemanticsObject>() {
                public int compare(SemanticsObject a, SemanticsObject b) {
                    final int top = Integer.compare(a.globalRect.top, b.globalRect.top);
                    // TODO(goderbauer): sort right-to-left in rtl environments.
                    return top == 0 ? Integer.compare(a.globalRect.left, b.globalRect.left) : top;
                }
            });
            for (SemanticsObject child : childrenInTraversalOrder) {
178 179
                result.addChild(mOwner, child.id);
            }
H
Hixie 已提交
180 181 182 183 184
        }

        return result;
    }

H
Hixie 已提交
185 186
    @Override
    public boolean performAction(int virtualViewId, int action, Bundle arguments) {
187
        SemanticsObject object = mObjects.get(virtualViewId);
188
        if (object == null) {
H
Hixie 已提交
189
            return false;
190
        }
H
Hixie 已提交
191 192
        switch (action) {
            case AccessibilityNodeInfo.ACTION_CLICK: {
193
                mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_TAP);
H
Hixie 已提交
194 195 196
                return true;
            }
            case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
197
                mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_LONG_PRESS);
H
Hixie 已提交
198 199
                return true;
            }
200
            case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
201 202 203
                if ((object.actions & SEMANTICS_ACTION_SCROLL_UP) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_UP);
                } else if ((object.actions & SEMANTICS_ACTION_SCROLL_LEFT) != 0) {
204
                    // TODO(ianh): bidi support using textDirection
205
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_LEFT);
206 207
                } else if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_INCREASE);
H
Hixie 已提交
208 209 210 211 212
                } else {
                    return false;
                }
                return true;
            }
213
            case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
214 215 216
                if ((object.actions & SEMANTICS_ACTION_SCROLL_DOWN) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_DOWN);
                } else if ((object.actions & SEMANTICS_ACTION_SCROLL_RIGHT) != 0) {
217
                    // TODO(ianh): bidi support using textDirection
218
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_RIGHT);
219 220
                } else if ((object.actions & SEMANTICS_ACTION_DECREASE) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_DECREASE);
H
Hixie 已提交
221 222 223 224 225
                } else {
                    return false;
                }
                return true;
            }
H
Hixie 已提交
226 227
            case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
                sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
228
                mFocusedObject = null;
H
Hixie 已提交
229 230 231 232
                return true;
            }
            case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
                sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
233
                if (mFocusedObject == null) {
H
Hixie 已提交
234 235 236 237 238
                    // When Android focuses a node, it doesn't invalidate the view.
                    // (It does when it sends ACTION_CLEAR_ACCESSIBILITY_FOCUS, so
                    // we only have to worry about this when the focused node is null.)
                    mOwner.invalidate();
                }
239
                mFocusedObject = object;
H
Hixie 已提交
240 241
                return true;
            }
242 243 244 245 246 247
            // TODO(goderbauer): Use ACTION_SHOW_ON_SCREEN from Android Support Library after
            //     https://github.com/flutter/flutter/issues/11099 is resolved.
            case 16908342: { // ACTION_SHOW_ON_SCREEN, added in API level 23
                mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SHOW_ON_SCREEN);
                return true;
            }
H
Hixie 已提交
248 249 250 251
        }
        return false;
    }

252 253 254
    // TODO(ianh): implement findAccessibilityNodeInfosByText()
    // TODO(ianh): implement findFocus()

255 256
    private SemanticsObject getRootObject() {
      assert mObjects.containsKey(0);
257 258 259
      return mObjects.get(0);
    }

260 261 262 263 264 265 266 267 268 269
    private SemanticsObject getOrCreateObject(int id) {
      SemanticsObject object = mObjects.get(id);
      if (object == null) {
          object = new SemanticsObject();
          object.id = id;
          mObjects.put(id, object);
      }
      return object;
    }

270
    void handleTouchExplorationExit() {
271 272 273
        if (mHoveredObject != null) {
            sendAccessibilityEvent(mHoveredObject.id, AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
            mHoveredObject = null;
274 275 276
        }
    }

277
    void handleTouchExploration(float x, float y) {
278
        if (mObjects.isEmpty()) {
279
            return;
280
        }
281
        SemanticsObject newObject = getRootObject().hitTest(new float[]{ x, y, 0, 1 });
282
        if (newObject != mHoveredObject) {
H
Hixie 已提交
283
            // sending ENTER before EXIT is how Android wants it
284 285
            if (newObject != null) {
                sendAccessibilityEvent(newObject.id, AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
286
            }
287 288
            if (mHoveredObject != null) {
                sendAccessibilityEvent(mHoveredObject.id, AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
289
            }
290
            mHoveredObject = newObject;
291 292 293
        }
    }

294 295 296 297 298 299
    void updateSemantics(ByteBuffer buffer, String[] strings) {
        ArrayList<Integer> updated = new ArrayList<Integer>();
        while (buffer.hasRemaining()) {
            int id = buffer.getInt();
            getOrCreateObject(id).updateWith(buffer, strings);
            updated.add(id);
300
        }
301

302 303 304 305 306 307
        Set<SemanticsObject> visitedObjects = new HashSet<SemanticsObject>();
        SemanticsObject rootObject = getRootObject();
        if (rootObject != null) {
          final float[] identity = new float[16];
          Matrix.setIdentityM(identity, 0);
          rootObject.updateRecursively(identity, visitedObjects, false);
308
        }
309 310 311 312

        Iterator<Map.Entry<Integer, SemanticsObject>> it = mObjects.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, SemanticsObject> entry = it.next();
A
Adam Barth 已提交
313 314 315
            SemanticsObject object = entry.getValue();
            if (!visitedObjects.contains(object)) {
                willRemoveSemanticsObject(object);
316
                it.remove();
317 318
            }
        }
319 320 321

        for (Integer id : updated) {
            sendAccessibilityEvent(id, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
322
        }
323 324 325
    }

    private void sendAccessibilityEvent(int virtualViewId, int eventType) {
326
        if (!mAccessibilityEnabled) {
H
Hixie 已提交
327 328
            return;
        }
329 330 331 332 333 334 335
        if (virtualViewId == 0) {
            mOwner.sendAccessibilityEvent(eventType);
        } else {
            AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
            event.setPackageName(mOwner.getContext().getPackageName());
            event.setSource(mOwner, virtualViewId);
            mOwner.getParent().requestSendAccessibilityEvent(mOwner, event);
H
Hixie 已提交
336 337 338
        }
    }

339 340 341
    // Message Handler for [mFlutterAccessibilityChannel].
    public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
        @SuppressWarnings("unchecked")
342 343 344 345 346 347 348 349 350 351 352
        final HashMap<String, Object> annotatedEvent = (HashMap<String, Object>)message;
        final int nodeId = (int)annotatedEvent.get("nodeId");
        final String type = (String)annotatedEvent.get("type");

        switch (type) {
            case "scroll":
                sendAccessibilityEvent(nodeId, AccessibilityEvent.TYPE_VIEW_SCROLLED);
                break;
            default:
                assert false;
        }
353 354
    }

355
    private void willRemoveSemanticsObject(SemanticsObject object) {
356 357 358 359 360 361
        assert mObjects.containsKey(object.id);
        assert mObjects.get(object.id) == object;
        object.parent = null;
        if (mFocusedObject == object) {
            sendAccessibilityEvent(mFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
            mFocusedObject = null;
H
Hixie 已提交
362
        }
363 364
        if (mHoveredObject == object) {
            mHoveredObject = null;
365
        }
H
Hixie 已提交
366 367
    }

368
    void reset() {
369
        mObjects.clear();
370 371
        if (mFocusedObject != null)
            sendAccessibilityEvent(mFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
372 373
        mFocusedObject = null;
        mHoveredObject = null;
374
        sendAccessibilityEvent(0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
H
Hixie 已提交
375 376
    }

377 378 379 380 381 382 383 384 385 386 387 388 389 390
    private enum TextDirection {
        UNKNOWN, LTR, RTL;

        public static TextDirection fromInt(int value) {
            switch (value) {
                case 1:
                    return RTL;
                case 2:
                    return LTR;
            }
            return UNKNOWN;
        }
    }

391 392
    private class SemanticsObject {
        SemanticsObject() { }
393

394
        int id = -1;
395 396

        int flags;
A
Adam Barth 已提交
397
        int actions;
398
        String label;
399
        TextDirection textDirection;
H
Hixie 已提交
400 401 402

        private float left;
        private float top;
403 404 405
        private float right;
        private float bottom;
        private float[] transform;
H
Hixie 已提交
406

407
        SemanticsObject parent;
408
        List<SemanticsObject> children;  // In inverse hit test order (i.e. paint order).
H
Hixie 已提交
409

410 411
        private boolean inverseTransformDirty = true;
        private float[] inverseTransform;
H
Hixie 已提交
412

413 414 415
        private boolean globalGeometryDirty = true;
        private float[] globalTransform;
        private Rect globalRect;
H
Hixie 已提交
416

A
Adam Barth 已提交
417 418 419 420 421 422 423 424 425 426 427 428
        void log(String indent) {
          Log.i(TAG, indent + "SemanticsObject id=" + id + " label=" + label + " actions=" +  actions + " flags=" + flags + "\n" +
                     indent + "  +-- rect.ltrb=(" + left + ", " + top + ", " + right + ", " + bottom + ")\n" +
                     indent + "  +-- transform=" + Arrays.toString(transform) + "\n");
          if (children != null) {
              String childIndent = indent + "  ";
              for (SemanticsObject child : children) {
                  child.log(childIndent);
              }
          }
        }

429 430
        void updateWith(ByteBuffer buffer, String[] strings) {
            flags = buffer.getInt();
A
Adam Barth 已提交
431
            actions = buffer.getInt();
H
Hixie 已提交
432

433 434 435 436 437 438
            final int stringIndex = buffer.getInt();
            if (stringIndex == -1)
                label = null;
            else
                label = strings[stringIndex];

439 440
            textDirection = TextDirection.fromInt(buffer.getInt());

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
            left = buffer.getFloat();
            top = buffer.getFloat();
            right = buffer.getFloat();
            bottom = buffer.getFloat();

            if (transform == null)
                transform = new float[16];
            for (int i = 0; i < 16; ++i)
                transform[i] = buffer.getFloat();
            inverseTransformDirty = true;
            globalGeometryDirty = true;

            final int childCount = buffer.getInt();
            if (childCount == 0) {
                children = null;
            } else {
                if (children == null)
                    children = new ArrayList<SemanticsObject>(childCount);
                else
                    children.clear();

                for (int i = 0; i < childCount; ++i) {
                    SemanticsObject child = getOrCreateObject(buffer.getInt());
                    child.parent = this;
A
Adam Barth 已提交
465
                    children.add(child);
466 467
                }
            }
H
Hixie 已提交
468 469
        }

470 471 472 473 474 475 476 477
        private void ensureInverseTransform() {
            if (!inverseTransformDirty)
                return;
            inverseTransformDirty = false;
            if (inverseTransform == null)
                inverseTransform = new float[16];
            if (!Matrix.invertM(inverseTransform, 0, transform, 0))
                Arrays.fill(inverseTransform, 0);
H
Hixie 已提交
478 479
        }

480
        Rect getGlobalRect() {
481
            assert !globalGeometryDirty;
H
Hixie 已提交
482 483
            return globalRect;
        }
484

485 486 487 488 489
        SemanticsObject hitTest(float[] point) {
            final float w = point[3];
            final float x = point[0] / w;
            final float y = point[1] / w;
            if (x < left || x >= right || y < top || y >= bottom)
490
                return null;
491
            if (children != null) {
492 493 494 495 496 497
                final float[] transformedPoint = new float[4];
                for (int i = children.size() - 1; i >= 0; i -= 1) {
                    final SemanticsObject child = children.get(i);
                    child.ensureInverseTransform();
                    Matrix.multiplyMV(transformedPoint, 0, child.inverseTransform, 0, point, 0);
                    final SemanticsObject result = child.hitTest(transformedPoint);
498 499 500
                    if (result != null) {
                        return result;
                    }
501 502 503 504
                }
            }
            return this;
        }
H
Hixie 已提交
505

506
        boolean isFocusable() {
507
            return flags != 0 || (label != null && !label.isEmpty()) || (actions & ~SEMANTICS_ACTION_SCROLLABLE) != 0;
508 509
        }

510 511
        void updateRecursively(float[] ancestorTransform, Set<SemanticsObject> visitedObjects, boolean forceUpdate) {
            visitedObjects.add(this);
H
Hixie 已提交
512

513 514 515 516 517 518
            if (globalGeometryDirty)
                forceUpdate = true;

            if (forceUpdate) {
                if (globalTransform == null)
                    globalTransform = new float[16];
I
Ian Hickson 已提交
519
                Matrix.multiplyMM(globalTransform, 0, ancestorTransform, 0, transform, 0);
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

                final float[] sample = new float[4];
                sample[2] = 0;
                sample[3] = 1;

                final float[] point1 = new float[4];
                final float[] point2 = new float[4];
                final float[] point3 = new float[4];
                final float[] point4 = new float[4];

                sample[0] = left;
                sample[1] = top;
                transformPoint(point1, globalTransform, sample);

                sample[0] = right;
                sample[1] = top;
                transformPoint(point2, globalTransform, sample);

                sample[0] = right;
                sample[1] = bottom;
                transformPoint(point3, globalTransform, sample);
H
Hixie 已提交
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
                sample[0] = left;
                sample[1] = bottom;
                transformPoint(point4, globalTransform, sample);

                if (globalRect == null)
                    globalRect = new Rect();

                globalRect.set(
                    Math.round(min(point1[0], point2[0], point3[0], point4[0])),
                    Math.round(min(point1[1], point2[1], point3[1], point4[1])),
                    Math.round(max(point1[0], point2[0], point3[0], point4[0])),
                    Math.round(max(point1[1], point2[1], point3[1], point4[1]))
                );

                globalGeometryDirty = false;
            }

            assert globalTransform != null;
            assert globalRect != null;

            if (children != null) {
                for (int i = 0; i < children.size(); ++i) {
                    children.get(i).updateRecursively(globalTransform, visitedObjects, forceUpdate);
                }
            }
        }

        private void transformPoint(float[] result, float[] transform, float[] point) {
            Matrix.multiplyMV(result, 0, transform, 0, point, 0);
            final float w = result[3];
            result[0] /= w;
            result[1] /= w;
            result[2] /= w;
            result[3] = 0;
        }

        private float min(float a, float b, float c, float d) {
            return Math.min(a, Math.min(b, Math.min(c, d)));
        }

        private float max(float a, float b, float c, float d) {
            return Math.max(a, Math.max(b, Math.max(c, d)));
        }
    }
H
Hixie 已提交
586
}