1717
1818import java .io .StringReader ;
1919import java .util .ArrayList ;
20+ import java .util .Date ;
2021
2122class DIProfileManagerCommand extends DICommandBase {
2223 public class ErrorHolder
@@ -55,6 +56,8 @@ public class ErrorHolder
5556 // Provides full error description string
5657 public String msErrorString = "" ;
5758
59+ private boolean bInitializing = false ;
60+
5861 // Status Listener implementation (ensure that we retrieve the profile manager asynchronously
5962 EMDKManager .StatusListener mStatusListener = new EMDKManager .StatusListener () {
6063 @ Override
@@ -114,6 +117,9 @@ public void execute(String mxProfile, String mxProfileName, IDIResultCallbacks r
114117
115118 private void initializeEMDK ()
116119 {
120+ if (bInitializing )
121+ return ;
122+ bInitializing = true ;
117123 if (mEMDKManager == null )
118124 {
119125 EMDKResults results = null ;
@@ -126,6 +132,7 @@ private void initializeEMDK()
126132 {
127133 logMessage ("Error while requesting EMDKManager.\n " + e .getLocalizedMessage (), EMessageType .ERROR );
128134 e .printStackTrace ();
135+ waitForEMDK ();
129136 return ;
130137 }
131138
@@ -134,6 +141,7 @@ private void initializeEMDK()
134141 logMessage ("EMDKManager request command issued with success" , EMessageType .DEBUG );
135142 }else {
136143 logMessage ("EMDKManager request command error" , EMessageType .ERROR );
144+ waitForEMDK ();
137145 }
138146 }
139147 else
@@ -142,6 +150,29 @@ private void initializeEMDK()
142150 }
143151 }
144152
153+ private void waitForEMDK ()
154+ {
155+ Thread t = new Thread (new Runnable () {
156+ @ Override
157+ public void run () {
158+ long startDate = new Date ().getTime ();
159+ long delta = 0 ;
160+ long maxWait = 1000 * 60 * 10 ; // We will try for 10 mns... before stopping
161+ while (mEMDKManager == null || delta < DIHelper .MAX_EMDK_TIMEOUT_IN_MS ) {
162+ initializeEMDK ();
163+ try {
164+ Thread .sleep (5000 );
165+ } catch (InterruptedException e ) {
166+ e .printStackTrace ();
167+ }
168+ delta = new Date ().getTime () - startDate ;
169+ }
170+ }
171+ });
172+ t .setPriority (Thread .MIN_PRIORITY );
173+ t .start ();
174+ }
175+
145176 private void onEMDKManagerRetrieved (EMDKManager emdkManager )
146177 {
147178 mEMDKManager = emdkManager ;
@@ -186,6 +217,7 @@ private void releaseManagers()
186217 private void onProfileManagerInitialized (ProfileManager profileManager )
187218 {
188219 mProfileManager = profileManager ;
220+ bInitializing = false ;
189221 logMessage ("Profile Manager retrieved." , EMessageType .DEBUG );
190222 processMXContent ();
191223 }
0 commit comments