summaryrefslogtreecommitdiff
path: root/plugins/cordova-plugin-crosswalk-webview/src/android/XWalkCordovaView.java
blob: 0be2e998fb5aa933a70e7fb15502fd90b5ca1052 (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
package org.crosswalk.engine;

import org.apache.cordova.CordovaPreferences;
import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkUIClient;
import org.xwalk.core.XWalkView;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.AttributeSet;
import android.view.KeyEvent;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewEngine;

public class XWalkCordovaView extends XWalkView implements CordovaWebViewEngine.EngineView {
    protected XWalkCordovaResourceClient resourceClient;
    protected XWalkCordovaUiClient uiClient;
    protected XWalkWebViewEngine parentEngine;

    private static boolean hasSetStaticPref;
    // This needs to run before the super's constructor.
    private static Context setGlobalPrefs(Context context, CordovaPreferences preferences) {
        if (!hasSetStaticPref) {
            hasSetStaticPref = true;
            ApplicationInfo ai = null;
            try {
                ai = context.getPackageManager().getApplicationInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
            boolean prefAnimatable = preferences == null ? false : preferences.getBoolean("CrosswalkAnimatable", false);
            boolean manifestAnimatable = ai.metaData == null ? false : ai.metaData.getBoolean("CrosswalkAnimatable");
            if (prefAnimatable || manifestAnimatable) {
                // Slows it down a bit, but allows for it to be animated by Android View properties.
                XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
            }
            if ((ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
                XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
            }
            XWalkPreferences.setValue(XWalkPreferences.JAVASCRIPT_CAN_OPEN_WINDOW, true);
            XWalkPreferences.setValue(XWalkPreferences.ALLOW_UNIVERSAL_ACCESS_FROM_FILE, true);
        }
        return context;
    }

    public XWalkCordovaView(Context context, CordovaPreferences preferences) {
        super(setGlobalPrefs(context, preferences), (AttributeSet)null);
    }

    public XWalkCordovaView(Context context, AttributeSet attrs) {
        super(setGlobalPrefs(context, null), attrs);
    }

    void init(XWalkWebViewEngine parentEngine) {
        this.parentEngine = parentEngine;
        if (resourceClient == null) {
            setResourceClient(new XWalkCordovaResourceClient(parentEngine));
        }
        if (uiClient == null) {
            setUIClient(new XWalkCordovaUiClient(parentEngine));
        }
    }

    @Override
    public void setResourceClient(XWalkResourceClient client) {
        // XWalk calls this method from its constructor.
        if (client instanceof XWalkCordovaResourceClient) {
            this.resourceClient = (XWalkCordovaResourceClient)client;
        }
        super.setResourceClient(client);
    }

    @Override
    public void setUIClient(XWalkUIClient client) {
        // XWalk calls this method from its constructor.
        if (client instanceof XWalkCordovaUiClient) {
            this.uiClient = (XWalkCordovaUiClient)client;
        }
        super.setUIClient(client);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Boolean ret = parentEngine.client.onDispatchKeyEvent(event);
        if (ret != null) {
            return ret.booleanValue();
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void pauseTimers() {
        // This is called by XWalkViewInternal.onActivityStateChange().
        // We don't want them paused by default though.
    }

    public void pauseTimersForReal() {
        super.pauseTimers();
    }

    @Override
    public CordovaWebView getCordovaWebView() {
        return parentEngine == null ? null : parentEngine.getCordovaWebView();
    }
}