ViewManager.java revision 9a67c82089e43d37f5038c74b0e1dca8edc4ac8a
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.view.View; 20import android.widget.AbsoluteLayout; 21 22import java.util.ArrayList; 23 24class ViewManager { 25 private final WebView mWebView; 26 private final ArrayList<ChildView> mChildren = new ArrayList<ChildView>(); 27 private boolean mHidden; 28 private boolean mReadyToDraw; 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(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 70 void attachViewOnUIThread(AbsoluteLayout.LayoutParams lp) { 71 mWebView.addView(mView, lp); 72 mChildren.add(this); 73 if (!mReadyToDraw) { 74 mView.setVisibility(View.GONE); 75 } 76 } 77 78 void removeView() { 79 if (mView == null) { 80 return; 81 } 82 mWebView.mPrivateHandler.post(new Runnable() { 83 public void run() { 84 removeViewOnUIThread(); 85 } 86 }); 87 } 88 89 void removeViewOnUIThread() { 90 mWebView.removeView(mView); 91 mChildren.remove(this); 92 } 93 } 94 95 ViewManager(WebView w) { 96 mWebView = w; 97 } 98 99 ChildView createView() { 100 return new ChildView(); 101 } 102 103 /** 104 * Shorthand for calling mWebView.contentToViewDimension. Used when 105 * obtaining a view dimension from a content dimension, whether it be in x 106 * or y. 107 */ 108 private int ctvD(int val) { 109 return mWebView.contentToViewDimension(val); 110 } 111 112 /** 113 * Shorthand for calling mWebView.contentToViewX. Used when obtaining a 114 * view x coordinate from a content x coordinate. 115 */ 116 private int ctvX(int val) { 117 return mWebView.contentToViewX(val); 118 } 119 120 /** 121 * Shorthand for calling mWebView.contentToViewY. Used when obtaining a 122 * view y coordinate from a content y coordinate. 123 */ 124 private int ctvY(int val) { 125 return mWebView.contentToViewY(val); 126 } 127 128 void scaleAll() { 129 for (ChildView v : mChildren) { 130 View view = v.mView; 131 AbsoluteLayout.LayoutParams lp = 132 (AbsoluteLayout.LayoutParams) view.getLayoutParams(); 133 lp.width = ctvD(v.width); 134 lp.height = ctvD(v.height); 135 lp.x = ctvX(v.x); 136 lp.y = ctvY(v.y); 137 view.setLayoutParams(lp); 138 } 139 } 140 141 void hideAll() { 142 if (mHidden) { 143 return; 144 } 145 for (ChildView v : mChildren) { 146 v.mView.setVisibility(View.GONE); 147 } 148 mHidden = true; 149 } 150 151 void showAll() { 152 if (!mHidden) { 153 return; 154 } 155 for (ChildView v : mChildren) { 156 v.mView.setVisibility(View.VISIBLE); 157 } 158 mHidden = false; 159 } 160 161 void postResetStateAll() { 162 mWebView.mPrivateHandler.post(new Runnable() { 163 public void run() { 164 mReadyToDraw = false; 165 } 166 }); 167 } 168 169 void postReadyToDrawAll() { 170 mWebView.mPrivateHandler.post(new Runnable() { 171 public void run() { 172 mReadyToDraw = true; 173 for (ChildView v : mChildren) { 174 v.mView.setVisibility(View.VISIBLE); 175 } 176 } 177 }); 178 } 179} 180