1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/prompts/PromptService.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.prompts; 1.10 + 1.11 +import org.json.JSONException; 1.12 +import org.json.JSONObject; 1.13 +import org.mozilla.gecko.EventDispatcher; 1.14 +import org.mozilla.gecko.GeckoAppShell; 1.15 +import org.mozilla.gecko.util.GeckoEventListener; 1.16 +import org.mozilla.gecko.util.ThreadUtils; 1.17 + 1.18 +import android.content.Context; 1.19 +import android.util.Log; 1.20 + 1.21 +public class PromptService implements GeckoEventListener { 1.22 + private static final String LOGTAG = "GeckoPromptService"; 1.23 + 1.24 + private final Context mContext; 1.25 + 1.26 + public PromptService(Context context) { 1.27 + GeckoAppShell.getEventDispatcher().registerEventListener("Prompt:Show", this); 1.28 + GeckoAppShell.getEventDispatcher().registerEventListener("Prompt:ShowTop", this); 1.29 + mContext = context; 1.30 + } 1.31 + 1.32 + public void destroy() { 1.33 + GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:Show", this); 1.34 + GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:ShowTop", this); 1.35 + } 1.36 + 1.37 + public void show(final String aTitle, final String aText, final PromptListItem[] aMenuList, 1.38 + final int aChoiceMode, final Prompt.PromptCallback callback) { 1.39 + // The dialog must be created on the UI thread. 1.40 + ThreadUtils.postToUiThread(new Runnable() { 1.41 + @Override 1.42 + public void run() { 1.43 + Prompt p; 1.44 + p = new Prompt(mContext, callback); 1.45 + p.show(aTitle, aText, aMenuList, aChoiceMode); 1.46 + } 1.47 + }); 1.48 + } 1.49 + 1.50 + // GeckoEventListener implementation 1.51 + @Override 1.52 + public void handleMessage(String event, final JSONObject message) { 1.53 + // The dialog must be created on the UI thread. 1.54 + ThreadUtils.postToUiThread(new Runnable() { 1.55 + @Override 1.56 + public void run() { 1.57 + Prompt p; 1.58 + p = new Prompt(mContext, new Prompt.PromptCallback() { 1.59 + public void onPromptFinished(String jsonResult) { 1.60 + try { 1.61 + EventDispatcher.sendResponse(message, new JSONObject(jsonResult)); 1.62 + } catch(JSONException ex) { 1.63 + Log.i(LOGTAG, "Error building json response", ex); 1.64 + } 1.65 + } 1.66 + }); 1.67 + p.show(message); 1.68 + } 1.69 + }); 1.70 + } 1.71 +}