/
TiApplication.java
912 lines (783 loc) Β· 25.1 KB
/
TiApplication.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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2015 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package org.appcelerator.titanium;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Looper;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;
import android.view.accessibility.AccessibilityManager;
import com.appcelerator.aps.APSAnalytics;
import com.appcelerator.aps.APSAnalytics.DeployType;
import org.appcelerator.kroll.KrollApplication;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.common.CurrentActivityListener;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.kroll.common.TiDeployData;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.kroll.util.KrollAssetHelper;
import org.appcelerator.kroll.util.TiTempFileHelper;
import org.appcelerator.titanium.analytics.TiAnalyticsEventFactory;
import org.appcelerator.titanium.util.TiBlobLruCache;
import org.appcelerator.titanium.util.TiFileHelper;
import org.appcelerator.titanium.util.TiImageLruCache;
import org.appcelerator.titanium.util.TiPlatformHelper;
import org.appcelerator.titanium.util.TiResponseCache;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.util.TiWeakList;
import org.json.JSONException;
import org.json.JSONObject;
import ti.modules.titanium.TitaniumModule;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* The main application entry point for all Titanium applications and services.
*/
public abstract class TiApplication extends Application implements KrollApplication
{
private static final String SYSTEM_UNIT = "system";
private static final String TAG = "TiApplication";
private static final String PROPERTY_THREAD_STACK_SIZE = "ti.android.threadstacksize";
private static final String PROPERTY_COMPILE_JS = "ti.android.compilejs";
private static final String PROPERTY_ENABLE_COVERAGE = "ti.android.enablecoverage";
private static final String PROPERTY_DEFAULT_UNIT = "ti.ui.defaultunit";
private static final String PROPERTY_USE_LEGACY_WINDOW = "ti.android.useLegacyWindow";
private static long mainThreadId = 0;
protected static WeakReference<TiApplication> tiApp = null;
public static final String DEPLOY_TYPE_DEVELOPMENT = "development";
public static final String DEPLOY_TYPE_TEST = "test";
public static final String DEPLOY_TYPE_PRODUCTION = "production";
public static final int DEFAULT_THREAD_STACK_SIZE = 16 * 1024; // 16K as a "sane" default
public static final String APPLICATION_PREFERENCES_NAME = "titanium";
public static final String PROPERTY_FASTDEV = "ti.android.fastdev";
public static final int TRIM_MEMORY_RUNNING_LOW = 10; // Application.TRIM_MEMORY_RUNNING_LOW for API 16+
// Whether or not using legacy window. This is set in the application's tiapp.xml with the
// "ti.android.useLegacyWindow" property.
public static boolean USE_LEGACY_WINDOW = false;
private boolean restartPending = false;
private String baseUrl;
private String startUrl;
private HashMap<String, SoftReference<KrollProxy>> proxyMap;
private TiWeakList<KrollProxy> appEventProxies = new TiWeakList<KrollProxy>();
private WeakReference<TiRootActivity> rootActivity;
private TiProperties appProperties;
private WeakReference<Activity> currentActivity;
private String density;
private String buildVersion = "", buildTimestamp = "", buildHash = "";
private String defaultUnit;
private TiResponseCache responseCache;
private BroadcastReceiver externalStorageReceiver;
private AccessibilityManager accessibilityManager = null;
private boolean forceFinishRootActivity = false;
protected TiDeployData deployData;
protected TiTempFileHelper tempFileHelper;
protected ITiAppInfo appInfo;
protected TiStylesheet stylesheet;
protected HashMap<String, WeakReference<KrollModule>> modules;
protected String[] filteredAnalyticsEvents;
public static AtomicBoolean isActivityTransition = new AtomicBoolean(false);
protected static ArrayList<ActivityTransitionListener> activityTransitionListeners =
new ArrayList<ActivityTransitionListener>();
protected static TiWeakList<Activity> activityStack = new TiWeakList<Activity>();
public static interface ActivityTransitionListener {
public void onActivityTransition(boolean state);
}
public static void addActivityTransitionListener(ActivityTransitionListener a)
{
activityTransitionListeners.add(a);
}
public static void removeActivityTransitionListener(ActivityTransitionListener a)
{
activityTransitionListeners.remove(a);
}
public static void updateActivityTransitionState(boolean state)
{
isActivityTransition.set(state);
for (int i = 0; i < activityTransitionListeners.size(); ++i) {
activityTransitionListeners.get(i).onActivityTransition(state);
}
}
public CountDownLatch rootActivityLatch = new CountDownLatch(1);
public TiApplication()
{
Log.checkpoint(TAG, "checkpoint, app created.");
loadBuildProperties();
mainThreadId = Looper.getMainLooper().getThread().getId();
tiApp = new WeakReference<TiApplication>(this);
modules = new HashMap<String, WeakReference<KrollModule>>();
TiMessenger.getMessenger(); // initialize message queue for main thread
Log.i(TAG, "Titanium " + buildVersion + " (" + buildTimestamp + " " + buildHash + ")");
}
/**
* Retrieves the instance of TiApplication. There is one instance per Android application.
* @return the instance of TiApplication.
* @module.api
*/
public static TiApplication getInstance()
{
if (tiApp != null) {
TiApplication tiAppRef = tiApp.get();
if (tiAppRef != null) {
return tiAppRef;
}
}
Log.e(TAG, "Unable to get the TiApplication instance");
return null;
}
public static void addToActivityStack(Activity activity)
{
activityStack.add(new WeakReference<Activity>(activity));
}
public static void removeFromActivityStack(Activity activity)
{
activityStack.remove(activity);
}
// Calls finish on the list of activities in the stack. This should only be called when we want to terminate the
// application (typically when the root activity is destroyed)
public static void terminateActivityStack()
{
if (activityStack == null || activityStack.size() == 0) {
return;
}
WeakReference<Activity> activityRef;
Activity currentActivity;
for (int i = activityStack.size() - 1; i >= 0; i--) {
// We need to check the stack size here again. Since we call finish(), that could potentially
// change the activity stack while we are looping through them. TIMOB-12487
if (i < activityStack.size()) {
activityRef = activityStack.get(i);
if (activityRef != null) {
currentActivity = activityRef.get();
if (currentActivity != null && !currentActivity.isFinishing()) {
currentActivity.finish();
}
}
}
}
activityStack.clear();
}
public boolean activityStackHasLaunchActivity()
{
if (activityStack == null || activityStack.size() == 0) {
return false;
}
for (WeakReference<Activity> activityRef : activityStack) {
if (activityRef != null && activityRef.get() instanceof TiLaunchActivity) {
return true;
}
}
return false;
}
/**
* Check whether the current activity is in foreground or not.
* @return true if the current activity is in foreground; false otherwise.
* @module.api
*/
public static boolean isCurrentActivityInForeground()
{
Activity currentActivity = getAppCurrentActivity();
if (currentActivity instanceof TiBaseActivity) {
return ((TiBaseActivity) currentActivity).isInForeground();
}
return false;
}
/**
* This is a convenience method to avoid having to check TiApplication.getInstance() is not null every
* time we need to grab the current activity.
* @return the current activity
* @module.api
*/
public static Activity getAppCurrentActivity()
{
TiApplication tiApp = getInstance();
if (tiApp == null) {
return null;
}
return tiApp.getCurrentActivity();
}
/**
* This is a convenience method to avoid having to check TiApplication.getInstance() is not null every
* time we need to grab the root or current activity.
* @return root activity if exists. If root activity doesn't exist, returns current activity if exists. Otherwise returns null.
* @module.api
*/
public static Activity getAppRootOrCurrentActivity()
{
TiApplication tiApp = getInstance();
if (tiApp == null) {
return null;
}
return tiApp.getRootOrCurrentActivity();
}
/**
* @return the current activity if exists. Otherwise, the thread will wait for a valid activity to be visible.
* @module.api
*/
public Activity getCurrentActivity()
{
int activityStackSize;
while ((activityStackSize = activityStack.size()) > 0) {
Activity activity = (activityStack.get(activityStackSize - 1)).get();
// Skip and remove any activities which are dead or in the process of finishing.
if (activity == null || activity.isFinishing()) {
activityStack.remove(activityStackSize - 1);
continue;
}
return activity;
}
Log.d(TAG, "activity stack is empty, unable to get current activity", Log.DEBUG_MODE);
return null;
}
/**
* @return root activity if exists. If root activity doesn't exist, returns current activity if exists. Otherwise returns null.
*/
public Activity getRootOrCurrentActivity()
{
Activity activity;
if (rootActivity != null) {
activity = rootActivity.get();
if (activity != null) {
return activity;
}
}
if (currentActivity != null) {
activity = currentActivity.get();
if (activity != null) {
return activity;
}
}
Log.e(TAG, "No valid root or current activity found for application instance");
return null;
}
protected void loadBuildProperties()
{
// Initialize build property member variables.
this.buildVersion = "1.0";
this.buildTimestamp = "N/A";
this.buildHash = "N/A";
// Attempt to read the "build.properties" file.
final String FILE_NAME = "org/appcelerator/titanium/build.properties";
try (InputStream stream = getClass().getClassLoader().getResourceAsStream(FILE_NAME)) {
if (stream != null) {
Properties properties = new Properties();
properties.load(stream);
this.buildVersion = properties.getProperty("build.version", this.buildVersion);
this.buildTimestamp = properties.getProperty("build.timestamp", this.buildTimestamp);
this.buildHash = properties.getProperty("build.githash", this.buildHash);
}
} catch (Exception e) {
}
}
public void loadAppProperties()
{
// Load the JSON file:
String appPropertiesString = KrollAssetHelper.readAsset("Resources/_app_props_.json");
if (appPropertiesString != null) {
try {
TiProperties.setSystemProperties(new JSONObject(appPropertiesString));
} catch (JSONException e) {
Log.e(TAG, "Unable to load app properties.");
}
}
}
@Override
protected void attachBaseContext(Context base)
{
super.attachBaseContext(base);
MultiDex.install(this);
}
@Override
public void onCreate()
{
super.onCreate();
Log.d(TAG, "Application onCreate", Log.DEBUG_MODE);
final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e)
{
if (isAnalyticsEnabled()) {
String tiVer = buildVersion + "," + buildTimestamp + "," + buildHash;
Log.e(TAG,
"Sending event: exception on thread: " + t.getName() + " msg:" + e.toString() + "; Titanium "
+ tiVer,
e);
TiPlatformHelper.getInstance().postAnalyticsEvent(
TiAnalyticsEventFactory.createErrorEvent(t, e, tiVer));
}
defaultHandler.uncaughtException(t, e);
}
});
appProperties = new TiProperties(getApplicationContext(), APPLICATION_PREFERENCES_NAME, false);
baseUrl = TiC.URL_ANDROID_ASSET_RESOURCES;
File fullPath = new File(baseUrl, getStartFilename("app.js"));
baseUrl = fullPath.getParent();
proxyMap = new HashMap<String, SoftReference<KrollProxy>>(5);
tempFileHelper = new TiTempFileHelper(this);
}
@Override
public void onTerminate()
{
stopExternalStorageMonitor();
accessibilityManager = null;
super.onTerminate();
}
@Override
public void onLowMemory()
{
// Release all the cached images
TiBlobLruCache.getInstance().evictAll();
TiImageLruCache.getInstance().evictAll();
super.onLowMemory();
}
@SuppressLint("NewApi")
@Override
public void onTrimMemory(int level)
{
if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_HONEYCOMB && level >= TRIM_MEMORY_RUNNING_LOW) {
// Release all the cached images
TiBlobLruCache.getInstance().evictAll();
TiImageLruCache.getInstance().evictAll();
}
super.onTrimMemory(level);
}
public void postAppInfo()
{
deployData = new TiDeployData(this);
TiPlatformHelper.getInstance().initialize();
// Fastdev has been deprecated
// TiFastDev.initFastDev(this);
if (isAnalyticsEnabled()) {
TiPlatformHelper.getInstance().initAnalytics();
TiPlatformHelper.getInstance().setSdkVersion("ti." + getTiBuildVersion());
TiPlatformHelper.getInstance().setAppName(getAppInfo().getName());
TiPlatformHelper.getInstance().setAppId(getAppInfo().getId());
TiPlatformHelper.getInstance().setAppVersion(getAppInfo().getVersion());
String deployType = appProperties.getString("ti.deploytype", "unknown");
String buildType = appInfo.getBuildType();
if ("unknown".equals(deployType)) {
deployType = getDeployType();
}
if (buildType != null && !buildType.equals("")) {
TiPlatformHelper.getInstance().setBuildType(buildType);
}
// Just use type 'other' enum since it's open ended.
DeployType.OTHER.setName(deployType);
TiPlatformHelper.getInstance().setDeployType(DeployType.OTHER);
APSAnalytics.getInstance().sendAppEnrollEvent();
} else {
Log.i(TAG, "Analytics have been disabled");
}
}
public void postOnCreate()
{
KrollRuntime runtime = KrollRuntime.getInstance();
if (runtime != null) {
Log.i(TAG, "Titanium Javascript runtime: " + runtime.getRuntimeName());
} else {
// This ought not to be possible.
Log.w(TAG, "Titanium Javascript runtime: unknown");
}
TiConfig.DEBUG = TiConfig.LOGD = appProperties.getBool("ti.android.debug", false);
USE_LEGACY_WINDOW = appProperties.getBool(PROPERTY_USE_LEGACY_WINDOW, false);
startExternalStorageMonitor();
// Register the default cache handler
responseCache = new TiResponseCache(getRemoteCacheDir(), this);
TiResponseCache.setDefault(responseCache);
KrollRuntime.setPrimaryExceptionHandler(new TiExceptionHandler());
}
private File getRemoteCacheDir()
{
File cacheDir = new File(tempFileHelper.getTempDirectory(), "remote-cache");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
tempFileHelper.excludeFileOnCleanup(cacheDir);
}
return cacheDir.getAbsoluteFile();
}
public void setRootActivity(TiRootActivity rootActivity)
{
this.rootActivity = new WeakReference<TiRootActivity>(rootActivity);
rootActivityLatch.countDown();
// calculate the display density
DisplayMetrics dm = new DisplayMetrics();
rootActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
switch (dm.densityDpi) {
case DisplayMetrics.DENSITY_HIGH: {
density = "high";
break;
}
case DisplayMetrics.DENSITY_MEDIUM: {
density = "medium";
break;
}
case DisplayMetrics.DENSITY_LOW: {
density = "low";
break;
}
}
tempFileHelper.scheduleCleanTempDir();
}
/**
* @return the app's root activity if exists, null otherwise.
*/
public TiRootActivity getRootActivity()
{
if (rootActivity == null) {
return null;
}
return rootActivity.get();
}
/**
* @return whether the root activity is available
*/
public boolean isRootActivityAvailable()
{
if (rootActivity != null) {
Activity activity = rootActivity.get();
if (activity != null) {
return !activity.isFinishing();
}
}
return false;
}
public void setCurrentActivity(Activity callingActivity, Activity newValue)
{
synchronized (this)
{
Activity currentActivity = getCurrentActivity();
if (currentActivity == null || callingActivity == currentActivity) {
this.currentActivity = new WeakReference<Activity>(newValue);
}
}
}
public String getBaseUrl()
{
return baseUrl;
}
public String getStartUrl()
{
return startUrl;
}
private String getStartFilename(String defaultStartFile)
{
return defaultStartFile;
}
public void addAppEventProxy(KrollProxy appEventProxy)
{
if (appEventProxy != null && !appEventProxies.contains(appEventProxy)) {
appEventProxies.add(new WeakReference<KrollProxy>(appEventProxy));
}
}
public void removeAppEventProxy(KrollProxy appEventProxy)
{
appEventProxies.remove(appEventProxy);
}
public boolean fireAppEvent(String eventName, KrollDict data)
{
boolean handled = false;
for (WeakReference<KrollProxy> weakProxy : appEventProxies) {
KrollProxy appEventProxy = weakProxy.get();
if (appEventProxy == null) {
continue;
}
boolean proxyHandled = appEventProxy.fireEvent(eventName, data);
handled = handled || proxyHandled;
}
return handled;
}
/**
* @return the app's properties, which are listed in tiapp.xml.
* App properties can also be set at runtime by the application in Javascript.
* @module.api
*/
public TiProperties getAppProperties()
{
return appProperties;
}
public ITiAppInfo getAppInfo()
{
return appInfo;
}
/**
* @return the app's GUID. Each application has a unique GUID.
*/
public String getAppGUID()
{
return getAppInfo().getGUID();
}
public KrollDict getStylesheet(String basename, Collection<String> classes, String objectId)
{
if (stylesheet != null) {
return stylesheet.getStylesheet(objectId, classes, density, basename);
}
return null;
}
public void registerProxy(KrollProxy proxy)
{
String proxyId = proxy.getProxyId();
if (!proxyMap.containsKey(proxyId)) {
proxyMap.put(proxyId, new SoftReference<KrollProxy>(proxy));
}
}
public KrollProxy unregisterProxy(String proxyId)
{
KrollProxy proxy = null;
SoftReference<KrollProxy> ref = proxyMap.remove(proxyId);
if (ref != null) {
proxy = ref.get();
}
return proxy;
}
public boolean isAnalyticsEnabled()
{
return getAppInfo().isAnalyticsEnabled();
}
public boolean runOnMainThread()
{
return getAppProperties().getBool("run-on-main-thread", DEFAULT_RUN_ON_MAIN_THREAD);
}
public boolean intentFilterNewTask()
{
return getAppProperties().getBool("intent-filter-new-task", false);
}
public void setFilterAnalyticsEvents(String[] events)
{
filteredAnalyticsEvents = events;
}
public boolean isAnalyticsFiltered(String eventName)
{
if (filteredAnalyticsEvents == null) {
return false;
}
for (int i = 0; i < filteredAnalyticsEvents.length; ++i) {
String currentName = filteredAnalyticsEvents[i];
if (eventName.equals(currentName)) {
return true;
}
}
return false;
}
public String getDeployType()
{
return getAppInfo().getDeployType();
}
/**
* @return the build version, which is built in as part of the SDK.
*/
public String getTiBuildVersion()
{
return buildVersion;
}
public String getSDKVersion()
{
return getTiBuildVersion();
}
public String getTiBuildTimestamp()
{
return buildTimestamp;
}
public String getTiBuildHash()
{
return buildHash;
}
public String getDefaultUnit()
{
if (defaultUnit == null) {
defaultUnit = getAppProperties().getString(PROPERTY_DEFAULT_UNIT, SYSTEM_UNIT);
// Check to make sure default unit is valid, otherwise use system
Pattern unitPattern = Pattern.compile("system|px|dp|dip|mm|cm|in");
Matcher m = unitPattern.matcher(defaultUnit);
if (!m.matches()) {
defaultUnit = SYSTEM_UNIT;
}
}
return defaultUnit;
}
public int getThreadStackSize()
{
return getAppProperties().getInt(PROPERTY_THREAD_STACK_SIZE, DEFAULT_THREAD_STACK_SIZE);
}
public boolean forceCompileJS()
{
return getAppProperties().getBool(PROPERTY_COMPILE_JS, false);
}
public TiDeployData getDeployData()
{
return deployData;
}
public boolean isFastDevMode()
{
/* Fast dev is enabled by default in development mode, and disabled otherwise
* When the property is set, it overrides the default behavior on emulator only
* Deploy types are as follow:
* Emulator: 'development'
* Device: 'test'
*/
boolean development = getDeployType().equals(TiApplication.DEPLOY_TYPE_DEVELOPMENT);
if (!development) {
return false;
}
return getAppProperties().getBool(TiApplication.PROPERTY_FASTDEV, development);
}
public boolean isCoverageEnabled()
{
if (!getDeployType().equals(TiApplication.DEPLOY_TYPE_PRODUCTION)) {
return getAppProperties().getBool(TiApplication.PROPERTY_ENABLE_COVERAGE, false);
}
return false;
}
public void scheduleRestart(int delay)
{
Log.w(TAG, "Scheduling application restart");
if (Log.isDebugModeEnabled()) {
Log.d(TAG,
"Here is call stack leading to restart. (NOTE: this is not a real exception, just a stack trace.) :");
(new Exception()).printStackTrace();
}
this.restartPending = true;
TiRootActivity rootActivity = getRootActivity();
if (rootActivity != null) {
rootActivity.restartActivity(delay);
}
}
public boolean isRestartPending()
{
return restartPending;
}
public TiTempFileHelper getTempFileHelper()
{
return tempFileHelper;
}
/**
* @return true if the current thread is the main thread, false otherwise.
* @module.api
*/
public static boolean isUIThread()
{
if (mainThreadId == Thread.currentThread().getId()) {
return true;
}
return false;
}
public KrollModule getModuleByName(String name)
{
WeakReference<KrollModule> module = modules.get(name);
if (module == null) {
return null;
}
return module.get();
}
public void registerModuleInstance(String name, KrollModule module)
{
if (modules.containsKey(name)) {
Log.w(TAG, "Registering module with name already in use.");
}
modules.put(name, new WeakReference<KrollModule>(module));
}
public void waitForCurrentActivity(CurrentActivityListener l)
{
TiUIHelper.waitForCurrentActivity(l);
}
public boolean isDebuggerEnabled()
{
return getDeployData().isDebuggerEnabled();
}
private void startExternalStorageMonitor()
{
externalStorageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) {
responseCache.setCacheDir(getRemoteCacheDir());
TiResponseCache.setDefault(responseCache);
Log.i(TAG, "SD card has been mounted. Enabling cache for http responses.", Log.DEBUG_MODE);
} else {
// if the sd card is removed, we don't cache http responses
TiResponseCache.setDefault(null);
Log.i(TAG, "SD card has been unmounted. Disabling cache for http responses.", Log.DEBUG_MODE);
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
filter.addDataScheme("file");
registerReceiver(externalStorageReceiver, filter);
}
private void stopExternalStorageMonitor()
{
unregisterReceiver(externalStorageReceiver);
}
public void dispose()
{
TiActivityWindows.dispose();
TiActivitySupportHelpers.dispose();
TiFileHelper.getInstance().destroyTempFiles();
}
public void cancelTimers()
{
TitaniumModule.cancelTimers();
}
/**
* Our forced restarts (for conditions such as android bug 2373, TIMOB-1911 and TIMOB-7293)
* don't create new processes or pass through TiApplication() (the ctor). We need to reset
* some state to better mimic a complete application restart.
*/
public void beforeForcedRestart()
{
restartPending = false;
currentActivity = null;
TiApplication.isActivityTransition.set(false);
if (TiApplication.activityTransitionListeners != null) {
TiApplication.activityTransitionListeners.clear();
}
if (TiApplication.activityStack != null) {
TiApplication.activityStack.clear();
}
}
public AccessibilityManager getAccessibilityManager()
{
if (accessibilityManager == null) {
accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
}
return accessibilityManager;
}
public void setForceFinishRootActivity(boolean forced)
{
forceFinishRootActivity = forced;
}
public boolean getForceFinishRootActivity()
{
return forceFinishRootActivity;
}
public abstract void verifyCustomModules(TiRootActivity rootActivity);
}