mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 776027 - Map and launch web activities as Android intents. r=wesj
This commit is contained in:
parent
391a2b9f03
commit
2c6dd358ce
@ -8,6 +8,7 @@ package org.mozilla.gecko;
|
||||
import org.mozilla.gecko.util.ActivityResultHandler;
|
||||
import org.mozilla.gecko.util.GeckoEventListener;
|
||||
import org.mozilla.gecko.util.JSONUtils;
|
||||
import org.mozilla.gecko.util.WebActivityMapper;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@ -26,7 +27,8 @@ public final class IntentHelper implements GeckoEventListener {
|
||||
private static final String[] EVENTS = {
|
||||
"Intent:GetHandlers",
|
||||
"Intent:Open",
|
||||
"Intent:OpenForResult"
|
||||
"Intent:OpenForResult",
|
||||
"WebActivity:Open"
|
||||
};
|
||||
private static IntentHelper instance;
|
||||
|
||||
@ -68,6 +70,8 @@ public final class IntentHelper implements GeckoEventListener {
|
||||
open(message);
|
||||
} else if (event.equals("Intent:OpenForResult")) {
|
||||
openForResult(message);
|
||||
} else if (event.equals("WebActivity:Open")) {
|
||||
openWebActivity(message);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
|
||||
@ -104,7 +108,11 @@ public final class IntentHelper implements GeckoEventListener {
|
||||
message.optString("title"));
|
||||
intent.setClassName(message.optString("packageName"), message.optString("className"));
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
ActivityHandlerHelper.startIntentForActivity(activity, intent, new ResultHandler(message));
|
||||
}
|
||||
|
||||
private void openWebActivity(JSONObject message) throws JSONException {
|
||||
final Intent intent = WebActivityMapper.getIntentForWebActivity(message.getJSONObject("activity"));
|
||||
ActivityHandlerHelper.startIntentForActivity(activity, intent, new ResultHandler(message));
|
||||
}
|
||||
|
||||
@ -116,13 +124,15 @@ public final class IntentHelper implements GeckoEventListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult (int resultCode, Intent data) {
|
||||
public void onActivityResult(int resultCode, Intent data) {
|
||||
JSONObject response = new JSONObject();
|
||||
|
||||
try {
|
||||
if (data != null) {
|
||||
response.put("extras", JSONUtils.bundleToJSON(data.getExtras()));
|
||||
response.put("uri", data.getData().toString());
|
||||
}
|
||||
|
||||
response.put("resultCode", resultCode);
|
||||
} catch (JSONException e) {
|
||||
Log.w(LOGTAG, "Error building JSON response.", e);
|
||||
|
@ -69,6 +69,7 @@ gujar.sources += [
|
||||
'util/StringUtils.java',
|
||||
'util/ThreadUtils.java',
|
||||
'util/UiAsyncTask.java',
|
||||
'util/WebActivityMapper.java',
|
||||
]
|
||||
gujar.extra_jars = [
|
||||
'gecko-mozglue.jar'
|
||||
|
@ -57,11 +57,11 @@ public final class JSONUtils {
|
||||
}
|
||||
|
||||
public static JSONObject bundleToJSON(Bundle bundle) {
|
||||
JSONObject json = new JSONObject();
|
||||
if (bundle == null) {
|
||||
return json;
|
||||
if (bundle == null || bundle.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
for (String key : bundle.keySet()) {
|
||||
try {
|
||||
json.put(key, bundle.get(key));
|
||||
|
151
mobile/android/base/util/WebActivityMapper.java
Normal file
151
mobile/android/base/util/WebActivityMapper.java
Normal file
@ -0,0 +1,151 @@
|
||||
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.util;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class WebActivityMapper {
|
||||
private static final Map<String, WebActivityMapping> activityMap = new HashMap<String, WebActivityMapping>();
|
||||
static {
|
||||
activityMap.put("dial", new DialMapping());
|
||||
activityMap.put("open", new OpenMapping());
|
||||
activityMap.put("pick", new PickMapping());
|
||||
activityMap.put("send", new SendMapping());
|
||||
activityMap.put("view", new ViewMapping());
|
||||
};
|
||||
|
||||
private static abstract class WebActivityMapping {
|
||||
// Cannot return null
|
||||
public abstract String getAction();
|
||||
|
||||
public String getMime(JSONObject data) throws JSONException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getUri(JSONObject data) throws JSONException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void putExtras(JSONObject data, Intent intent) throws JSONException {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides useful defaults for mime type and uri.
|
||||
*/
|
||||
private static abstract class BaseMapping extends WebActivityMapping {
|
||||
/**
|
||||
* If 'type' is present in data object, uses the value as the MIME type.
|
||||
*/
|
||||
public String getMime(JSONObject data) throws JSONException {
|
||||
return data.optString("type", null);
|
||||
}
|
||||
|
||||
/**
|
||||
* If 'uri' or 'url' is present in data object, uses the respecitve value as the Uri.
|
||||
*/
|
||||
public String getUri(JSONObject data) throws JSONException {
|
||||
// Will return uri or url if present.
|
||||
String uri = data.optString("uri", null);
|
||||
return uri != null ? uri : data.optString("url", null);
|
||||
}
|
||||
}
|
||||
|
||||
public static Intent getIntentForWebActivity(JSONObject message) throws JSONException {
|
||||
final String name = message.getString("name").toLowerCase();
|
||||
final JSONObject data = message.getJSONObject("data");
|
||||
|
||||
final WebActivityMapping mapping = activityMap.get(name);
|
||||
final Intent intent = new Intent(mapping.getAction());
|
||||
|
||||
final String mime = mapping.getMime(data);
|
||||
if (!TextUtils.isEmpty(mime)) {
|
||||
intent.setType(mime);
|
||||
}
|
||||
|
||||
final String uri = mapping.getUri(data);
|
||||
if (!TextUtils.isEmpty(uri)) {
|
||||
intent.setData(Uri.parse(uri));
|
||||
}
|
||||
|
||||
mapping.putExtras(data, intent);
|
||||
|
||||
return intent;
|
||||
}
|
||||
|
||||
private static class DialMapping extends WebActivityMapping {
|
||||
@Override
|
||||
public String getAction() {
|
||||
return Intent.ACTION_DIAL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUri(JSONObject data) throws JSONException {
|
||||
return "tel:" + data.getString("number");
|
||||
}
|
||||
}
|
||||
|
||||
private static class OpenMapping extends BaseMapping {
|
||||
@Override
|
||||
public String getAction() {
|
||||
return Intent.ACTION_VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
private static class PickMapping extends BaseMapping {
|
||||
@Override
|
||||
public String getAction() {
|
||||
return Intent.ACTION_GET_CONTENT;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SendMapping extends BaseMapping {
|
||||
@Override
|
||||
public String getAction() {
|
||||
return Intent.ACTION_SEND;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putExtras(JSONObject data, Intent intent) throws JSONException {
|
||||
optPutExtra("text", Intent.EXTRA_TEXT, data, intent);
|
||||
optPutExtra("html_text", Intent.EXTRA_HTML_TEXT, data, intent);
|
||||
optPutExtra("stream", Intent.EXTRA_STREAM, data, intent);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ViewMapping extends BaseMapping {
|
||||
@Override
|
||||
public String getAction() {
|
||||
return Intent.ACTION_VIEW;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMime(JSONObject data) {
|
||||
// MozActivity adds a type 'url' here, we don't want to set the MIME to 'url'.
|
||||
String type = data.optString("type", null);
|
||||
if ("url".equals(type) || "uri".equals(type)) {
|
||||
return null;
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void optPutExtra(String key, String extraName, JSONObject data, Intent intent) {
|
||||
final String extraValue = data.optString(key);
|
||||
if (!TextUtils.isEmpty(extraValue)) {
|
||||
intent.putExtra(extraName, extraValue);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user