ReverseLinearLayout.java revision 4da1b4edf60f65dae3ae159c6d13e0eee3b64aba
1a20818257e6927560a676d99dbc563a0394f74d8Jason Monk/*
2a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Copyright (C) 2016 The Android Open Source Project
3a20818257e6927560a676d99dbc563a0394f74d8Jason Monk *
4a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * except in compliance with the License. You may obtain a copy of the License at
6a20818257e6927560a676d99dbc563a0394f74d8Jason Monk *
7a20818257e6927560a676d99dbc563a0394f74d8Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
8a20818257e6927560a676d99dbc563a0394f74d8Jason Monk *
9a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Unless required by applicable law or agreed to in writing, software distributed under the
10a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * KIND, either express or implied. See the License for the specific language governing
12a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * permissions and limitations under the License.
13a20818257e6927560a676d99dbc563a0394f74d8Jason Monk */
14a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
15a20818257e6927560a676d99dbc563a0394f74d8Jason Monkpackage com.android.systemui.statusbar.phone;
16a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
17a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.annotation.Nullable;
18a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.content.Context;
194da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monkimport android.content.res.Configuration;
20a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.util.AttributeSet;
21a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.view.View;
22a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.view.ViewGroup;
23a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.widget.LinearLayout;
24a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
254da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monkimport java.util.ArrayList;
264da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk
27a20818257e6927560a676d99dbc563a0394f74d8Jason Monk/**
28a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Automatically reverses the order of children as they are added.
29a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Also reverse the width and height values of layout params
30a20818257e6927560a676d99dbc563a0394f74d8Jason Monk */
31a20818257e6927560a676d99dbc563a0394f74d8Jason Monkpublic class ReverseLinearLayout extends LinearLayout {
32a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
334da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    private boolean mIsLayoutRtl;
344da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk
35a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    public ReverseLinearLayout(Context context, @Nullable AttributeSet attrs) {
36a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        super(context, attrs);
37a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    }
38a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
39a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    @Override
404da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    protected void onFinishInflate() {
414da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        super.onFinishInflate();
424da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        mIsLayoutRtl = getResources().getConfiguration()
434da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk                .getLayoutDirection() == LAYOUT_DIRECTION_RTL;
444da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    }
454da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk
464da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    @Override
47a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    public void addView(View child) {
48a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        reversParams(child.getLayoutParams());
494da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        if (mIsLayoutRtl) {
504da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            super.addView(child);
514da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        } else {
524da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            super.addView(child, 0);
534da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        }
54a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    }
55a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
56a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    @Override
57a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    public void addView(View child, ViewGroup.LayoutParams params) {
58a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        reversParams(params);
594da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        if (mIsLayoutRtl) {
604da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            super.addView(child, params);
614da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        } else {
624da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            super.addView(child, 0, params);
634da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        }
644da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    }
654da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk
664da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    @Override
674da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    protected void onConfigurationChanged(Configuration newConfig) {
684da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        super.onConfigurationChanged(newConfig);
694da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        updateRTLOrder();
704da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    }
714da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk
724da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    /**
734da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk     * In landscape, the LinearLayout is not auto mirrored since it is vertical. Therefore we
744da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk     * have to do it manually
754da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk     */
764da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk    private void updateRTLOrder() {
774da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        boolean isLayoutRtl = getResources().getConfiguration()
784da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk                .getLayoutDirection() == LAYOUT_DIRECTION_RTL;
794da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        if (mIsLayoutRtl != isLayoutRtl) {
804da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            // RTL changed, swap the order of all views.
814da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            int childCount = getChildCount();
824da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            ArrayList<View> childList = new ArrayList<>(childCount);
834da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            for (int i = 0; i < childCount; i++) {
844da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk                childList.add(getChildAt(i));
854da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            }
864da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            removeAllViews();
874da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            for (int i = childCount - 1; i >= 0; i--) {
884da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk                super.addView(childList.get(i));
894da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            }
904da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk            mIsLayoutRtl = isLayoutRtl;
914da1b4edf60f65dae3ae159c6d13e0eee3b64abaJason Monk        }
92a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    }
93a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
94a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    private void reversParams(ViewGroup.LayoutParams params) {
95a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        if (params == null) {
96a20818257e6927560a676d99dbc563a0394f74d8Jason Monk            return;
97a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        }
98a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        int width = params.width;
99a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        params.width = params.height;
100a20818257e6927560a676d99dbc563a0394f74d8Jason Monk        params.height = width;
101a20818257e6927560a676d99dbc563a0394f74d8Jason Monk    }
102a20818257e6927560a676d99dbc563a0394f74d8Jason Monk
103a20818257e6927560a676d99dbc563a0394f74d8Jason Monk}
104