APN 开关实现原理

关闭APN的原理是在APN信息表(content://telephony/carriers/current )的apn, type字段添加自定义的后缀(参考自APNDroid

package com.android.settings.widget;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

/**
*UtilityclassforchangingAPNstate
*/
public class ApnUtils{
private static final StringSUFFIX_APN = " _suffix_apn " ; // type1

private static final StringMMS = " mms " ;

private static final StringCOLUMN_ID = BaseColumns._ID;
private static final StringCOLUMN_APN = " apn " ;
private static final StringCOLUMN_TYPE = " type " ;
private static final StringCOLUMN_APN_ID = " apn_id " ; // forpreferredAPN

private static final String[]PROJECTION = new String[]{COLUMN_ID,
COLUMN_APN,COLUMN_TYPE};
private static final UriCURRENT_APNS = Uri
.parse(
" content://telephony/carriers/current " );
private static final UriPREFERRED_APN = Uri
.parse(
" content://telephony/carriers/preferapn " );

/**
*GetsthestateofAPN
*
*
@param context
*
@return trueifenabled
*/
public static boolean getApnState(Contextcontext){

ContentResolverresolver
= context.getContentResolver();
int counter = 0 ;
Cursorcursor
= null ;
try {
cursor
= resolver.query(CURRENT_APNS,PROJECTION, null , null , null );
int typeIndex = cursor.getColumnIndex(COLUMN_TYPE);
cursor.moveToNext();
while ( ! cursor.isAfterLast()){
Stringtype
= cursor.getString(typeIndex);

if (isDisabled(type)){
return false ; // noneedtocontinue
}

if ( ! isMms(type) || /* thisisMMMandwe */ shouldDisableMms()){
counter
++ ;
}

cursor.moveToNext();
}
}
finally {
if (cursor != null ){
cursor.close();
}
}
return counter != 0 ;
}

private static boolean isMms(Stringtype){
return type != null && type.toLowerCase().endsWith(MMS);
}

private static boolean shouldDisableMms(){
// TODOAuto-generatedmethodstub
return false ;
}

private static boolean isDisabled(Stringvalue){
return value != null && value.endsWith(SUFFIX_APN);
}

/**
*SetAPNState
*
*
@param context
*
@param enabled
*/
public static void setApnState(Contextcontext, boolean enabled){
boolean shouldDisableMms = shouldDisableMms();

ContentResolverresolver
= context.getContentResolver();
ContentValuesvalues
= new ContentValues();
Cursorcursor
= null ;

String[]args
= new String[ 1 ];
try {
// COLUMN_ID{0},COLUMN_APN{1},COLUMN_TYPE{2}
cursor = resolver.query(CURRENT_APNS,PROJECTION, null , null , null );
int idIndex = cursor.getColumnIndex(COLUMN_ID);
int apnIndex = cursor.getColumnIndex(COLUMN_APN);
int typeIndex = cursor.getColumnIndex(COLUMN_TYPE);
cursor.moveToFirst();
while ( ! cursor.isAfterLast()){

StringtypeValue
= cursor.getString(typeIndex);
if ( ! enabled // weshoulddisable
&& isMms(typeValue) // andthisisMMS
&& ! shouldDisableMms){ // butusersdisallowusto
// disableMMS
// ignore
cursor.moveToNext();
continue ;
}

args[
0 ] = String.valueOf(cursor.getInt(idIndex)); // id
values.put(COLUMN_APN,getAdaptedValue(cursor.getString(apnIndex),enabled));
values.put(COLUMN_TYPE,getAdaptedValue(typeValue,enabled));

resolver.update(CURRENT_APNS,values,COLUMN_ID
+ " =? " ,args);

// movetonext
cursor.moveToNext();
}

}
catch (Exceptione){
throw new RuntimeException(e);
}
finally {
if (cursor != null ){
cursor.close();
}
}
}

private static StringgetAdaptedValue(Stringvalue, boolean enable){

final Stringmodifier = SUFFIX_APN;

// handlenull-value
if (value == null ) return enable ? value:modifier;

// removeanymodifiersothatvaluebecomesenabledinanycase
value = removeModifiers(value);

if ( ! enable){ // addrequiredmodifier
value = addModifier(value);
}

return value;
}

private static StringremoveModifiers(Stringvalue){
if (value.endsWith(SUFFIX_APN))
return value.substring( 0 ,value.length() - SUFFIX_APN.length());
else
return value;
}

private static StringaddModifier(Stringvalue){
return value + SUFFIX_APN;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值