aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/CanvasProofActivity.java
blob: 9363585bbff7a51d379d478c76808593b01211f3 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

package org.skia.canvasproof;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Picture;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;
import android.widget.LinearLayout;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class CanvasProofActivity extends Activity {
    private static final String TAG = "CanvasProofActivity";
    private GaneshPictureRenderer ganeshPictureRenderer;
    private HwuiPictureView hwuiPictureView;
    private GLSurfaceView ganeshPictureView;
    private LinearLayout splitPaneView;
    private View currentView;
    private float x, y;
    private int resourcesIndex;
    private class PictureAsset {
        public String path;
        public long ptr;
        public Picture picture;
    };
    private PictureAsset[] assets;

    @SuppressWarnings("deprecation")  // purposely using this
    private static Picture ReadPicture(InputStream inputStream)
        throws IOException {
        Picture p = null;
        try {
            p = Picture.createFromStream(inputStream);
        } catch (java.lang.Exception e) {
            Log.e(TAG, "Exception in Picture.createFromStream", e);
        }
        inputStream.close();
        return p;
    }

    private void getAssetPaths() {
        String directory = "skps";
        AssetManager mgr = this.getAssets();
        assert (mgr != null);
        String[] resources;
        try {
            resources = mgr.list(directory);
        } catch (IOException e) {
            Log.e(TAG, "IOException in getAssetPaths", e);
            return;
        }
        if (resources == null || resources.length == 0) {
            Log.e(TAG, "SKP assets should be packaged in " +
                  ".../apps/canvasproof/src/main/assets/skps/" +
                  ", but none were found.");
            return;
        }
        CreateSkiaPicture.init();
        this.assets = new PictureAsset[resources.length];
        for (int i = 0; i < resources.length; ++i) {
            String path = directory + File.separator + resources[i];
            this.assets[i] = new PictureAsset();
            this.assets[i].path = path;
            try {
                this.assets[i].ptr = CreateSkiaPicture.create(mgr.open(path));
                if (0 == this.assets[i].ptr) {
                    Log.e(TAG, "CreateSkiaPicture.create returned 0 " + path);
                }
                Picture p = CanvasProofActivity.ReadPicture(mgr.open(path));
                if (null == p) {
                    Log.e(TAG, "CanvasProofActivity.ReadPicture.create " +
                          "returned null " + path);
                } else if (0 == p.getHeight() || 0 == p.getWidth()) {
                    Log.e(TAG, "CanvasProofActivity.ReadPicture.create " +
                          "empty picture" + path);
                    p = null;
                }
                this.assets[i].picture = p;
            } catch (IOException e) {
                Log.e(TAG, "IOException in getAssetPaths " + path + e);
                return;
            }
        }
    }

    private void nextView() {
        if (this.currentView == this.hwuiPictureView) {
            this.currentView = this.ganeshPictureView;
            this.ganeshPictureView.setRenderMode(
                    GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        } else if (this.currentView == null ||
                   this.currentView == this.ganeshPictureView) {
            this.setContentView(new View(this));
            LayoutParams layoutParams =
                new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.5f);
            this.ganeshPictureView.setLayoutParams(layoutParams);
            this.ganeshPictureView.setRenderMode(
                    GLSurfaceView.RENDERMODE_WHEN_DIRTY);
            this.splitPaneView.addView(this.ganeshPictureView);
            this.hwuiPictureView.setLayoutParams(layoutParams);
            this.splitPaneView.addView(this.hwuiPictureView);
            this.currentView = this.splitPaneView;
            this.hwuiPictureView.fullTime = false;
        } else if (this.currentView == this.splitPaneView) {
            this.splitPaneView.removeAllViews();
            this.currentView = this.hwuiPictureView;
            this.hwuiPictureView.fullTime = true;
        } else {
            Log.e(TAG, "unexpected value");
            this.setContentView(null);
            return;
        }
        this.setContentView(this.currentView);
        this.currentView.invalidate();
    }

    private void nextPicture(int d) {
        if (this.assets == null) {
            Log.w(TAG, "this.assets == null");
            return;
        }
        assert (this.assets.length > 0);
        resourcesIndex = (resourcesIndex + d) % this.assets.length;
        while (resourcesIndex < 0) {
            resourcesIndex += this.assets.length;
        }
        while (resourcesIndex >= this.assets.length) {
            resourcesIndex -= this.assets.length;
        }
        this.ganeshPictureRenderer.setPicture(assets[resourcesIndex].ptr);
        this.hwuiPictureView.setPicture(assets[resourcesIndex].picture);
        this.currentView.invalidate();
    }

    @Override
    protected void onStop() {
        this.ganeshPictureRenderer.releaseResources();
        super.onStop();
    }

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

        this.getAssetPaths();
        this.ganeshPictureRenderer = new GaneshPictureRenderer();
        this.hwuiPictureView = new HwuiPictureView(this);

        this.ganeshPictureRenderer.setScale(2.0f);
        this.hwuiPictureView.setScale(2.0f);
        this.ganeshPictureView = ganeshPictureRenderer.makeView(this);
        this.splitPaneView = new LinearLayout(this);
        this.splitPaneView.setOrientation(LinearLayout.VERTICAL);
        this.splitPaneView.setGravity(Gravity.FILL);

        LayoutParams layoutParams =
            new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.5f);
        this.ganeshPictureView.setLayoutParams(layoutParams);
        this.hwuiPictureView.setLayoutParams(layoutParams);

        this.nextView();
        this.nextPicture(0);
    }

    // TODO: replace this funtion with onTouchEvent().
    // @Override public boolean onTouchEvent(MotionEvent event)...
    @Override
    public boolean dispatchTouchEvent (MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                this.x = event.getX();
                this.y = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                float dx = event.getX() - this.x;
                float dy = event.getY() - this.y;
                float dx2 = dx * dx;
                float dy2 = dy * dy;
                if (dx2 + dy2 > 22500.0) {
                    if (dy2 < dx2) {
                        this.nextPicture(dx > 0 ? 1 : -1);
                    } else if (dy > 0) {
                        this.nextView();
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}