AccessibilityBridge.java 23.1 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 16 17 18
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.JSONMessageCodec;
import org.json.JSONException;
import org.json.JSONObject;
H
Hixie 已提交
19

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

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

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

41 42
    private final BasicMessageChannel<Object> mFlutterAccessibilityChannel;

43 44 45 46 47 48 49 50
    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;
51
    private static final int SEMANTICS_ACTION_SHOW_ON_SCREEN = 1 << 8;
52 53 54 55 56 57 58 59

    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;
60
    private static final int SEMANTICS_FLAG_IS_SELECTED = 1 << 2;
61 62

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

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

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

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

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

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

108 109 110
        Rect bounds = object.getGlobalRect();
        if (object.parent != null) {
            Rect parentBounds = object.parent.getGlobalRect();
H
Hixie 已提交
111 112 113 114 115 116 117 118
            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 已提交
119
        result.setEnabled(true); // TODO(ianh): Expose disabled subtrees
H
Hixie 已提交
120

121
        if ((object.actions & SEMANTICS_ACTION_TAP) != 0) {
122
            result.addAction(AccessibilityNodeInfo.ACTION_CLICK);
H
Hixie 已提交
123 124
            result.setClickable(true);
        }
125
        if ((object.actions & SEMANTICS_ACTION_LONG_PRESS) != 0) {
126
            result.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
H
Hixie 已提交
127 128
            result.setLongClickable(true);
        }
129
        if ((object.actions & SEMANTICS_ACTION_SCROLLABLE) != 0) {
H
Hixie 已提交
130
            result.setScrollable(true);
131 132 133
            // 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");
134 135 136 137 138 139 140 141 142 143 144
            // 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 已提交
145
        }
146 147
        if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0
                || (object.actions & SEMANTICS_ACTION_DECREASE) != 0 ) {
148 149 150 151 152 153 154 155
            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 已提交
156

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

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

169
        if (object.children != null) {
170 171 172 173 174 175 176 177 178 179
            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) {
180 181
                result.addChild(mOwner, child.id);
            }
H
Hixie 已提交
182 183 184 185 186
        }

        return result;
    }

H
Hixie 已提交
187 188
    @Override
    public boolean performAction(int virtualViewId, int action, Bundle arguments) {
189
        SemanticsObject object = mObjects.get(virtualViewId);
190
        if (object == null) {
H
Hixie 已提交
191
            return false;
192
        }
H
Hixie 已提交
193 194
        switch (action) {
            case AccessibilityNodeInfo.ACTION_CLICK: {
195
                mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_TAP);
H
Hixie 已提交
196 197 198
                return true;
            }
            case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
199
                mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_LONG_PRESS);
H
Hixie 已提交
200 201
                return true;
            }
202
            case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
203 204 205
                if ((object.actions & SEMANTICS_ACTION_SCROLL_UP) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_UP);
                } else if ((object.actions & SEMANTICS_ACTION_SCROLL_LEFT) != 0) {
206
                    // TODO(ianh): bidi support using textDirection
207
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_LEFT);
208 209
                } else if ((object.actions & SEMANTICS_ACTION_INCREASE) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_INCREASE);
H
Hixie 已提交
210 211 212 213 214
                } else {
                    return false;
                }
                return true;
            }
215
            case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
216 217 218
                if ((object.actions & SEMANTICS_ACTION_SCROLL_DOWN) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_DOWN);
                } else if ((object.actions & SEMANTICS_ACTION_SCROLL_RIGHT) != 0) {
219
                    // TODO(ianh): bidi support using textDirection
220
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_SCROLL_RIGHT);
221 222
                } else if ((object.actions & SEMANTICS_ACTION_DECREASE) != 0) {
                    mOwner.dispatchSemanticsAction(virtualViewId, SEMANTICS_ACTION_DECREASE);
H
Hixie 已提交
223 224 225 226 227
                } else {
                    return false;
                }
                return true;
            }
H
Hixie 已提交
228 229
            case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
                sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
230
                mFocusedObject = null;
H
Hixie 已提交
231 232 233 234
                return true;
            }
            case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
                sendAccessibilityEvent(virtualViewId, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
235
                if (mFocusedObject == null) {
H
Hixie 已提交
236 237 238 239 240
                    // 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();
                }
241
                mFocusedObject = object;
H
Hixie 已提交
242 243
                return true;
            }
244 245 246 247 248 249
            // 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 已提交
250 251 252 253
        }
        return false;
    }

