1a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka/*
2a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * Copyright (C) 2011 The Android Open Source Project
3a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka *
4a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License");
5a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * you may not use this file except in compliance with the License.
6a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * You may obtain a copy of the License at
7a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka *
8a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka *      http://www.apache.org/licenses/LICENSE-2.0
9a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka *
10a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software
11a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS,
12a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * See the License for the specific language governing permissions and
14a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka * limitations under the License.
15a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka */
16a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka
17a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaokapackage com.android.inputmethod.compat;
18a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka
192442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaokaimport android.view.View;
20a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaokaimport android.view.ViewGroup;
21a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaokaimport android.view.ViewGroup.MarginLayoutParams;
22a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaokaimport android.widget.FrameLayout;
23a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaokaimport android.widget.RelativeLayout;
24a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka
25a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaokapublic class FrameLayoutCompatUtils {
26a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka    private static final boolean NEEDS_FRAME_LAYOUT_HACK = (
27a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            android.os.Build.VERSION.SDK_INT < 11 /* Honeycomb */);
28a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka
29a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka    public static ViewGroup getPlacer(ViewGroup container) {
30a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        if (NEEDS_FRAME_LAYOUT_HACK) {
31a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            // Insert RelativeLayout to be able to setMargin because pre-Honeycomb FrameLayout
32a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            // could not handle setMargin properly.
33a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            final ViewGroup placer = new RelativeLayout(container.getContext());
34a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            container.addView(placer);
35a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            return placer;
36a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        } else {
37a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            return container;
38a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        }
39a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka    }
40a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka
41a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka    public static MarginLayoutParams newLayoutParam(ViewGroup placer, int width, int height) {
42a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        if (placer instanceof FrameLayout) {
43a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            return new FrameLayout.LayoutParams(width, height);
44a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        } else if (placer instanceof RelativeLayout) {
45a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            return new RelativeLayout.LayoutParams(width, height);
46a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        } else if (placer == null) {
47a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            throw new NullPointerException("placer is null");
48a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        } else {
49a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka            throw new IllegalArgumentException("placer is neither FrameLayout nor RelativeLayout: "
50a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka                    + placer.getClass().getName());
51a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka        }
52a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka    }
532442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka
542442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka    public static void placeViewAt(View view, int x, int y, int w, int h) {
552442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka        final ViewGroup.LayoutParams lp = view.getLayoutParams();
562442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka        if (lp instanceof MarginLayoutParams) {
572442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka            final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp;
582442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka            marginLayoutParams.width = w;
592442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka            marginLayoutParams.height = h;
602442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka            marginLayoutParams.setMargins(x, y, 0, 0);
612442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka        }
622442e779857e7eda253aadcb1c4dff5ccb3e53f4Tadashi G. Takaoka    }
63a7eed902f1b0d6871d416412b3c6f91163fa2578Tadashi G. Takaoka}
64