ViewManager.java revision 0236e673d8b980fd6333c18505129c6b49e38cd1
1/*
2 * Copyright (C) 2009 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
17package android.webkit;
18
19import android.content.Context;
20import android.view.View;
21import android.widget.AbsoluteLayout;
22
23import java.util.ArrayList;
24
25class ViewManager {
26    private final WebView mWebView;
27    private final ArrayList<ChildView> mChildren = new ArrayList<ChildView>();
28    private boolean mHidden;
29
30    class ChildView {
31        int x;
32        int y;
33        int width;
34        int height;
35        View mView; // generic view to show
36
37        ChildView() {
38        }
39
40        void setBounds(int x, int y, int width, int height) {
41            this.x = x;
42            this.y = y;
43            this.width = width;
44            this.height = height;
45        }
46
47        void attachView(int x, int y, int width, int height) {
48            if (mView == null) {
49                return;
50            }
51            setBounds(x, y, width, height);
52            final AbsoluteLayout.LayoutParams lp =
53                    new AbsoluteLayout.LayoutParams(ctvX(width), ctvX(height),
54                            ctvX(x), ctvY(y));
55            mWebView.mPrivateHandler.post(new Runnable() {
56                public void run() {
57                    // This method may be called multiple times. If the view is
58                    // already attached, just set the new LayoutParams,
59                    // otherwise attach the view and add it to the list of
60                    // children.
61                    if (mView.getParent() != null) {
62                        mView.setLayoutParams(lp);
63                    } else {
64                        attachViewOnUIThread(lp);
65                    }
66                }
67            });
68        }
69
70        void attachViewOnUIThread(AbsoluteLayout.LayoutParams lp) {
71            mWebView.addView(mView, lp);
72            mChildren.add(this);
73        }
74
75        void removeView() {
76            if (mView == null) {
77                return;
78            }
79            mWebView.mPrivateHandler.post(new Runnable() {
80                public void run() {
81                    removeViewOnUIThread();
82                }
83            });
84        }
85
86        void removeViewOnUIThread() {
87            mWebView.removeView(mView);
88            mChildren.remove(this);
89        }
90    }
91
92    ViewManager(WebView w) {
93        mWebView = w;
94    }
95
96    ChildView createView() {
97        return new ChildView();
98    }
99
100    /**
101     * Shorthand for calling mWebView.contentToViewX.  Used when obtaining a
102     * view x coordinate from a content x coordinate, or when getting a
103     * view dimension from a content dimension, whether it be in x or y.
104     */
105    private int ctvX(int val) {
106        return mWebView.contentToViewX(val);
107    }
108
109    /**
110     * Shorthand for calling mWebView.contentToViewY.  Used when obtaining a
111     * view y coordinate from a content y coordinate.
112     */
113    private int ctvY(int val) {
114        return mWebView.contentToViewY(val);
115    }
116
117    void scaleAll() {
118        for (ChildView v : mChildren) {
119            View view = v.mView;
120            AbsoluteLayout.LayoutParams lp =
121                    (AbsoluteLayout.LayoutParams) view.getLayoutParams();
122            lp.width = ctvX(v.width);
123            lp.height = ctvX(v.height);
124            lp.x = ctvX(v.x);
125            lp.y = ctvY(v.y);
126            view.setLayoutParams(lp);
127        }
128    }
129
130    void hideAll() {
131        if (mHidden) {
132            return;
133        }
134        for (ChildView v : mChildren) {
135            v.mView.setVisibility(View.GONE);
136        }
137        mHidden = true;
138    }
139
140    void showAll() {
141        if (!mHidden) {
142            return;
143        }
144        for (ChildView v : mChildren) {
145            v.mView.setVisibility(View.VISIBLE);
146        }
147        mHidden = false;
148    }
149}
150