diff --git a/src/com/github/iusmac/sevensim/telephony/SubscriptionController.java b/src/com/github/iusmac/sevensim/telephony/SubscriptionController.java index e2bae32..48d7abd 100644 --- a/src/com/github/iusmac/sevensim/telephony/SubscriptionController.java +++ b/src/com/github/iusmac/sevensim/telephony/SubscriptionController.java @@ -71,6 +71,9 @@ public void setUiccApplicationsEnabled(final int subId, final boolean enabled) { return; } + // Ensure SIM subscription syncing cannot start in parallel during this operation + mSubscriptions.mBlockSubscriptionsSyncFlag.set(true); + sub.setSimState(TelephonyUtils.simStateInt(enabled)); sub.setLastActivatedTime(enabled ? LocalDateTime.now(ZoneId.systemDefault()) : LocalDateTime.MIN); @@ -79,5 +82,7 @@ public void setUiccApplicationsEnabled(final int subId, final boolean enabled) { mSubscriptions.persistSubscription(sub); mSubManager.setUiccApplicationsEnabled(subId, enabled); + + mSubscriptions.mBlockSubscriptionsSyncFlag.set(false); } } diff --git a/src/com/github/iusmac/sevensim/telephony/Subscriptions.java b/src/com/github/iusmac/sevensim/telephony/Subscriptions.java index 6999717..5899e0c 100644 --- a/src/com/github/iusmac/sevensim/telephony/Subscriptions.java +++ b/src/com/github/iusmac/sevensim/telephony/Subscriptions.java @@ -99,6 +99,12 @@ public void onReceive(final Context context, final Intent intent) { */ private final AtomicBoolean mCarrierConfigChangedReceiverRegistered = new AtomicBoolean(); + /** + * Atomic flag indicating whether the synchronization of the internal state of all SIM + * subscriptions is blocked or not. Use this to ensure atomic SIM subscription state mutations. + */ + final AtomicBoolean mBlockSubscriptionsSyncFlag = new AtomicBoolean(); + private final Context mContext; protected final Logger mLogger; protected final SubscriptionManager mSubscriptionManager; @@ -295,6 +301,11 @@ public void removeOnSubscriptionsChangedListener( */ @WorkerThread public void syncSubscriptions(final LocalDateTime dateTime) { + if (mBlockSubscriptionsSyncFlag.get()) { + mLogger.w("syncSubscriptions(dateTime=%s) : Operation has been disallowed.", dateTime); + return; + } + final List removedSubIds = new ArrayList<>(getPersistedUsableSubIds()); final List usableSubIds = new ArrayList<>(); diff --git a/src/com/github/iusmac/sevensim/telephony/TelephonyController.java b/src/com/github/iusmac/sevensim/telephony/TelephonyController.java index c309bda..27ea610 100644 --- a/src/com/github/iusmac/sevensim/telephony/TelephonyController.java +++ b/src/com/github/iusmac/sevensim/telephony/TelephonyController.java @@ -135,6 +135,9 @@ public void setSimState(final int slotIndex, final boolean enabled, return; } + // Ensure SIM subscription syncing cannot start in parallel during this operation + mSubscriptions.mBlockSubscriptionsSyncFlag.set(true); + // Keep track of SIM state whenever it's mutated. This will be persisted in a volatile // memory, so that we can further restore all relevant data. This because when powering // down the SIM is the same as removing it, which means the SIM will completely @@ -226,6 +229,8 @@ public void setSimState(final int slotIndex, final boolean enabled, } handleOnSetSimPowerStateForSlotFinished(resCode); } + + mSubscriptions.mBlockSubscriptionsSyncFlag.set(false); } }