session-android/src/org/thoughtcrime/securesms/util/TelephonyUtil.java

43 lines
1.7 KiB
Java
Raw Normal View History

2014-08-15 00:23:03 +00:00
package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.util.Locale;
2014-08-15 00:23:03 +00:00
public class TelephonyUtil {
private static final String TAG = TelephonyUtil.class.getSimpleName();
public static TelephonyManager getManager(final Context context) {
return (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
}
2014-08-15 00:23:03 +00:00
public static String getMccMnc(final Context context) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final int configMcc = context.getResources().getConfiguration().mcc;
final int configMnc = context.getResources().getConfiguration().mnc;
if (tm.getSimState() == TelephonyManager.SIM_STATE_READY) {
Log.w(TAG, "Choosing MCC+MNC info from TelephonyManager.getSimOperator()");
return tm.getSimOperator();
} else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) {
Log.w(TAG, "Choosing MCC+MNC info from TelephonyManager.getNetworkOperator()");
return tm.getNetworkOperator();
} else if (configMcc != 0 && configMnc != 0) {
Log.w(TAG, "Choosing MCC+MNC info from current context's Configuration");
return String.format(Locale.ROOT, "%03d%d",
2014-08-15 00:23:03 +00:00
configMcc,
configMnc == Configuration.MNC_ZERO ? 0 : configMnc);
} else {
return null;
}
}
public static String getApn(final Context context) {
final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS).getExtraInfo();
}
}