This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathEngine.java
More file actions
4190 lines (3492 loc) · 125 KB
/
Engine.java
File metadata and controls
4190 lines (3492 loc) · 125 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
package com.runrev.android;
import com.runrev.android.billing.*;
/*
import com.runrev.android.billing.C.ResponseCode;
import com.runrev.android.billing.PurchaseUpdate.Purchase;
import com.runrev.android.billing.BillingService.RestoreTransactions;
import com.runrev.android.billing.BillingService.GetPurchaseInformation;
import com.runrev.android.billing.BillingService.ConfirmNotification;
import com.runrev.android.billing.BillingService.RequestPurchase;
*/
import com.runrev.android.nativecontrol.NativeControlModule;
import com.runrev.android.nativecontrol.VideoControl;
import android.content.*;
import android.content.res.*;
import android.content.pm.*;
import android.database.*;
import android.util.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.view.*;
import android.view.inputmethod.*;
import android.os.*;
import android.app.*;
import android.text.*;
import android.widget.*;
import android.provider.*;
import android.hardware.*;
import android.media.*;
import android.net.*;
import android.telephony.SmsManager;
import android.os.Vibrator;
import android.os.Environment;
import android.provider.MediaStore.*;
import android.provider.MediaStore.Images.Media;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.lang.reflect.*;
import java.util.*;
import java.text.Collator;
import java.lang.Math;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
// This is the main class that interacts with the engine. Although only one
// instance of the engine is allowed, we still need an object on which we can
// invoke methods from the native code so we wrap all this up into a single
// view object.
public class Engine extends View implements EngineApi
{
public interface LifecycleListener
{
public abstract void OnResume();
public abstract void OnPause();
}
public static final String TAG = "revandroid.Engine";
// This is true if the engine is not suspended.
private static boolean s_running;
private static Engine s_engine_instance;
private Handler m_handler;
private boolean m_wake_on_event;
private boolean m_wake_scheduled;
private boolean m_video_is_playing;
private VideoControl m_video_control;
private BusyIndicator m_busy_indicator_module;
private TextMessaging m_text_messaging_module;
private Alert m_beep_vibrate_module;
private Contact m_contact_module;
private CalendarEvents m_calendar_module;
private OpenGLView m_opengl_view;
private boolean m_disabling_opengl;
private boolean m_enabling_opengl;
private BitmapView m_bitmap_view;
private File m_temp_image_file;
private Email m_email;
private ShakeEventListener m_shake_listener;
private ScreenOrientationEventListener m_orientation_listener;
private boolean m_text_editor_visible;
private int m_text_editor_mode;
private int m_default_text_editor_mode;
private int m_default_ime_action;
private int m_ime_action;
private SensorModule m_sensor_module;
private DialogModule m_dialog_module;
private NetworkModule m_network_module;
private NativeControlModule m_native_control_module;
private SoundModule m_sound_module;
private NotificationModule m_notification_module;
private NFCModule m_nfc_module;
private AbsoluteLayout m_view_layout;
private PowerManager.WakeLock m_wake_lock;
// AL-2013-14-07 [[ Bug 10445 ]] Sort international on Android
private Collator m_collator;
// MM-2015-06-11: [[ MobileSockets ]] Trust manager and last verification error, used for verifying ssl certificates.
private X509TrustManager m_trust_manager;
private String m_last_certificate_verification_error;
private boolean m_new_intent;
private int m_photo_width, m_photo_height;
private int m_jpeg_quality;
private int m_night_mode;
private List<LifecycleListener> m_lifecycle_listeners;
////////////////////////////////////////////////////////////////////////////////
public Engine(Context p_context)
{
super(p_context);
s_engine_instance = this;
// Temporary for testing purposes
OpenGLView.listConfigs();
//
setFocusable(true);
setFocusableInTouchMode(true);
// Create the main handler, this simply calls the 'doProcess' method
// of the engine since we only use it for wake-up notifications.
m_handler = new Handler() {
public void handleMessage(Message p_message) {
m_wake_scheduled = false;
doProcess(true);
}
};
// create our text editor
// IM-2012-03-23: switch to monitoring the InputConnection to the EditText field to fix
// bugs introduced by the previous method of checking for changes to the field
m_default_text_editor_mode = 1;
m_text_editor_mode = 0;
m_text_editor_visible = false;
m_default_ime_action = EditorInfo.IME_FLAG_NO_ENTER_ACTION | EditorInfo.IME_ACTION_DONE;
m_ime_action = 0;
// initialise modules
m_sensor_module = new SensorModule(this);
m_dialog_module = new DialogModule(this);
m_network_module = new NetworkModule(this);
m_busy_indicator_module = new BusyIndicator (this);
m_text_messaging_module = new TextMessaging (this);
m_beep_vibrate_module = new Alert (this);
m_contact_module = new Contact (this, ((LiveCodeActivity)getContext()));
m_calendar_module = new CalendarEvents (this, ((LiveCodeActivity)getContext()));
m_native_control_module = new NativeControlModule(this, ((LiveCodeActivity)getContext()).s_main_layout);
m_sound_module = new SoundModule(this);
m_notification_module = new NotificationModule(this);
m_nfc_module = new NFCModule(this);
m_view_layout = null;
// MM-2012-08-03: [[ Bug 10316 ]] Initialise the wake lock object.
PowerManager t_power_manager = (PowerManager) p_context.getSystemService(p_context.POWER_SERVICE);
m_wake_lock = t_power_manager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
// Create listeners for shake events
m_shake_listener = new ShakeEventListener(p_context)
{
public void onShake(int type, long timestamp)
{
doShake(type, timestamp);
// Make sure we trigger handling
if (m_wake_on_event)
doProcess(false);
}
};
m_orientation_listener = new ScreenOrientationEventListener(p_context, 0)
{
public void onScreenOrientationChanged(int orientation)
{
doOrientationChanged(orientation);
if (m_wake_on_event)
doProcess(false);
}
};
m_shake_listener.setListening(true);
// We have no opengl view to begin with.
m_opengl_view = null;
m_disabling_opengl = false;
m_enabling_opengl = false;
// But we do have a bitmap view.
m_bitmap_view = new BitmapView(getContext());
// AL-2013-14-07 [[ Bug 10445 ]] Sort international on Android
m_collator = Collator.getInstance(Locale.getDefault());
// MM-2015-06-11: [[ MobileSockets ]] Trust manager and last verification error, used for verifying ssl certificates.
m_trust_manager = null;
m_last_certificate_verification_error = null;
// MW-2013-10-09: [[ Bug 11266 ]] Turn off keep-alive connections to
// work-around a general bug in android:
// https://code.google.com/p/google-http-java-client/issues/detail?id=116
System.setProperty("http.keepAlive", "false");
m_new_intent = false;
m_photo_width = 0;
m_photo_height = 0;
m_jpeg_quality = 100;
m_night_mode =
p_context.getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
m_lifecycle_listeners = new ArrayList<LifecycleListener>();
}
////////////////////////////////////////////////////////////////////////////////
public void wakeEngineThread()
{
post(new Runnable() {
public void run()
{
if (m_wake_on_event)
doProcess(false);
}
});
}
public void nativeNotify(long p_callback, long p_context)
{
final long t_callback = p_callback;
final long t_context = p_context;
post(new Runnable() {
public void run()
{
s_engine_instance . doNativeNotify(t_callback, t_context);
}});
}
public static boolean isRunning()
{
return s_running;
}
public static Engine getEngine()
{
return s_engine_instance;
}
////////////////////////////////////////////////////////////////////////////////
public void showSplashScreen()
{
m_bitmap_view . showSplashScreen();
}
public void hideSplashScreen()
{
m_bitmap_view . hideSplashScreen();
}
////////////////////////////////////////////////////////////////////////////////
public void clearWakeUp()
{
if (m_wake_scheduled)
{
m_handler . removeMessages(0);
m_wake_scheduled = false;
}
}
// MM-2015-06-08: [[ MobileSockets ]] This can now potentially be called from several threads so make method synchronized.
public synchronized void scheduleWakeUp(int p_in_time, boolean p_any_event)
{
if (m_wake_scheduled)
{
m_handler . removeMessages(0);
m_wake_scheduled = false;
}
m_wake_scheduled = true;
m_wake_on_event = p_any_event;
m_handler . sendEmptyMessageDelayed(0, p_in_time);
}
public String getPackagePath()
{
return getContext() . getApplicationInfo() . sourceDir;
}
// IM-2016-03-04: [[ Bug 16917 ]] Return location of native libraries installed with this app
public String getLibraryPath()
{
return getContext() . getApplicationInfo() . nativeLibraryDir;
}
public void finishActivity()
{
// MM-2012-03-19: [[ Bug 10104 ]] Stop tracking any sensors on shutdown - not doing so prevents a restart for some reason.
if (m_sensor_module != null)
m_sensor_module.finish();
((LiveCodeActivity)getContext()).finish();
}
////////////////////////////////////////////////////////////////////////////////
public String loadExternalLibrary(String name)
{
try
{
System . loadLibrary(name);
return System . mapLibraryName(name);
}
catch ( UnsatisfiedLinkError e )
{
Log.i("revandroid", e.toString());
return null;
}
catch ( SecurityException e )
{
Log.i("revandroid", e.toString());
return null;
}
}
////////////////////////////////////////////////////////////////////////////////
public void onConfigurationChanged(Configuration p_new_config)
{
int t_night_mode =
getContext().getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
if (t_night_mode != m_night_mode)
{
m_night_mode = t_night_mode;
doSystemAppearanceChanged();
}
}
////////////////////////////////////////////////////////////////////////////////
public float getPixelDensity()
{
DisplayMetrics t_metrics;
t_metrics = new DisplayMetrics();
getWindowManager() . getDefaultDisplay() . getMetrics(t_metrics);
return t_metrics . density;
}
////////////////////////////////////////////////////////////////////////////////
public String getBuildInfo(String p_key)
{
try
{
if (p_key.startsWith("VERSION."))
{
Class t_version_class = Class.forName("android.os.Build$VERSION");
Field t_field = t_version_class.getField(p_key.substring(p_key.indexOf('.') + 1));
return t_field.get(null).toString();
}
else
{
Class t_build_class = Class.forName("android.os.Build");
Field t_field = t_build_class.getField(p_key);
return t_field.get(null).toString();
}
}
catch (Exception e)
{
Log.i("revandroid", e.toString());
return null;
}
}
////////////////////////////////////////////////////////////////////////////////
public int getDeviceRotation()
{
return m_orientation_listener.getOrientation();
}
public int getDisplayOrientation()
{
return getContext().getResources().getConfiguration().orientation;
}
public int getDisplayRotation()
{
WindowManager t_wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display t_display = t_wm.getDefaultDisplay();
// screen rotation direction is opposite to device rotation
switch (t_display.getRotation())
{
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 270;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 90;
}
return 0;
}
private static final int[] s_orientation_map = new int[] {
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
8, // ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
9, // ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
};
public void setDisplayOrientation(int p_orientation)
{
if ((p_orientation == 1 || p_orientation == 2) && Build.VERSION.SDK_INT < 9) // Build.VERSION_CODES.GINGERBREAD
return;
// MM-2014-03-25: [[ Bug 11708 ]] Moved call to update orientation (from onScreenOrientationChanged).
// This way we only flag orientation changed if the we've set it in the activity (i.e. or app has actually rotated).
// Prevents changes in device orientation during lock screen confusing things.
// IM-2013-11-15: [[ Bug 10485 ]] Record the change in orientation
updateOrientation(p_orientation);
((LiveCodeActivity)getContext()).setRequestedOrientation(s_orientation_map[p_orientation]);
}
////////////////////////////////////////////////////////////////////////////////
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
{
Log.i(TAG, "onCreateInputConnection()");
if (!m_text_editor_visible)
return null;
InputConnection t_connection = new BaseInputConnection(this, true) {
String m_current_text = "";
void handleKey(int keyCode, int charCode)
{
if (charCode == 0)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DEL:
keyCode = 0xff08;
break;
// Hao-2017-02-08: [[ Bug 11727 ]] Detect arrow key for field input
case KeyEvent.KEYCODE_DPAD_LEFT:
keyCode = 0xff51;
break;
case KeyEvent.KEYCODE_DPAD_UP:
keyCode = 0xff52;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
keyCode = 0xff53;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
keyCode = 0xff54;
break;
default:
}
}
else if (charCode == 10)
{
// check for return key
charCode = 0;
keyCode = 0xff0d;
}
Log.i(TAG, "doing keypress for char " + charCode);
doKeyPress(0, charCode, keyCode);
}
@Override
public boolean sendKeyEvent(KeyEvent key)
{
int t_key_code = key.getKeyCode();
int t_char_code = key.getUnicodeChar();
if (key.getAction() == KeyEvent.ACTION_DOWN)
handleKey(t_key_code, t_char_code);
else if (key.getAction() == KeyEvent.ACTION_MULTIPLE)
{
// IM-2013-02-21: [[ BZ 10684 ]]
// allow BaseInputConnection to do the handling of commitText(), etc
// and instead catch the raw key events that are generated.
if (t_key_code == KeyEvent.KEYCODE_UNKNOWN)
{
// handle string of chars
CharSequence t_chars = key.getCharacters();
for (int i = 0; i < t_chars.length(); i++)
handleKey(t_key_code, t_chars.charAt(i));
}
else
{
// handle repeated char
for (int i = 0; i < key.getRepeatCount(); i++)
handleKey(t_key_code, t_char_code);
}
}
if (m_wake_on_event)
doProcess(false);
return true;
}
// Show text changes in the field as the composing text is modified.
// We do this by removing edited text with fake backspace key events
// and sending key events for each new character.
void updateComposingText()
{
String t_new = getEditable().toString();
// send changes to the engine as a sequence of key events.
int t_match_length = 0;
int t_current_length = 0;
int t_new_length = 0;
int t_max_length = 0;
t_current_length = m_current_text.length();
t_new_length = t_new.length();
t_max_length = Math.min(t_current_length, t_new_length);
for (int i = 0; i < t_max_length; i++)
{
if (t_new.charAt(i) != m_current_text.charAt(i))
break;
t_match_length += 1;
}
// send backspaces
for (int i = 0; i < t_current_length - t_match_length; i++)
handleKey(KeyEvent.KEYCODE_DEL, 0);
// send new text
for (int i = t_match_length; i < t_new_length; i++)
handleKey(KeyEvent.KEYCODE_UNKNOWN, t_new.charAt(i));
m_current_text = t_new;
if (m_wake_on_event)
doProcess(false);
}
// override input connection methods to catch changes to the composing text
@Override
public boolean commitText(CharSequence text, int newCursorPosition)
{
boolean t_return_value = super.commitText(text, newCursorPosition);
updateComposingText();
return t_return_value;
}
@Override
public boolean finishComposingText()
{
boolean t_return_value = super.finishComposingText();
updateComposingText();
return t_return_value;
}
@Override
public boolean setComposingText(CharSequence text, int newCursorPosition)
{
boolean t_return_value = super.setComposingText(text, newCursorPosition);
updateComposingText();
return t_return_value;
}
@Override
public boolean performEditorAction (int editorAction)
{
handleKey(0, 10);
return true;
}
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength)
{
boolean t_return_value = super.deleteSurroundingText(beforeLength, afterLength);
updateComposingText();
return t_return_value;
}
};
int t_type = getInputType(false);
outAttrs.actionLabel = null;
outAttrs.inputType = t_type;
if (m_ime_action != 0)
{
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI | m_ime_action;
}
else
{
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI | m_default_ime_action;
}
return t_connection;
}
public void showKeyboard()
{
if (!m_text_editor_visible)
return;
requestFocus();
InputMethodManager imm;
imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.restartInput(this);
// HH-2017-01-18: [[ Bug 18058 ]] Fix keyboard not show in landscape orientation
imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
}
@Override
public void getFocusedRect(Rect r_rect)
{
Rect t_rect = doGetFocusedRect();
if (t_rect == null)
{
super.getFocusedRect(r_rect);
}
else
{
r_rect.set(t_rect);
}
}
private static final int KEYBOARD_DISPLAY_OVER = 0;
private static final int KEYBOARD_DISPLAY_PAN = 1;
public void setKeyboardDisplay(int p_mode)
{
if (p_mode == KEYBOARD_DISPLAY_PAN)
{
getActivity()
.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
else if (p_mode == KEYBOARD_DISPLAY_OVER)
{
getActivity()
.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
public void hideKeyboard()
{
// Hide the IME
InputMethodManager imm;
imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.restartInput(this);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
public void resetKeyboard()
{
InputMethodManager imm;
imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.restartInput(this);
}
public void setTextInputVisible(boolean p_visible, int p_input_mode, int p_ime_action)
{
m_text_editor_visible = p_visible;
if (!s_running)
return;
if (p_visible)
{
m_text_editor_mode = p_input_mode;
m_ime_action = p_ime_action;
showKeyboard();
}
else
{
hideKeyboard();
m_text_editor_mode = 0;
m_ime_action = 0;
}
}
public void setKeyboardReturnKey(int p_ime_action)
{
m_default_ime_action = p_ime_action;
}
public void setTextInputMode(int p_mode)
{
// 0 is none
// 1 is text (normal)
// 2 is number
// 3 is decimal
// 4 is phone
// 5 is email
boolean t_reset = s_running && m_text_editor_visible && p_mode != m_default_text_editor_mode;
m_default_text_editor_mode = p_mode;
if (t_reset)
resetKeyboard();
}
public static final int TYPE_NUMBER_VARIATION_PASSWORD = 16;
public int getInputType(boolean p_password)
{
int t_type;
int t_mode = m_text_editor_mode;
if (t_mode == 0)
{
t_mode = m_default_text_editor_mode;
}
// the phone class does not support a password variant, so we switch this for one of the number types
if (p_password && t_mode == 4)
t_mode = 2;
// the number password variant is not supported pre-honeycomb, so rather than show passwords in plain-text,
// we switch to the default text input type
if (p_password && Build.VERSION.SDK_INT < 11 && (t_mode == 2 || t_mode == 3))
t_mode = 1;
switch(t_mode)
{
default:
case 1:
t_type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
if (p_password)
t_type |= InputType.TYPE_TEXT_VARIATION_PASSWORD;
break;
case 2:
t_type = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED;
if (p_password)
t_type |= TYPE_NUMBER_VARIATION_PASSWORD;
break;
case 3:
t_type = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL;
if (p_password)
t_type |= TYPE_NUMBER_VARIATION_PASSWORD;
break;
case 4:
t_type = InputType.TYPE_CLASS_PHONE;
break;
case 5:
t_type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
if (p_password)
t_type |= InputType.TYPE_TEXT_VARIATION_PASSWORD;
break;
}
return t_type;
}
public void configureTextInput(int p_mode)
{
m_text_editor_mode = p_mode;
if (!s_running)
return;
if (p_mode == 0)
{
hideKeyboard();
}
else
{
// Show the IME
showKeyboard();
}
}
protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect)
{
if (!gainFocus)
{
hideKeyboard();
}
else if (gainFocus)
{
if (m_text_editor_visible)
showKeyboard();
else
hideKeyboard();
}
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
}
////////////////////////////////////////////////////////////////////////////////
// string utility functions
public int conversionByteCount(byte[] p_input, String p_in_charset, String p_out_charset)
{
Charset t_in_charset = Charset.forName(p_in_charset);
Charset t_out_charset = Charset.forName(p_out_charset);
if (t_in_charset == null || t_out_charset == null)
return 0;
CharsetDecoder t_decode = t_in_charset.newDecoder();
CharsetEncoder t_encode = t_out_charset.newEncoder();
t_decode.replaceWith("?");
try
{
CharBuffer t_utf16;
t_utf16 = t_decode.decode(ByteBuffer.wrap(p_input));
ByteBuffer t_encoded;
t_encoded = t_encode.encode(t_utf16);
return t_encoded.limit();
}
catch (CharacterCodingException e)
{
return 0;
}
}
public byte[] convertCharset(byte[] p_input, String p_in_charset, String p_out_charset)
{
Charset t_in_charset = Charset.forName(p_in_charset);
Charset t_out_charset = Charset.forName(p_out_charset);
if (t_in_charset == null || t_out_charset == null)
{
return null;
}
CharsetDecoder t_decode = t_in_charset.newDecoder();
CharsetEncoder t_encode = t_out_charset.newEncoder();
t_decode.onUnmappableCharacter(CodingErrorAction.REPLACE);
t_decode.replaceWith("?");
t_encode.onUnmappableCharacter(CodingErrorAction.REPLACE);
byte[] t_bytes = null;
try
{
CharBuffer t_utf16;
t_utf16 = t_decode.decode(ByteBuffer.wrap(p_input));
ByteBuffer t_encoded;
t_encoded = t_encode.encode(t_utf16);
t_bytes = new byte[t_encoded.limit()];
t_encoded.get(t_bytes);
}
catch (Exception e)
{
return null;
}
return t_bytes;
}
////////////////////////////////////////////////////////////////////////////////
static final String s_external_prefix = "external ";
public String getSpecialFolderPath(String p_name)
{
boolean t_external = false;
if (p_name.startsWith(s_external_prefix))
{
t_external = true;
p_name = p_name.substring(s_external_prefix.length());
}
try
{
if (p_name.equalsIgnoreCase("documents"))
{
if (t_external)
return getContext().getExternalFilesDir(null).getAbsolutePath();
else
return getContext().getFilesDir().getAbsolutePath();
}
else if (p_name.equalsIgnoreCase("temporary") || p_name.equalsIgnoreCase("cache"))
{
if (t_external)
return getContext().getExternalCacheDir().getAbsolutePath();
else
return getContext().getCacheDir().getAbsolutePath();
}
}
catch (Exception e)
{
return "";
}
return "";
}
////////////////////////////////////////////////////////////////////////////////
public int getAssetFileLength(String p_path)
{
int t_result;
try
{
AssetFileDescriptor t_descriptor;
t_descriptor = getContext() . getAssets() . openFd(p_path);
t_result = (int)t_descriptor . getLength();
t_descriptor . close();
}
catch(Exception e)
{
t_result = -1;
}
return t_result;
}
public int getAssetFileStartOffset(String p_path)
{
int t_result;
try
{
AssetFileDescriptor t_descriptor;
t_descriptor = getContext() . getAssets() . openFd(p_path);
t_result = (int)t_descriptor . getStartOffset();
t_descriptor . close();
}
catch(Exception e)
{
t_result = -1;
}
return t_result;
}
public int getAssetInfo(String p_filename, int p_field)
{
if (p_field == 0)
return getAssetFileStartOffset(p_filename);
else
return getAssetFileLength(p_filename);