Android P StatusBar icon更新逻辑 (1)

MobilePhoneStateListener 这里包括 2G 3G 4G E H H+之类网络图标 、cs ps图标、信号强度图标,数据上下行图标

1. 继承 PhoneStateListener ,然后变化之后执行updateTelephony更新icon

/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
class MobilePhoneStateListener extends PhoneStateListener {
    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        if (DEBUG) {
            Log.d(mTag, "onSignalStrengthsChanged signalStrength=" + signalStrength +
                    ((signalStrength == null) ? "" : (" level=" + signalStrength.getLevel())));
        }
        mSignalStrength = signalStrength;
        updateTelephony();
    }

    @Override
    public void onServiceStateChanged(ServiceState state) {
        if (DEBUG) {
            Log.d(mTag, "onServiceStateChanged voiceState=" + state.getVoiceRegState()
                    + " dataState=" + state.getDataRegState());
        }
        mServiceState = state;
        mDataNetType = state.getDataNetworkType();
        if (mDataNetType == TelephonyManager.NETWORK_TYPE_LTE && mServiceState != null &&
                mServiceState.isUsingCarrierAggregation()) {
            mDataNetType = TelephonyManager.NETWORK_TYPE_LTE_CA;
        }
        updateTelephony();
    }

    @Override
    public void onDataConnectionStateChanged(int state, int networkType) {
        if (DEBUG) {
            Log.d(mTag, "onDataConnectionStateChanged: state=" + state
                    + " type=" + networkType);
        }
        mDataState = state;
        mDataNetType = networkType;
        if (mDataNetType == TelephonyManager.NETWORK_TYPE_LTE && mServiceState != null &&
                mServiceState.isUsingCarrierAggregation()) {
            mDataNetType = TelephonyManager.NETWORK_TYPE_LTE_CA;
        }
        updateTelephony();
    }

    @Override
    public void onDataActivity(int direction) {
        if (DEBUG) {
            Log.d(mTag, "onDataActivity: direction=" + direction);
        }
        setActivity(direction);
    }

    @Override
    public void onCarrierNetworkChange(boolean active) {
        if (DEBUG) {
            Log.d(mTag, "onCarrierNetworkChange: active=" + active);
        }
        mCurrentState.carrierNetworkChangeMode = active;

        updateTelephony();
    }
};

2.updateTelephony

private final void updateTelephony() {
    if (DEBUG) {
        Log.d(mTag, "updateTelephonySignalStrength: hasService=" + hasService()
                + " ss=" + mSignalStrength);
    }
    mCurrentState.connected = hasService() && mSignalStrength != null;
    if (mCurrentState.connected) {
        if (!mSignalStrength.isGsm() && mConfig.alwaysShowCdmaRssi) {
            mCurrentState.level = mSignalStrength.getCdmaLevel();
        } else {
            mCurrentState.level = mSignalStrength.getLevel();
        }
    }
    if (mNetworkToIconLookup.indexOfKey(mDataNetType) >= 0) {
        mCurrentState.iconGroup = mNetworkToIconLookup.get(mDataNetType);
    } else {
        mCurrentState.iconGroup = mDefaultIcons;
    }
    mCurrentState.dataConnected = mCurrentState.connected
            && mDataState == TelephonyManager.DATA_CONNECTED;

    mCurrentState.roaming = isRoaming();
    if (isCarrierNetworkChangeActive()) {
        mCurrentState.iconGroup = TelephonyIcons.CARRIER_NETWORK_CHANGE;
    } else if (isDataDisabled()) {
        mCurrentState.iconGroup = TelephonyIcons.DATA_DISABLED;
    }
    if (isEmergencyOnly() != mCurrentState.isEmergency) {
        mCurrentState.isEmergency = isEmergencyOnly();
        mNetworkController.recalculateEmergency();
    }
    // Fill in the network name if we think we have it.
    if (mCurrentState.networkName == mNetworkNameDefault && mServiceState != null
            && !TextUtils.isEmpty(mServiceState.getOperatorAlphaShort())) {
        mCurrentState.networkName = mServiceState.getOperatorAlphaShort();
    }

    notifyListenersIfNecessary();
}

3.MobileSignalController.java::notifyListeners()

@ /frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/SignalController.java
public void notifyListenersIfNecessary() {
    if (isDirty()) {
        saveLastState();
        notifyListeners();
    }
}

public final void notifyListeners() {
    notifyListeners(mCallbackHandler);
}


@/frameworks/base/packages/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);
    final boolean dataDisabled = mCurrentState.iconGroup == TelephonyIcons.DATA_DISABLED
            && mCurrentState.userSetup;

    // Show icon in QS when we are connected or data is disabled.
    boolean showDataIcon = mCurrentState.dataConnected || dataDisabled;
    IconState statusIcon = new IconState(mCurrentState.enabled && !mCurrentState.airplaneMode,
            getCurrentIconId(), contentDescription);

    int qsTypeIcon = 0;
    IconState qsIcon = null;
    String description = null;
    // Only send data sim callbacks to QS.
    if (mCurrentState.dataSim) {
        qsTypeIcon = showDataIcon ? 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 ? icons.mDataType : 0;
    callback.setMobileDataIndicators(statusIcon, qsIcon, typeIcon, qsTypeIcon,
            activityIn, activityOut, dataContentDescription, description, icons.mIsWide,
            mSubscriptionInfo.getSubscriptionId(), mCurrentState.roaming);
}

