16cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati/*
26cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * Copyright (C) 2014 The Android Open Source Project
36cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati *
46cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * Licensed under the Apache License, Version 2.0 (the "License");
56cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * you may not use this file except in compliance with the License.
66cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * You may obtain a copy of the License at
76cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati *
86cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati *      http://www.apache.org/licenses/LICENSE-2.0
96cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati *
106cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * Unless required by applicable law or agreed to in writing, software
116cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * distributed under the License is distributed on an "AS IS" BASIS,
126cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * See the License for the specific language governing permissions and
146cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * limitations under the License.
156cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati */
166cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
1769c182afb0e6d82a341a28b4317aa703af768906Gary Maipackage com.android.contacts.widget;
186cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
196cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapatiimport android.app.Activity;
206cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapatiimport android.content.res.Resources;
21bf6731b85f825f153d839183ea767435617f662fAndrew Leeimport android.graphics.drawable.Drawable;
220a49afa2ad697307cc04ef4cb86570574fa720f2Gary Maiimport android.view.View;
236cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapatiimport android.view.animation.AnimationUtils;
246cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapatiimport android.view.animation.Interpolator;
25bf6731b85f825f153d839183ea767435617f662fAndrew Leeimport android.widget.ImageButton;
266cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
273f6a2444e0134b7380cdb2e13abf4bf1163336d0Arthur Wangimport com.android.contacts.R;
280a49afa2ad697307cc04ef4cb86570574fa720f2Gary Maiimport com.android.contacts.util.ViewUtil;
297c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Leeimport com.android.phone.common.animation.AnimUtils;
306cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
316cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati/**
326cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati * Controls the movement and appearance of the FAB (Floating Action Button).
336cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati */
346cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapatipublic class FloatingActionButtonController {
356cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    public static final int ALIGN_MIDDLE = 0;
36a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee    public static final int ALIGN_QUARTER_END = 1;
37a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee    public static final int ALIGN_END = 2;
386cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
39e383ecf2b067289ae467bc69fb4e82481059ddccWenyi Wang    private static final int FAB_SCALE_IN_DURATION = 186;
40e383ecf2b067289ae467bc69fb4e82481059ddccWenyi Wang    private static final int FAB_SCALE_IN_FADE_IN_DELAY = 70;
41e383ecf2b067289ae467bc69fb4e82481059ddccWenyi Wang    private static final int FAB_ICON_FADE_OUT_DURATION = 46;
42435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee
436cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    private final int mAnimationDuration;
446cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    private final int mFloatingActionButtonWidth;
456cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    private final int mFloatingActionButtonMarginRight;
466cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    private final View mFloatingActionButtonContainer;
47bf6731b85f825f153d839183ea767435617f662fAndrew Lee    private final ImageButton mFloatingActionButton;
486cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    private final Interpolator mFabInterpolator;
496cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    private int mScreenWidth;
506cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
51bf6731b85f825f153d839183ea767435617f662fAndrew Lee    public FloatingActionButtonController(Activity activity, View container, ImageButton button) {
526cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        Resources resources = activity.getResources();
531ba9d8a8d7b17fafb7fdfff3f913c2ab205a83c8Wenyi Wang        mFabInterpolator = AnimationUtils.loadInterpolator(activity,
541ba9d8a8d7b17fafb7fdfff3f913c2ab205a83c8Wenyi Wang                android.R.interpolator.fast_out_slow_in);
556cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        mFloatingActionButtonWidth = resources.getDimensionPixelSize(
566cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                R.dimen.floating_action_button_width);
576cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        mFloatingActionButtonMarginRight = resources.getDimensionPixelOffset(
586cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                R.dimen.floating_action_button_margin_right);
596cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        mAnimationDuration = resources.getInteger(
606cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                R.integer.floating_action_button_animation_duration);
616cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        mFloatingActionButtonContainer = container;
62435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee        mFloatingActionButton = button;
636cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, resources);
646cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    }
656cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
666cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    /**
676cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * Passes the screen width into the class. Necessary for translation calculations.
686cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * Should be called as soon as parent View width is available.
696cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     *
706cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @param screenWidth The width of the screen in pixels.
716cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     */
726cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    public void setScreenWidth(int screenWidth) {
736cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        mScreenWidth = screenWidth;
746cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    }
756cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
766cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    /**
776cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * Sets FAB as View.VISIBLE or View.GONE.
786cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     *
796cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @param visible Whether or not to make the container visible.
806cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     */
816cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    public void setVisible(boolean visible) {
826cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
836cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    }
846cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
85d01d238a7e6ff30d6d025eee96e41a8a0c511691Andrew Lee    public boolean isVisible() {
8641e1641c1e38daa0cae56e90a8b9e343e252b0a9Andrew Lee        return mFloatingActionButtonContainer.getVisibility() == View.VISIBLE;
8741e1641c1e38daa0cae56e90a8b9e343e252b0a9Andrew Lee    }
8841e1641c1e38daa0cae56e90a8b9e343e252b0a9Andrew Lee
89bf6731b85f825f153d839183ea767435617f662fAndrew Lee    public void changeIcon(Drawable icon, String description) {
90bf6731b85f825f153d839183ea767435617f662fAndrew Lee        if (mFloatingActionButton.getDrawable() != icon
91bf6731b85f825f153d839183ea767435617f662fAndrew Lee                || !mFloatingActionButton.getContentDescription().equals(description)) {
92bf6731b85f825f153d839183ea767435617f662fAndrew Lee            mFloatingActionButton.setImageDrawable(icon);
93bf6731b85f825f153d839183ea767435617f662fAndrew Lee            mFloatingActionButton.setContentDescription(description);
94bf6731b85f825f153d839183ea767435617f662fAndrew Lee        }
95bf6731b85f825f153d839183ea767435617f662fAndrew Lee    }
96bf6731b85f825f153d839183ea767435617f662fAndrew Lee
976cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    /**
986cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * Updates the FAB location (middle to right position) as the PageView scrolls.
996cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     *
1006cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @param positionOffset A fraction used to calculate position of the FAB during page scroll.
1016cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     */
1026cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    public void onPageScrolled(float positionOffset) {
1036cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        // As the page is scrolling, if we're on the first tab, update the FAB position so it
1046cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        // moves along with it.
1056cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        mFloatingActionButtonContainer.setTranslationX(
106a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee                (int) (positionOffset * getTranslationXForAlignment(ALIGN_END)));
1076cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    }
1086cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
1096cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    /**
110365fb20e43007ac56133770299f156a334029248Andrew Lee     * Aligns the FAB to the described location
111365fb20e43007ac56133770299f156a334029248Andrew Lee     *
112365fb20e43007ac56133770299f156a334029248Andrew Lee     * @param align One of ALIGN_MIDDLE, ALIGN_QUARTER_RIGHT, or ALIGN_RIGHT.
113365fb20e43007ac56133770299f156a334029248Andrew Lee     * @param animate Whether or not to animate the transition.
114365fb20e43007ac56133770299f156a334029248Andrew Lee     */
115365fb20e43007ac56133770299f156a334029248Andrew Lee    public void align(int align, boolean animate) {
116365fb20e43007ac56133770299f156a334029248Andrew Lee        align(align, 0 /*offsetX */, 0 /* offsetY */, animate);
117365fb20e43007ac56133770299f156a334029248Andrew Lee    }
118365fb20e43007ac56133770299f156a334029248Andrew Lee
119365fb20e43007ac56133770299f156a334029248Andrew Lee    /**
1206cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * Aligns the FAB to the described location plus specified additional offsets.
1216cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     *
1226cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @param align One of ALIGN_MIDDLE, ALIGN_QUARTER_RIGHT, or ALIGN_RIGHT.
1236cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @param offsetX Additional offsetX to translate by.
124365fb20e43007ac56133770299f156a334029248Andrew Lee     * @param offsetY Additional offsetY to translate by.
1256cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @param animate Whether or not to animate the transition.
1266cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     */
1276cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    public void align(int align, int offsetX, int offsetY, boolean animate) {
1287c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee        if (mScreenWidth == 0) {
1297c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee            return;
1307c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee        }
1317c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee
1326cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        int translationX = getTranslationXForAlignment(align);
133955e10d3b07db60791c88f156bb05e568b28f7a2Andrew Lee
134955e10d3b07db60791c88f156bb05e568b28f7a2Andrew Lee        // Skip animation if container is not shown; animation causes container to show again.
135955e10d3b07db60791c88f156bb05e568b28f7a2Andrew Lee        if (animate && mFloatingActionButtonContainer.isShown()) {
1366cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati            mFloatingActionButtonContainer.animate()
1376cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                    .translationX(translationX + offsetX)
1386cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                    .translationY(offsetY)
1396cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                    .setInterpolator(mFabInterpolator)
1407c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee                    .setDuration(mAnimationDuration)
1417c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee                    .start();
1426cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        } else {
1436cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati            mFloatingActionButtonContainer.setTranslationX(translationX + offsetX);
1446cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati            mFloatingActionButtonContainer.setTranslationY(offsetY);
1456cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        }
1466cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    }
1476cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
1486cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    /**
1497c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee     * Resizes width and height of the floating action bar container.
1507c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee     * @param dimension The new dimensions for the width and height.
1517c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee     * @param animate Whether to animate this change.
1527c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee     */
1537c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee    public void resize(int dimension, boolean animate) {
1547c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee        if (animate) {
1557c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee            AnimUtils.changeDimensions(mFloatingActionButtonContainer, dimension, dimension);
1567c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee        } else {
1577c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee            mFloatingActionButtonContainer.getLayoutParams().width = dimension;
1587c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee            mFloatingActionButtonContainer.getLayoutParams().height = dimension;
1597c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee            mFloatingActionButtonContainer.requestLayout();
1607c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee        }
1617c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee    }
1627c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee
1637c7eaa7651588ae6e278446d5ebea56fe6dcc84dAndrew Lee    /**
164435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee     * Scales the floating action button from no height and width to its actual dimensions. This is
165435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee     * an animation for showing the floating action button.
166d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee     * @param delayMs The delay for the effect, in milliseconds.
167435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee     */
168d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee    public void scaleIn(int delayMs) {
169d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee        setVisible(true);
170d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee        AnimUtils.scaleIn(mFloatingActionButtonContainer, FAB_SCALE_IN_DURATION, delayMs);
171d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee        AnimUtils.fadeIn(mFloatingActionButton, FAB_SCALE_IN_DURATION,
172d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee                delayMs + FAB_SCALE_IN_FADE_IN_DELAY, null);
173435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee    }
174435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee
175435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee    /**
176143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell     * Immediately remove the affects of the last call to {@link #scaleOut}.
177143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell     */
178143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell    public void resetIn() {
1796aa50bc71fd11826ca85b0d3541d08c7f567d2b9Brian Attwell        mFloatingActionButton.setAlpha(1f);
180143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell        mFloatingActionButton.setVisibility(View.VISIBLE);
181143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell        mFloatingActionButtonContainer.setScaleX(1);
182143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell        mFloatingActionButtonContainer.setScaleY(1);
183143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell    }
184143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell
185143c1893ce5dd80038a730e48c6d80b5ee1b298dBrian Attwell    /**
186435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee     * Scales the floating action button from its actual dimensions to no height and width. This is
187435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee     * an animation for hiding the floating action button.
188435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee     */
189435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee    public void scaleOut() {
190435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee        AnimUtils.scaleOut(mFloatingActionButtonContainer, mAnimationDuration);
191d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee        // Fade out the icon faster than the scale out animation, so that the icon scaling is less
192d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee        // obvious. We don't want it to scale, but the resizing the container is not as performant.
193d2bbc2c5dbb7c0e81721e32031e35703761660daAndrew Lee        AnimUtils.fadeOut(mFloatingActionButton, FAB_ICON_FADE_OUT_DURATION, null);
194435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee    }
195435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee
196435dbb93a393399972acf2b9ecad5f7c89ac4e6aAndrew Lee    /**
197a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee     * Calculates the X offset of the FAB to the given alignment, adjusted for whether or not the
198a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee     * view is in RTL mode.
1996cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     *
2006cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @param align One of ALIGN_MIDDLE, ALIGN_QUARTER_RIGHT, or ALIGN_RIGHT.
2016cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     * @return The translationX for the given alignment.
2026cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati     */
2036cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    public int getTranslationXForAlignment(int align) {
204a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee        int result = 0;
2056cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        switch (align) {
2066cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati            case ALIGN_MIDDLE:
2076cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                // Moves the FAB to exactly center screen.
2086cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                return 0;
209a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee            case ALIGN_QUARTER_END:
2106cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                // Moves the FAB a quarter of the screen width.
211a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee                result = mScreenWidth / 4;
212a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee                break;
213a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee            case ALIGN_END:
2146cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                // Moves the FAB half the screen width. Same as aligning right with a marginRight.
215a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee                result = mScreenWidth / 2
2166cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                        - mFloatingActionButtonWidth / 2
2176cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati                        - mFloatingActionButtonMarginRight;
218a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee                break;
219a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee        }
220a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee        if (isLayoutRtl()) {
221a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee            result *= -1;
2226cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati        }
223a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee        return result;
2246cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati    }
2256cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati
226a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee    private boolean isLayoutRtl() {
227923859f758f551ce8e32e4690d120e912bfd1777Yorke Lee        return mFloatingActionButtonContainer.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
228a2fa4ba1d5032620be04691f8fda6d31b92bc50fYorke Lee    }
2296cff9cf3437ffb6214f9e95023c52fa2253e887dSai Cheemalapati}
230