Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.prompts; |
michael@0 | 7 | |
michael@0 | 8 | import org.json.JSONException; |
michael@0 | 9 | import org.json.JSONObject; |
michael@0 | 10 | import org.mozilla.gecko.EventDispatcher; |
michael@0 | 11 | import org.mozilla.gecko.GeckoAppShell; |
michael@0 | 12 | import org.mozilla.gecko.util.GeckoEventListener; |
michael@0 | 13 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 14 | |
michael@0 | 15 | import android.content.Context; |
michael@0 | 16 | import android.util.Log; |
michael@0 | 17 | |
michael@0 | 18 | public class PromptService implements GeckoEventListener { |
michael@0 | 19 | private static final String LOGTAG = "GeckoPromptService"; |
michael@0 | 20 | |
michael@0 | 21 | private final Context mContext; |
michael@0 | 22 | |
michael@0 | 23 | public PromptService(Context context) { |
michael@0 | 24 | GeckoAppShell.getEventDispatcher().registerEventListener("Prompt:Show", this); |
michael@0 | 25 | GeckoAppShell.getEventDispatcher().registerEventListener("Prompt:ShowTop", this); |
michael@0 | 26 | mContext = context; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | public void destroy() { |
michael@0 | 30 | GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:Show", this); |
michael@0 | 31 | GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:ShowTop", this); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | public void show(final String aTitle, final String aText, final PromptListItem[] aMenuList, |
michael@0 | 35 | final int aChoiceMode, final Prompt.PromptCallback callback) { |
michael@0 | 36 | // The dialog must be created on the UI thread. |
michael@0 | 37 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 38 | @Override |
michael@0 | 39 | public void run() { |
michael@0 | 40 | Prompt p; |
michael@0 | 41 | p = new Prompt(mContext, callback); |
michael@0 | 42 | p.show(aTitle, aText, aMenuList, aChoiceMode); |
michael@0 | 43 | } |
michael@0 | 44 | }); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | // GeckoEventListener implementation |
michael@0 | 48 | @Override |
michael@0 | 49 | public void handleMessage(String event, final JSONObject message) { |
michael@0 | 50 | // The dialog must be created on the UI thread. |
michael@0 | 51 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 52 | @Override |
michael@0 | 53 | public void run() { |
michael@0 | 54 | Prompt p; |
michael@0 | 55 | p = new Prompt(mContext, new Prompt.PromptCallback() { |
michael@0 | 56 | public void onPromptFinished(String jsonResult) { |
michael@0 | 57 | try { |
michael@0 | 58 | EventDispatcher.sendResponse(message, new JSONObject(jsonResult)); |
michael@0 | 59 | } catch(JSONException ex) { |
michael@0 | 60 | Log.i(LOGTAG, "Error building json response", ex); |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | }); |
michael@0 | 64 | p.show(message); |
michael@0 | 65 | } |
michael@0 | 66 | }); |
michael@0 | 67 | } |
michael@0 | 68 | } |