- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / services / native_viewport / android / src / org / chromium / mojo / MojoViewport.java
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.util.AttributeSet;
10 import android.view.Surface;
11 import android.view.SurfaceHolder;
12 import android.view.SurfaceView;
13
14 import org.chromium.base.CalledByNative;
15 import org.chromium.base.JNINamespace;
16
17 @JNINamespace("mojo::services")
18 public class MojoViewport extends SurfaceView {
19
20     private int mNativeMojoViewport;
21     private final SurfaceHolder.Callback mSurfaceCallback;
22
23     @SuppressWarnings("unused")
24     @CalledByNative
25     public static void createForActivity(Activity activity, int init) {
26         activity.setContentView(new MojoViewport(activity, init));
27     }
28
29     public MojoViewport(Context context, int init) {
30         super(context);
31
32         mNativeMojoViewport = nativeInit(init);
33         assert mNativeMojoViewport != 0;
34
35         mSurfaceCallback = new SurfaceHolder.Callback() {
36             @Override
37             public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
38                 assert mNativeMojoViewport != 0;
39                 nativeSurfaceSetSize(mNativeMojoViewport, width, height);
40             }
41
42             @Override
43             public void surfaceCreated(SurfaceHolder holder) {
44                 assert mNativeMojoViewport != 0;
45                 nativeSurfaceCreated(mNativeMojoViewport, holder.getSurface());
46             }
47
48             @Override
49             public void surfaceDestroyed(SurfaceHolder holder) {
50                 assert mNativeMojoViewport != 0;
51                 nativeSurfaceDestroyed(mNativeMojoViewport);
52             }
53         };
54         getHolder().addCallback(mSurfaceCallback);
55
56     }
57
58     // TODO(abarth): Someone needs to call destroy at some point.
59     public void destroy() {
60         getHolder().removeCallback(mSurfaceCallback);
61         nativeDestroy(mNativeMojoViewport);
62         mNativeMojoViewport = 0;
63     }
64
65     private static native int nativeInit(int init);
66     private static native void nativeDestroy(int nativeMojoViewport);
67     private static native void nativeSurfaceCreated(int nativeMojoViewport, Surface surface);
68     private static native void nativeSurfaceDestroyed(int nativeMojoViewport);
69     private static native void nativeSurfaceSetSize(int nativeMojoViewport, int width, int height);
70 };