1 | /*
|
2 | Licensed to the Apache Software Foundation (ASF) under one
|
3 | or more contributor license agreements. See the NOTICE file
|
4 | distributed with this work for additional information
|
5 | regarding copyright ownership. The ASF licenses this file
|
6 | to you under the Apache License, Version 2.0 (the
|
7 | "License"); you may not use this file except in compliance
|
8 | with the License. You may obtain a copy of the License at
|
9 |
|
10 | http://www.apache.org/licenses/LICENSE-2.0
|
11 |
|
12 | Unless required by applicable law or agreed to in writing,
|
13 | software distributed under the License is distributed on an
|
14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15 | KIND, either express or implied. See the License for the
|
16 | specific language governing permissions and limitations
|
17 | under the License.
|
18 | */
|
19 | package org.apache.cordova.inappbrowser;
|
20 |
|
21 | import org.apache.cordova.CordovaWebView;
|
22 | import org.apache.cordova.LOG;
|
23 | import org.apache.cordova.PluginResult;
|
24 | import org.json.JSONArray;
|
25 | import org.json.JSONException;
|
26 |
|
27 | import com.amazon.android.webkit.AmazonWebChromeClient;
|
28 | import com.amazon.android.webkit.AmazonGeolocationPermissions.Callback;
|
29 | import com.amazon.android.webkit.AmazonJsPromptResult;
|
30 | import com.amazon.android.webkit.AmazonWebStorage;
|
31 | import com.amazon.android.webkit.AmazonWebView;
|
32 | import com.amazon.android.webkit.AmazonWebViewClient;
|
33 |
|
34 | public class InAppChromeClient extends AmazonWebChromeClient {
|
35 |
|
36 | private CordovaWebView webView;
|
37 | private String LOG_TAG = "InAppChromeClient";
|
38 | private long MAX_QUOTA = 100 * 1024 * 1024;
|
39 |
|
40 | public InAppChromeClient(CordovaWebView webView) {
|
41 | super();
|
42 | this.webView = webView;
|
43 | }
|
44 | /**
|
45 | * Handle database quota exceeded notification.
|
46 | *
|
47 | * @param url
|
48 | * @param databaseIdentifier
|
49 | * @param currentQuota
|
50 | * @param estimatedSize
|
51 | * @param totalUsedQuota
|
52 | * @param quotaUpdater
|
53 | */
|
54 |
|
55 | public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
|
56 | long totalUsedQuota, AmazonWebStorage.QuotaUpdater quotaUpdater)
|
57 | {
|
58 | LOG.d(LOG_TAG, "onExceededDatabaseQuota estimatedSize: %d currentQuota: %d totalUsedQuota: %d", estimatedSize, currentQuota, totalUsedQuota);
|
59 |
|
60 | if (estimatedSize < MAX_QUOTA)
|
61 | {
|
62 | //increase for 1Mb
|
63 | long newQuota = estimatedSize;
|
64 | LOG.d(LOG_TAG, "calling quotaUpdater.updateQuota newQuota: %d", newQuota);
|
65 | quotaUpdater.updateQuota(newQuota);
|
66 | }
|
67 | else
|
68 | {
|
69 | // Set the quota to whatever it is and force an error
|
70 | // TODO: get docs on how to handle this properly
|
71 | quotaUpdater.updateQuota(currentQuota);
|
72 | }
|
73 | }
|
74 |
|
75 | /**
|
76 | * Instructs the client to show a prompt to ask the user to set the Geolocation permission state for the specified origin.
|
77 | *
|
78 | * @param origin
|
79 | * @param callback
|
80 | */
|
81 |
|
82 | public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
|
83 | super.onGeolocationPermissionsShowPrompt(origin, callback);
|
84 | callback.invoke(origin, true, false);
|
85 | }
|
86 |
|
87 | /**
|
88 | * Tell the client to display a prompt dialog to the user.
|
89 | * If the client returns true, WebView will assume that the client will
|
90 | * handle the prompt dialog and call the appropriate JsPromptResult method.
|
91 | *
|
92 | * The prompt bridge provided for the InAppBrowser is capable of executing any
|
93 | * oustanding callback belonging to the InAppBrowser plugin. Care has been
|
94 | * taken that other callbacks cannot be triggered, and that no other code
|
95 | * execution is possible.
|
96 | *
|
97 | * To trigger the bridge, the prompt default value should be of the form:
|
98 | *
|
99 | * gap-iab://<callbackId>
|
100 | *
|
101 | * where <callbackId> is the string id of the callback to trigger (something
|
102 | * like "InAppBrowser0123456789")
|
103 | *
|
104 | * If present, the prompt message is expected to be a JSON-encoded value to
|
105 | * pass to the callback. A JSON_EXCEPTION is returned if the JSON is invalid.
|
106 | *
|
107 | * @param view
|
108 | * @param url
|
109 | * @param message
|
110 | * @param defaultValue
|
111 | * @param result
|
112 | */
|
113 |
|
114 | public boolean onJsPrompt(AmazonWebView view, String url, String message, String defaultValue, AmazonJsPromptResult result) {
|
115 | // See if the prompt string uses the 'gap-iab' protocol. If so, the remainder should be the id of a callback to execute.
|
116 | if (defaultValue != null && defaultValue.startsWith("gap")) {
|
117 | if(defaultValue.startsWith("gap-iab://")) {
|
118 | PluginResult scriptResult;
|
119 | String scriptCallbackId = defaultValue.substring(10);
|
120 | if (scriptCallbackId.startsWith("InAppBrowser")) {
|
121 | if(message == null || message.length() == 0) {
|
122 | scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray());
|
123 | } else {
|
124 | try {
|
125 | scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray(message));
|
126 | } catch(JSONException e) {
|
127 | scriptResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
|
128 | }
|
129 | }
|
130 | this.webView.sendPluginResult(scriptResult, scriptCallbackId);
|
131 | result.confirm("");
|
132 | return true;
|
133 | }
|
134 | }
|
135 | else
|
136 | {
|
137 | // Anything else with a gap: prefix should get this message
|
138 | LOG.w(LOG_TAG, "InAppBrowser does not support Cordova API calls: " + url + " " + defaultValue);
|
139 | result.cancel();
|
140 | return true;
|
141 | }
|
142 | }
|
143 | return false;
|
144 | }
|
145 |
|
146 | }
|