/
MoSyncThread.java
5685 lines (4960 loc) · 141 KB
/
MoSyncThread.java
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) 2010 MoSync AB
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.
This program 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 this program; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
package com.mosync.internal.android;
import static com.mosync.internal.android.MoSyncHelpers.DebugPrint;
import static com.mosync.internal.android.MoSyncHelpers.EXTENT;
import static com.mosync.internal.android.MoSyncHelpers.SYSLOG;
import static com.mosync.internal.generated.MAAPI_consts.EVENT_TYPE_BLUETOOTH_TURNED_OFF;
import static com.mosync.internal.generated.MAAPI_consts.EVENT_TYPE_BLUETOOTH_TURNED_ON;
import static com.mosync.internal.generated.MAAPI_consts.EVENT_TYPE_SCREEN_STATE_OFF;
import static com.mosync.internal.generated.MAAPI_consts.EVENT_TYPE_SCREEN_STATE_ON;
import static com.mosync.internal.generated.MAAPI_consts.IOCTL_UNAVAILABLE;
import static com.mosync.internal.generated.MAAPI_consts.MAS_CREATE_IF_NECESSARY;
import static com.mosync.internal.generated.MAAPI_consts.MA_NFC_NOT_AVAILABLE;
import static com.mosync.internal.generated.MAAPI_consts.NOTIFICATION_TYPE_APPLICATION_LAUNCHER;
import static com.mosync.internal.generated.MAAPI_consts.RES_BAD_INPUT;
import static com.mosync.internal.generated.MAAPI_consts.RES_OK;
import static com.mosync.internal.generated.MAAPI_consts.RES_OUT_OF_MEMORY;
import static com.mosync.internal.generated.MAAPI_consts.SCREEN_ORIENTATION_DYNAMIC;
import static com.mosync.internal.generated.MAAPI_consts.SCREEN_ORIENTATION_LANDSCAPE;
import static com.mosync.internal.generated.MAAPI_consts.SCREEN_ORIENTATION_PORTRAIT;
import static com.mosync.internal.generated.MAAPI_consts.STERR_GENERIC;
import static com.mosync.internal.generated.MAAPI_consts.STERR_NONEXISTENT;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_MIRROR;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_MIRROR_ROT180;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_MIRROR_ROT270;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_MIRROR_ROT90;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_NONE;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_ROT180;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_ROT270;
import static com.mosync.internal.generated.MAAPI_consts.TRANS_ROT90;
import static com.mosync.internal.generated.MAAPI_consts.EVENT_TYPE_ALERT;
import static com.mosync.internal.generated.MAAPI_consts.MA_IMAGE_PICKER_EVENT_RETURN_TYPE_IMAGE_HANDLE;
import static com.mosync.internal.generated.MAAPI_consts.MA_RESOURCE_OPEN;
import static com.mosync.internal.generated.MAAPI_consts.MA_RESOURCE_CLOSE;
import static com.mosync.internal.generated.MAAPI_consts.MA_WAKE_LOCK_ON;
import static com.mosync.internal.generated.MAAPI_consts.MA_WAKE_LOCK_OFF;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.Date;
import java.util.Hashtable;
import java.util.Locale;
import java.util.TimeZone;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.Region;
import android.net.NetworkInfo;
import android.net.Uri;
import android.opengl.GLUtils;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.provider.Settings.Secure;
import android.net.ConnectivityManager;
import com.mosync.internal.android.MoSyncFont.MoSyncFontHandle;
import com.mosync.internal.android.billing.PurchaseManager;
import com.mosync.internal.android.nfc.MoSyncNFC;
import com.mosync.internal.android.nfc.MoSyncNFCService;
import com.mosync.internal.generated.IX_OPENGL_ES;
import com.mosync.internal.generated.IX_WIDGET;
import com.mosync.java.android.MoSync;
import com.mosync.java.android.MoSyncPanicDialog;
import com.mosync.java.android.MoSyncService;
import com.mosync.java.android.TextBox;
import com.mosync.nativeui.ui.widgets.MoSyncCameraPreview;
import com.mosync.nativeui.ui.widgets.ScreenWidget;
import com.mosync.nativeui.util.AsyncWait;
import com.mosync.nativeui.util.properties.IntConverter;
import com.mosync.nativeui.util.properties.PropertyConversionException;
/**
* Thread that runs the MoSync virtual machine and handles all syscalls.
* @author Anders Malm
*/
public class MoSyncThread extends Thread
{
// When this class is loaded we load the native library.
static
{
System.loadLibrary("mosync");
}
/**
* Global reference to the single instance of this class.
*/
private static MoSyncThread sMoSyncThread;
// Declaration of native methods.
public native boolean nativeInitRuntime();
public native boolean nativeLoad(
FileDescriptor program,
long programOffset,
FileDescriptor resource,
long resourceOffset);
//public native boolean nativeLoadResource(ByteBuffer resource);
public native boolean nativeLoadResource(
FileDescriptor resource,
long resoruceOffset,
int handle,
int placeholder);
public native ByteBuffer nativeLoadCombined(ByteBuffer combined);
public native void nativeRun();
public native void nativePostEvent(int[] eventBuffer);
public native int nativeGetEventQueueSize();
public native int nativeCreateBinaryResource(
int resourceIndex,
int length);
public native int nativeCreatePlaceholder();
public native void nativeExit();
// Modules that handle syscalls for various subsystems.
// We delegate syscalls from this class to the modules.
MoSyncNetwork mMoSyncNetwork;
MoSyncBluetooth mMoSyncBluetooth;
MoSyncSound mMoSyncSound;
MoSyncAudio mMoSyncAudio;
MoSyncLocation mMoSyncLocation;
MoSyncHomeScreen mMoSyncHomeScreen;
MoSyncNativeUI mMoSyncNativeUI;
MoSyncFile mMoSyncFile;
// Module for device fonts.
static MoSyncFont mMoSyncFont;
MoSyncCameraController mMoSyncCameraController;
MoSyncSMS mMoSyncSMS;
MoSyncSensor mMoSyncSensor;
MoSyncPIM mMoSyncPIM;
MoSyncNFC mMoSyncNFC;
MoSyncAds mMoSyncAds;
MoSyncNotifications mMoSyncNotifications;
MoSyncCapture mMoSyncCapture;
MoSyncPurchase mMoSyncPurchase;
MoSyncDB mMoSyncDB;
/**
* Synchronization monitor for postEvent
*/
private final Object mPostEventMonitor = new Object();
static final String PROGRAM_FILE = "program.mp3";
static final String RESOURCE_FILE = "resources.mp3";
/**
* Chunk size used when loading resources.
*/
private static final int CHUNK_READ_SIZE = 1024;
/**
* This is the MoSync Activity.
*/
private final MoSync mContext;
/**
* The standard MoSync view.
*/
private MoSyncView mMoSyncView;
/**
* Flag that tells if the display should be updated.
*/
volatile private boolean mUpdateDisplay = true;
/**
* true if the MoSync program is considered to be dead,
* used for maPanic.
*/
volatile private boolean mHasDied;
/**
* a handle used for full screen camera preview
*/
private int cameraScreen;
FileDescriptor mResourceFd = null;
/**
* This is the size of the header of the asset file
* (an Android file header, this is not a MoSync header).
* The actual resource data is found at this offset.
* On the Java side an input stream to the asset file
* will begin at this offset.
* UBinData resources have offsets that start at the
* very beginning of the resource file.
* Therefore mResourceOffset needs to be used on the
* Java side when reading data from ubin resource file.
*/
private long mResourceOffset = 0;
ByteBuffer mRecompilerCodeBlock;
ByteBuffer mRecompilerEntryBlock;
/**
* Table that holds binary resources.
*/
Hashtable<Integer, ByteBuffer> mBinaryResources =
new Hashtable<Integer, ByteBuffer>();
/**
* Table that holds ubin resources.
*/
Hashtable<Integer, UBinData> mUBinaryResources =
new Hashtable<Integer, UBinData>();
/**
* Table that holds image resources.
*/
Hashtable<Integer, ImageCache> mImageResources =
new Hashtable<Integer, ImageCache>();
/**
* Table that holds data stores.
*/
Hashtable<Integer, String> mStores =
new Hashtable<Integer, String>();
// Various variables, should be moved to subsystems
// along with the syscalls.
public ByteBuffer mMemDataSection;
ByteBuffer mResourceFile;
Canvas mCanvas;
Bitmap mBitmap;
int mWidth;
int mHeight;
int mClipLeft, mClipTop, mClipWidth, mClipHeight;
volatile boolean mUsingFrameBuffer;
int mFrameBufferAddress;
int mFrameBufferSize;
Bitmap mFrameBufferBitmap;
ImageCache mDrawTargetScreen = null;
ImageCache mDrawTarget = null;
int mDrawTargetHandle = 0;
Paint mPaint = new Paint();
Paint mBlitPaint = new Paint();
int mTextConsoleHeight;
/**
* Flag used to signal if the thread is sleeping.
*/
private volatile boolean mIsSleeping;
/**
* Ascent of text in the default console font.
*/
//int mTextConsoleAscent;
/**
* Rectangle that is used to get the extent of a text string.
*/
Rect mTextSizeRect = new Rect();
// Rectangle objects used for drawing in maDrawImageRegion().
private final Rect mMaDrawImageRegionTempSourceRect = new Rect();
private final Rect mMaDrawImageRegionTempDestRect = new Rect();
/**
* An Instance of Connectivity Manager used for detecting connection type
*/
private ConnectivityManager mConnectivityManager;
int mMaxStoreId = 0;
// TODO: Make this private and access via a method.
volatile public boolean mIsUpdatingScreen = false;
final static String storesPath = "MAStore";
/**
* BroadcastReceiver that handles screen on/off events.
*/
BroadcastReceiver mScreenActivatedReceiver = null;
/**
* MoSyncThread constructor
*/
public MoSyncThread(Context context, Handler handler) throws Exception
{
mContext = (MoSync) context;
// TODO: Clean this up! The static reference should be in one place.
// Now the instance of MoSyncThread is passed to many classes and
// also accessed via the static variables. We use use one consistent way.
EventQueue.sMoSyncThread = this;
sMoSyncThread = this;
MoSyncError.getSingletonObject().setThread(this);
mHasDied = false;
mIsSleeping = false;
mMoSyncNetwork = new MoSyncNetwork(this);
mMoSyncSound = new MoSyncSound(this);
mMoSyncAudio = new MoSyncAudio(this);
mMoSyncLocation = new MoSyncLocation(this);
mMoSyncHomeScreen = new MoSyncHomeScreen(this);
mMoSyncNativeUI = new MoSyncNativeUI(this, mImageResources);
mMoSyncFile = new MoSyncFile(this);
try
{
mMoSyncFont = new MoSyncFont(this);
}
catch (Throwable e)
{
mMoSyncFont = null;
}
cameraScreen = 0;
//Do not access camera if it is not available
try
{
mMoSyncCameraController = new MoSyncCameraController(this);
}
catch (Throwable e)
{
mMoSyncCameraController = null;
}
// Bluetooth is not available on all platforms and
// therefore we do a conditional loading of the
// MoSyncBluetooth class.
try
{
mMoSyncBluetooth = new MoSyncBluetooth(this);
}
catch (java.lang.VerifyError error)
{
mMoSyncBluetooth = null;
}
// SMS is not available on all platforms and
// therefore we do a conditional loading of the
// MoSyncSMS class.
try
{
mMoSyncSMS = new MoSyncSMS(this);
}
catch (java.lang.VerifyError error)
{
mMoSyncSMS = null;
}
//nativeInitRuntime();
mMoSyncPurchase = new MoSyncPurchase(this);
mMoSyncSensor = new MoSyncSensor(this);
mMoSyncPIM = new MoSyncPIM(this);
try
{
mMoSyncNFC = MoSyncNFCService.getDefault();
if (mMoSyncNFC != null)
{
mMoSyncNFC.setMoSyncThread(this);
}
}
catch (Throwable t)
{
mMoSyncNFC = null;
}
mMoSyncAds = new MoSyncAds(this);
mMoSyncNotifications = new MoSyncNotifications(this);
mMoSyncCapture = new MoSyncCapture(this, mImageResources);
mMoSyncDB = new MoSyncDB();
mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
nativeInitRuntime();
}
public static MoSyncThread getInstance()
{
return sMoSyncThread;
}
public void onResume()
{
// Turn on display update again.
mUpdateDisplay = true;
// Turn on sensors.
mMoSyncSensor.onResume();
mMoSyncPurchase.restoreService();
}
public void onPause()
{
// Do not update the display when paused.
mUpdateDisplay = false;
// Pause sensors.
mMoSyncSensor.onPause();
}
/**
* Called from the MoSync activity in onDestroy.
* Do cleanup here (but it is not guaranteed that
* this will be called.)
*/
public void onDestroy()
{
if (null != mMoSyncBluetooth)
{
// Delegate onDestroy to the Bluetooth object.
mMoSyncBluetooth.onDestroy();
mMoSyncBluetooth = null;
}
if(null != mMoSyncNetwork)
{
mMoSyncNetwork.killAllConnections();
mMoSyncNetwork = null;
}
if (null != mMoSyncPurchase)
{
/**
* Unbind from the MarketBillingService.
*/
mMoSyncPurchase.unbindService();
mMoSyncPurchase = null;
}
}
/**
* Return the activity that this thread is related to.
* @return the Activity object.
*/
public Activity getActivity()
{
return mContext;
}
/**
* Check if the Bluetooth API is available.
* @return true if the API is available.
*/
public boolean isBluetoothApiAvailable()
{
return mMoSyncBluetooth != null;
}
/**
* Called from broadcast listener in MoSyncBluetooth.
*/
public void bluetoothTurnedOn()
{
// Send event.
int[] event = new int[1];
event[0] = EVENT_TYPE_BLUETOOTH_TURNED_ON;
postEvent(event);
}
/**
* Called from broadcast listener in MoSyncBluetooth.
*/
public void bluetoothTurnedOff()
{
// Close Bluetooth connections.
mMoSyncNetwork.bluetoothTurnedOff();
// Send event.
int[] event = new int[1];
event[0] = EVENT_TYPE_BLUETOOTH_TURNED_OFF;
postEvent(event);
}
/**
* Check if the Font API is available.
* @return true if the API is available.
*/
public boolean isFontApiAvailable()
{
return mMoSyncFont != null;
}
/**
* setMoSyncView
*/
public void setMoSyncView(MoSyncView moSyncView)
{
mMoSyncView = moSyncView;
// Each time the MoSync view is changed its reference
// must be updated in native UI.
mMoSyncNativeUI.setMoSyncScreen( moSyncView );
}
/**
* @return true of the application is considered to to have died.
*/
public boolean isDead()
{
return mHasDied;
}
/**
* Update the size of the drawing surface.
*/
public synchronized void updateSurfaceSize(int width, int height)
{
SYSLOG("updateSurfaceSize");
// If the surface has same width and height we
// do not create a new canvas.
if((mWidth == width) && (mHeight == height)) return;
mWidth = width;
mHeight = height;
Bitmap bitmap = createBitmap(mWidth, mHeight);
Canvas canvas = new Canvas(bitmap);
mDrawTargetScreen = new ImageCache(canvas, bitmap);
mCanvas = mDrawTargetScreen.mCanvas;
mBitmap = mDrawTargetScreen.mBitmap;
// Set/reset clip rect.
mClipLeft = 0;
mClipTop = 0;
mClipWidth = mWidth;
mClipHeight = mHeight;
// Set original clip rect.
// Save the clip state since we have a new canvas created.
// This initial save is needed for the clip rect stack to
// work correctly when calling restore clip rect.
mCanvas.save();
mCanvas.clipRect(mClipLeft, mClipTop, mClipWidth, mClipHeight, Region.Op.REPLACE);
updateScreen();
SYSLOG("mBitmap width:" + mBitmap.getWidth()
+ " height:" + mBitmap.getHeight());
}
/**
* @brief Returns a slice of the memory ByteBuffer
*
* All calls which are accessing memory shall use this function!
* The use of .position in any other call is strictly forbidden!
*
* @param addr The address to the beginning of memory block
* @param len The size of the block, -1 means that the whole
* block should be used.
* @return
*/
public synchronized ByteBuffer getMemorySlice(int addr, int len)
{
mMemDataSection.position(addr);
ByteBuffer slice = mMemDataSection.slice();
if(-1 != len)
slice.limit(len);
return slice;
}
/**
* Allocate memory for the program data section.
*/
public boolean generateDataSection(ByteBuffer byteBuffer)
{
try
{
mMemDataSection = byteBuffer;
mMemDataSection.order(null);
}
catch (Exception e)
{
logError("MoSyncThread - Out of Memory!", e);
mMemDataSection = null;
return false;
}
catch (Error e)
{
logError("MoSyncThread - Out of Memory!", e);
mMemDataSection = null;
return false;
}
return true;
}
/**
* Issue a panic. Launches a panic dialog.
* @param errorCode
* @param message
*/
public void threadPanic(int errorCode, String message)
{
// Print debug data to the logcat console.
Log.e("@@@ MoSync",
"threadPanic errorCode: " + errorCode + " message: " + message);
new Exception("STACKTRACE: threadPanic").printStackTrace();
mHasDied = true;
// TODO: Run on UI thread?
// Launch panic dialog.
MoSyncPanicDialog.sPanicMessage = message;
Intent myIntent = new Intent(
mMoSyncView.getContext(), MoSyncPanicDialog.class);
mMoSyncView.getContext().startActivity(myIntent);
while(true)
{
try
{
sleep(500);
}
catch(Exception e)
{
Log.i("MoSync Thread","oops.. got an exception, conutine until application ends");
}
}
}
/**
* Loads the MoSync program.
*
* This function loads the program file and the resource file.
* It loads all the resources and generates the data
* section used by the program.
*
* @return false if failed, true if succeeded.
*/
public boolean loadProgram()
{
SYSLOG("loadProgram");
try
{
AssetManager assetManager = mContext.getAssets();
AssetFileDescriptor pAfd = assetManager.openFd(PROGRAM_FILE);
FileDescriptor pFd = pAfd.getFileDescriptor();
long pFdOffset = pAfd.getStartOffset();
FileDescriptor rFd = null;
mResourceOffset = 0;
// Try to load the resource file, if we get an exception
// it just means that this application has no resource file
// and that is not an error.
try
{
AssetFileDescriptor rAfd = assetManager.openFd(RESOURCE_FILE);
rFd = rAfd.getFileDescriptor();
mResourceOffset = rAfd.getStartOffset();
}
catch (FileNotFoundException fnfe)
{
logError("loadProgram - Has no resources! exception: "
+ fnfe.toString(), fnfe);
}
// We have a program file so now we sends it to the native side
// so it will be loaded into memory. The data section will also be
// created and if there are any resources they will be loaded.
if (null != pFd)
{
if (false == nativeLoad(pFd, pFdOffset, rFd, mResourceOffset))
{
logError("loadProgram - "
+ "ERROR Load program was unsuccesfull");
if (null == mMemDataSection)
{
threadPanic(
0,
"This device does not have enough" +
"memory to run this application."
);
}
else
{
threadPanic(0, "Unable to load program or resources");
}
return false;
}
SYSLOG("loadProgram - Load program was succesfull");
return true;
}
}
catch (Exception e)
{
logError("loadProgram - Exception while loading program:"
+ e.toString(), e);
}
return false;
}
/**
* Get file descriptor for the resource file.
*/
public FileDescriptor getResourceFileDesriptor()
{
try
{
AssetManager assetManager = mContext.getAssets();
AssetFileDescriptor pAfd = assetManager.openFd(RESOURCE_FILE);
FileDescriptor pFd = pAfd.getFileDescriptor();
mResourceOffset = pAfd.getStartOffset();
return pFd;
}
catch (Exception e)
{
logError("getResourceFileDesriptor exception:" + e.toString(), e);
}
return null;
}
/**
* Get the offset to the start of the resources.
*/
public int getResourceStartOffset()
{
return (int) mResourceOffset;
}
/**
* Read data from an input stream to a ByteBuffer.
* @param is The imput stream.
* @return ByteBuffer with the data.
* @throws Exception
*/
public synchronized ByteBuffer readInputStream(InputStream is) throws Exception
{
ReadableByteChannel byteChannel = Channels.newChannel(is);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(CHUNK_READ_SIZE);
while (byteChannel.read(byteBuffer) != -1)
{
if (byteBuffer.remaining() < CHUNK_READ_SIZE)
{
ByteBuffer temp =
ByteBuffer.allocateDirect(byteBuffer.capacity() * 2);
byteBuffer.flip();
temp.put(byteBuffer);
byteBuffer = temp;
}
}
return byteBuffer;
}
/**
* Create a data object by (indirectly) calling maCreatePlaceholder
* (if no placeholder is supplied), and maCreateData, then copy the
* data given in the data parameter to the new buffer.
* @param placeholder The placeholder that should refer to the data,
* if this parameter is zero, a new placeholder is created.
* @param data The data to fill the new data object with.
* @return The handle to the data if successful, <0 on error.
* If a placeholder is supplied, that value is returned on success.
*
* TODO: This method needs improved error checking.
*/
public int createDataObject(int placeholder, byte[] data)
{
// The handle to the data.
int dataHandle = placeholder;
// Create handle if no placeholder is given.
if (0 == placeholder)
{
// This calls maCreatePlaceholder.
// TODO: Check return value for error.
dataHandle = nativeCreatePlaceholder();
}
// Allocate data. This calls maCreateData and will create a
// new data object.
int result = nativeCreateBinaryResource(dataHandle, data.length);
if (result < 0)
{
return result;
}
// Get byte buffer for the handle and copy data
// to the data object.
// TODO: Handle error if this fails.
ByteBuffer buf = getBinaryResource(dataHandle);
if (null != buf)
{
// Copy data.
buf.put(data);
}
return dataHandle;
}
/**
* Called from JNI, delegate to the audio module.
* @param soundHandle
*/
void storeIfBinaryAudioResource(int soundHandle)
{
mMoSyncSound.storeIfBinaryAudioResource(soundHandle, 0);
}
/*
* @brief Function that decodes an image into a Bitmap
*
* @param data The decoded image data
* @param options The bitmapFactory options
*
* @return The created Bitmap, null if it failed
*
* TODO: There is no need for this method to be synchronized.
* Find out why it was synchronized.
*/
//synchronized
Bitmap decodeImageFromData(
final byte[] data,
final BitmapFactory.Options options)
{
try
{
final AsyncWait<Bitmap> waiter = new AsyncWait<Bitmap>();
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
// Here we can get:
// java.lang.OutOfMemoryError: bitmap size exceeds VM budget
try
{
Bitmap bitmap = BitmapFactory.decodeByteArray(
data, 0, data.length, options);
waiter.setResult(bitmap);
}
catch (OutOfMemoryError e1)
{
//Log.i("@@@", "decodeImageFromData - " + "Out of memory error : " + e1);
waiter.setResult(null);
}
catch (Throwable e2)
{
//Log.i("@@@", "decodeImageFromData - " + "Error : " + e2);
waiter.setResult(null);
}
}
});
return waiter.getResult();
}
catch(InterruptedException ie)
{
//Log.i("MoSync", "Couldn't decode image data.");
return null;
}
}
/*
* @brief Recycles the bitmap
*
* @param bitmap The bitmap which is being recycled
*/
void recycleImageData(final Bitmap bitmap)
{
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
bitmap.recycle();
}
});
return;
}
/*
* @brief Function that creates an empty Bitmap
*
* @param width The width of the created Bitmap
* @param height The width of the created Bitmap
*
* @return The created Bitmap, null if it failed
*
* TODO: There is no need for this method to be synchronized.
* Find out why it was synchronized.
*
* TODO: Look into eliminating code duplication between
* createBitmap and createBitmapFromData.
*/
//synchronized
Bitmap createBitmap(final int width, final int height)
{
try
{
final AsyncWait<Bitmap> waiter = new AsyncWait<Bitmap>();
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
// Here we can get:
// java.lang.OutOfMemoryError: bitmap size exceeds VM budget
try
{
Bitmap bitmap = Bitmap.createBitmap(
width, height, Bitmap.Config.ARGB_8888);
waiter.setResult(bitmap);
}
catch (OutOfMemoryError e1)
{
//Log.i("@@@", "createBitmapFromData - " + "Out of memory error : " + e1);
waiter.setResult(null);
}
catch (Throwable e2)
{
//Log.i("@@@", "createBitmapFromData - " + "Error : " + e2);
waiter.setResult(null);
}
}
});
return waiter.getResult();
}
catch(InterruptedException ie)
{
Log.i("MoSync", "Couldn't create empty bitmap.");
}
return null;
}
/*
* @brief Function that creates a Bitmap from pixel data
*
* @param width The width of the created Bitmap
* @param height The width of the created Bitmap
* @param pixels The pixel data