/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.telephony; import android.annotation.NonNull; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; import android.net.Uri; import android.telephony.Rlog; import android.os.Handler; import android.os.Message; import android.os.ServiceManager; import android.os.RemoteException; import android.util.DisplayMetrics; import com.android.internal.telephony.ISub; import com.android.internal.telephony.IOnSubscriptionsChangedListener; import com.android.internal.telephony.ITelephonyRegistry; import com.android.internal.telephony.PhoneConstants; import java.util.ArrayList; import java.util.List; /** * SubscriptionManager is the application interface to SubscriptionController * and provides information about the current Telephony Subscriptions. * *
* You do not instantiate this class directly; instead, you retrieve * a reference to an instance through {@link #from}. *
* All SDK public methods require android.Manifest.permission.READ_PHONE_STATE. */ public class SubscriptionManager { private static final String LOG_TAG = "SubscriptionManager"; private static final boolean DBG = false; private static final boolean VDBG = false; /** An invalid subscription identifier */ /** @hide */ public static final int INVALID_SUBSCRIPTION_ID = -1; /** Base value for Dummy SUBSCRIPTION_ID's. */ /** FIXME: Remove DummySubId's, but for now have them map just below INVALID_SUBSCRIPTION_ID /** @hide */ public static final int DUMMY_SUBSCRIPTION_ID_BASE = INVALID_SUBSCRIPTION_ID - 1; /** An invalid phone identifier */ /** @hide */ public static final int INVALID_PHONE_INDEX = -1; /** An invalid slot identifier */ /** @hide */ public static final int INVALID_SIM_SLOT_INDEX = -1; /** Indicates the caller wants the default sub id. */ /** @hide */ public static final int DEFAULT_SUBSCRIPTION_ID = Integer.MAX_VALUE; /** * Indicates the caller wants the default phone id. * Used in SubscriptionController and PhoneBase but do we really need it??? * @hide */ public static final int DEFAULT_PHONE_INDEX = Integer.MAX_VALUE; /** Indicates the caller wants the default slot id. NOT used remove? */ /** @hide */ public static final int DEFAULT_SIM_SLOT_INDEX = Integer.MAX_VALUE; /** Minimum possible subid that represents a subscription */ /** @hide */ public static final int MIN_SUBSCRIPTION_ID_VALUE = 0; /** Maximum possible subid that represents a subscription */ /** @hide */ public static final int MAX_SUBSCRIPTION_ID_VALUE = DEFAULT_SUBSCRIPTION_ID - 1; /** @hide */ public static final Uri CONTENT_URI = Uri.parse("content://telephony/siminfo"); /** * TelephonyProvider unique key column name is the subscription id. *
Type: TEXT (String)
*/ /** @hide */ public static final String UNIQUE_KEY_SUBSCRIPTION_ID = "_id"; /** * TelephonyProvider column name for SIM ICC Identifier *Type: TEXT (String)
*/ /** @hide */ public static final String ICC_ID = "icc_id"; /** * TelephonyProvider column name for user SIM_SlOT_INDEX *Type: INTEGER (int)
*/ /** @hide */ public static final String SIM_SLOT_INDEX = "sim_id"; /** SIM is not inserted */ /** @hide */ public static final int SIM_NOT_INSERTED = -1; /** * The Network mode of SIM/sub. *Type: INTEGER (int)
*/ /** @hide */ public static final String NETWORK_MODE = "network_mode"; /** * The user configured Network mode of SIM/sub. *Type: INTEGER (int)
* {@hide} */ public static final String USER_NETWORK_MODE = "user_network_mode"; /** @hide */ public static final int DEFAULT_NW_MODE = -1; /** * The activation state of SIM/sub. *Type: INTEGER (int)
*/ /** @hide */ public static final String SUB_STATE = "sub_state"; /** @hide */ public static final int INACTIVE = 0; /** @hide */ public static final int ACTIVE = 1; /** @hide */ public static final int SUB_CONFIGURATION_IN_PROGRESS = 2; /** * TelephonyProvider column name for user displayed name. *Type: TEXT (String)
*/ /** @hide */ public static final String DISPLAY_NAME = "display_name"; /** * TelephonyProvider column name for the service provider name for the SIM. *Type: TEXT (String)
*/ /** @hide */ public static final String CARRIER_NAME = "carrier_name"; /** * Default name resource * @hide */ public static final int DEFAULT_NAME_RES = com.android.internal.R.string.unknownName; /** * TelephonyProvider column name for source of the user displayed name. *Type: INT (int)
with one of the NAME_SOURCE_XXXX values below * * @hide */ public static final String NAME_SOURCE = "name_source"; /** * The name_source is undefined * @hide */ public static final int NAME_SOURCE_UNDEFINDED = -1; /** * The name_source is the default * @hide */ public static final int NAME_SOURCE_DEFAULT_SOURCE = 0; /** * The name_source is from the SIM * @hide */ public static final int NAME_SOURCE_SIM_SOURCE = 1; /** * The name_source is from the user * @hide */ public static final int NAME_SOURCE_USER_INPUT = 2; /** * TelephonyProvider column name for the color of a SIM. *Type: INTEGER (int)
*/ /** @hide */ public static final String COLOR = "color"; /** @hide */ public static final int COLOR_1 = 0; /** @hide */ public static final int COLOR_2 = 1; /** @hide */ public static final int COLOR_3 = 2; /** @hide */ public static final int COLOR_4 = 3; /** @hide */ public static final int COLOR_DEFAULT = COLOR_1; /** * TelephonyProvider column name for the phone number of a SIM. *Type: TEXT (String)
*/ /** @hide */ public static final String NUMBER = "number"; /** * TelephonyProvider column name for the number display format of a SIM. *Type: INTEGER (int)
*/ /** @hide */ public static final String DISPLAY_NUMBER_FORMAT = "display_number_format"; /** @hide */ public static final int DISPLAY_NUMBER_NONE = 0; /** @hide */ public static final int DISPLAY_NUMBER_FIRST = 1; /** @hide */ public static final int DISPLAY_NUMBER_LAST = 2; /** @hide */ public static final int DISPLAY_NUMBER_DEFAULT = DISPLAY_NUMBER_FIRST; /** * TelephonyProvider column name for permission for data roaming of a SIM. *Type: INTEGER (int)
*/ /** @hide */ public static final String DATA_ROAMING = "data_roaming"; /** Indicates that data roaming is enabled for a subscription */ public static final int DATA_ROAMING_ENABLE = 1; /** Indicates that data roaming is disabled for a subscription */ public static final int DATA_ROAMING_DISABLE = 0; /** @hide */ public static final int DATA_ROAMING_DEFAULT = DATA_ROAMING_DISABLE; /** * TelephonyProvider column name for the MCC associated with a SIM. *Type: INTEGER (int)
* @hide */ public static final String MCC = "mcc"; /** * TelephonyProvider column name for the MNC associated with a SIM. *Type: INTEGER (int)
* @hide */ public static final String MNC = "mnc"; /** * TelephonyProvider column name for extreme threat in CB settings * @hide */ public static final String CB_EXTREME_THREAT_ALERT = "enable_cmas_extreme_threat_alerts"; /** * TelephonyProvider column name for severe threat in CB settings *@hide */ public static final String CB_SEVERE_THREAT_ALERT = "enable_cmas_severe_threat_alerts"; /** * TelephonyProvider column name for amber alert in CB settings *@hide */ public static final String CB_AMBER_ALERT = "enable_cmas_amber_alerts"; /** * TelephonyProvider column name for emergency alert in CB settings *@hide */ public static final String CB_EMERGENCY_ALERT = "enable_emergency_alerts"; /** * TelephonyProvider column name for alert sound duration in CB settings *@hide */ public static final String CB_ALERT_SOUND_DURATION = "alert_sound_duration"; /** * TelephonyProvider column name for alert reminder interval in CB settings *@hide */ public static final String CB_ALERT_REMINDER_INTERVAL = "alert_reminder_interval"; /** * TelephonyProvider column name for enabling vibrate in CB settings *@hide */ public static final String CB_ALERT_VIBRATE = "enable_alert_vibrate"; /** * TelephonyProvider column name for enabling alert speech in CB settings *@hide */ public static final String CB_ALERT_SPEECH = "enable_alert_speech"; /** * TelephonyProvider column name for ETWS test alert in CB settings *@hide */ public static final String CB_ETWS_TEST_ALERT = "enable_etws_test_alerts"; /** * TelephonyProvider column name for enable channel50 alert in CB settings *@hide */ public static final String CB_CHANNEL_50_ALERT = "enable_channel_50_alerts"; /** * TelephonyProvider column name for enable channel60 alert in CB settings *@hide */ public static final String CB_CHANNEL_60_ALERT = "enable_channel_60_alerts"; /** * TelephonyProvider column name for CMAS test alert in CB settings *@hide */ public static final String CB_CMAS_TEST_ALERT= "enable_cmas_test_alerts"; /** * TelephonyProvider column name for Opt out dialog in CB settings *@hide */ public static final String CB_OPT_OUT_DIALOG = "show_cmas_opt_out_dialog"; /** * Broadcast Action: The user has changed one of the default subs related to * data, phone calls, or sms * * TODO: Change to a listener * @hide */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SUB_DEFAULT_CHANGED_ACTION = "android.intent.action.SUB_DEFAULT_CHANGED"; private final Context mContext; /** * A listener class for monitoring changes to {@link SubscriptionInfo} records. ** Override the onSubscriptionsChanged method in the object that extends this * class and pass it to {@link #addOnSubscriptionsChangedListener(OnSubscriptionsChangedListener)} * to register your listener and to unregister invoke * {@link #removeOnSubscriptionsChangedListener(OnSubscriptionsChangedListener)} *
* Permissions android.Manifest.permission.READ_PHONE_STATE is required
* for #onSubscriptionsChanged to be invoked.
*/
public static class OnSubscriptionsChangedListener {
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (DBG) {
log("handleMessage: invoke the overriden onSubscriptionsChanged()");
}
OnSubscriptionsChangedListener.this.onSubscriptionsChanged();
}
};
/**
* Callback invoked when there is any change to any SubscriptionInfo. Typically
* this method would invoke {@link #getActiveSubscriptionInfoList}
*/
public void onSubscriptionsChanged() {
if (DBG) log("onSubscriptionsChanged: NOT OVERRIDDEN");
}
/**
* The callback methods need to be called on the handler thread where
* this object was created. If the binder did that for us it'd be nice.
*/
IOnSubscriptionsChangedListener callback = new IOnSubscriptionsChangedListener.Stub() {
@Override
public void onSubscriptionsChanged() {
if (DBG) log("callback: received, sendEmptyMessage(0) to handler");
mHandler.sendEmptyMessage(0);
}
};
private void log(String s) {
Rlog.d(LOG_TAG, s);
}
}
/** @hide */
public SubscriptionManager(Context context) {
if (DBG) logd("SubscriptionManager created");
mContext = context;
}
/**
* Get an instance of the SubscriptionManager from the Context.
* This invokes {@link android.content.Context#getSystemService
* Context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)}.
*
* @param context to use.
* @return SubscriptionManager instance
*/
public static SubscriptionManager from(Context context) {
return (SubscriptionManager) context.getSystemService(
Context.TELEPHONY_SUBSCRIPTION_SERVICE);
}
/**
* Register for changes to the list of active {@link SubscriptionInfo} records or to the
* individual records themselves. When a change occurs the onSubscriptionsChanged method of
* the listener will be invoked immediately if there has been a notification.
*
* @param listener an instance of {@link OnSubscriptionsChangedListener} with
* onSubscriptionsChanged overridden.
*/
public void addOnSubscriptionsChangedListener(OnSubscriptionsChangedListener listener) {
String pkgForDebug = mContext != null ? mContext.getOpPackageName() : "
* Availability: Only when user registered to a network.
*
* @param subId The subscription ID
* @return true if the network for the subscription is roaming, false otherwise
*/
public boolean isNetworkRoaming(int subId) {
final int phoneId = getPhoneId(subId);
if (phoneId < 0) {
// What else can we do?
return false;
}
return TelephonyManager.getDefault().isNetworkRoaming(subId);
}
/**
* Returns a constant indicating the state of sim for the slot idx.
*
* @param slotIdx
*
* {@See TelephonyManager#SIM_STATE_UNKNOWN}
* {@See TelephonyManager#SIM_STATE_ABSENT}
* {@See TelephonyManager#SIM_STATE_PIN_REQUIRED}
* {@See TelephonyManager#SIM_STATE_PUK_REQUIRED}
* {@See TelephonyManager#SIM_STATE_NETWORK_LOCKED}
* {@See TelephonyManager#SIM_STATE_READY}
* {@See TelephonyManager#SIM_STATE_NOT_READY}
* {@See TelephonyManager#SIM_STATE_PERM_DISABLED}
* {@See TelephonyManager#SIM_STATE_CARD_IO_ERROR}
*
* {@hide}
*/
public static int getSimStateForSlotIdx(int slotIdx) {
int simState = TelephonyManager.SIM_STATE_UNKNOWN;
try {
ISub iSub = ISub.Stub.asInterface(ServiceManager.getService("isub"));
if (iSub != null) {
simState = iSub.getSimStateForSlotIdx(slotIdx);
}
} catch (RemoteException ex) {
}
logd("getSimStateForSubscriber: simState=" + simState + " slotIdx=" + slotIdx);
return simState;
}
/**
* Store properties associated with SubscriptionInfo in database
* @param subId Subscription Id of Subscription
* @param propKey Column name in database associated with SubscriptionInfo
* @param propValue Value to store in DB for particular subId & column name
* @hide
*/
public static void setSubscriptionProperty(int subId, String propKey, String propValue) {
try {
ISub iSub = ISub.Stub.asInterface(ServiceManager.getService("isub"));
if (iSub != null) {
iSub.setSubscriptionProperty(subId, propKey, propValue);
}
} catch (RemoteException ex) {
// ignore it
}
}
/**
* Store properties associated with SubscriptionInfo in database
* @param subId Subscription Id of Subscription
* @param propKey Column name in SubscriptionInfo database
* @return Value associated with subId and propKey column in database
* @hide
*/
private static String getSubscriptionProperty(int subId, String propKey,
Context context) {
String resultValue = null;
try {
ISub iSub = ISub.Stub.asInterface(ServiceManager.getService("isub"));
if (iSub != null) {
resultValue = iSub.getSubscriptionProperty(subId, propKey,
context.getOpPackageName());
}
} catch (RemoteException ex) {
// ignore it
}
return resultValue;
}
/**
* Returns boolean value corresponding to query result.
* @param subId Subscription Id of Subscription
* @param propKey Column name in SubscriptionInfo database
* @param defValue Default boolean value to be returned
* @return boolean result value to be returned
* @hide
*/
public static boolean getBooleanSubscriptionProperty(int subId, String propKey,
boolean defValue, Context context) {
String result = getSubscriptionProperty(subId, propKey, context);
if (result != null) {
try {
return Integer.parseInt(result) == 1;
} catch (NumberFormatException err) {
logd("getBooleanSubscriptionProperty NumberFormat exception");
}
}
return defValue;
}
/**
* Returns integer value corresponding to query result.
* @param subId Subscription Id of Subscription
* @param propKey Column name in SubscriptionInfo database
* @param defValue Default integer value to be returned
* @return integer result value to be returned
* @hide
*/
public static int getIntegerSubscriptionProperty(int subId, String propKey, int defValue,
Context context) {
String result = getSubscriptionProperty(subId, propKey, context);
if (result != null) {
try {
return Integer.parseInt(result);
} catch (NumberFormatException err) {
logd("getBooleanSubscriptionProperty NumberFormat exception");
}
}
return defValue;
}
/**
* Returns the resources associated with Subscription.
* @param context Context object
* @param subId Subscription Id of Subscription who's resources are required
* @return Resources associated with Subscription.
* @hide
*/
public static Resources getResourcesForSubId(Context context, int subId) {
final SubscriptionInfo subInfo =
SubscriptionManager.from(context).getActiveSubscriptionInfo(subId);
Configuration config = context.getResources().getConfiguration();
Configuration newConfig = new Configuration();
newConfig.setTo(config);
if (subInfo != null) {
newConfig.mcc = subInfo.getMcc();
newConfig.mnc = subInfo.getMnc() == 0 ? Configuration.MNC_ZERO : subInfo.getMnc();
}
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
DisplayMetrics newMetrics = new DisplayMetrics();
newMetrics.setTo(metrics);
return new Resources(context.getResources().getAssets(), newMetrics, newConfig);
}
/**
* @return true if the sub ID is active. i.e. The sub ID corresponds to a known subscription
* and the SIM providing the subscription is present in a slot and in "LOADED" state.
* @hide
*/
public boolean isActiveSubId(int subId) {
try {
ISub iSub = ISub.Stub.asInterface(ServiceManager.getService("isub"));
if (iSub != null) {
return iSub.isActiveSubId(subId);
}
} catch (RemoteException ex) {
}
return false;
}
}
*
*/
public List