• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.webkit;
18 
19 import android.content.Context;
20 import android.view.View;
21 import android.widget.AbsoluteLayout;
22 
23 import java.util.ArrayList;
24 
25 class 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 
ChildView()37         ChildView() {
38         }
39 
setBounds(int x, int y, int width, int height)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 
attachView(int x, int y, int width, int height)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(ctvD(width), ctvD(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 
attachViewOnUIThread(AbsoluteLayout.LayoutParams lp)70         void attachViewOnUIThread(AbsoluteLayout.LayoutParams lp) {
71             mWebView.addView(mView, lp);
72             mChildren.add(this);
73         }
74 
removeView()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 
removeViewOnUIThread()86         void removeViewOnUIThread() {
87             mWebView.removeView(mView);
88             mChildren.remove(this);
89         }
90     }
91 
ViewManager(WebView w)92     ViewManager(WebView w) {
93         mWebView = w;
94     }
95 
createView()96     ChildView createView() {
97         return new ChildView();
98     }
99 
100     /**
101      * Shorthand for calling mWebView.contentToViewDimension.  Used when
102      * obtaining a view dimension from a content dimension, whether it be in x
103      * or y.
104      */
ctvD(int val)105     private int ctvD(int val) {
106         return mWebView.contentToViewDimension(val);
107     }
108 
109     /**
110      * Shorthand for calling mWebView.contentToViewX.  Used when obtaining a
111      * view x coordinate from a content x coordinate.
112      */
ctvX(int val)113     private int ctvX(int val) {
114         return mWebView.contentToViewX(val);
115     }
116 
117     /**
118      * Shorthand for calling mWebView.contentToViewY.  Used when obtaining a
119      * view y coordinate from a content y coordinate.
120      */
ctvY(int val)121     private int ctvY(int val) {
122         return mWebView.contentToViewY(val);
123     }
124 
scaleAll()125     void scaleAll() {
126         for (ChildView v : mChildren) {
127             View view = v.mView;
128             AbsoluteLayout.LayoutParams lp =
129                     (AbsoluteLayout.LayoutParams) view.getLayoutParams();
130             lp.width = ctvD(v.width);
131             lp.height = ctvD(v.height);
132             lp.x = ctvX(v.x);
133             lp.y = ctvY(v.y);
134             view.setLayoutParams(lp);
135         }
136     }
137 
hideAll()138     void hideAll() {
139         if (mHidden) {
140             return;
141         }
142         for (ChildView v : mChildren) {
143             v.mView.setVisibility(View.GONE);
144         }
145         mHidden = true;
146     }
147 
showAll()148     void showAll() {
149         if (!mHidden) {
150             return;
151         }
152         for (ChildView v : mChildren) {
153             v.mView.setVisibility(View.VISIBLE);
154         }
155         mHidden = false;
156     }
157 }
158