FileDocCategorySizeDatePackage
JsDialogHelper.javaAPI DocAndroid 5.1 API6915Thu Mar 12 22:22:10 GMT 2015android.webkit

JsDialogHelper

public class JsDialogHelper extends Object
Helper class to create JavaScript dialogs. It is used by different WebView implementations.
hide

Fields Summary
private static final String
TAG
public static final int
ALERT
public static final int
CONFIRM
public static final int
PROMPT
public static final int
UNLOAD
private final String
mDefaultValue
private final JsPromptResult
mResult
private final String
mMessage
private final int
mType
private final String
mUrl
Constructors Summary
public JsDialogHelper(JsPromptResult result, int type, String defaultValue, String message, String url)


            
              
        mResult = result;
        mDefaultValue = defaultValue;
        mMessage = message;
        mType = type;
        mUrl = url;
    
public JsDialogHelper(JsPromptResult result, android.os.Message msg)

        mResult = result;
        mDefaultValue = msg.getData().getString("default");
        mMessage = msg.getData().getString("message");
        mType = msg.getData().getInt("type");
        mUrl = msg.getData().getString("url");
    
Methods Summary
private static booleancanShowAlertDialog(android.content.Context context)

        // We can only display the alert dialog if mContext is
        // an Activity context.
        // FIXME: Should we display dialogs if mContext does
        // not have the window focus (e.g. if the user is viewing
        // another Activity when the alert should be displayed) ?
        // See bug 3166409
        return context instanceof Activity;
    
private java.lang.StringgetJsDialogTitle(android.content.Context context)

        String title = mUrl;
        if (URLUtil.isDataUrl(mUrl)) {
            // For data: urls, we just display 'JavaScript' similar to Chrome.
            title = context.getString(com.android.internal.R.string.js_dialog_title_default);
        } else {
            try {
                URL alertUrl = new URL(mUrl);
                // For example: "The page at 'http://www.mit.edu' says:"
                title = context.getString(com.android.internal.R.string.js_dialog_title,
                        alertUrl.getProtocol() + "://" + alertUrl.getHost());
            } catch (MalformedURLException ex) {
                // do nothing. just use the url as the title
            }
        }
        return title;
    
public booleaninvokeCallback(WebChromeClient client, WebView webView)

        switch (mType) {
            case ALERT:
                return client.onJsAlert(webView, mUrl, mMessage, mResult);
            case CONFIRM:
                return client.onJsConfirm(webView, mUrl, mMessage, mResult);
            case UNLOAD:
                return client.onJsBeforeUnload(webView, mUrl, mMessage, mResult);
            case PROMPT:
                return client.onJsPrompt(webView, mUrl, mMessage, mDefaultValue, mResult);
            default:
                throw new IllegalArgumentException("Unexpected type: " + mType);
        }
    
public voidshowDialog(android.content.Context context)

        if (!canShowAlertDialog(context)) {
            Log.w(TAG, "Cannot create a dialog, the WebView context is not an Activity");
            mResult.cancel();
            return;
        }

        String title, displayMessage;
        int positiveTextId, negativeTextId;
        if (mType == UNLOAD) {
            title = context.getString(com.android.internal.R.string.js_dialog_before_unload_title);
            displayMessage = context.getString(
                    com.android.internal.R.string.js_dialog_before_unload, mMessage);
            positiveTextId = com.android.internal.R.string.js_dialog_before_unload_positive_button;
            negativeTextId = com.android.internal.R.string.js_dialog_before_unload_negative_button;
        } else {
            title = getJsDialogTitle(context);
            displayMessage = mMessage;
            positiveTextId = com.android.internal.R.string.ok;
            negativeTextId = com.android.internal.R.string.cancel;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setOnCancelListener(new CancelListener());
        if (mType != PROMPT) {
            builder.setMessage(displayMessage);
            builder.setPositiveButton(positiveTextId, new PositiveListener(null));
        } else {
            final View view = LayoutInflater.from(context).inflate(
                    com.android.internal.R.layout.js_prompt, null);
            EditText edit = ((EditText) view.findViewById(com.android.internal.R.id.value));
            edit.setText(mDefaultValue);
            builder.setPositiveButton(positiveTextId, new PositiveListener(edit));
            ((TextView) view.findViewById(com.android.internal.R.id.message)).setText(mMessage);
            builder.setView(view);
        }
        if (mType != ALERT) {
            builder.setNegativeButton(negativeTextId, new CancelListener());
        }
        builder.show();