mobile/android/base/prompts/PromptService.java

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

mercurial