修改设置数据流量警告默认为关闭状态
客户有需求,需要设置中的设置数据流量警告默认为关闭状态。
代码跟踪流程:
Settings中搜索“设置数据流量警告”,strings.xml内容如下:
再找set_data_warning对应的布局文件billing_cycle.xml
billing_cycle.xml对应的java文件为BillingCycleSettings.java
@VisibleForTesting void updatePrefs() { mBillingCycle.setSummary(null); final long warningBytes = services.mPolicyEditor.getPolicyWarningBytes(mNetworkTemplate); if (warningBytes != WARNING_DISABLED) { mDataWarning.setSummary(DataUsageUtils.formatDataUsage(getContext(), warningBytes)); mDataWarning.setEnabled(true); mEnableDataWarning.setChecked(true); } else { mDataWarning.setSummary(null); mDataWarning.setEnabled(false); mEnableDataWarning.setChecked(false); } final long limitBytes = services.mPolicyEditor.getPolicyLimitBytes(mNetworkTemplate); if (limitBytes != LIMIT_DISABLED) { mDataLimit.setSummary(DataUsageUtils.formatDataUsage(getContext(), limitBytes)); mDataLimit.setEnabled(true); mEnableDataLimit.setChecked(true); } else { mDataLimit.setSummary(null); mDataLimit.setEnabled(false); mEnableDataLimit.setChecked(false); } }
根据services.mPolicyEditor.getPolicyWarningBytes(mNetworkTemplate)获取的默认值,决定是打开还是关闭。
getPolicyWarningBytes在frameworks\base\packages\SettingsLib\src\com\android\settingslib\NetworkPolicyEditor.java中定义:
public long getPolicyWarningBytes(NetworkTemplate template) { final NetworkPolicy policy = getPolicy(template); return (policy != null) ? policy.warningBytes : WARNING_DISABLED; }
public NetworkPolicy getPolicy(NetworkTemplate template) { for (NetworkPolicy policy : mPolicies) { if (policy.template.equals(template)) { return policy; } } return null; }
policy在NetworkPolicyManagerService.java中赋值:
frameworks\base\services\core\java\com\android\server\net\NetworkPolicyManagerService.java
ensureActiveMobilePolicyAL
--> buildDefaultMobilePolicy
--> buildDefaultMobilePolicy
private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) { 。。。。 final NetworkPolicy policy = buildDefaultMobilePolicy(subId, subscriberId); addNetworkPolicyAL(policy); return true; }
@VisibleForTesting NetworkPolicy buildDefaultMobilePolicy(int subId, String subscriberId) { final NetworkTemplate template = buildTemplateMobileAll(subscriberId); final RecurrenceRule cycleRule = NetworkPolicy .buildRule(ZonedDateTime.now().getDayOfMonth(), ZoneId.systemDefault()); final NetworkPolicy policy = new NetworkPolicy(template, cycleRule, getPlatformDefaultWarningBytes(), getPlatformDefaultLimitBytes(), SNOOZE_NEVER, SNOOZE_NEVER, true, true); synchronized (mUidRulesFirstLock) { synchronized (mNetworkPoliciesSecondLock) { updateDefaultMobilePolicyAL(subId, policy); } } return policy; }
private long getPlatformDefaultWarningBytes() { final int dataWarningConfig = mContext.getResources().getInteger( com.android.internal.R.integer.config_networkPolicyDefaultWarning); // if (dataWarningConfig == WARNING_DISABLED) { // return WARNING_DISABLED; // } else { // return dataWarningConfig * MB_IN_BYTES; // } return WARNING_DISABLED; }
这个地方不做判断,直接return WARNING_DISABLED,即是默认关闭。
R.integer.config_networkPolicyDefaultWarning的值作为流量警告字节,定义如下:
也就是默认的2G,若需要修改更大的值,修改这个值即可。