Merge "Lazy initialization must be synchronized to avoid parallel instances cretation."

This commit is contained in:
Brad Fitzpatrick
2011-03-15 11:22:43 -07:00
committed by Android Code Review

View File

@@ -118,6 +118,8 @@ public class CatService extends Handler implements AppInterface {
private static IccRecords mIccRecords; private static IccRecords mIccRecords;
// Service members. // Service members.
// Protects singleton instance lazy initialization.
private static final Object sInstanceLock = new Object();
private static CatService sInstance; private static CatService sInstance;
private CommandsInterface mCmdIf; private CommandsInterface mCmdIf;
private Context mContext; private Context mContext;
@@ -515,26 +517,28 @@ public class CatService extends Handler implements AppInterface {
*/ */
public static CatService getInstance(CommandsInterface ci, IccRecords ir, public static CatService getInstance(CommandsInterface ci, IccRecords ir,
Context context, IccFileHandler fh, IccCard ic) { Context context, IccFileHandler fh, IccCard ic) {
if (sInstance == null) { synchronized (sInstanceLock) {
if (ci == null || ir == null || context == null || fh == null if (sInstance == null) {
|| ic == null) { if (ci == null || ir == null || context == null || fh == null
return null; || ic == null) {
} return null;
HandlerThread thread = new HandlerThread("Cat Telephony service"); }
thread.start(); HandlerThread thread = new HandlerThread("Cat Telephony service");
sInstance = new CatService(ci, ir, context, fh, ic); thread.start();
CatLog.d(sInstance, "NEW sInstance"); sInstance = new CatService(ci, ir, context, fh, ic);
} else if ((ir != null) && (mIccRecords != ir)) { CatLog.d(sInstance, "NEW sInstance");
CatLog.d(sInstance, "Reinitialize the Service with SIMRecords"); } else if ((ir != null) && (mIccRecords != ir)) {
mIccRecords = ir; CatLog.d(sInstance, "Reinitialize the Service with SIMRecords");
mIccRecords = ir;
// re-Register for SIM ready event. // re-Register for SIM ready event.
mIccRecords.registerForRecordsLoaded(sInstance, MSG_ID_ICC_RECORDS_LOADED, null); mIccRecords.registerForRecordsLoaded(sInstance, MSG_ID_ICC_RECORDS_LOADED, null);
CatLog.d(sInstance, "sr changed reinitialize and return current sInstance"); CatLog.d(sInstance, "sr changed reinitialize and return current sInstance");
} else { } else {
CatLog.d(sInstance, "Return current sInstance"); CatLog.d(sInstance, "Return current sInstance");
}
return sInstance;
} }
return sInstance;
} }
/** /**