CordovaDialogsHelper.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova;
  18. import android.app.AlertDialog;
  19. import android.content.Context;
  20. import android.content.DialogInterface;
  21. import android.view.KeyEvent;
  22. import android.widget.EditText;
  23. /**
  24. * Helper class for WebViews to implement prompt(), alert(), confirm() dialogs.
  25. */
  26. public class CordovaDialogsHelper {
  27. private final Context context;
  28. private AlertDialog lastHandledDialog;
  29. public CordovaDialogsHelper(Context context) {
  30. this.context = context;
  31. }
  32. public void showAlert(String message, final Result result) {
  33. AlertDialog.Builder dlg = new AlertDialog.Builder(context);
  34. dlg.setMessage(message);
  35. dlg.setTitle("Alert");
  36. //Don't let alerts break the back button
  37. dlg.setCancelable(true);
  38. dlg.setPositiveButton(android.R.string.ok,
  39. new AlertDialog.OnClickListener() {
  40. public void onClick(DialogInterface dialog, int which) {
  41. result.gotResult(true, null);
  42. }
  43. });
  44. dlg.setOnCancelListener(
  45. new DialogInterface.OnCancelListener() {
  46. public void onCancel(DialogInterface dialog) {
  47. result.gotResult(false, null);
  48. }
  49. });
  50. dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
  51. //DO NOTHING
  52. public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
  53. if (keyCode == KeyEvent.KEYCODE_BACK)
  54. {
  55. result.gotResult(true, null);
  56. return false;
  57. }
  58. else
  59. return true;
  60. }
  61. });
  62. lastHandledDialog = dlg.show();
  63. }
  64. public void showConfirm(String message, final Result result) {
  65. AlertDialog.Builder dlg = new AlertDialog.Builder(context);
  66. dlg.setMessage(message);
  67. dlg.setTitle("Confirm");
  68. dlg.setCancelable(true);
  69. dlg.setPositiveButton(android.R.string.ok,
  70. new DialogInterface.OnClickListener() {
  71. public void onClick(DialogInterface dialog, int which) {
  72. result.gotResult(true, null);
  73. }
  74. });
  75. dlg.setNegativeButton(android.R.string.cancel,
  76. new DialogInterface.OnClickListener() {
  77. public void onClick(DialogInterface dialog, int which) {
  78. result.gotResult(false, null);
  79. }
  80. });
  81. dlg.setOnCancelListener(
  82. new DialogInterface.OnCancelListener() {
  83. public void onCancel(DialogInterface dialog) {
  84. result.gotResult(false, null);
  85. }
  86. });
  87. dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
  88. //DO NOTHING
  89. public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
  90. if (keyCode == KeyEvent.KEYCODE_BACK)
  91. {
  92. result.gotResult(false, null);
  93. return false;
  94. }
  95. else
  96. return true;
  97. }
  98. });
  99. lastHandledDialog = dlg.show();
  100. }
  101. /**
  102. * Tell the client to display a prompt dialog to the user.
  103. * If the client returns true, WebView will assume that the client will
  104. * handle the prompt dialog and call the appropriate JsPromptResult method.
  105. *
  106. * Since we are hacking prompts for our own purposes, we should not be using them for
  107. * this purpose, perhaps we should hack console.log to do this instead!
  108. */
  109. public void showPrompt(String message, String defaultValue, final Result result) {
  110. // Returning false would also show a dialog, but the default one shows the origin (ugly).
  111. AlertDialog.Builder dlg = new AlertDialog.Builder(context);
  112. dlg.setMessage(message);
  113. final EditText input = new EditText(context);
  114. if (defaultValue != null) {
  115. input.setText(defaultValue);
  116. }
  117. dlg.setView(input);
  118. dlg.setCancelable(false);
  119. dlg.setPositiveButton(android.R.string.ok,
  120. new DialogInterface.OnClickListener() {
  121. public void onClick(DialogInterface dialog, int which) {
  122. String userText = input.getText().toString();
  123. result.gotResult(true, userText);
  124. }
  125. });
  126. dlg.setNegativeButton(android.R.string.cancel,
  127. new DialogInterface.OnClickListener() {
  128. public void onClick(DialogInterface dialog, int which) {
  129. result.gotResult(false, null);
  130. }
  131. });
  132. lastHandledDialog = dlg.show();
  133. }
  134. public void destroyLastDialog(){
  135. if (lastHandledDialog != null){
  136. lastHandledDialog.cancel();
  137. }
  138. }
  139. public interface Result {
  140. public void gotResult(boolean success, String value);
  141. }
  142. }