OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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.mojo; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.content.Context; |
| 9 import android.view.KeyEvent; |
| 10 import android.view.MotionEvent; |
| 11 import android.view.Surface; |
| 12 import android.view.SurfaceHolder; |
| 13 import android.view.SurfaceView; |
| 14 import android.view.View; |
| 15 |
| 16 import org.chromium.base.CalledByNative; |
| 17 import org.chromium.base.JNINamespace; |
| 18 |
| 19 /** |
| 20 * Exposes SurfaceView to native code. |
| 21 */ |
| 22 @JNINamespace("native_viewport") |
| 23 public class PlatformViewportAndroid extends SurfaceView { |
| 24 |
| 25 private long mNativeMojoViewport; |
| 26 private final SurfaceHolder.Callback mSurfaceCallback; |
| 27 |
| 28 @SuppressWarnings("unused") |
| 29 @CalledByNative |
| 30 public static void createForActivity(Activity activity, long nativeViewport)
{ |
| 31 activity.setContentView(new PlatformViewportAndroid(activity, nativeView
port)); |
| 32 } |
| 33 |
| 34 public PlatformViewportAndroid(Context context, long nativeViewport) { |
| 35 super(context); |
| 36 |
| 37 setFocusable(true); |
| 38 setFocusableInTouchMode(true); |
| 39 |
| 40 mNativeMojoViewport = nativeViewport; |
| 41 assert mNativeMojoViewport != 0; |
| 42 |
| 43 final float density = context.getResources().getDisplayMetrics().density
; |
| 44 |
| 45 mSurfaceCallback = new SurfaceHolder.Callback() { |
| 46 @Override |
| 47 public void surfaceChanged(SurfaceHolder holder, int format, int wid
th, int height) { |
| 48 assert mNativeMojoViewport != 0; |
| 49 nativeSurfaceSetSize(mNativeMojoViewport, width, height, density
); |
| 50 } |
| 51 |
| 52 @Override |
| 53 public void surfaceCreated(SurfaceHolder holder) { |
| 54 assert mNativeMojoViewport != 0; |
| 55 nativeSurfaceCreated(mNativeMojoViewport, holder.getSurface()); |
| 56 } |
| 57 |
| 58 @Override |
| 59 public void surfaceDestroyed(SurfaceHolder holder) { |
| 60 assert mNativeMojoViewport != 0; |
| 61 nativeSurfaceDestroyed(mNativeMojoViewport); |
| 62 } |
| 63 }; |
| 64 getHolder().addCallback(mSurfaceCallback); |
| 65 |
| 66 } |
| 67 |
| 68 // TODO(abarth): Someone needs to call destroy at some point. |
| 69 public void destroy() { |
| 70 getHolder().removeCallback(mSurfaceCallback); |
| 71 nativeDestroy(mNativeMojoViewport); |
| 72 mNativeMojoViewport = 0; |
| 73 } |
| 74 |
| 75 @Override |
| 76 protected void onWindowVisibilityChanged(int visibility) { |
| 77 super.onWindowVisibilityChanged(visibility); |
| 78 if (visibility == View.VISIBLE) { |
| 79 requestFocusFromTouch(); |
| 80 requestFocus(); |
| 81 } |
| 82 } |
| 83 |
| 84 @Override |
| 85 public boolean onTouchEvent(MotionEvent event) { |
| 86 final int actionMasked = event.getActionMasked(); |
| 87 if (actionMasked == MotionEvent.ACTION_POINTER_DOWN |
| 88 || actionMasked == MotionEvent.ACTION_POINTER_UP) { |
| 89 // Up/down events identify a single point. |
| 90 return notifyTouchEventAtIndex(event, event.getActionIndex()); |
| 91 } |
| 92 assert event.getPointerCount() != 0; |
| 93 // All other types can have more than one point. |
| 94 boolean result = false; |
| 95 for (int i = 0, count = event.getPointerCount(); i < count; i++) { |
| 96 final boolean sub_result = notifyTouchEventAtIndex(event, i); |
| 97 result |= sub_result; |
| 98 } |
| 99 return result; |
| 100 } |
| 101 |
| 102 @Override |
| 103 public boolean dispatchKeyEvent(KeyEvent event) { |
| 104 if (privateDispatchKeyEvent(event)) { |
| 105 return true; |
| 106 } |
| 107 return super.dispatchKeyEvent(event); |
| 108 } |
| 109 |
| 110 @Override |
| 111 public boolean dispatchKeyEventPreIme(KeyEvent event) { |
| 112 if (privateDispatchKeyEvent(event)) { |
| 113 return true; |
| 114 } |
| 115 return super.dispatchKeyEventPreIme(event); |
| 116 } |
| 117 |
| 118 @Override |
| 119 public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
| 120 if (privateDispatchKeyEvent(event)) { |
| 121 return true; |
| 122 } |
| 123 return super.dispatchKeyShortcutEvent(event); |
| 124 } |
| 125 |
| 126 private boolean notifyTouchEventAtIndex(MotionEvent event, int index) { |
| 127 return nativeTouchEvent(mNativeMojoViewport, event.getEventTime(), event
.getActionMasked(), |
| 128 event.getPointerId(index), event.getX(index), event.getY(index), |
| 129 event.getPressure(index), event.getTouchMajor(index), event.getT
ouchMinor(index), |
| 130 event.getOrientation(index), event.getAxisValue(MotionEvent.AXIS
_HSCROLL, index), |
| 131 event.getAxisValue(MotionEvent.AXIS_VSCROLL, index)); |
| 132 } |
| 133 |
| 134 private boolean privateDispatchKeyEvent(KeyEvent event) { |
| 135 if (event.getAction() == KeyEvent.ACTION_MULTIPLE) { |
| 136 boolean result = false; |
| 137 if (event.getKeyCode() == KeyEvent.KEYCODE_UNKNOWN && event.getChara
cters() != null) { |
| 138 String characters = event.getCharacters(); |
| 139 for (int i = 0; i < characters.length(); ++i) { |
| 140 char c = characters.charAt(i); |
| 141 int codepoint = c; |
| 142 if (codepoint >= Character.MIN_SURROGATE |
| 143 && codepoint < (Character.MAX_SURROGATE + 1)) { |
| 144 i++; |
| 145 char c2 = characters.charAt(i); |
| 146 codepoint = Character.toCodePoint(c, c2); |
| 147 } |
| 148 result |= nativeKeyEvent(mNativeMojoViewport, true, 0, codep
oint); |
| 149 result |= nativeKeyEvent(mNativeMojoViewport, false, 0, code
point); |
| 150 } |
| 151 } else { |
| 152 for (int i = 0; i < event.getRepeatCount(); ++i) { |
| 153 result |= nativeKeyEvent( |
| 154 mNativeMojoViewport, true, event.getKeyCode(), event
.getUnicodeChar()); |
| 155 result |= nativeKeyEvent( |
| 156 mNativeMojoViewport, false, event.getKeyCode(), even
t.getUnicodeChar()); |
| 157 } |
| 158 } |
| 159 return result; |
| 160 } else { |
| 161 return nativeKeyEvent(mNativeMojoViewport, event.getAction() == KeyE
vent.ACTION_DOWN, |
| 162 event.getKeyCode(), event.getUnicodeChar()); |
| 163 } |
| 164 } |
| 165 |
| 166 private static native void nativeDestroy(long nativePlatformViewportAndroid)
; |
| 167 |
| 168 private static native void nativeSurfaceCreated( |
| 169 long nativePlatformViewportAndroid, Surface surface); |
| 170 |
| 171 private static native void nativeSurfaceDestroyed( |
| 172 long nativePlatformViewportAndroid); |
| 173 |
| 174 private static native void nativeSurfaceSetSize( |
| 175 long nativePlatformViewportAndroid, int width, int height, float den
sity); |
| 176 |
| 177 private static native boolean nativeTouchEvent(long nativePlatformViewportAn
droid, long timeMs, |
| 178 int maskedAction, int pointerId, float x, float y, float pressure, f
loat touchMajor, |
| 179 float touchMinor, float orientation, float hWheel, float vWheel); |
| 180 |
| 181 private static native boolean nativeKeyEvent( |
| 182 long nativePlatformViewportAndroid, boolean pressed, int keyCode, in
t unicodeCharacter); |
| 183 } |
OLD | NEW |