介绍
此功能是当信号低于多少dbm时切换另一张数据卡的功能
修改
首先定义开关字符串英文标题和总结名称
路径:vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values/strings.xml
<!-- soda water.20230912 add Intelligent switching data card switch -->
<string name="intelligent_data">Allow Mobile Data Switching</string>
<string name="intelligent_data_summary">Turning this feature on will allow your phone to use mobile data from both SIMs depending on coverage and availability</string>
添加中文
路径:vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values-zh-rCN/strings.xml
<!-- soda water.20230912 add Intelligent switching data card switch -->
<string name="intelligent_data">允许切换数据网络</string>
<string name="intelligent_data_summary">打开此功能将允许手机根据覆盖率和可用情况,自由使用两个号码的数据网络</string>
添加开关布局
路径:/vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/sim_settings.xml
<!-- soda water.20230912 add Intelligent switching data card switch -->
<SwitchPreference
android:key="Intelligent_data"
android:title="@string/intelligent_data"
android:summary="@string/intelligent_data_summary"
android:persistent="false"/>
<!-- soda water.20230912 add Intelligent switching data card switch -->
定义一个系统属性保存开关状态
路径:vendor/freeme/frameworks/base/core-export/java/com/freeme/provider/FreemeSettings.java
//*/soda water.20230912 add Intelligent switching data card switch
public static final String INTELLIGENT_SWITCH_DATA = "intelligent_switching_data";
//*/
绑定开关控件并进行监听
路径:vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/sim/SimSettings.java
//soda water.20230912 add Intelligent switching data card switch
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.SwitchPreference;
import android.provider.Settings;
import com.freeme.provider.FreemeSettings;
//*/
//*/soda water.20230912 add Intelligent switching data card switch
private SwitchPreference mIntelligentData;
private static final String INTELLIGENT_DATA_KEY = "Intelligent_data";
//*/
//*/soda water.20230912 add Intelligent switching data card switch
boolean mIntelligentDataState = Settings.System.getInt(mContext.getContentResolver(),FreemeSettings.System.INTELLIGENT_SWITCH_DATA, 0) ==1;
mIntelligentData = (SwitchPreference) findPreference(INTELLIGENT_DATA_KEY);
mIntelligentData.setChecked(mIntelligentDataState);
mIntelligentData.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
Settings.System.putInt(getContentResolver(),FreemeSettings.System.INTELLIGENT_SWITCH_DATA, (Boolean) value ? 1 : 0);
Log.d("soda water check=",String.valueOf(value));
return true;
}
});
//*/
以上就是开关的建立 接着进行功能的实现
首先定义我们后续要用到的变量
路径:vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
//*/soda water.20230912 Intelligent switching data card
public static int subID1 = -1;
public static int subID2 = -1;
public static int subIDDbm1 = 0;
public static int subIDDbm2 = 0;
public static int subIDDbmIndex1 = 0;
public static int subIDDbmIndex2 = 0;
public static int subIDDbmArrays1[]= new int[5];
public static int subIDDbmArrays2[] = new int[5];
public static long startSwitchTime = 0;
public static long endSwitchTime = 0;
//*/
接着在sim监听位置加上我们的限制条件
路径:vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
//*/soda water.20230912 Intelligent switching data card
import com.freeme.provider.FreemeSettings;
import android.provider.Settings;
//*/
//*/soda water.20230912 Intelligent switching data card
private SubscriptionManager mSubscriptionManager = null;
//*/
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (DEBUG) {
Log.d(mTag, "onSignalStrengthsChanged signalStrength=" + signalStrength +
((signalStrength == null) ? "" : (" level=" + signalStrength.getLevel())));
}
//*/soda water.20230912 Intelligent switching data card
if(mSubscriptionInfo.getSimSlotIndex()==0){
NetworkControllerImpl.subID1 = mSubscriptionInfo.getSubscriptionId();
if(signalStrength.getDbm() <= 0 && signalStrength.getDbm() >= -120){
NetworkControllerImpl.subIDDbmArrays1[NetworkControllerImpl.subIDDbmIndex1] = signalStrength.getDbm();
NetworkControllerImpl.subIDDbmIndex1++;
if(NetworkControllerImpl.subIDDbmIndex1 ==5)NetworkControllerImpl.subIDDbm1 = getSignalMean(NetworkControllerImpl.subIDDbmArrays1);
if(NetworkControllerImpl.subIDDbmIndex1 > 4)NetworkControllerImpl.subIDDbmIndex1 = 0;
}
}else{
NetworkControllerImpl.subID2 = mSubscriptionInfo.getSubscriptionId();
if(signalStrength.getDbm() <= 0 && signalStrength.getDbm() >= -120){
NetworkControllerImpl.subIDDbmArrays2[NetworkControllerImpl.subIDDbmIndex2] = signalStrength.getDbm();
NetworkControllerImpl.subIDDbmIndex2++;
if(NetworkControllerImpl.subIDDbmIndex2 ==5)NetworkControllerImpl.subIDDbm2 = getSignalMean(NetworkControllerImpl.subIDDbmArrays2);
if(NetworkControllerImpl.subIDDbmIndex2 > 4)NetworkControllerImpl.subIDDbmIndex2 = 0;
}
}
Log.d("soda water", "subID1=: " + NetworkControllerImpl.subID1+" subIDDbm1 ="+NetworkControllerImpl.subIDDbm1+" subID2=: " + NetworkControllerImpl.subID2+" subIDDbm2 ="+NetworkControllerImpl.subIDDbm2);
boolean mIntelligentDataState = Settings.System.getInt(mContext.getContentResolver(),FreemeSettings.System.INTELLIGENT_SWITCH_DATA, 0) ==1;
if(NetworkControllerImpl.endSwitchTime == 0)NetworkControllerImpl.endSwitchTime=System.currentTimeMillis();
if(mIntelligentDataState == true && "true".equalsIgnoreCase(SystemProperties.get("ro.com.android.mobiledata","true")) && (System.currentTimeMillis() - NetworkControllerImpl.endSwitchTime >= 5000)){
Log.d("soda water", "join: ");
if(mPhone != null){
if(mPhone.getPhoneCount()>1){
if(NetworkControllerImpl.subIDDbm1 < -110 && NetworkControllerImpl.subIDDbm2 < -110){
int goodSubID = ((NetworkControllerImpl.subIDDbm1-3)>NetworkControllerImpl.subIDDbm2)?NetworkControllerImpl.subIDDbm1:NetworkControllerImpl.subIDDbm2;
if(NetworkControllerImpl.subID1 != -1 && NetworkControllerImpl.subID2 != -1 && goodSubID !=mDefaults.getDefaultDataSubId())setDefaultDataSubId(goodSubID);
Log.d("soda water", "setDefaultDataSubId=: subID1 -110");
}else{
if(mDefaults.getDefaultDataSubId() == NetworkControllerImpl.subID1){
if(signalStrength.getDbm()<-110){
if(NetworkControllerImpl.subID2 != -1)setDefaultDataSubId(NetworkControllerImpl.subID2);
Log.d("soda water", "setDefaultDataSubId=: subID2");
}
}else if(mDefaults.getDefaultDataSubId() == NetworkControllerImpl.subID2){
if(signalStrength.getDbm()<-110){
if(NetworkControllerImpl.subID1 != -1)setDefaultDataSubId(NetworkControllerImpl.subID1);
Log.d("soda water", "setDefaultDataSubId=: subID1");
}
}
}
Log.d("soda water","phoneid = "+mSubscriptionInfo.getSimSlotIndex()+" dbm2 ="+signalStrength.getDbm()+" defaultdataId = "+mDefaults.getDefaultDataSubId() );
}
}
NetworkControllerImpl.endSwitchTime = System.currentTimeMillis();
}
//*/soda water.20230912 Intelligent switching data card
mSignalStrength = signalStrength;
updateTelephony();
}
//*/soda water.20230912 Intelligent switching data card
private void setDefaultDataSubId(int subId) {
if(mSubscriptionManager == null){
mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
}
if(mSubscriptionManager != null)mSubscriptionManager.setDefaultDataSubId(subId);
}
private int getSignalMean(int [] tmp) {
int sum = 0;
for(int i:tmp){
sum += i;
}
return sum/tmp.length;
}
//*/soda water.20230912 Intelligent switching data card