254 255 256
    // TODO(ianh): implement findAccessibilityNodeInfosByText()
    // TODO(ianh): implement findFocus()

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

262 263 264 265 266 267 268 269 270 271
    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;
    }

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

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

296 297 298 299 300 301
    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);
302
        }
303

304 305 306 307 308 309
        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);
310
        }
311 312 313 314

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

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

    private void sendAccessibilityEvent(int virtualViewId, int eventType) {
328
        if (!mAccessibilityEnabled) {
H
Hixie 已提交
329 330
            return;
        }
331 332 333 334 335 336 337
        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 已提交
338 339 340
        }
    }

341 342 343 344 345 346
    // Message Handler for [mFlutterAccessibilityChannel].
    public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
        @SuppressWarnings("unchecked")
        final JSONObject annotatedEvent = (JSONObject)message;
        try {
            final int nodeId = annotatedEvent.getInt("nodeId");
347
            final String type = annotatedEvent.getString("type");
348 349 350 351 352 353 354 355 356 357 358 359 360

            switch (type) {
                case "scroll":
                    sendAccessibilityEvent(nodeId, AccessibilityEvent.TYPE_VIEW_SCROLLED);
                    break;
                default:
                    assert false;
            }
        } catch (JSONException e) {
          throw new IllegalArgumentException("Invalid JSON", e);
       }
    }

361
    private void willRemoveSemanticsObject(SemanticsObject object) {
362 363 364 365 366 367
        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 已提交
368
        }
369 370
        if (mHoveredObject == object) {
            mHoveredObject = null;
371
        }
H
Hixie 已提交
372 373
    }

374
    void reset() {
375
        mObjects.clear();
376 377
        if (mFocusedObject != null)
            sendAccessibilityEvent(mFocusedObject.id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
378 379
        mFocusedObject = null;
        mHoveredObject = null;
380
        sendAccessibilityEvent(0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
H
Hixie 已提交
381 382
    }

383 384 385 386 387 388 389 390 391 392 393 394 395 396
    private enum TextDirection {
        UNKNOWN, LTR, RTL;

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

397 398
    private class SemanticsObject {
        SemanticsObject() { }
399

400
        int id = -1;
401 402

        int flags;
A
Adam Barth 已提交
403
        int actions;
404
        String label;
405
        TextDirection textDirection;
H
Hixie 已提交
406 407 408

        private float left;
        private float top;
409 410 411
        private float right;
        private float bottom;
        private float[] transform;
H
Hixie 已提交
412

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

416 417
        private boolean inverseTransformDirty = true;
        private float[] inverseTransform;
H
Hixie 已提交
418

419 420 421
        private boolean globalGeometryDirty = true;
        private float[] globalTransform;
        private Rect globalRect;
H
Hixie 已提交
422

A
Adam Barth 已提交
423 424 425 426 427 428 429 430 431 432 433 434
        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);
              }
          }
        }

435 436
        void updateWith(ByteBuffer buffer, String[] strings) {
            flags = buffer.getInt();
A
Adam Barth 已提交
437
            actions = buffer.getInt();
H
Hixie 已提交
438

439 440 441 442 443 444
            final int stringIndex = buffer.getInt();
            if (stringIndex == -1)
                label = null;
            else
                label = strings[stringIndex];

445 446
            textDirection = TextDirection.fromInt(buffer.getInt());

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
            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 已提交
471
                    children.add(child);
472 473
                }
            }
H
Hixie 已提交
474 475
        }

476 477 478 479 480 481 482 483
        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 已提交
484 485
        }

486
        Rect getGlobalRect() {
487
            assert !globalGeometryDirty;
H
Hixie 已提交
488 489
            return globalRect;
        }
490

491 492 493 494 495
        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)
496
                return null;
497
            if (children != null) {
498 499 500 501 502 503
                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);
504 505 506
                    if (result != null) {
                        return result;
                    }
507 508 509 510
                }
            }
            return this;
        }
H
Hixie 已提交
511

512 513 514 515
        boolean isFocusable() {
            return flags != 0 || label != null || (actions & ~SEMANTICS_ACTION_SCROLLABLE) != 0;
        }

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

519 520 521 522 523 524
            if (globalGeometryDirty)
                forceUpdate = true;

            if (forceUpdate) {
                if (globalTransform == null)
                    globalTransform = new float[16];
I
Ian Hickson 已提交
525
                Matrix.multiplyMM(globalTransform, 0, ancestorTransform, 0, transform, 0);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

                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 已提交
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 586 587 588 589 590 591
                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 已提交
592
}