欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android9.0 SystemUI 网络信号栏定制修改

程序员文章站 2022-11-08 09:37:48
前情提要 "Android 8.1平台SystemUI 导航栏加载流程解析" 9.0 改动点简要说明 1、新增 StatusBarMobileView 替代 SignalClusterView ,用以控制信号栏显示 同时增加的还有 StatusBarIconView 、 StatusBarWifiV ......

前情提要

android 8.1平台systemui 导航栏加载流程解析

9.0 改动点简要说明

1、新增 statusbarmobileview 替代 signalclusterview,用以控制信号栏显示

同时增加的还有 statusbariconviewstatusbarwifiview

2、整体流程和 8.1 类似

效果图

Android9.0 SystemUI 网络信号栏定制修改

整体流程图

Android9.0 SystemUI 网络信号栏定制修改

上代码

Android9.0 SystemUI 网络信号栏定制修改

先来看初始赋值的地方 mobilesignalcontroller.java,在 notifylisteners() 方法中进行我们对应的定制,

最后通过 callback.setmobiledataindicators() 将状态值传递到 statusbarsignalpolicy.java 解析显示

vendor\mediatek\proprietary\packages\apps\systemui\src\com\android\systemui\statusbar\policy\mobilesignalcontroller.java

@override
    public void notifylisteners(signalcallback callback) {
        mobileicongroup icons = geticons();

        string contentdescription = getstringifexists(getcontentdescription());
        string datacontentdescription = getstringifexists(icons.mdatacontentdescription);
        if (mcurrentstate.inetcondition == 0) {
            datacontentdescription = mcontext.getstring(r.string.data_connection_no_internet);
        }
        final boolean datadisabled = mcurrentstate.icongroup == telephonyicons.data_disabled
                && mcurrentstate.usersetup;

        /// m: customize the signal strength icon id. @ {
        int iconid = getcurrenticonid();
        iconid = mstatusbarext.getcustomizesignalstrengthicon(
                    msubscriptioninfo.getsubscriptionid(),
                    iconid,
                    msignalstrength,
                    mdatanettype,
                    mservicestate);
        /// @ }
        if (msignalstrength != null) {
            /*int dbm = msignalstrength.getdbm();
            int asu = msignalstrength.getasulevel();
            log.i("ccz","dbm="+dbm + " asu="+asu);*/
            log.e("ccz", "isgsm=="+msignalstrength.isgsm() 
                + " connected=="+mcurrentstate.connected+" dataconnected=="+mcurrentstate.dataconnected);
        }

        if (mcurrentstate.connected) {
            //cczheng add get signal icon  [s]
            //通过 getsignalstrengthicon 方法,根据自定义规则,返回要显示对应资源id
            iconid = telephonyicons.getsignalstrengthicon(msignalstrength != null ? msignalstrength.getdbm() : -120,
                msignalstrength != null ? msignalstrength.getasulevel() : 0);
            //iconid = telephonyicons.getsignalstrengthicon(mcurrentstate.level);
            //cczheng add get signal icon  [e]
        }else{//未连接成功时显示 x
            iconid = r.drawable.stat_sys_signal_disable;
        }

        // show icon in qs when we are connected or data is disabled.
        boolean showdataicon = mcurrentstate.dataconnected || datadisabled;
        log.i("ccz","datadisabled="+datadisabled + " showdataicon="+showdataicon);
        iconstate statusicon = new iconstate(mcurrentstate.enabled && !mcurrentstate.airplanemode,
                iconid, contentdescription);

        int qstypeicon = 0;
        iconstate qsicon = null;
        string description = null;
        // only send data sim callbacks to qs.
        if (mcurrentstate.datasim) {
            qstypeicon = (showdataicon || mconfig.alwaysshowdataraticon) ? icons.mqsdatatype : 0;
            qsicon = new iconstate(mcurrentstate.enabled
                    && !mcurrentstate.isemergency, getqscurrenticonid(), contentdescription);
            description = mcurrentstate.isemergency ? null : mcurrentstate.networkname;
        }
        boolean activityin = mcurrentstate.dataconnected
                && !mcurrentstate.carriernetworkchangemode
                && mcurrentstate.activityin;
        boolean activityout = mcurrentstate.dataconnected
                && !mcurrentstate.carriernetworkchangemode
                && mcurrentstate.activityout;
        showdataicon &= mcurrentstate.isdefault || datadisabled;
        int typeicon = (showdataicon || mconfig.alwaysshowdataraticon) ? icons.mdatatype : 0;

        /// m: add for lwa.
        typeicon = mcurrentstate.lwaregstate == networktypeutils.lwa_state_conncted
                && showdataicon ? networktypeutils.lwa_icon : typeicon;
        /** m: support [network type on statusbar], change the implement methods.
          * get the network icon base on service state.
          * add one more parameter for network type.
          * @ { **/
        int networkicon = mcurrentstate.networkicon;
        /// m: support volte icon.bug fix when airplane mode is on go to hide volte icon
        int volteicon = mcurrentstate.airplanemode && !isimsoverwfc()
                ? 0 : mcurrentstate.volteicon;

        /// m: when data disabled, common show data icon as x, but op do not need show it @ {
        mstatusbarext.isdatadisabled(msubscriptioninfo.getsubscriptionid(), datadisabled);
        /// @ }

        /// m: customize the data type icon id. @ {
        typeicon = mstatusbarext.getdatatypeicon(
                        msubscriptioninfo.getsubscriptionid(),
                        typeicon,
                        mdatanettype,
                        mcurrentstate.dataconnected ? telephonymanager.data_connected :
                            telephonymanager.data_disconnected,
                        mservicestate);
        /// @ }
        /// m: customize the network type icon id. @ {
        networkicon = mstatusbarext.getnetworktypeicon(
                        msubscriptioninfo.getsubscriptionid(),
                        networkicon,
                        mdatanettype,
                        mservicestate);
        /// for operator
        //通过 description 传递运营商信息
        description = mcurrentstate.operator;
        // for qsdatetype
        qstypeicon = mcurrentstate.mqsdatatype;

        /// @ }
        //cczheng add  for when datadisable set mmobiledataactivity gone [s]
        //数据网络关闭时,id 置为0,不显示上下行小箭头
        if (isdatadisabled()) {
            typeicon = 0;
        }
        log.d("ccz", "qstypeicon="+qstypeicon);
        //cczheng add  for when datadisable set mmobiledataactivity gone [e]
        log.e("ccz", " showdataicon="+showdataicon+" activityin="+activityin
            +" activityout="+activityout);
        log.i("ccz", "networkname="+mcurrentstate.networkname + "  description="+description);
        callback.setmobiledataindicators(statusicon, qsicon, typeicon, networkicon, volteicon,
                qstypeicon,activityin, activityout, datacontentdescription, description,
                 icons.miswide, msubscriptioninfo.getsubscriptionid(), mcurrentstate.roaming,
                 mcurrentstate.isdefaultdata, mcurrentstate.customizedstate);

        /// m: update plmn label @{
        mnetworkcontroller.refreshplmncarrierlabel();
        /// @}
    }



    private final void updatetelephony() {
    
        .....

        /// m: for network type big icon.
        mcurrentstate.networkicon =
            networktypeutils.getnetworktypeicon(mservicestate, mconfig, hasservice());
    
        /// m: for volte type icon.
        mcurrentstate.volteicon = getvolteicon();
    
        //cczheng add for qsdatetype
        mcurrentstate.mqsdatatype = ((mobileicongroup)mcurrentstate.icongroup).mqsdatatype; 
    
        //cczheng add for operator
        mcurrentstate.operator = getoperatortype();
    
        notifylistenersifnecessary();

   }

    private string getoperatortype(){
        if (!hasservice()) {
            // not in service, don't show operator 
            return "0";
        }

       int operatortype = networktypeutils.getoperatortype(mphone);
       if (operatortype == 0) 
           return "0";
       else
           return mcontext.getresources().getstring(operatortype); 
    }

