Skip to content

Commit 96568cb

Browse files
committed
Merge pull request #56 from intercom/james/initialize
Removed race condition in pluginInitialize
2 parents ece64b0 + 7ea1b47 commit 96568cb

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/android/IntercomBridge.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,50 @@
3030
public class IntercomBridge extends CordovaPlugin {
3131

3232
@Override protected void pluginInitialize() {
33-
this.setUpIntercom();
34-
Bridge.getApi().ping();
33+
cordova.getActivity().runOnUiThread(new Runnable() {
34+
@Override public void run() {
35+
setUpIntercom();
36+
if (Bridge.getApi() != null) {
37+
Bridge.getApi().ping();
38+
}
39+
}
40+
});
3541
}
3642

3743
@Override public void onStart() {
38-
//We also initialize intercom here just in case it has died. If Intercom is already set up, this won't do anything.
39-
this.setUpIntercom();
44+
cordova.getActivity().runOnUiThread(new Runnable() {
45+
@Override public void run() {
46+
//We also initialize intercom here just in case it has died. If Intercom is already set up, this won't do anything.
47+
setUpIntercom();
4048

41-
if (Intercom.client().openGCMMessage(cordova.getActivity().getIntent().getData())) {
42-
cordova.getActivity().getIntent().setData(null);
43-
}
49+
if (Intercom.client().openGCMMessage(cordova.getActivity().getIntent().getData())) {
50+
cordova.getActivity().getIntent().setData(null);
51+
}
52+
}
53+
});
4454
}
4555

4656
@Override public void onNewIntent(Intent intent) {
4757
cordova.getActivity().setIntent(intent);
4858
}
4959

5060
private void setUpIntercom() {
51-
cordova.getActivity().runOnUiThread(new Runnable() {
52-
@Override public void run() {
53-
try {
54-
Context context = IntercomBridge.this.cordova.getActivity().getApplicationContext();
61+
try {
62+
Context context = IntercomBridge.this.cordova.getActivity().getApplicationContext();
5563

56-
HeaderInterceptor.setCordovaVersion(context, "1.1.6");
64+
HeaderInterceptor.setCordovaVersion(context, "1.1.6");
5765

58-
ApplicationInfo app = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
59-
Bundle bundle = app.metaData;
66+
ApplicationInfo app = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
67+
Bundle bundle = app.metaData;
6068

61-
//Get app credentials from config.xml or the app bundle if they can't be found
62-
String apiKey = IntercomBridge.this.preferences.getString("intercom-android-api-key", bundle.getString("intercom_api_key"));
63-
String appId = IntercomBridge.this.preferences.getString("intercom-app-id", bundle.getString("intercom_app_id"));
69+
//Get app credentials from config.xml or the app bundle if they can't be found
70+
String apiKey = IntercomBridge.this.preferences.getString("intercom-android-api-key", bundle.getString("intercom_api_key"));
71+
String appId = IntercomBridge.this.preferences.getString("intercom-app-id", bundle.getString("intercom_app_id"));
6472

65-
Intercom.initialize(IntercomBridge.this.cordova.getActivity().getApplication(), apiKey, appId);
66-
} catch (Exception e) {
67-
System.err.println("[Intercom-Cordova] ERROR: Something went wrong when initializing Intercom. Have you set your APP_ID and ANDROID_API_KEY?");
68-
}
69-
}
70-
});
73+
Intercom.initialize(IntercomBridge.this.cordova.getActivity().getApplication(), apiKey, appId);
74+
} catch (Exception e) {
75+
System.err.println("[Intercom-Cordova] ERROR: Something went wrong when initializing Intercom. Have you set your APP_ID and ANDROID_API_KEY?");
76+
}
7177
}
7278

7379
private enum Action {

0 commit comments

Comments
 (0)