1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.support.v7.widget;
19
20/**
21 * RtlSpacingHelper manages the relationship between left/right and start/end for views
22 * that need to maintain both absolute and relative settings for a form of spacing similar
23 * to view padding.
24 */
25class RtlSpacingHelper {
26    public static final int UNDEFINED = Integer.MIN_VALUE;
27
28    private int mLeft = 0;
29    private int mRight = 0;
30    private int mStart = UNDEFINED;
31    private int mEnd = UNDEFINED;
32    private int mExplicitLeft = 0;
33    private int mExplicitRight = 0;
34
35    private boolean mIsRtl = false;
36    private boolean mIsRelative = false;
37
38    public int getLeft() {
39        return mLeft;
40    }
41
42    public int getRight() {
43        return mRight;
44    }
45
46    public int getStart() {
47        return mIsRtl ? mRight : mLeft;
48    }
49
50    public int getEnd() {
51        return mIsRtl ? mLeft : mRight;
52    }
53
54    public void setRelative(int start, int end) {
55        mStart = start;
56        mEnd = end;
57        mIsRelative = true;
58        if (mIsRtl) {
59            if (end != UNDEFINED) mLeft = end;
60            if (start != UNDEFINED) mRight = start;
61        } else {
62            if (start != UNDEFINED) mLeft = start;
63            if (end != UNDEFINED) mRight = end;
64        }
65    }
66
67    public void setAbsolute(int left, int right) {
68        mIsRelative = false;
69        if (left != UNDEFINED) mLeft = mExplicitLeft = left;
70        if (right != UNDEFINED) mRight = mExplicitRight = right;
71    }
72
73    public void setDirection(boolean isRtl) {
74        if (isRtl == mIsRtl) {
75            return;
76        }
77        mIsRtl = isRtl;
78        if (mIsRelative) {
79            if (isRtl) {
80                mLeft = mEnd != UNDEFINED ? mEnd : mExplicitLeft;
81                mRight = mStart != UNDEFINED ? mStart : mExplicitRight;
82            } else {
83                mLeft = mStart != UNDEFINED ? mStart : mExplicitLeft;
84                mRight = mEnd != UNDEFINED ? mEnd : mExplicitRight;
85            }
86        } else {
87            mLeft = mExplicitLeft;
88            mRight = mExplicitRight;
89        }
90    }
91}