16
16
import android .webkit .WebView ;
17
17
import android .webkit .WebViewClient ;
18
18
import android .widget .PopupWindow ;
19
+ import android .widget .Toast ;
20
+
21
+ import org .json .JSONArray ;
22
+ import org .json .JSONException ;
23
+ import org .json .JSONObject ;
24
+
25
+ import java .util .Iterator ;
26
+ import java .util .LinkedList ;
27
+ import java .util .List ;
19
28
20
29
public class AceEditor extends WebView
21
30
{
22
31
Context context ;
23
32
private PopupWindow pw ;
24
33
private View popupView ;
34
+ private LayoutInflater inflater ;
25
35
26
36
private ResultReceivedListener received ;
27
37
private OnLoadedEditorListener onLoadedEditorListener ;
38
+ private OnSelectionActionPerformedListener onSelectionActionPerformedListener ;
28
39
29
- private LayoutInflater inflater ;
30
40
private float x ;
31
41
private float y ;
32
42
private boolean actAfterSelect ;
33
43
private int requestedValue ;
44
+ private String findString ;
34
45
35
46
private boolean loadedUI ;
36
47
@@ -62,7 +73,7 @@ private void initialize()
62
73
63
74
setResultReceivedListener (new ResultReceivedListener () {
64
75
@ Override
65
- public void onReceived (String text , int FLAG_VALUE ) {
76
+ public void onReceived (int FLAG_VALUE , String ... results ) {
66
77
67
78
}
68
79
});
@@ -74,11 +85,66 @@ public void onCreate() {
74
85
}
75
86
});
76
87
88
+ setOnSelectionActionPerformedListener (new OnSelectionActionPerformedListener () {
89
+ @ Override
90
+ public void onSelectionFinished (boolean usingSelectAllOption ) {
91
+
92
+ }
93
+
94
+ @ Override
95
+ public void onCut () {
96
+
97
+ }
98
+
99
+ @ Override
100
+ public void onCopy () {
101
+
102
+ }
103
+
104
+ @ Override
105
+ public void onPaste () {
106
+
107
+ }
108
+
109
+ @ Override
110
+ public void onUndo () {
111
+
112
+ }
113
+
114
+ @ Override
115
+ public void onRedo () {
116
+
117
+ }
118
+ });
119
+
77
120
setWebChromeClient (new WebChromeClient () {
78
121
@ Override
79
122
public boolean onJsAlert (WebView view , String url , String message , JsResult result ) {
80
123
result .confirm ();
81
- received .onReceived (message , requestedValue );
124
+ List <String > results = new LinkedList <>();
125
+ try {
126
+ JSONArray objArr = new JSONArray (message );
127
+ for (int i = 0 ; i <objArr .length (); i ++) {
128
+ results .add (String .valueOf (objArr .get (i )));
129
+ }
130
+ }
131
+ catch (JSONException e )
132
+ {
133
+ try {
134
+ JSONObject obj = new JSONObject (message );
135
+ Iterator <String > keyInerator = obj .keys ();
136
+ while (keyInerator .hasNext ()) {
137
+ String key = keyInerator .next ();
138
+ results .add (String .valueOf (obj .get (key )));
139
+ }
140
+ }
141
+ catch (JSONException e1 ) {
142
+ results .add (message );
143
+ }
144
+ }
145
+ String []res = new String [results .size ()];
146
+ res = results .toArray (res );
147
+ received .onReceived (requestedValue , res );
82
148
return true ;
83
149
}
84
150
});
@@ -98,6 +164,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
98
164
return false ;
99
165
}
100
166
});
167
+
101
168
setOnTouchListener (new View .OnTouchListener ()
102
169
{
103
170
float downTime ;
@@ -119,9 +186,11 @@ public boolean onTouch(View v, MotionEvent event) {
119
186
y = event .getY ();
120
187
if (tot <= 500 )
121
188
v .performClick ();
122
- else
123
- if (actAfterSelect )
124
- pw .showAtLocation (v , Gravity .NO_GRAVITY ,(int )x - getResources ().getDisplayMetrics ().widthPixels /3 ,getResources ().getDisplayMetrics ().heightPixels /12 + (int )y );
189
+ else {
190
+ if (actAfterSelect )
191
+ pw .showAtLocation (v , Gravity .NO_GRAVITY , (int ) x - getResources ().getDisplayMetrics ().widthPixels / 3 , getResources ().getDisplayMetrics ().heightPixels / 12 + (int ) y );
192
+ onSelectionActionPerformedListener .onSelectionFinished (false );
193
+ }
125
194
break ;
126
195
case MotionEvent .ACTION_MOVE :
127
196
xtimes = (int ) (x - event .getX ()) / 25 ;
@@ -197,6 +266,7 @@ public void onClick(View v) {
197
266
AceEditor .this .dispatchKeyEvent (new KeyEvent (0 , 0 , KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_X , 0 , KeyEvent .META_CTRL_ON ));
198
267
AceEditor .this .requestFocus ();
199
268
pw .dismiss ();
269
+ onSelectionActionPerformedListener .onCut ();
200
270
}
201
271
});
202
272
popupView .findViewById (R .id .copy ).setOnClickListener (new OnClickListener () {
@@ -205,6 +275,7 @@ public void onClick(View v) {
205
275
AceEditor .this .dispatchKeyEvent (new KeyEvent (0 , 0 , KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_C , 0 , KeyEvent .META_CTRL_ON ));
206
276
AceEditor .this .requestFocus ();
207
277
pw .dismiss ();
278
+ onSelectionActionPerformedListener .onCopy ();
208
279
}
209
280
});
210
281
popupView .findViewById (R .id .paste ).setOnClickListener (new OnClickListener () {
@@ -213,25 +284,29 @@ public void onClick(View v) {
213
284
AceEditor .this .dispatchKeyEvent (new KeyEvent (0 , 0 , KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_V , 0 , KeyEvent .META_CTRL_ON ));
214
285
AceEditor .this .requestFocus ();
215
286
pw .dismiss ();
287
+ onSelectionActionPerformedListener .onPaste ();
216
288
}
217
289
});
218
290
popupView .findViewById (R .id .selectall ).setOnClickListener (new OnClickListener () {
219
291
@ Override
220
292
public void onClick (View v ) {
221
293
AceEditor .this .dispatchKeyEvent (new KeyEvent (0 , 0 , KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_A , 0 , KeyEvent .META_CTRL_ON ));
222
294
popupView .findViewById (R .id .prevOptSet ).performClick ();
295
+ onSelectionActionPerformedListener .onSelectionFinished (true );
223
296
}
224
297
});
225
298
popupView .findViewById (R .id .undo ).setOnClickListener (new OnClickListener () {
226
299
@ Override
227
300
public void onClick (View v ) {
228
301
AceEditor .this .dispatchKeyEvent (new KeyEvent (0 , 0 , KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_Z , 0 , KeyEvent .META_CTRL_ON ));
302
+ onSelectionActionPerformedListener .onUndo ();
229
303
}
230
304
});
231
305
popupView .findViewById (R .id .redo ).setOnClickListener (new OnClickListener () {
232
306
@ Override
233
307
public void onClick (View v ) {
234
308
AceEditor .this .dispatchKeyEvent (new KeyEvent (0 , 0 , KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_Z , 0 , KeyEvent .META_CTRL_ON |KeyEvent .META_SHIFT_ON ));
309
+ onSelectionActionPerformedListener .onRedo ();
235
310
}
236
311
});
237
312
pw .setOnDismissListener (new PopupWindow .OnDismissListener () {
@@ -254,10 +329,9 @@ public void setOnLoadedEditorListener(OnLoadedEditorListener listener)
254
329
this .onLoadedEditorListener = listener ;
255
330
}
256
331
257
- public void requestText ( )
332
+ public void setOnSelectionActionPerformedListener ( OnSelectionActionPerformedListener listener )
258
333
{
259
- requestedValue = Request .VALUE_TEXT ;
260
- loadUrl ("javascript:alert(editor.getValue());" );
334
+ this .onSelectionActionPerformedListener = listener ;
261
335
}
262
336
263
337
public void showOptionsAfterSelection (boolean show )
@@ -280,16 +354,97 @@ public void insertTextAtCursor(String text)
280
354
loadUrl ("javascript:editor.insert(\" " + text +"\" );" );
281
355
}
282
356
283
- public void requestLines ()
357
+
358
+ public void requestText ()
284
359
{
285
- requestedValue = Request .VALUE_LINES ;
360
+ requestedValue = Request .TEXT_REQUEST ;
361
+ loadUrl ("javascript:alert(editor.getValue());" );
362
+ }
363
+
364
+ public void requestRowCount ()
365
+ {
366
+ requestedValue = Request .ROW_COUNT_REQUEST ;
286
367
loadUrl ("javascript:alert(editor.session.getLength());" );
287
368
}
288
369
289
370
public void requsetSelectedText ()
290
371
{
291
- requestedValue = Request .VALUE_SELECTED_TEXT ;
292
- loadUrl ("javascript:alert(editor.getCopyText());" );
372
+ requestedValue = Request .TEXT_REQUEST ;
373
+ loadUrl ("javascript:alert(editor.getSelectedText());" );
374
+ }
375
+
376
+ public void requestCursorCoords ()
377
+ {
378
+ requestedValue = Request .CURSOR_COORDS_REQUEST ;
379
+ loadUrl ("javascript:alert(JSON.stringify(editor.getCursorPosition()))" );
380
+ }
381
+
382
+ public void requestLine (int lineNumber )
383
+ {
384
+ requestedValue = Request .TEXT_REQUEST ;
385
+ loadUrl ("javascript:alert(editor.session.getLine(" + String .valueOf (lineNumber ) + "));" );
386
+ }
387
+
388
+ public void requestLinesBetween (int startLine , int endLine )
389
+ {
390
+ requestedValue = Request .MULTIPLE_LINES_REQUEST ;
391
+ loadUrl ("javascript:alert(JSON.stringify(editor.session.getLines(" + String .valueOf (startLine ) + ", " + String .valueOf (endLine ) + ")));" );
392
+ }
393
+
394
+ public void startFind (String toFind , boolean backwards , boolean wrap , boolean caseSensitive , boolean wholeWord )
395
+ {
396
+ findString = toFind ;
397
+ loadUrl ("javascript:editor.find('" + toFind + "', backwards: " + String .valueOf (backwards ) +
398
+ ", wrap: " + String .valueOf (wrap ) +
399
+ ",caseSensitive: " + String .valueOf (caseSensitive ) +
400
+ ",wholeWord: " + String .valueOf (wholeWord ) +",regExp: false});" );
401
+ }
402
+
403
+ public void findNext ()
404
+ {
405
+ if (findString == null ) {
406
+ return ;
407
+ }
408
+ loadUrl ("javascript:editor.findNext();" );
409
+ }
410
+
411
+ public void findNext (String errorToastMessage , int showFor )
412
+ {
413
+ if (findString == null ) {
414
+ Toast .makeText (context ,errorToastMessage ,showFor ).show ();
415
+ return ;
416
+ }
417
+ loadUrl ("javascript:editor.findNext();" );
418
+ }
419
+
420
+ public void findPrevious ()
421
+ {
422
+ if (findString == null ) {
423
+ return ;
424
+ }
425
+ loadUrl ("javascript:editor.findPrevious();" );
426
+ }
427
+
428
+ public void findPrevious (String toastMessage , int showFor )
429
+ {
430
+ if (findString == null ) {
431
+ Toast .makeText (context ,toastMessage ,showFor ).show ();
432
+ return ;
433
+ }
434
+ loadUrl ("javascript:editor.findPrevious();" );
435
+ }
436
+
437
+ public void replace (String replaceText , boolean replaceAll )
438
+ {
439
+ if (replaceAll )
440
+ loadUrl ("javascript:editor.replaceAll('" + replaceText + "');" );
441
+ else
442
+ loadUrl ("javascript:editor.replace('" + replaceText + "');" );
443
+ }
444
+
445
+ public void endFind ()
446
+ {
447
+ findString = null ;
293
448
}
294
449
295
450
public void setTheme (Theme theme )
@@ -303,9 +458,11 @@ public void setMode(Mode mode)
303
458
}
304
459
305
460
public static class Request {
306
- public static int VALUE_TEXT = 0 ;
307
- public static int VALUE_LINES = 1 ;
308
- public static int VALUE_SELECTED_TEXT = 2 ;
461
+ public static int GENERIC_REQUEST = 0 ;
462
+ public static int TEXT_REQUEST = 1 ;
463
+ public static int ROW_COUNT_REQUEST = 2 ;
464
+ public static int CURSOR_COORDS_REQUEST = 3 ;
465
+ public static int MULTIPLE_LINES_REQUEST = 4 ;
309
466
}
310
467
311
468
public static enum Theme
0 commit comments