aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/viewer/src/main/java/org/skia/viewer/ViewerApplication.java
blob: 7253eb75034d6bca6a1f0be08b384f08b795493f (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
/*
 * 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.viewer;

import android.app.Application;
import android.content.res.AssetManager;

public class ViewerApplication extends Application {
    private long mNativeHandle = 0;
    private ViewerActivity mViewerActivity;
    private String mStateJsonStr, mTitle;

    static {
        System.loadLibrary("viewer");
    }

    private native long createNativeApp(AssetManager assetManager);
    private native void destroyNativeApp(long handle);

    @Override
    public void onCreate() {
        super.onCreate();
        mNativeHandle = createNativeApp(this.getResources().getAssets());
    }

    @Override
    public void onTerminate() {
        if (mNativeHandle != 0) {
            destroyNativeApp(mNativeHandle);
            mNativeHandle = 0;
        }
        super.onTerminate();
    }

    public long getNativeHandle() {
        return mNativeHandle;
    }

    public void setViewerActivity(ViewerActivity viewerActivity) {
        mViewerActivity = viewerActivity;
        // Note that viewerActivity might be null (called by onDestroy)
        if (mViewerActivity != null) {
            // A new ViewerActivity is created; initialize its state and title
            if (mStateJsonStr != null) {
                mViewerActivity.setState(mStateJsonStr);
            }
            if (mTitle != null) {
                mViewerActivity.setTitle(mTitle);
            }
        }
    }

    public void setTitle(String title) {
        mTitle = title; // Similar to mStateJsonStr, we have to store this.
        if (mTitle.startsWith("Viewer: ")) { // Quick hack to shorten the title
            mTitle = mTitle.replaceFirst("Viewer: ", "");
        }
        if (mViewerActivity != null) {
            mViewerActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mViewerActivity.setTitle(mTitle);
                }
            });
        }
    }

    public void setState(String stateJsonStr) {
        // We have to store this state because ViewerActivity may be destroyed while the native app
        // is still running. When a new ViewerActivity is created, we'll pass the state to it.
        mStateJsonStr = stateJsonStr;
        if (mViewerActivity != null) {
            mViewerActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mViewerActivity.setState(mStateJsonStr);
                }
            });
        }
    }
}