| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.blimp; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.graphics.Color; | |
| 9 import android.graphics.Point; | |
| 10 import android.os.Build; | |
| 11 import android.util.AttributeSet; | |
| 12 import android.view.MotionEvent; | |
| 13 import android.view.Surface; | |
| 14 import android.view.SurfaceHolder; | |
| 15 import android.view.SurfaceView; | |
| 16 import android.view.View; | |
| 17 import android.view.WindowManager; | |
| 18 | |
| 19 import org.chromium.base.annotations.CalledByNative; | |
| 20 import org.chromium.base.annotations.JNINamespace; | |
| 21 import org.chromium.blimp.session.BlimpClientSession; | |
| 22 import org.chromium.ui.UiUtils; | |
| 23 | |
| 24 /** | |
| 25 * A {@link View} that will visually represent the Blimp rendered content. This
{@link View} starts | |
| 26 * a native compositor. | |
| 27 */ | |
| 28 @JNINamespace("blimp::client::app") | |
| 29 public class BlimpView | |
| 30 extends SurfaceView implements SurfaceHolder.Callback, View.OnLayoutChan
geListener { | |
| 31 private long mNativeBlimpViewPtr; | |
| 32 | |
| 33 /** | |
| 34 * Builds a new {@link BlimpView}. | |
| 35 * @param context A {@link Context} instance. | |
| 36 * @param attrs An {@link AttributeSet} instance. | |
| 37 */ | |
| 38 public BlimpView(Context context, AttributeSet attrs) { | |
| 39 super(context, attrs); | |
| 40 setFocusable(true); | |
| 41 setFocusableInTouchMode(true); | |
| 42 addOnLayoutChangeListener(this); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Starts up rendering for this {@link View}. This will start up the native
compositor and will | |
| 47 * display it's contents. | |
| 48 * @param blimpClientSession The {@link BlimpClientSession} that contains th
e content-lite | |
| 49 * features required by the native components of t
he compositor. | |
| 50 */ | |
| 51 public void initializeRenderer(BlimpClientSession blimpClientSession) { | |
| 52 assert mNativeBlimpViewPtr == 0; | |
| 53 | |
| 54 WindowManager windowManager = | |
| 55 (WindowManager) getContext().getSystemService(Context.WINDOW_SER
VICE); | |
| 56 Point displaySize = new Point(); | |
| 57 windowManager.getDefaultDisplay().getSize(displaySize); | |
| 58 Point physicalSize = new Point(); | |
| 59 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
| 60 windowManager.getDefaultDisplay().getRealSize(physicalSize); | |
| 61 } | |
| 62 float deviceScaleFactor = getContext().getResources().getDisplayMetrics(
).density; | |
| 63 mNativeBlimpViewPtr = nativeInit(blimpClientSession, physicalSize.x, phy
sicalSize.y, | |
| 64 displaySize.x, displaySize.y, deviceScaleFactor); | |
| 65 getHolder().addCallback(this); | |
| 66 setBackgroundColor(Color.WHITE); | |
| 67 setVisibility(VISIBLE); | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Stops rendering for this {@link View} and destroys all internal state. T
his {@link View} | |
| 72 * should not be used after this. | |
| 73 */ | |
| 74 public void destroyRenderer() { | |
| 75 getHolder().removeCallback(this); | |
| 76 if (mNativeBlimpViewPtr != 0) { | |
| 77 nativeDestroy(mNativeBlimpViewPtr); | |
| 78 mNativeBlimpViewPtr = 0; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 // View.OnLayoutChangeListener implementation. | |
| 83 @Override | |
| 84 public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, | |
| 85 int oldTop, int oldRight, int oldBottom) { | |
| 86 if (mNativeBlimpViewPtr == 0) return; | |
| 87 nativeOnContentAreaSizeChanged(mNativeBlimpViewPtr, right - left, bottom
- top, | |
| 88 getContext().getResources().getDisplayMetrics().density); | |
| 89 } | |
| 90 | |
| 91 // View overrides. | |
| 92 @Override | |
| 93 public boolean onTouchEvent(MotionEvent event) { | |
| 94 if (mNativeBlimpViewPtr == 0) return false; | |
| 95 | |
| 96 int eventAction = event.getActionMasked(); | |
| 97 | |
| 98 // Close the IME. It might be open for typing URL into toolbar. | |
| 99 // TODO(shaktisahu): Detect if the IME was open and return immediately (
crbug/606977) | |
| 100 UiUtils.hideKeyboard(this); | |
| 101 | |
| 102 if (!isValidTouchEventActionForNative(eventAction)) return false; | |
| 103 | |
| 104 int pointerCount = event.getPointerCount(); | |
| 105 | |
| 106 float[] touchMajor = {event.getTouchMajor(), pointerCount > 1 ? event.ge
tTouchMajor(1) : 0}; | |
| 107 float[] touchMinor = {event.getTouchMinor(), pointerCount > 1 ? event.ge
tTouchMinor(1) : 0}; | |
| 108 | |
| 109 for (int i = 0; i < 2; i++) { | |
| 110 if (touchMajor[i] < touchMinor[i]) { | |
| 111 float tmp = touchMajor[i]; | |
| 112 touchMajor[i] = touchMinor[i]; | |
| 113 touchMinor[i] = tmp; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 boolean consumed = nativeOnTouchEvent(mNativeBlimpViewPtr, event, | |
| 118 event.getEventTime(), eventAction, | |
| 119 pointerCount, event.getHistorySize(), event.getActionIndex(), | |
| 120 event.getX(), event.getY(), | |
| 121 pointerCount > 1 ? event.getX(1) : 0, | |
| 122 pointerCount > 1 ? event.getY(1) : 0, | |
| 123 event.getPointerId(0), pointerCount > 1 ? event.getPointerId(1)
: -1, | |
| 124 touchMajor[0], touchMajor[1], | |
| 125 touchMinor[0], touchMinor[1], | |
| 126 event.getOrientation(), pointerCount > 1 ? event.getOrientation(
1) : 0, | |
| 127 event.getAxisValue(MotionEvent.AXIS_TILT), | |
| 128 pointerCount > 1 ? event.getAxisValue(MotionEvent.AXIS_TILT, 1)
: 0, | |
| 129 event.getRawX(), event.getRawY(), | |
| 130 event.getToolType(0), | |
| 131 pointerCount > 1 ? event.getToolType(1) : MotionEvent.TOOL_TYPE_
UNKNOWN, | |
| 132 event.getButtonState(), | |
| 133 event.getMetaState()); | |
| 134 | |
| 135 return consumed; | |
| 136 } | |
| 137 | |
| 138 // SurfaceView overrides. | |
| 139 @Override | |
| 140 protected void onFinishInflate() { | |
| 141 super.onFinishInflate(); | |
| 142 | |
| 143 setZOrderMediaOverlay(true); | |
| 144 setVisibility(GONE); | |
| 145 } | |
| 146 | |
| 147 // SurfaceHolder.Callback2 interface. | |
| 148 @Override | |
| 149 public void surfaceChanged(SurfaceHolder holder, int format, int width, int
height) { | |
| 150 if (mNativeBlimpViewPtr == 0) return; | |
| 151 nativeOnSurfaceChanged(mNativeBlimpViewPtr, format, width, height, holde
r.getSurface()); | |
| 152 } | |
| 153 | |
| 154 @Override | |
| 155 public void surfaceCreated(SurfaceHolder holder) { | |
| 156 if (mNativeBlimpViewPtr == 0) return; | |
| 157 nativeOnSurfaceCreated(mNativeBlimpViewPtr); | |
| 158 } | |
| 159 | |
| 160 @Override | |
| 161 public void surfaceDestroyed(SurfaceHolder holder) { | |
| 162 if (mNativeBlimpViewPtr == 0) return; | |
| 163 nativeOnSurfaceDestroyed(mNativeBlimpViewPtr); | |
| 164 } | |
| 165 | |
| 166 private static boolean isValidTouchEventActionForNative(int eventAction) { | |
| 167 // Only these actions have any effect on gesture detection. Other | |
| 168 // actions have no corresponding WebTouchEvent type and may confuse the | |
| 169 // touch pipline, so we ignore them entirely. | |
| 170 return eventAction == MotionEvent.ACTION_DOWN | |
| 171 || eventAction == MotionEvent.ACTION_UP | |
| 172 || eventAction == MotionEvent.ACTION_CANCEL | |
| 173 || eventAction == MotionEvent.ACTION_MOVE | |
| 174 || eventAction == MotionEvent.ACTION_POINTER_DOWN | |
| 175 || eventAction == MotionEvent.ACTION_POINTER_UP; | |
| 176 } | |
| 177 | |
| 178 @CalledByNative | |
| 179 public void onSwapBuffersCompleted() { | |
| 180 if (getBackground() == null) return; | |
| 181 | |
| 182 setBackgroundResource(0); | |
| 183 } | |
| 184 | |
| 185 // Native Methods | |
| 186 private native long nativeInit(BlimpClientSession blimpClientSession, int ph
ysicalWidth, | |
| 187 int physicalHeight, int displayWidth, int displayHeight, float dpToP
ixel); | |
| 188 private native void nativeDestroy(long nativeBlimpView); | |
| 189 private native void nativeOnContentAreaSizeChanged( | |
| 190 long nativeBlimpView, int width, int height, float dpToPx); | |
| 191 private native void nativeOnSurfaceChanged( | |
| 192 long nativeBlimpView, int format, int width, int height, Surface sur
face); | |
| 193 private native void nativeOnSurfaceCreated(long nativeBlimpView); | |
| 194 private native void nativeOnSurfaceDestroyed(long nativeBlimpView); | |
| 195 private native boolean nativeOnTouchEvent( | |
| 196 long nativeBlimpView, MotionEvent event, | |
| 197 long timeMs, int action, int pointerCount, int historySize, int acti
onIndex, | |
| 198 float x0, float y0, float x1, float y1, | |
| 199 int pointerId0, int pointerId1, | |
| 200 float touchMajor0, float touchMajor1, | |
| 201 float touchMinor0, float touchMinor1, | |
| 202 float orientation0, float orientation1, | |
| 203 float tilt0, float tilt1, | |
| 204 float rawX, float rawY, | |
| 205 int androidToolType0, int androidToolType1, | |
| 206 int androidButtonState, int androidMetaState); | |
| 207 } | |
| OLD | NEW |