Android 10.0 信号强度低时切换数据的功能实现

介绍

此功能是当信号低于多少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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不太正常的移动开发工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值