package dttc.com.main;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.NetworkInfo.State;
import android.telephony.TelephonyManager;
/**
* @description 管理APN的一些操作:插入更改
* @author tangzhi
* @date 20120705
* */
public class APNManager
{
/***APN---URI----DEFINE**/
private static final Uri APN_URI = Uri.parse("content://telephony/carriers");
private static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
/**
* @Description 插入apn节点 ,其中MCC 移动国家好为460,MNC移动网号为00
* @param context 上下文环境
* @param newAPNName 待插入APN的名称
* @param newapn 待插入的APN节点
* @param proxy 代理服务器IP
* @param port 代理服务端口
* @return true 插入成功,false插入失败
*
* */
public static boolean InsertAPNNode(Context context,String newAPNName,String newapn,String proxy,String port)
{
boolean localState = false;
String localApn = newapn;
int id = -1;
String localmnc = null;
String localNumeric = null;
TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imsi = mTelephonyMgr.getSubscriberId();
printLog.print("获取到的IMSI为:" + imsi);
if(imsi != null)
{
if(imsi.startsWith("46000") || imsi.startsWith("46002"))
{
localmnc = imsi.substring(3,5);
localNumeric =imsi.substring(0,5);
printLog.print("中国移动");
}
else
{
localmnc = "00";
localNumeric = "46000";
printLog.print("不是中国移动");
}
}
else
{
}
printLog.print("MNC的值为:" + localmnc);
printLog.print("numeric的值为:" + localNumeric);
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put("name",newAPNName);
values.put("apn",localApn);
values.put("mcc", "460");
values.put("mnc", localmnc);
values.put("numeric", localNumeric);
values.put("proxy", "");
values.put("port", "");
values.put("mmsc", "");
Cursor c = null;
try
{
Uri newRow = resolver.insert(APN_URI, values);
if(newRow != null)
{
c = resolver.query(newRow, null, null, null, null);
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idindex);
localState = true;
}
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
finally
{
if(c !=null )
c.close();
}
return localState;
}
/**
* @description 切换APN
* @param context 上下文环境
* @param newAPN 要更新的APN
* @param true 更新成功,false更新失败
* **/
public static boolean updateCurrentAPN(Context context, String newAPN)
{
Cursor cursor = null;
ContentResolver resolver = context.getContentResolver();
boolean localState = true;
String apnId = null;
int index = 0;
String localApn = newAPN;
try
{
do
{
cursor = resolver.query(APN_URI, null, "apn = ? and current = 1", new String []{localApn},null);
if(cursor != null && cursor.moveToFirst())
{
delayTime(1000);
apnId = cursor.getString(cursor.getColumnIndex("_id"));
printLog.print("apnId == " + apnId);
index ++;
if(index == 6)
{
break;
}
else
{
}
}
else
{
}
}while(apnId == null);
cursor.close();
if(apnId != null)
{
ContentValues values = new ContentValues();
values.put("apn_id", apnId);
resolver.update(CURRENT_APN_URI, values, null, null);
}
else
{
localState = false ;
}
}
catch(SQLException e)
{
e.getStackTrace();
localState = false;
}
finally
{
if(cursor != null)
{
cursor.close();
}
}
return localState;
}
/**
* @description 删除APN节点
* @param context 上下文环境
* @param APNName APN名称
* @return void
* */
public static void deleteAPN(Context context,String APNName) //delete a apn
{
printLog.print("开始删除APN节点" + APNName);
String localWhere = "'" + APNName + "'";
try
{
context.getContentResolver().delete(APN_URI, "name=" + localWhere, null);
}
catch(Exception e)
{
e.printStackTrace();
}
printLog.print("删除成功");
}
/**
* @description 检测APN节点是否存在
* @param context 上下文环境
* @param apnNode APN节点
* @return true 存在 ,false不存在
* */
public static boolean checkAPN(Context context,String apnNode)
{
Cursor cursor_need = null;
boolean localState = false;
try
{
cursor_need = context.getContentResolver().query(APN_URI, null, "apn = \'"+ apnNode + "\' and current = 1",null,null);
if((cursor_need != null) && (cursor_need.moveToFirst()))
{
localState = true;
}
else
{
localState = false;
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(cursor_need != null)
{
cursor_need.close();
}
}
return localState;
}
/**
* @description 获取当前APN的类型
* @param context 上下文环境
* @return String 返回APN类型
* **/
public static String getCurrentApnType(Context context)
{
String apn = null;
ConnectivityManager conManager = null;
try
{
conManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = conManager.getActiveNetworkInfo();
apn = ni.getExtraInfo().toLowerCase();
}
catch(Exception e)
{
e.printStackTrace();
}
return apn;
}
/**
* @description 检测手机联网状态
* @param context 上下文环境
* @return true 联网成功,false联网失败
*
* **/
public static boolean checkNetState(Context context)
{
boolean mState = false;
boolean SUCCESS = true;
boolean FAILURE = false;
State state = null;
ConnectivityManager connManager = null;
try
{
connManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
}
catch(Exception e)
{
e.printStackTrace();
}