Remove DataSummaryUsageLegacy.
Change-Id: I8da039530cafe547008b434b6016add5f9383e94 Fixes: 109943959 Test: robotest/atest
This commit is contained in:
@@ -14,24 +14,20 @@
|
||||
|
||||
package com.android.settings.datausage;
|
||||
|
||||
import static android.net.NetworkPolicy.CYCLE_NONE;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.os.Bundle;
|
||||
import android.os.RemoteException;
|
||||
import androidx.preference.Preference;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.FeatureFlagUtils;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
|
||||
import com.android.settings.core.FeatureFlags;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.datausage.CellDataPreference.DataStateListener;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
public class BillingCyclePreference extends Preference implements TemplatePreference {
|
||||
|
||||
private NetworkTemplate mTemplate;
|
||||
@@ -60,14 +56,8 @@ public class BillingCyclePreference extends Preference implements TemplatePrefer
|
||||
mTemplate = template;
|
||||
mSubId = subId;
|
||||
mServices = services;
|
||||
final int cycleDay = services.mPolicyEditor.getPolicyCycleDay(mTemplate);
|
||||
if (FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.DATA_USAGE_SETTINGS_V2)) {
|
||||
setSummary(null);
|
||||
} else if (cycleDay != CYCLE_NONE) {
|
||||
setSummary(getContext().getString(R.string.billing_cycle_fragment_summary, cycleDay));
|
||||
} else {
|
||||
setSummary(null);
|
||||
}
|
||||
setSummary(null);
|
||||
|
||||
setIntent(getIntent());
|
||||
}
|
||||
|
||||
|
@@ -14,7 +14,6 @@
|
||||
|
||||
package com.android.settings.datausage;
|
||||
|
||||
import static android.net.NetworkPolicy.CYCLE_NONE;
|
||||
import static android.net.NetworkPolicy.LIMIT_DISABLED;
|
||||
import static android.net.NetworkPolicy.WARNING_DISABLED;
|
||||
|
||||
@@ -27,10 +26,7 @@ import android.content.res.Resources;
|
||||
import android.net.NetworkPolicy;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.os.Bundle;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import androidx.preference.Preference;
|
||||
import android.text.format.Time;
|
||||
import android.util.FeatureFlagUtils;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -41,11 +37,13 @@ import android.widget.Spinner;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.FeatureFlags;
|
||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||
import com.android.settingslib.NetworkPolicyEditor;
|
||||
import com.android.settingslib.net.DataUsageController;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
public class BillingCycleSettings extends DataUsageBase implements
|
||||
Preference.OnPreferenceChangeListener, DataUsageEditController {
|
||||
|
||||
@@ -63,7 +61,8 @@ public class BillingCycleSettings extends DataUsageBase implements
|
||||
private static final String KEY_BILLING_CYCLE = "billing_cycle";
|
||||
private static final String KEY_SET_DATA_WARNING = "set_data_warning";
|
||||
private static final String KEY_DATA_WARNING = "data_warning";
|
||||
@VisibleForTesting static final String KEY_SET_DATA_LIMIT = "set_data_limit";
|
||||
@VisibleForTesting
|
||||
static final String KEY_SET_DATA_LIMIT = "set_data_limit";
|
||||
private static final String KEY_DATA_LIMIT = "data_limit";
|
||||
|
||||
private NetworkTemplate mNetworkTemplate;
|
||||
@@ -76,11 +75,11 @@ public class BillingCycleSettings extends DataUsageBase implements
|
||||
|
||||
@VisibleForTesting
|
||||
void setUpForTest(NetworkPolicyEditor policyEditor,
|
||||
Preference billingCycle,
|
||||
Preference dataLimit,
|
||||
Preference dataWarning,
|
||||
SwitchPreference enableLimit,
|
||||
SwitchPreference enableWarning) {
|
||||
Preference billingCycle,
|
||||
Preference dataLimit,
|
||||
Preference dataWarning,
|
||||
SwitchPreference enableLimit,
|
||||
SwitchPreference enableWarning) {
|
||||
services.mPolicyEditor = policyEditor;
|
||||
mBillingCycle = billingCycle;
|
||||
mDataLimit = dataLimit;
|
||||
@@ -118,14 +117,7 @@ public class BillingCycleSettings extends DataUsageBase implements
|
||||
|
||||
@VisibleForTesting
|
||||
void updatePrefs() {
|
||||
final int cycleDay = services.mPolicyEditor.getPolicyCycleDay(mNetworkTemplate);
|
||||
if (FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.DATA_USAGE_SETTINGS_V2)) {
|
||||
mBillingCycle.setSummary(null);
|
||||
} else if (cycleDay != CYCLE_NONE) {
|
||||
mBillingCycle.setSummary(getString(R.string.billing_cycle_fragment_summary, cycleDay));
|
||||
} else {
|
||||
mBillingCycle.setSummary(null);
|
||||
}
|
||||
mBillingCycle.setSummary(null);
|
||||
final long warningBytes = services.mPolicyEditor.getPolicyWarningBytes(mNetworkTemplate);
|
||||
if (warningBytes != WARNING_DISABLED) {
|
||||
mDataWarning.setSummary(DataUsageUtils.formatDataUsage(getContext(), warningBytes));
|
||||
@@ -402,7 +394,8 @@ public class BillingCycleSettings extends DataUsageBase implements
|
||||
*/
|
||||
public static class ConfirmLimitFragment extends InstrumentedDialogFragment implements
|
||||
DialogInterface.OnClickListener {
|
||||
@VisibleForTesting static final String EXTRA_LIMIT_BYTES = "limitBytes";
|
||||
@VisibleForTesting
|
||||
static final String EXTRA_LIMIT_BYTES = "limitBytes";
|
||||
public static final float FLOAT = 1.2f;
|
||||
|
||||
public static void show(BillingCycleSettings parent) {
|
||||
|
@@ -14,25 +14,19 @@
|
||||
|
||||
package com.android.settings.datausage;
|
||||
|
||||
import static android.net.ConnectivityManager.TYPE_ETHERNET;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.INetworkStatsService;
|
||||
import android.net.INetworkStatsSession;
|
||||
import android.net.NetworkPolicy;
|
||||
import android.net.NetworkPolicyManager;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.net.TrafficStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.INetworkManagementService;
|
||||
import android.os.RemoteException;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.SystemProperties;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settingslib.NetworkPolicyEditor;
|
||||
|
||||
@@ -99,34 +93,4 @@ public abstract class DataUsageBase extends SettingsPreferenceFragment {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if device has an ethernet network connection.
|
||||
*/
|
||||
public boolean hasEthernet(Context context) {
|
||||
if (DataUsageUtils.TEST_RADIOS) {
|
||||
return SystemProperties.get(DataUsageUtils.TEST_RADIOS_PROP).contains(ETHERNET);
|
||||
}
|
||||
|
||||
final ConnectivityManager conn = ConnectivityManager.from(context);
|
||||
final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
|
||||
|
||||
final long ethernetBytes;
|
||||
try {
|
||||
INetworkStatsSession statsSession = services.mStatsService.openSession();
|
||||
if (statsSession != null) {
|
||||
ethernetBytes = statsSession.getSummaryForNetwork(
|
||||
NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
|
||||
.getTotalBytes();
|
||||
TrafficStats.closeQuietly(statsSession);
|
||||
} else {
|
||||
ethernetBytes = 0;
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// only show ethernet when both hardware present and traffic has occurred
|
||||
return hasEthernet && ethernetBytes > 0;
|
||||
}
|
||||
}
|
||||
|
@@ -14,27 +14,19 @@
|
||||
|
||||
package com.android.settings.datausage;
|
||||
|
||||
import static android.net.ConnectivityManager.TYPE_ETHERNET;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.INetworkStatsService;
|
||||
import android.net.INetworkStatsSession;
|
||||
import android.net.NetworkPolicy;
|
||||
import android.net.NetworkPolicyManager;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.net.TrafficStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.INetworkManagementService;
|
||||
import android.os.RemoteException;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.SystemProperties;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settingslib.NetworkPolicyEditor;
|
||||
|
||||
@@ -54,7 +46,7 @@ public abstract class DataUsageBaseFragment extends DashboardFragment {
|
||||
ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
|
||||
services.mStatsService = INetworkStatsService.Stub.asInterface(
|
||||
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
|
||||
services.mPolicyManager = (NetworkPolicyManager)context
|
||||
services.mPolicyManager = (NetworkPolicyManager) context
|
||||
.getSystemService(Context.NETWORK_POLICY_SERVICE);
|
||||
|
||||
services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager);
|
||||
@@ -98,35 +90,4 @@ public abstract class DataUsageBaseFragment extends DashboardFragment {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if device has an ethernet network connection.
|
||||
* TODO(b/77590489): Remove this method when DataUsageSummaryLegacy is deprecated.
|
||||
*/
|
||||
public boolean hasEthernet(Context context) {
|
||||
if (DataUsageUtils.TEST_RADIOS) {
|
||||
return SystemProperties.get(DataUsageUtils.TEST_RADIOS_PROP).contains(ETHERNET);
|
||||
}
|
||||
|
||||
final ConnectivityManager conn = ConnectivityManager.from(context);
|
||||
final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
|
||||
|
||||
final long ethernetBytes;
|
||||
try {
|
||||
INetworkStatsSession statsSession = services.mStatsService.openSession();
|
||||
if (statsSession != null) {
|
||||
ethernetBytes = statsSession.getSummaryForNetwork(
|
||||
NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
|
||||
.getTotalBytes();
|
||||
TrafficStats.closeQuietly(statsSession);
|
||||
} else {
|
||||
ethernetBytes = 0;
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// only show ethernet when both hardware present and traffic has occurred
|
||||
return hasEthernet && ethernetBytes > 0;
|
||||
}
|
||||
}
|
||||
|
@@ -19,17 +19,16 @@ import android.content.Intent;
|
||||
import android.content.res.TypedArray;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.os.Bundle;
|
||||
import androidx.core.content.res.TypedArrayUtils;
|
||||
import androidx.preference.Preference;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.FeatureFlagUtils;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.FeatureFlags;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settingslib.net.DataUsageController;
|
||||
|
||||
import androidx.core.content.res.TypedArrayUtils;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
public class DataUsagePreference extends Preference implements TemplatePreference {
|
||||
|
||||
private NetworkTemplate mTemplate;
|
||||
@@ -54,20 +53,13 @@ public class DataUsagePreference extends Preference implements TemplatePreferenc
|
||||
mSubId = subId;
|
||||
DataUsageController controller = new DataUsageController(getContext());
|
||||
DataUsageController.DataUsageInfo usageInfo = controller.getDataUsageInfo(mTemplate);
|
||||
if (FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.DATA_USAGE_SETTINGS_V2)) {
|
||||
if (mTemplate.isMatchRuleMobile()) {
|
||||
setTitle(R.string.app_cellular_data_usage);
|
||||
} else {
|
||||
setTitle(mTitleRes);
|
||||
setSummary(getContext().getString(R.string.data_usage_template,
|
||||
DataUsageUtils.formatDataUsage(getContext(), usageInfo.usageLevel),
|
||||
usageInfo.period));
|
||||
}
|
||||
if (mTemplate.isMatchRuleMobile()) {
|
||||
setTitle(R.string.app_cellular_data_usage);
|
||||
} else {
|
||||
setTitle(mTitleRes);
|
||||
setSummary(getContext().getString(R.string.data_usage_template,
|
||||
DataUsageUtils.formatDataUsage(getContext(), usageInfo.usageLevel),
|
||||
usageInfo.period));
|
||||
usageInfo.period));
|
||||
}
|
||||
setIntent(getIntent());
|
||||
}
|
||||
@@ -81,18 +73,10 @@ public class DataUsagePreference extends Preference implements TemplatePreferenc
|
||||
.setArguments(args)
|
||||
.setDestination(DataUsageList.class.getName())
|
||||
.setSourceMetricsCategory(MetricsProto.MetricsEvent.VIEW_UNKNOWN);
|
||||
if (FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.DATA_USAGE_SETTINGS_V2)) {
|
||||
if (mTemplate.isMatchRuleMobile()) {
|
||||
launcher.setTitleRes(R.string.app_cellular_data_usage);
|
||||
} else {
|
||||
launcher.setTitleRes(mTitleRes);
|
||||
}
|
||||
if (mTemplate.isMatchRuleMobile()) {
|
||||
launcher.setTitleRes(R.string.app_cellular_data_usage);
|
||||
} else {
|
||||
if (mTitleRes > 0) {
|
||||
launcher.setTitleRes(mTitleRes);
|
||||
} else {
|
||||
launcher.setTitleText(getTitle());
|
||||
}
|
||||
launcher.setTitleRes(mTitleRes);
|
||||
}
|
||||
return launcher.toIntent();
|
||||
}
|
||||
|
@@ -15,15 +15,10 @@
|
||||
package com.android.settings.datausage;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.os.Bundle;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.SubscriptionPlan;
|
||||
@@ -33,7 +28,6 @@ import android.text.SpannableString;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.Formatter;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
@@ -49,6 +43,10 @@ import com.android.settingslib.search.SearchIndexable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
/**
|
||||
* Settings preference fragment that displays data usage summary.
|
||||
*/
|
||||
@@ -120,20 +118,6 @@ public class DataUsageSummary extends DataUsageBaseFragment implements Indexable
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.data_usage_menu_cellular_networks: {
|
||||
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
intent.setComponent(new ComponentName("com.android.phone",
|
||||
"com.android.phone.MobileNetworkSettings"));
|
||||
startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(Preference preference) {
|
||||
if (preference == findPreference(KEY_STATUS_HEADER)) {
|
||||
|
@@ -1,359 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the
|
||||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.datausage;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.NetworkPolicyManager;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.text.BidiFormatter;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.Formatter;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SummaryPreference;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.dashboard.SummaryLoader;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settingslib.NetworkPolicyEditor;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.net.DataUsageController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
/**
|
||||
* Legacy {@link DataUsageSummary} fragment.
|
||||
*/
|
||||
public class DataUsageSummaryLegacy extends DataUsageBaseFragment implements Indexable,
|
||||
DataUsageEditController {
|
||||
|
||||
private static final String TAG = "DataUsageSummaryLegacy";
|
||||
|
||||
static final boolean LOGD = false;
|
||||
|
||||
public static final String KEY_RESTRICT_BACKGROUND = "restrict_background_legacy";
|
||||
|
||||
private static final String KEY_STATUS_HEADER = "status_header";
|
||||
private static final String KEY_LIMIT_SUMMARY = "limit_summary";
|
||||
|
||||
// Mobile data keys
|
||||
public static final String KEY_MOBILE_USAGE_TITLE = "mobile_category";
|
||||
public static final String KEY_MOBILE_DATA_USAGE_TOGGLE = "data_usage_enable";
|
||||
public static final String KEY_MOBILE_DATA_USAGE = "cellular_data_usage";
|
||||
public static final String KEY_MOBILE_BILLING_CYCLE = "billing_preference";
|
||||
|
||||
// Wifi keys
|
||||
public static final String KEY_WIFI_USAGE_TITLE = "wifi_category";
|
||||
public static final String KEY_WIFI_DATA_USAGE = "wifi_data_usage";
|
||||
|
||||
private DataUsageController mDataUsageController;
|
||||
private DataUsageInfoController mDataInfoController;
|
||||
private SummaryPreference mSummaryPreference;
|
||||
private Preference mLimitPreference;
|
||||
private NetworkTemplate mDefaultTemplate;
|
||||
private int mDataUsageTemplate;
|
||||
private NetworkPolicyEditor mPolicyEditor;
|
||||
|
||||
@Override
|
||||
public int getHelpResource() {
|
||||
return R.string.help_url_data_usage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
final Context context = getContext();
|
||||
NetworkPolicyManager policyManager = NetworkPolicyManager.from(context);
|
||||
mPolicyEditor = new NetworkPolicyEditor(policyManager);
|
||||
|
||||
boolean hasMobileData = DataUsageUtils.hasMobileData(context);
|
||||
mDataUsageController = new DataUsageController(context);
|
||||
mDataInfoController = new DataUsageInfoController();
|
||||
|
||||
int defaultSubId = DataUsageUtils.getDefaultSubscriptionId(context);
|
||||
if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
hasMobileData = false;
|
||||
}
|
||||
mDefaultTemplate = DataUsageUtils.getDefaultTemplate(context, defaultSubId);
|
||||
mSummaryPreference = (SummaryPreference) findPreference(KEY_STATUS_HEADER);
|
||||
|
||||
if (!hasMobileData || !isAdmin()) {
|
||||
removePreference(KEY_RESTRICT_BACKGROUND);
|
||||
}
|
||||
if (hasMobileData) {
|
||||
mLimitPreference = findPreference(KEY_LIMIT_SUMMARY);
|
||||
List<SubscriptionInfo> subscriptions =
|
||||
services.mSubscriptionManager.getActiveSubscriptionInfoList();
|
||||
if (subscriptions == null || subscriptions.size() == 0) {
|
||||
addMobileSection(defaultSubId);
|
||||
}
|
||||
for (int i = 0; subscriptions != null && i < subscriptions.size(); i++) {
|
||||
SubscriptionInfo subInfo = subscriptions.get(i);
|
||||
if (subscriptions.size() > 1) {
|
||||
addMobileSection(subInfo.getSubscriptionId(), subInfo);
|
||||
} else {
|
||||
addMobileSection(subInfo.getSubscriptionId());
|
||||
}
|
||||
}
|
||||
mSummaryPreference.setSelectable(true);
|
||||
} else {
|
||||
removePreference(KEY_LIMIT_SUMMARY);
|
||||
mSummaryPreference.setSelectable(false);
|
||||
}
|
||||
boolean hasWifiRadio = DataUsageUtils.hasWifiRadio(context);
|
||||
if (hasWifiRadio) {
|
||||
addWifiSection();
|
||||
}
|
||||
if (hasEthernet(context)) {
|
||||
addEthernetSection();
|
||||
}
|
||||
mDataUsageTemplate = hasMobileData ? R.string.cell_data_template
|
||||
: hasWifiRadio ? R.string.wifi_data_template
|
||||
: R.string.ethernet_data_template;
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
if (UserManager.get(getContext()).isAdminUser()) {
|
||||
inflater.inflate(R.menu.data_usage, menu);
|
||||
}
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.data_usage_menu_cellular_networks: {
|
||||
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
intent.setComponent(new ComponentName("com.android.phone",
|
||||
"com.android.phone.MobileNetworkSettings"));
|
||||
startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(Preference preference) {
|
||||
if (preference == findPreference(KEY_STATUS_HEADER)) {
|
||||
BillingCycleSettings.BytesEditorFragment.show(this, false);
|
||||
return false;
|
||||
}
|
||||
return super.onPreferenceTreeClick(preference);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.data_usage_legacy;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addMobileSection(int subId) {
|
||||
addMobileSection(subId, null);
|
||||
}
|
||||
|
||||
private void addMobileSection(int subId, SubscriptionInfo subInfo) {
|
||||
TemplatePreferenceCategory category = (TemplatePreferenceCategory)
|
||||
inflatePreferences(R.xml.data_usage_cellular);
|
||||
category.setTemplate(getNetworkTemplate(subId), subId, services);
|
||||
category.pushTemplates(services);
|
||||
if (subInfo != null && !TextUtils.isEmpty(subInfo.getDisplayName())) {
|
||||
Preference title = category.findPreference(KEY_MOBILE_USAGE_TITLE);
|
||||
title.setTitle(subInfo.getDisplayName());
|
||||
}
|
||||
}
|
||||
|
||||
private void addWifiSection() {
|
||||
TemplatePreferenceCategory category = (TemplatePreferenceCategory)
|
||||
inflatePreferences(R.xml.data_usage_wifi);
|
||||
category.setTemplate(NetworkTemplate.buildTemplateWifiWildcard(), 0, services);
|
||||
}
|
||||
|
||||
private void addEthernetSection() {
|
||||
TemplatePreferenceCategory category = (TemplatePreferenceCategory)
|
||||
inflatePreferences(R.xml.data_usage_ethernet);
|
||||
category.setTemplate(NetworkTemplate.buildTemplateEthernet(), 0, services);
|
||||
}
|
||||
|
||||
private Preference inflatePreferences(int resId) {
|
||||
PreferenceScreen rootPreferences = getPreferenceManager().inflateFromResource(
|
||||
getPrefContext(), resId, null);
|
||||
Preference pref = rootPreferences.getPreference(0);
|
||||
rootPreferences.removeAll();
|
||||
|
||||
PreferenceScreen screen = getPreferenceScreen();
|
||||
pref.setOrder(screen.getPreferenceCount());
|
||||
screen.addPreference(pref);
|
||||
|
||||
return pref;
|
||||
}
|
||||
|
||||
private NetworkTemplate getNetworkTemplate(int subscriptionId) {
|
||||
NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
|
||||
services.mTelephonyManager.getSubscriberId(subscriptionId));
|
||||
return NetworkTemplate.normalize(mobileAll,
|
||||
services.mTelephonyManager.getMergedSubscriberIds());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
updateState();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static CharSequence formatUsage(Context context, String template, long usageLevel) {
|
||||
final float LARGER_SIZE = 1.25f * 1.25f; // (1/0.8)^2
|
||||
final float SMALLER_SIZE = 1.0f / LARGER_SIZE; // 0.8^2
|
||||
final int FLAGS = Spannable.SPAN_INCLUSIVE_INCLUSIVE;
|
||||
|
||||
final Formatter.BytesResult usedResult = Formatter.formatBytes(context.getResources(),
|
||||
usageLevel, Formatter.FLAG_CALCULATE_ROUNDED);
|
||||
final SpannableString enlargedValue = new SpannableString(usedResult.value);
|
||||
enlargedValue.setSpan(new RelativeSizeSpan(LARGER_SIZE), 0, enlargedValue.length(), FLAGS);
|
||||
|
||||
final SpannableString amountTemplate = new SpannableString(
|
||||
context.getString(com.android.internal.R.string.fileSizeSuffix)
|
||||
.replace("%1$s", "^1").replace("%2$s", "^2"));
|
||||
final CharSequence formattedUsage = TextUtils.expandTemplate(amountTemplate,
|
||||
enlargedValue, usedResult.units);
|
||||
|
||||
final SpannableString fullTemplate = new SpannableString(template);
|
||||
fullTemplate.setSpan(new RelativeSizeSpan(SMALLER_SIZE), 0, fullTemplate.length(), FLAGS);
|
||||
return TextUtils.expandTemplate(fullTemplate,
|
||||
BidiFormatter.getInstance().unicodeWrap(formattedUsage.toString()));
|
||||
}
|
||||
|
||||
private void updateState() {
|
||||
DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo(
|
||||
mDefaultTemplate);
|
||||
Context context = getContext();
|
||||
mDataInfoController.updateDataLimit(info,
|
||||
services.mPolicyEditor.getPolicy(mDefaultTemplate));
|
||||
|
||||
if (mSummaryPreference != null) {
|
||||
mSummaryPreference.setTitle(
|
||||
formatUsage(context, getString(mDataUsageTemplate), info.usageLevel));
|
||||
final long limit = mDataInfoController.getSummaryLimit(info);
|
||||
mSummaryPreference.setSummary(info.period);
|
||||
if (limit <= 0) {
|
||||
mSummaryPreference.setChartEnabled(false);
|
||||
} else {
|
||||
mSummaryPreference.setChartEnabled(true);
|
||||
mSummaryPreference.setLabels(Formatter.formatFileSize(context, 0),
|
||||
Formatter.formatFileSize(context, limit));
|
||||
mSummaryPreference.setRatios(info.usageLevel / (float) limit, 0,
|
||||
(limit - info.usageLevel) / (float) limit);
|
||||
}
|
||||
}
|
||||
if (mLimitPreference != null && (info.warningLevel > 0 || info.limitLevel > 0)) {
|
||||
String warning = Formatter.formatFileSize(context, info.warningLevel);
|
||||
String limit = Formatter.formatFileSize(context, info.limitLevel);
|
||||
mLimitPreference.setSummary(getString(info.limitLevel <= 0 ? R.string.cell_warning_only
|
||||
: R.string.cell_warning_and_limit, warning, limit));
|
||||
} else if (mLimitPreference != null) {
|
||||
mLimitPreference.setSummary(null);
|
||||
}
|
||||
|
||||
PreferenceScreen screen = getPreferenceScreen();
|
||||
for (int i = 1; i < screen.getPreferenceCount(); i++) {
|
||||
((TemplatePreferenceCategory) screen.getPreference(i)).pushTemplates(services);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.DATA_USAGE_SUMMARY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkPolicyEditor getNetworkPolicyEditor() {
|
||||
return services.mPolicyEditor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkTemplate getNetworkTemplate() {
|
||||
return mDefaultTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDataUsage() {
|
||||
updateState();
|
||||
}
|
||||
|
||||
private static class SummaryProvider
|
||||
implements SummaryLoader.SummaryProvider {
|
||||
|
||||
private final Activity mActivity;
|
||||
private final SummaryLoader mSummaryLoader;
|
||||
private final DataUsageController mDataController;
|
||||
|
||||
public SummaryProvider(Activity activity, SummaryLoader summaryLoader) {
|
||||
mActivity = activity;
|
||||
mSummaryLoader = summaryLoader;
|
||||
mDataController = new DataUsageController(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setListening(boolean listening) {
|
||||
if (listening) {
|
||||
DataUsageController.DataUsageInfo info = mDataController.getDataUsageInfo();
|
||||
String used;
|
||||
if (info == null) {
|
||||
used = Formatter.formatFileSize(mActivity, 0);
|
||||
} else if (info.limitLevel <= 0) {
|
||||
used = Formatter.formatFileSize(mActivity, info.usageLevel);
|
||||
} else {
|
||||
used = Utils.formatPercentage(info.usageLevel, info.limitLevel);
|
||||
}
|
||||
mSummaryLoader.setSummary(this,
|
||||
mActivity.getString(R.string.data_usage_summary_format, used));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
|
||||
= SummaryProvider::new;
|
||||
}
|
Reference in New Issue
Block a user