diff --git a/res/values/strings.xml b/res/values/strings.xml
index de4e811a962..67765a0fe65 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -12311,7 +12311,7 @@
Cross SIM calling
- Allow another SIM calls and SMSs over this SIM.
+ Allow %1$s call over the default data SIM when %1$s is unavailable.
cross sim calling
diff --git a/src/com/android/settings/network/telephony/CrossSimCallingPreferenceController.java b/src/com/android/settings/network/telephony/CrossSimCallingPreferenceController.java
index 01c970779ad..f0b4e681eeb 100644
--- a/src/com/android/settings/network/telephony/CrossSimCallingPreferenceController.java
+++ b/src/com/android/settings/network/telephony/CrossSimCallingPreferenceController.java
@@ -17,12 +17,32 @@
package com.android.settings.network.telephony;
import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.ims.ImsException;
+import android.telephony.ims.ImsManager;
+import android.telephony.ims.ImsMmTelManager;
+import android.util.Log;
+
+import androidx.preference.Preference;
+import androidx.preference.SwitchPreference;
+
+import com.android.settings.R;
+
+import java.util.Objects;
/**
* Preference controller for "Cross SIM Calling"
**/
public class CrossSimCallingPreferenceController extends TelephonyTogglePreferenceController {
+ private static final String LOG_TAG = "CrossSimCallingPrefCtrl";
+
+ private int mSubId;
+ private Preference mPreference;
+
/**
* Class constructor of cross sim calling.
*
@@ -40,26 +60,101 @@ public class CrossSimCallingPreferenceController extends TelephonyTogglePreferen
* @return this instance after initialization
**/
public CrossSimCallingPreferenceController init(int subId) {
+ mSubId = subId;
return this;
}
@Override
public int getAvailabilityStatus(int subId) {
- return CONDITIONALLY_UNAVAILABLE;
+ return hasCrossSimCallingFeature(subId) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
/**
* Implementation of abstract methods
**/
public boolean setChecked(boolean isChecked) {
- return false;
+ ImsMmTelManager imsMmTelMgr = getImsMmTelManager();
+ if (imsMmTelMgr == null) {
+ return false;
+ }
+ try {
+ imsMmTelMgr.setCrossSimCallingEnabled(isChecked);
+ } catch (ImsException exception) {
+ Log.w(LOG_TAG, "fail to change cross SIM calling configuration: " + isChecked,
+ exception);
+ return false;
+ }
+ return true;
}
/**
* Implementation of abstract methods
**/
public boolean isChecked() {
+ ImsMmTelManager imsMmTelMgr = getImsMmTelManager();
+ if (imsMmTelMgr == null) {
+ return false;
+ }
+ try {
+ return imsMmTelMgr.isCrossSimCallingEnabledByUser();
+ } catch (ImsException exception) {
+ Log.w(LOG_TAG, "fail to get cross SIM calling configuration", exception);
+ }
return false;
}
+ @Override
+ public void updateState(Preference preference) {
+ super.updateState(preference);
+ if ((preference == null) || (!(preference instanceof SwitchPreference))) {
+ return;
+ }
+ mPreference = preference;
+
+ final SwitchPreference switchPreference = (SwitchPreference) preference;
+ switchPreference.setChecked(isChecked());
+
+ updateSummary(getLatestSummary());
+ }
+
+ private String getLatestSummary() {
+ SubscriptionInfo subInfo = getSubscriptionInfo();
+ return Objects.toString((subInfo == null) ? null : subInfo.getDisplayName(), "");
+ }
+
+ private void updateSummary(String displayName) {
+ Preference preference = mPreference;
+ if (preference == null) {
+ return;
+ }
+ String summary = displayName;
+ String finalText = String.format(
+ getResourcesForSubId().getString(R.string.cross_sim_calling_setting_summary),
+ summary)
+ .toString();
+ preference.setSummary(finalText);
+ }
+
+ private boolean hasCrossSimCallingFeature(int subscriptionId) {
+ PersistableBundle carrierConfig = getCarrierConfigForSubId(subscriptionId);
+ return (carrierConfig != null)
+ && carrierConfig.getBoolean(
+ CarrierConfigManager.KEY_CARRIER_CROSS_SIM_IMS_AVAILABLE_BOOL, false);
+ }
+
+ private ImsMmTelManager getImsMmTelManager() {
+ if (!SubscriptionManager.isUsableSubscriptionId(mSubId)) {
+ return null;
+ }
+ ImsManager imsMgr = mContext.getSystemService(ImsManager.class);
+ return (imsMgr == null) ? null : imsMgr.getImsMmTelManager(mSubId);
+ }
+
+ private SubscriptionInfo getSubscriptionInfo() {
+ SubscriptionManager subInfoMgr = mContext.getSystemService(SubscriptionManager.class);
+ if (subInfoMgr == null) {
+ return null;
+ }
+ return subInfoMgr.getActiveSubscriptionInfo(mSubId);
+ }
}
diff --git a/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java b/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java
index c9ce9b04b76..7fe5f1d99d6 100644
--- a/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java
+++ b/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java
@@ -17,6 +17,7 @@
package com.android.settings.network.telephony;
import android.content.Context;
+import android.content.res.Resources;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
@@ -79,4 +80,13 @@ public abstract class TelephonyTogglePreferenceController extends TogglePreferen
mContext.getSystemService(CarrierConfigManager.class);
return carrierConfigMgr.getConfigForSubId(subId);
}
+
+ /**
+ * Returns the resources associated with Subscription.
+ *
+ * @return Resources associated with Subscription.
+ */
+ public Resources getResourcesForSubId() {
+ return SubscriptionManager.getResourcesForSubId(mContext, mSubId);
+ }
}