michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.prompts; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.EventDispatcher; michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.util.GeckoEventListener; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: import android.content.Context; michael@0: import android.util.Log; michael@0: michael@0: public class PromptService implements GeckoEventListener { michael@0: private static final String LOGTAG = "GeckoPromptService"; michael@0: michael@0: private final Context mContext; michael@0: michael@0: public PromptService(Context context) { michael@0: GeckoAppShell.getEventDispatcher().registerEventListener("Prompt:Show", this); michael@0: GeckoAppShell.getEventDispatcher().registerEventListener("Prompt:ShowTop", this); michael@0: mContext = context; michael@0: } michael@0: michael@0: public void destroy() { michael@0: GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:Show", this); michael@0: GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:ShowTop", this); michael@0: } michael@0: michael@0: public void show(final String aTitle, final String aText, final PromptListItem[] aMenuList, michael@0: final int aChoiceMode, final Prompt.PromptCallback callback) { michael@0: // The dialog must be created on the UI thread. michael@0: ThreadUtils.postToUiThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: Prompt p; michael@0: p = new Prompt(mContext, callback); michael@0: p.show(aTitle, aText, aMenuList, aChoiceMode); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: // GeckoEventListener implementation michael@0: @Override michael@0: public void handleMessage(String event, final JSONObject message) { michael@0: // The dialog must be created on the UI thread. michael@0: ThreadUtils.postToUiThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: Prompt p; michael@0: p = new Prompt(mContext, new Prompt.PromptCallback() { michael@0: public void onPromptFinished(String jsonResult) { michael@0: try { michael@0: EventDispatcher.sendResponse(message, new JSONObject(jsonResult)); michael@0: } catch(JSONException ex) { michael@0: Log.i(LOGTAG, "Error building json response", ex); michael@0: } michael@0: } michael@0: }); michael@0: p.show(message); michael@0: } michael@0: }); michael@0: } michael@0: }