根据通用标准自定义显示规则

dbm >= -107 4格

-107 > dbm >= -111 3格

-111 > dbm >= -114 2格

-114 > dbm >= -117 1格

vendor\mediatek\proprietary\packages\apps\systemui\src\com\android\systemui\statusbar\policy\telephonyicons.java

    static final int[] telephony_signal_strength_full = {
            r.drawable.stat_sys_signal_0_fully,
            r.drawable.stat_sys_signal_1_fully,
            r.drawable.stat_sys_signal_2_fully,
            r.drawable.stat_sys_signal_3_fully,
            r.drawable.stat_sys_signal_4_fully,
    };
    /**
     * customize signal strength icon.
     * @param level telephony signal strength leve.
     * @return signal strength icon id.
     */
    static final int getsignalstrengthicon(int level) {
        android.util.log.e("ccz", "getsignalstrengthicon() level=="+level);
        if (level >= 0 && level < telephony_signal_strength_full.length) {
            return telephony_signal_strength_full[level];
        }

        return 0;
    }

   
    static final int getsignalstrengthicon(int dbm, int asu) {
        int level = 0;

        if(dbm >= -107) level = 4;
        else if (dbm >= -111) level = 3; 
        else if (dbm >= -114) level = 2; 
        else if (dbm >= -117) level = 1; 


        android.util.log.e("ccz", "getsignalstrengthicon() dbm=="+dbm + " asu=="+asu + " level=="+level);
        return telephony_signal_strength_full[level];
    }

