1/*
2 * Copyright (C) 2011 Skia
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 com.skia.sampleapp;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.os.Bundle;
23import android.util.AttributeSet;
24import android.view.KeyEvent;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.LinearLayout;
29import android.widget.TextView;
30
31public class SampleApp extends Activity
32{
33    private TextView mTitle;
34
35    public class SampleView extends View {
36        public SampleView(Context context) {
37            super(context);
38            createOSWindow(this);
39        }
40
41        @Override
42        protected void onDraw(Canvas canvas) {
43            drawToCanvas(canvas);
44        }
45
46        @Override
47        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
48            updateSize(w, h);
49        }
50
51        @Override
52        public boolean onTouchEvent(MotionEvent event) {
53            final int x = (int) event.getX();
54            final int y = (int) event.getY();
55            final int action = event.getAction();
56            handleClick(x, y, action);
57            return true;
58        }
59    }
60
61    @Override
62    public void onCreate(Bundle savedInstanceState)
63    {
64        super.onCreate(savedInstanceState);
65
66        init();
67        setContentView(R.layout.layout);
68        mTitle = (TextView) findViewById(R.id.title_view);
69        LinearLayout holder = (LinearLayout) findViewById(R.id.holder);
70        View view = new SampleView(this);
71        holder.addView(view, new LinearLayout.LayoutParams(
72                ViewGroup.LayoutParams.MATCH_PARENT,
73                ViewGroup.LayoutParams.MATCH_PARENT));
74    }
75
76    @Override
77    public void onDestroy()
78    {
79        term();
80        super.onDestroy();
81    }
82
83    @Override
84    public boolean dispatchKeyEvent(KeyEvent event) {
85        switch (event.getAction()) {
86            case KeyEvent.ACTION_DOWN:
87                int uni = event.getUnicodeChar(event.getMetaState());
88                return handleKeyDown(event.getKeyCode(), uni);
89            case KeyEvent.ACTION_UP:
90                return handleKeyUp(event.getKeyCode());
91            default:
92                return false;
93        }
94    }
95
96    @Override
97    public void setTitle(CharSequence title) {
98        mTitle.setText(title);
99    }
100
101    private native void drawToCanvas(Canvas canvas);
102    private native void init();
103    private native void term();
104    // Currently depends on init having already been called.
105    private native void createOSWindow(SampleView view);
106    private native void updateSize(int w, int h);
107    private native void handleClick(int x, int y, int state);
108    private native boolean handleKeyDown(int key, int uni);
109    private native boolean handleKeyUp(int key);
110
111    static {
112        System.loadLibrary("skia-sample");
113    }
114}
115