ViewManager.java revision 3c946a1a9637e85e4256f40b68f3b9d4b9f40c27
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(ctv(width), ctv(height),
54                            ctv(x), ctv(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    // contentToView shorthand.
101    private int ctv(int val) {
102        return mWebView.contentToView(val);
103    }
104
105    void scaleAll() {
106        for (ChildView v : mChildren) {
107            View view = v.mView;
108            AbsoluteLayout.LayoutParams lp =
109                    (AbsoluteLayout.LayoutParams) view.getLayoutParams();
110            lp.width = ctv(v.width);
111            lp.height = ctv(v.height);
112            lp.x = ctv(v.x);
113            lp.y = ctv(v.y);
114            view.setLayoutParams(lp);
115        }
116    }
117
118    void hideAll() {
119        if (mHidden) {
120            return;
121        }
122        for (ChildView v : mChildren) {
123            v.mView.setVisibility(View.GONE);
124        }
125        mHidden = true;
126    }
127
128    void showAll() {
129        if (!mHidden) {
130            return;
131        }
132        for (ChildView v : mChildren) {
133            v.mView.setVisibility(View.VISIBLE);
134        }
135        mHidden = false;
136    }
137}
138