| 1 |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
| 2 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
package org.mozilla.gecko.gfx; |
| 7 |
|
| 8 |
|
| 9 |
import android.content.Context; |
| 10 |
import android.content.res.Resources; |
| 11 |
import android.graphics.Bitmap; |
| 12 |
import android.graphics.BitmapFactory; |
| 13 |
import android.graphics.PixelFormat; |
| 14 |
import android.util.AttributeSet; |
| 15 |
import android.util.Log; |
| 16 |
import android.view.KeyEvent; |
| 17 |
import android.view.MotionEvent; |
| 18 |
import android.view.SurfaceHolder; |
| 19 |
import android.view.SurfaceView; |
| 20 |
import android.view.View; |
| 21 |
import android.view.ViewGroup; |
| 22 |
import android.view.inputmethod.EditorInfo; |
| 23 |
import android.view.inputmethod.InputConnection; |
| 24 |
import android.widget.FrameLayout; |
| 25 |
|
| 26 |
import org.libreoffice.LOEvent; |
| 27 |
import org.libreoffice.LOKitShell; |
| 28 |
import org.libreoffice.LibreOfficeMainActivity; |
| 29 |
import org.libreoffice.R; |
| 30 |
import org.mozilla.gecko.OnSlideSwipeListener; |
| 31 |
|
| 32 |
/** |
| 33 |
* A view rendered by the layer compositor. |
| 34 |
* |
| 35 |
* This view delegates to LayerRenderer to actually do the drawing. Its role is largely that of a |
| 36 |
* mediator between the LayerRenderer and the LayerController. |
| 37 |
*/ |
| 38 |
public class LayerView extends FrameLayout { |
| 39 |
private static final String LOGTAG = LayerView.class.getName(); |
| 40 |
|
| 41 |
private GeckoLayerClient mLayerClient; |
| 42 |
private JavaPanZoomController mPanZoomController; |
| 43 |
private final GLController mGLController; |
| 44 |
private InputConnectionHandler mInputConnectionHandler; |
| 45 |
private LayerRenderer mRenderer; |
| 46 |
|
| 47 |
private final SurfaceView mSurfaceView; |
| 48 |
|
| 49 |
private Listener mListener; |
| 50 |
|
| 51 |
public LayerView(Context context, AttributeSet attrs) { |
| 52 |
super(context, attrs); |
| 53 |
|
| 54 |
mSurfaceView = new SurfaceView(context); |
| 55 |
addView(mSurfaceView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); |
| 56 |
|
| 57 |
SurfaceHolder holder = mSurfaceView.getHolder(); |
| 58 |
holder.addCallback(new SurfaceListener()); |
| 59 |
holder.setFormat(PixelFormat.RGB_565); |
| 60 |
|
| 61 |
mGLController = new GLController(this); |
| 62 |
} |
| 63 |
|
| 64 |
void connect(GeckoLayerClient layerClient) { |
| 65 |
mLayerClient = layerClient; |
| 66 |
mPanZoomController = mLayerClient.getPanZoomController(); |
| 67 |
mRenderer = new LayerRenderer(this); |
| 68 |
mInputConnectionHandler = null; |
| 69 |
|
| 70 |
setFocusable(true); |
| 71 |
setFocusableInTouchMode(true); |
| 72 |
|
| 73 |
createGLThread(); |
| 74 |
setOnTouchListener(new OnSlideSwipeListener(getContext(), mLayerClient)); |
| 75 |
} |
| 76 |
|
| 77 |
public void show() { |
| 78 |
// Fix this if TextureView support is turned back on above |
| 79 |
mSurfaceView.setVisibility(View.VISIBLE); |
| 80 |
} |
| 81 |
|
| 82 |
public void hide() { |
| 83 |
// Fix this if TextureView support is turned back on above |
| 84 |
mSurfaceView.setVisibility(View.INVISIBLE); |
| 85 |
} |
| 86 |
|
| 87 |
public void destroy() { |
| 88 |
if (mRenderer != null) { |
| 89 |
mRenderer.destroy(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public void setInputConnectionHandler(InputConnectionHandler inputConnectionHandler) { |
| 94 |
mInputConnectionHandler = inputConnectionHandler; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public boolean onTouchEvent(MotionEvent event) { |
| 99 |
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 100 |
requestFocus(); |
| 101 |
} |
| 102 |
|
| 103 |
return mPanZoomController != null && mPanZoomController.onTouchEvent(event); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public boolean onHoverEvent(MotionEvent event) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public boolean onGenericMotionEvent(MotionEvent event) { |
| 113 |
return mPanZoomController != null && mPanZoomController.onMotionEvent(event); |
| 114 |
} |
| 115 |
|
| 116 |
public GeckoLayerClient getLayerClient() { return mLayerClient; } |
| 117 |
|
| 118 |
public ImmutableViewportMetrics getViewportMetrics() { |
| 119 |
return mLayerClient.getViewportMetrics(); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
| 124 |
if (mInputConnectionHandler != null) |
| 125 |
return mInputConnectionHandler.onCreateInputConnection(outAttrs); |
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 131 |
return mInputConnectionHandler != null && mInputConnectionHandler.onKeyPreIme(keyCode, event); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 136 |
return mInputConnectionHandler != null && mInputConnectionHandler.onKeyDown(keyCode, event); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 141 |
return mInputConnectionHandler != null && mInputConnectionHandler.onKeyLongPress(keyCode, event); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 146 |
return mInputConnectionHandler != null && mInputConnectionHandler.onKeyMultiple(keyCode, repeatCount, event); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 151 |
return mInputConnectionHandler != null && mInputConnectionHandler.onKeyUp(keyCode, event); |
| 152 |
} |
| 153 |
|
| 154 |
public void requestRender() { |
| 155 |
if (mListener != null) { |
| 156 |
mListener.renderRequested(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public void addLayer(Layer layer) { |
| 161 |
mRenderer.addLayer(layer); |
| 162 |
} |
| 163 |
|
| 164 |
public void removeLayer(Layer layer) { |
| 165 |
mRenderer.removeLayer(layer); |
| 166 |
} |
| 167 |
|
| 168 |
public void setLayerRenderer(LayerRenderer renderer) { |
| 169 |
mRenderer = renderer; |
| 170 |
} |
| 171 |
|
| 172 |
public LayerRenderer getRenderer() { |
| 173 |
return mRenderer; |
| 174 |
} |
| 175 |
|
| 176 |
public void setListener(Listener listener) { |
| 177 |
mListener = listener; |
| 178 |
} |
| 179 |
|
| 180 |
public GLController getGLController() { |
| 181 |
return mGLController; |
| 182 |
} |
| 183 |
|
| 184 |
public Bitmap getDrawable(String name) { |
| 185 |
Context context = getContext(); |
| 186 |
Resources resources = context.getResources(); |
| 187 |
String packageName = resources.getResourcePackageName(R.id.dummy_id_for_package_name_resolution); |
| 188 |
int resourceID = resources.getIdentifier(name, "drawable", packageName); |
| 189 |
BitmapFactory.Options options = new BitmapFactory.Options(); |
| 190 |
options.inScaled = false; |
| 191 |
return BitmapFactory.decodeResource(context.getResources(), resourceID, options); |
| 192 |
} |
| 193 |
|
| 194 |
Bitmap getBackgroundPattern() { |
| 195 |
return getDrawable("background"); |
| 196 |
} |
| 197 |
|
| 198 |
Bitmap getShadowPattern() { |
| 199 |
return getDrawable("shadow"); |
| 200 |
} |
| 201 |
|
| 202 |
private void onSizeChanged(int width, int height) { |
| 203 |
mGLController.surfaceChanged(width, height); |
| 204 |
|
| 205 |
mLayerClient.setViewportSize(new FloatSize(width, height), false); |
| 206 |
|
| 207 |
if (mListener != null) { |
| 208 |
mListener.surfaceChanged(width, height); |
| 209 |
} |
| 210 |
|
| 211 |
LOKitShell.sendEvent(new LOEvent(LOEvent.UPDATE_ZOOM_CONSTRAINTS)); |
| 212 |
} |
| 213 |
|
| 214 |
private void onDestroyed() { |
| 215 |
mGLController.surfaceDestroyed(); |
| 216 |
|
| 217 |
if (mListener != null) { |
| 218 |
mListener.compositionPauseRequested(); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
public Object getNativeWindow() { |
| 223 |
return mSurfaceView.getHolder(); |
| 224 |
} |
| 225 |
|
| 226 |
public interface Listener { |
| 227 |
void renderRequested(); |
| 228 |
void compositionPauseRequested(); |
| 229 |
void surfaceChanged(int width, int height); |
| 230 |
} |
| 231 |
|
| 232 |
private class SurfaceListener implements SurfaceHolder.Callback { |
| 233 |
public void surfaceChanged(SurfaceHolder holder, int format, int width, |
| 234 |
int height) { |
| 235 |
onSizeChanged(width, height); |
| 236 |
} |
| 237 |
|
| 238 |
public void surfaceCreated(SurfaceHolder holder) { |
| 239 |
if (mRenderControllerThread != null) { |
| 240 |
mRenderControllerThread.surfaceCreated(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public void surfaceDestroyed(SurfaceHolder holder) { |
| 245 |
onDestroyed(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 251 |
super.onLayout(changed, left, top, right, bottom); |
| 252 |
if (changed) { |
| 253 |
mLayerClient.setViewportSize(new FloatSize(right - left, bottom - top), true); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
private RenderControllerThread mRenderControllerThread; |
| 258 |
|
| 259 |
public synchronized void createGLThread() { |
| 260 |
if (mRenderControllerThread != null) { |
| 261 |
throw new LayerViewException ("createGLThread() called with a GL thread already in place!"); |
| 262 |
} |
| 263 |
|
| 264 |
Log.e(LOGTAG, "### Creating GL thread!"); |
| 265 |
mRenderControllerThread = new RenderControllerThread(mGLController); |
| 266 |
mRenderControllerThread.start(); |
| 267 |
setListener(mRenderControllerThread); |
| 268 |
notifyAll(); |
| 269 |
} |
| 270 |
|
| 271 |
public synchronized Thread destroyGLThread() { |
| 272 |
// Wait for the GL thread to be started. |
| 273 |
Log.e(LOGTAG, "### Waiting for GL thread to be created..."); |
| 274 |
while (mRenderControllerThread == null) { |
| 275 |
try { |
| 276 |
wait(); |
| 277 |
} catch (InterruptedException e) { |
| 278 |
throw new RuntimeException(e); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
Log.e(LOGTAG, "### Destroying GL thread!"); |
| 283 |
Thread thread = mRenderControllerThread; |
| 284 |
mRenderControllerThread.shutdown(); |
| 285 |
setListener(null); |
| 286 |
mRenderControllerThread = null; |
| 287 |
return thread; |
| 288 |
} |
| 289 |
|
| 290 |
public static class LayerViewException extends RuntimeException { |
| 291 |
public static final long serialVersionUID = 1L; |
| 292 |
|
| 293 |
LayerViewException(String e) { |
| 294 |
super(e); |
| 295 |
} |
| 296 |
} |
| 297 |
} |