aboutsummaryrefslogtreecommitdiffhomepage
path: root/android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java
blob: b02c4d7fe403cceed287e3811db3cf77a910d9de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2011 Skia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.skia.sampleapp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SampleApp extends Activity
{
    private TextView mTitle;

    public class SampleView extends View {
        public SampleView(Context context) {
            super(context);
            createOSWindow(this);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            drawToCanvas(canvas);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            updateSize(w, h);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            final int x = (int) event.getX();
            final int y = (int) event.getY();
            final int action = event.getAction();
            handleClick(x, y, action);
            return true;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        init();
        setContentView(R.layout.layout);
        mTitle = (TextView) findViewById(R.id.title_view);
        LinearLayout holder = (LinearLayout) findViewById(R.id.holder);
        View view = new SampleView(this);
        holder.addView(view, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void onDestroy()
    {
        term();
        super.onDestroy();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getAction()) {
            case KeyEvent.ACTION_DOWN:
                int uni = event.getUnicodeChar(event.getMetaState());
                return handleKeyDown(event.getKeyCode(), uni);
            case KeyEvent.ACTION_UP:
                return handleKeyUp(event.getKeyCode());
            default:
                return false;
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle.setText(title);
    }

    private native void drawToCanvas(Canvas canvas);
    private native void init();
    private native void term();
    // Currently depends on init having already been called.
    private native void createOSWindow(SampleView view);
    private native void updateSize(int w, int h);
    private native void handleClick(int x, int y, int state);
    private native boolean handleKeyDown(int key, int uni);
    private native boolean handleKeyUp(int key);

    static {
        System.loadLibrary("skia-sample");
    }
}