舍弃原来的网络大图标,新增网络小图标,这样可以在下方显示网络数据图标

vendor\mediatek\proprietary\packages\apps\systemui\src\com\mediatek\systemui\statusbar\networktype\networktypeutils.java

//cczheng add show small networktype
static final map<integer, integer> snetworktypesmallicons = new hashmap<integer, integer>() {
    {
            // for cdma 3g
            put(telephonymanager.network_type_evdo_0, r.drawable.stat_sys_data_fully_connected_3g);
            put(telephonymanager.network_type_evdo_a, r.drawable.stat_sys_data_fully_connected_3g);
            put(telephonymanager.network_type_evdo_b, r.drawable.stat_sys_data_fully_connected_3g);
            put(telephonymanager.network_type_ehrpd, r.drawable.stat_sys_data_fully_connected_3g);
            // for cdma 1x
            put(telephonymanager.network_type_cdma, r.drawable.stat_sys_data_fully_connected_1x);
            put(telephonymanager.network_type_1xrtt, r.drawable.stat_sys_data_fully_connected_1x);
            // edge
            put(telephonymanager.network_type_edge, r.drawable.stat_sys_data_fully_connected_e);
            // 3g
            put(telephonymanager.network_type_umts, r.drawable.stat_sys_data_fully_connected_3g);
            // for 4g
            put(telephonymanager.network_type_lte, r.drawable.stat_sys_data_fully_connected_4g);
            // 3g
            put(telephonymanager.network_type_hsdpa, r.drawable.stat_sys_data_fully_connected_3g);
            put(telephonymanager.network_type_hsupa, r.drawable.stat_sys_data_fully_connected_3g);
            put(telephonymanager.network_type_hspa, r.drawable.stat_sys_data_fully_connected_3g);
            put(telephonymanager.network_type_hspap, r.drawable.stat_sys_data_fully_connected_3g);
            put(telephonymanager.network_type_iwlan, 0);
        }
    };


public static int getnetworktypeicon(servicestate servicestate, config config,
            boolean hasservice) {
        if (!hasservice) {
            // not in service, no network type.
            return 0;
        }
        int tempnetworktype = getnetworktype(servicestate);

        //cczheng change  snetworktypeicons to snetworktypesmallicons, show small networktype
        //integer iconid = snetworktypeicons.get(tempnetworktype);
        integer iconid = snetworktypesmallicons.get(tempnetworktype);//add
        if (iconid == null) {
            iconid = tempnetworktype == telephonymanager.network_type_unknown ? 0 :
                     config.showatleast3g ? r.drawable.stat_sys_network_type_3g :
                                            r.drawable.stat_sys_network_type_g;
        }
        log.i("ccz", "operator=="+ servicestate.getoperatoralphalong());
        return iconid.intvalue();
    }

//cczheng add for operatortype
static final int[] operator_type = {
        r.string.operator_cmcc,//china_mobile
        r.string.operator_cucc,//china_unicom
        r.string.operator_ctcc//china_telecom
};

public static int getoperatortype(telephonymanager telephonymanager) {
    int type = 0;
    string operator = telephonymanager.getsimoperator();

    switch (operator) {
        case "46000":
        case "46002":
        case "46007":
        case "41004": 
            type = operator_type[0];
            break;
        case "46001":
        case "46006":
            type = operator_type[1];
            break;
        case "46003":
        case "46011":
            type = operator_type[2];
            break;
        default:
            break;
    }
    return type;
}

statusbarsignalpolicy 通过 description 传递运营商类型

vendor\mediatek\proprietary\packages\apps\systemui\src\com\android\systemui\statusbar\phone\statusbarsignalpolicy.java

@override
    public void setmobiledataindicators(iconstate statusicon, iconstate qsicon, int statustype,
            int networktype, int volteicon, int qstype, boolean activityin, boolean activityout,
            string typecontentdescription, string description, boolean iswide, int subid,
            boolean roaming, boolean isdefaultdata, int customizedstate) {
        log.i("ccz","statusbarsignalpolicy setmobiledataindicators()");
        mobileiconstate state = getstate(subid);
        if (state == null) {
            return;
        }
        
        .....

        //cczheng add for operator because description is unless
        state.moperator = description;
        log.e("ccz","mmobilestrengthid="+statusicon.icon);

        // always send a copy to maintain value type semantics
        miconcontroller.setmobileicons(mslotmobile, mobileiconstate.copystates(mmobilestates));

}

