11d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell/* 21d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Copyright (C) 2013 The Android Open Source Project 31d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 41d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 51d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * you may not use this file except in compliance with the License. 61d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * You may obtain a copy of the License at 71d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 81d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * http://www.apache.org/licenses/LICENSE-2.0 91d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Unless required by applicable law or agreed to in writing, software 111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * distributed under the License is distributed on an "AS IS" BASIS, 121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * See the License for the specific language governing permissions and 141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * limitations under the License. 151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellpackage android.support.v4.view; 191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.graphics.Rect; 211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.os.Build; 221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.view.Gravity; 231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell/** 251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Compatibility shim for accessing newer functionality from {@link android.view.Gravity}. 261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 27c5847d13e40f5d52459f5c0dab32dc08f1a9a683Chris Banespublic final class GravityCompat { 281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell interface GravityCompatImpl { 291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell int getAbsoluteGravity(int gravity, int layoutDirection); 301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell void apply(int gravity, int w, int h, Rect container, Rect outRect, int layoutDirection); 311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell void apply(int gravity, int w, int h, Rect container, int xAdj, int yAdj, 321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell Rect outRect, int layoutDirection); 331d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell void applyDisplay(int gravity, Rect display, Rect inoutObj, int layoutDirection); 341d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 351d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 361d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell static class GravityCompatImplBase implements GravityCompatImpl { 371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public int getAbsoluteGravity(int gravity, int layoutDirection) { 391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell // Just strip off the relative bit to get LEFT/RIGHT. 401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell return gravity & ~RELATIVE_LAYOUT_DIRECTION; 411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public void apply(int gravity, int w, int h, Rect container, Rect outRect, 451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell int layoutDirection) { 461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell Gravity.apply(gravity, w, h, container, outRect); 471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 481d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 491d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public void apply(int gravity, int w, int h, Rect container, int xAdj, int yAdj, 511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell Rect outRect, int layoutDirection) { 521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell Gravity.apply(gravity, w, h, container, xAdj, yAdj, outRect); 531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 541d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 551d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 561d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public void applyDisplay(int gravity, Rect display, Rect inoutObj, 571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell int layoutDirection) { 581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell Gravity.applyDisplay(gravity, display, inoutObj); 591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell static class GravityCompatImplJellybeanMr1 implements GravityCompatImpl { 631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public int getAbsoluteGravity(int gravity, int layoutDirection) { 651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell return GravityCompatJellybeanMr1.getAbsoluteGravity(gravity, layoutDirection); 661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public void apply(int gravity, int w, int h, Rect container, Rect outRect, 701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell int layoutDirection) { 711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell GravityCompatJellybeanMr1.apply(gravity, w, h, container, outRect, layoutDirection); 721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public void apply(int gravity, int w, int h, Rect container, int xAdj, int yAdj, 761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell Rect outRect, int layoutDirection) { 771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell GravityCompatJellybeanMr1.apply(gravity, w, h, container, xAdj, yAdj, outRect, 781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell layoutDirection); 791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell @Override 821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public void applyDisplay(int gravity, Rect display, Rect inoutObj, int layoutDirection) { 831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell GravityCompatJellybeanMr1.applyDisplay(gravity, display, inoutObj, layoutDirection); 841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell static final GravityCompatImpl IMPL; 881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell static { 891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell final int version = Build.VERSION.SDK_INT; 901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell if (version >= 17) { 911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell IMPL = new GravityCompatImplJellybeanMr1(); 921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } else { 931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell IMPL = new GravityCompatImplBase(); 941d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** Raw bit controlling whether the layout direction is relative or not (START/END instead of 981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * absolute LEFT/RIGHT). 991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 1001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static final int RELATIVE_LAYOUT_DIRECTION = 0x00800000; 1011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 1021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** Push object to x-axis position at the start of its container, not changing its size. */ 1031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static final int START = RELATIVE_LAYOUT_DIRECTION | Gravity.LEFT; 1041d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 1051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** Push object to x-axis position at the end of its container, not changing its size. */ 1061d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static final int END = RELATIVE_LAYOUT_DIRECTION | Gravity.RIGHT; 1071d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 1081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** 1091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Binary mask for the horizontal gravity and script specific direction bit. 1101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 1111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static final int RELATIVE_HORIZONTAL_GRAVITY_MASK = START | END; 1121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 1131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** 1141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Apply a gravity constant to an object and take care if layout direction is RTL or not. 1151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param gravity The desired placement of the object, as defined by the 1171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * constants in this class. 1181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param w The horizontal size of the object. 1191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param h The vertical size of the object. 1201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param container The frame of the containing space, in which the object 1211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * will be placed. Should be large enough to contain the 1221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * width and height of the object. 1231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param outRect Receives the computed frame of the object in its 1241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * container. 1251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param layoutDirection The layout direction. 1261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @see ViewCompat#LAYOUT_DIRECTION_LTR 1281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @see ViewCompat#LAYOUT_DIRECTION_RTL 1291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 1301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static void apply(int gravity, int w, int h, Rect container, 1311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell Rect outRect, int layoutDirection) { 1321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell IMPL.apply(gravity, w, h, container, outRect, layoutDirection); 1331d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 1341d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 1351d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** 1361d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Apply a gravity constant to an object. 1371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param gravity The desired placement of the object, as defined by the 1391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * constants in this class. 1401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param w The horizontal size of the object. 1411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param h The vertical size of the object. 1421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param container The frame of the containing space, in which the object 1431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * will be placed. Should be large enough to contain the 1441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * width and height of the object. 1451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param xAdj Offset to apply to the X axis. If gravity is LEFT this 1461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * pushes it to the right; if gravity is RIGHT it pushes it to 1471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * the left; if gravity is CENTER_HORIZONTAL it pushes it to the 1481d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * right or left; otherwise it is ignored. 1491d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param yAdj Offset to apply to the Y axis. If gravity is TOP this pushes 1501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * it down; if gravity is BOTTOM it pushes it up; if gravity is 1511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * CENTER_VERTICAL it pushes it down or up; otherwise it is 1521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * ignored. 1531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param outRect Receives the computed frame of the object in its 1541d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * container. 1551d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param layoutDirection The layout direction. 1561d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @see ViewCompat#LAYOUT_DIRECTION_LTR 1581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @see ViewCompat#LAYOUT_DIRECTION_RTL 1591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 1601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static void apply(int gravity, int w, int h, Rect container, 1611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell int xAdj, int yAdj, Rect outRect, int layoutDirection) { 1621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell IMPL.apply(gravity, w, h, container, xAdj, yAdj, outRect, layoutDirection); 1631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 1641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 1651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** 1661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Apply additional gravity behavior based on the overall "display" that an 1671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * object exists in. This can be used after 1681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * {@link android.view.Gravity#apply(int, int, int, Rect, int, int, Rect)} to place the object 1691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * within a visible display. By default this moves or clips the object 1701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * to be visible in the display; the gravity flags 1711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * {@link android.view.Gravity#DISPLAY_CLIP_HORIZONTAL} and 1721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * {@link android.view.Gravity#DISPLAY_CLIP_VERTICAL} can be used to change this behavior. 1731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param gravity Gravity constants to modify the placement within the 1751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * display. 1761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param display The rectangle of the display in which the object is 1771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * being placed. 1781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param inoutObj Supplies the current object position; returns with it 1791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * modified if needed to fit in the display. 1801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param layoutDirection The layout direction. 1811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @see ViewCompat#LAYOUT_DIRECTION_LTR 1831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @see ViewCompat#LAYOUT_DIRECTION_RTL 1841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 1851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static void applyDisplay(int gravity, Rect display, Rect inoutObj, int layoutDirection) { 1861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell IMPL.applyDisplay(gravity, display, inoutObj, layoutDirection); 1871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 1881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell 1891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell /** 1901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * <p>Convert script specific gravity to absolute horizontal value.</p> 1911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * if horizontal direction is LTR, then START will set LEFT and END will set RIGHT. 1931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * if horizontal direction is RTL, then START will set RIGHT and END will set LEFT. 1941d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * 1961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param gravity The gravity to convert to absolute (horizontal) values. 1971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @param layoutDirection The layout direction. 1981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * @return gravity converted to absolute (horizontal) values. 1991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */ 2001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell public static int getAbsoluteGravity(int gravity, int layoutDirection) { 2011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell return IMPL.getAbsoluteGravity(gravity, layoutDirection); 2021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell } 203c5847d13e40f5d52459f5c0dab32dc08f1a9a683Chris Banes 204c5847d13e40f5d52459f5c0dab32dc08f1a9a683Chris Banes private GravityCompat() {} 2051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell} 206