summaryrefslogtreecommitdiff
path: root/plugins/hu.dpal.phonegap.plugins.PinDialog/src/android/PinDialog.java
blob: 6be1d6e3c4d9ca07d2ccbe6ea16c1ce283da3199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package hu.dpal.phonegap.plugins;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;


public class PinDialog extends CordovaPlugin {

    public ProgressDialog spinnerDialog = null;

    public PinDialog() {
    }

    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if (action.equals("prompt")) {
        	
        	final String message = args.getString(0);
        	final String title = args.getString(1);
        	final JSONArray buttonLabels = args.getJSONArray(2);
        	
        	final CordovaInterface cordova = this.cordova;
            final EditText promptInput =  new EditText(cordova.getActivity());
            promptInput.setInputType(InputType.TYPE_CLASS_NUMBER);
            promptInput.setTransformationMethod(PasswordTransformationMethod.getInstance());
           
            Runnable runnable = new Runnable() {
                public void run() {
                    AlertDialog.Builder dlg = new AlertDialog.Builder(cordova.getActivity());
                    dlg.setMessage(message);
                    dlg.setTitle(title);
                    dlg.setCancelable(true);
                    
                    dlg.setView(promptInput);
                    
                    final JSONObject result = new JSONObject();
                    
                    // First button
                    if (buttonLabels.length() > 0) {
                        try {
                            dlg.setNegativeButton(buttonLabels.getString(0),
                                new AlertDialog.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        try {
                                            result.put("buttonIndex",1);
                                            result.put("input1", promptInput.getText().toString().trim().length()==0 ? "" : promptInput.getText());                                                                                        
                                        } catch (JSONException e) { e.printStackTrace(); }
                                        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                                    }
                                });
                        } catch (JSONException e) { }
                    }

                    // Second button
                    if (buttonLabels.length() > 1) {
                        try {
                            dlg.setNeutralButton(buttonLabels.getString(1),
                                new AlertDialog.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        try {
                                            result.put("buttonIndex",2);
                                            result.put("input1", promptInput.getText().toString().trim().length()==0 ? "" : promptInput.getText());
                                        } catch (JSONException e) { e.printStackTrace(); }
                                        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                                    }
                                });
                        } catch (JSONException e) { }
                    }

                    // Third button
                    if (buttonLabels.length() > 2) {
                        try {
                            dlg.setPositiveButton(buttonLabels.getString(2),
                                new AlertDialog.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        try {
                                            result.put("buttonIndex",3);
                                            result.put("input1", promptInput.getText().toString().trim().length()==0 ? "" : promptInput.getText());
                                        } catch (JSONException e) { e.printStackTrace(); }
                                        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                                    }
                                });
                        } catch (JSONException e) { }
                    }
                    
                    dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
                        public void onCancel(DialogInterface dialog){
                            dialog.dismiss();
                            try {
                                result.put("buttonIndex",0);
                                result.put("input1", promptInput.getText().toString().trim().length()==0 ? "" : promptInput.getText());
                            } catch (JSONException e) { e.printStackTrace(); }
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                        }
                    });

                    dlg.create();
                    dlg.show();

                };
            };
            this.cordova.getActivity().runOnUiThread(runnable);
        	
        	
        }

        return true;
    }

}