statusbarmobileview 获取传递的资源id值显示对应的图标

vendor\mediatek\proprietary\packages\apps\systemui\src\com\android\systemui\statusbar\statusbarmobileview.java

public class statusbarmobileview extends framelayout implements darkreceiver,
        statusicondisplayable {
    private static final string tag = "statusbarmobileview";
    
    ....

    
    //cczheng add
    private imageview mmobiledataactivity;
    private textview moperatortype;

    
     private void init() {
        ....

        mnetworktype    = findviewbyid(r.id.network_type);
        mvoltetype      = findviewbyid(r.id.volte_indicator_ext);
        /// @}
        mmobiledataactivity = findviewbyid(r.id.data_inout);
        moperatortype = findviewbyid(r.id.tv_operator);

        mmobiledrawable = new signaldrawable(getcontext());
        //cczheng annotaion don't use system full style
        //mmobile.setimagedrawable(mmobiledrawable);

        initdotview();

        miswfcenable = systemproperties.get("persist.vendor.mtk_wfc_support").equals("1");

        /// m: add for plugin feature @ {
        mstatusbarext = opsystemuicustomizationfactorybase.getopfactory(mcontext)
                                     .makesystemuistatusbar(mcontext);
        /// @ }
    }

    
    private void initviewstate() {
        setcontentdescription(mstate.contentdescription);
        if (!mstate.visible) {
            mmobilegroup.setvisibility(view.gone);
        } else {
            mmobilegroup.setvisibility(view.visible);
        }
        //cczheng don't use system style change empty line style 注释原来的实心信号格
        //mmobiledrawable.setlevel(mstate.strengthid);
        mmobile.setimageresource(mstate.strengthid);
        //show  date in out icon 数据流量小箭头
        mmobiledataactivity.setimageresource(getdataactivityicon(mstate.activityin, mstate.activityout));

        if (mstate.typeid > 0) {
            if (!mstatusbarext.disablehostfunction()) {
                mmobiletype.setcontentdescription(mstate.typecontentdescription);
                mmobiletype.setimageresource(mstate.typeid);
            }
            //cczheng hide small datatype icon x or 4g
            mmobiletype.setvisibility(view.gone /*view.visible*/);
        } else {
            mmobiletype.setvisibility(view.gone);
        }

        mmobileroaming.setvisibility(mstate.roaming ? view.visible : view.gone);
        min.setvisibility(mstate.activityin ? view.visible : view.gone);
        mout.setvisibility(mstate.activityin ? view.visible : view.gone);
        //minoutcontainer.setvisibility((mstate.activityin || mstate.activityout)
        //        ? view.visible : view.gone);
        mmobiledataactivity.setvisibility(mstate.typeid != 0 ? view.visible : view.gone);

        /// m: add for [network type and volte on statusbar] @{
        setcustomizeviewproperty();
        /// @}

        showwfcifairplanemode();

        /// m: add data group for plugin feature. @ {
        mstatusbarext.addcustomizedview(mstate.subid, mcontext, mmobilegroup);
        setcustomizedopviews();
        /// @ }
    }

    private void updatestate(mobileiconstate state) {
        setcontentdescription(state.contentdescription);
        if (mstate.visible != state.visible) {
            mmobilegroup.setvisibility(state.visible ? view.visible : view.gone);
            // to avoid statusbarmobileview will not show in extreme case,
            // force request layout once if visible state changed.
            requestlayout();
        }
        if (mstate.strengthid != state.strengthid) {
            //cczheng don't use system style change empty line style
            //mmobiledrawable.setlevel(state.strengthid);
            mmobile.setimageresource(state.strengthid);
        }
        //show  date in out icon 
        mmobiledataactivity.setimageresource(getdataactivityicon(state.activityin, state.activityout));

        if (mstate.typeid != state.typeid) {
            if (state.typeid != 0) {
                if (!mstatusbarext.disablehostfunction()) {
                    mmobiletype.setcontentdescription(state.typecontentdescription);
                    mmobiletype.setimageresource(state.typeid);
                }
                //cczheng hide small datatype icon x or 4g
                //数据流量标识 mmobiledataactivity 替代 mmobiletype
                mmobiletype.setvisibility(view.gone /*view.visible*/);
            } else {
                mmobiletype.setvisibility(view.gone);
            }
        }

        mmobileroaming.setvisibility(state.roaming ? view.visible : view.gone);
        min.setvisibility(state.activityin ? view.visible : view.gone);
        mout.setvisibility(state.activityin ? view.visible : view.gone);
        //minoutcontainer.setvisibility((state.activityin || state.activityout)
        //        ? view.visible : view.gone);
        mmobiledataactivity.setvisibility(state.typeid != 0 ? view.visible : view.gone);

        /// m: add for [network type and volte on statusbar] @{
        if (mstate.networkicon != state.networkicon) {
            setnetworkicon(state.networkicon);
        }
        if (mstate.volteicon != state.volteicon) {
            setvolteicon(state.volteicon);
        }
        if (mstate.moperator != state.moperator) {
            setoperatortext(state.moperator);
        }

        if (mstate.mcustomizedstate != state.mcustomizedstate
                || mstate.networkicon != state.networkicon) {
            // if cs reg state has changed or network icon change to lte,need to update.
            mstatusbarext.setdisvolteview(mstate.subid, state.volteicon, mvoltetype);
        }
        /// @}

        mstate = state;
        // should added after set mstate
        showwfcifairplanemode();
        setcustomizedopviews();
    }


    //cczheng add for data in out icon [s]  
     final int data_activity_none = r.drawable.ct_stat_sys_signal_not_inout;
     final int data_activity_in = r.drawable.ct_stat_sys_signal_in;
     final int data_activity_out = r.drawable.ct_stat_sys_signal_out;
     final int data_activity_inout = r.drawable.ct_stat_sys_signal_inout;

    /**
     * m: getdataactivityicon: get dataactivity icon by dataactivity type.
     * @param activityin : dataactivity type
     * @param activityout : dataactivity type
     * @return  dataactivity icon id
     */
     public int getdataactivityicon(boolean activityin, boolean activityout) {
        log.i("ccz", "mactivityin="+activityin+" mactivityout="+activityout);  
        int icon = data_activity_none;

        if (activityin && activityout) {
            icon = data_activity_inout;
        }else if (activityin) {
            icon = data_activity_in;
        }else if (activityout) {
            icon = data_activity_out;
        }

        return icon;
    }
    //cczheng add for data in out icon [s]  


    private void setoperatortext(string moperator){
        if ("0".equals(moperator)) {
            moperatortype.setvisibility(view.gone);
        } else {
            moperatortype.settext(moperator);
            moperatortype.setvisibility(view.visible);
        }
    }

statusbarmobileview 对应布局文件 status_bar_mobile_signal_group 修改

vendor\mediatek\proprietary\packages\apps\systemui\res\layout\status_bar_mobile_signal_group.xml

<com.android.systemui.statusbar.statusbarmobileview
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mobile_combo"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical" >

    <com.android.keyguard.alphaoptimizedlinearlayout
        android:id="@+id/mobile_group"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <imageview
            android:id="@+id/volte_indicator_ext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:visibility="gone"
            />
        <!-- <imageview
            android:id="@+id/network_type"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:visibility="gone"
            /> -->
        <!-- cczheng  annotaion network_type and add data in out view-->
        <framelayout
            android:layout_height="17dp"
            android:layout_width="wrap_content">
            <imageview
                android:id="@+id/network_type"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:paddingend="2dp"
                android:visibility="gone"/>
            <imageview
                android:id="@+id/data_inout"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:paddingend="2dp"
                android:visibility="gone"/>
        </framelayout> 
        <!-- end -->
        
        .......

        <imageview
            android:id="@+id/mobile_roaming"
            android:layout_width="wrap_content"
            android:layout_height="17dp"
            android:paddingstart="1dp"
            android:paddingtop="1.5dp"
            android:paddingbottom="3dp"
            android:paddingend="1dp"
            android:scaletype="fitcenter"
            android:src="@drawable/stat_sys_roaming_ext"
            android:contentdescription="@string/data_connection_roaming"
            android:visibility="gone" />
        <!-- cczheng add for sim operator  -->
        <textview
            android:id="@+id/tv_operator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"/>
    </com.android.keyguard.alphaoptimizedlinearlayout>
</com.android.systemui.statusbar.statusbarmobileview>


欢迎关注我的英文公众号【十句英文】,每日1首英文金曲+10句英文,伴你共同进步。

微信扫一扫下方二维码即可关注:

Android9.0 SystemUI 网络信号栏定制修改