4.SignalClusterView.java::setMobileDataIndicators -- apply() 

apply()就开始更新界面

//最终显示是在SignalClusterView.java,看statusbar布局也能够知道
@/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java
@Override
public void setMobileDataIndicators(IconState statusIcon, IconState qsIcon, int statusType,
        int qsType, boolean activityIn, boolean activityOut, String typeContentDescription,
        String description, boolean isWide, int subId, boolean roaming) {
    PhoneState state = getState(subId);
    if (state == null) {
        return;
    }
    state.mMobileVisible = statusIcon.visible && !mBlockMobile;
    state.mMobileStrengthId = statusIcon.icon;
    state.mMobileTypeId = statusType;
    state.mMobileDescription = statusIcon.contentDescription;
    state.mMobileTypeDescription = typeContentDescription;
    state.mIsMobileTypeIconWide = statusType != 0 && isWide;
    state.mRoaming = roaming;
    state.mActivityIn = activityIn && mActivityEnabled;
    state.mActivityOut = activityOut && mActivityEnabled;

    apply();
}

// Run after each indicator change.
private void apply() {
    if (mWifiGroup == null) return;

    mVpn.setVisibility(mVpnVisible ? View.VISIBLE : View.GONE);
    if (mVpnVisible) {
        if (mLastVpnIconId != mVpnIconId) {
            setIconForView(mVpn, mVpnIconId);
            mLastVpnIconId = mVpnIconId;
        }
        mVpn.setVisibility(View.VISIBLE);
    } else {
        mVpn.setVisibility(View.GONE);
    }
    if (DEBUG) Log.d(TAG, String.format("vpn: %s", mVpnVisible ? "VISIBLE" : "GONE"));

    if (mEthernetVisible) {
        if (mLastEthernetIconId != mEthernetIconId) {
            setIconForView(mEthernet, mEthernetIconId);
            setIconForView(mEthernetDark, mEthernetIconId);
            mLastEthernetIconId = mEthernetIconId;
        }
        mEthernetGroup.setContentDescription(mEthernetDescription);
        mEthernetGroup.setVisibility(View.VISIBLE);
    } else {
        mEthernetGroup.setVisibility(View.GONE);
    }

    if (DEBUG) Log.d(TAG,
            String.format("ethernet: %s",
                (mEthernetVisible ? "VISIBLE" : "GONE")));

    if (mWifiVisible) {
        if (mWifiStrengthId != mLastWifiStrengthId || mWifiBadgeId != mLastWifiBadgeId) {
            if (mWifiBadgeId == -1) {
                setIconForView(mWifi, mWifiStrengthId);
                setIconForView(mWifiDark, mWifiStrengthId);
            } else {
                setBadgedWifiIconForView(mWifi, mWifiStrengthId, mWifiBadgeId);
                setBadgedWifiIconForView(mWifiDark, mWifiStrengthId, mWifiBadgeId);
            }
            mLastWifiStrengthId = mWifiStrengthId;
            mLastWifiBadgeId = mWifiBadgeId;
        }
        mWifiGroup.setContentDescription(mWifiDescription);
        mWifiGroup.setVisibility(View.VISIBLE);
    } else {
        mWifiGroup.setVisibility(View.GONE);
    }

    if (DEBUG) Log.d(TAG,
            String.format("wifi: %s sig=%d",
                (mWifiVisible ? "VISIBLE" : "GONE"),
                mWifiStrengthId));

    mWifiActivityIn.setVisibility(mWifiIn ? View.VISIBLE : View.GONE);
    mWifiActivityOut.setVisibility(mWifiOut ? View.VISIBLE : View.GONE);

    boolean anyMobileVisible = false;
    int firstMobileTypeId = 0;
    for (PhoneState state : mPhoneStates) {
        if (state.apply(anyMobileVisible)) {
            if (!anyMobileVisible) {
                firstMobileTypeId = state.mMobileTypeId;
                anyMobileVisible = true;
            }
        }
    }

    if (mIsAirplaneMode) {
        if (mLastAirplaneIconId != mAirplaneIconId) {
            setIconForView(mAirplane, mAirplaneIconId);
            mLastAirplaneIconId = mAirplaneIconId;
        }
        mAirplane.setContentDescription(mAirplaneContentDescription);
        mAirplane.setVisibility(View.VISIBLE);
    } else {
        mAirplane.setVisibility(View.GONE);
    }

    if (mIsAirplaneMode && mWifiVisible) {
        mWifiAirplaneSpacer.setVisibility(View.VISIBLE);
    } else {
        mWifiAirplaneSpacer.setVisibility(View.GONE);
    }

    if (((anyMobileVisible && firstMobileTypeId != 0) || mNoSimsVisible) && mWifiVisible) {
        mWifiSignalSpacer.setVisibility(View.VISIBLE);
    } else {
        mWifiSignalSpacer.setVisibility(View.GONE);
    }

    mNoSimsCombo.setVisibility(mNoSimsVisible ? View.VISIBLE : View.GONE);

    boolean anythingVisible = mNoSimsVisible || mWifiVisible || mIsAirplaneMode
            || anyMobileVisible || mVpnVisible || mEthernetVisible;
    setPaddingRelative(0, 0, anythingVisible ? mEndPadding : mEndPaddingNothingVisible, 0);
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值