Allow data usage policy on Wi-Fi networks.
Create NetworkPolicy when it doesn't already exist. Bug: 4646471, 5085110 Change-Id: I38b8b03689710df06d1734a4b4d9e12d98794154
This commit is contained in:
@@ -3414,6 +3414,8 @@ found in the list of installed applications.</string>
|
|||||||
<string name="data_usage_disable_4g_limit">Disable 4G data at limit</string>
|
<string name="data_usage_disable_4g_limit">Disable 4G data at limit</string>
|
||||||
<!-- Checkbox label that will disable 2G-3G network data connection when user-defined limit is reached. [CHAR LIMIT=32] -->
|
<!-- Checkbox label that will disable 2G-3G network data connection when user-defined limit is reached. [CHAR LIMIT=32] -->
|
||||||
<string name="data_usage_disable_3g_limit">Disable 2G-3G data at limit</string>
|
<string name="data_usage_disable_3g_limit">Disable 2G-3G data at limit</string>
|
||||||
|
<!-- Checkbox label that will disable Wi-Fi network data connection when user-defined limit is reached. [CHAR LIMIT=32] -->
|
||||||
|
<string name="data_usage_disable_wifi_limit">Disable Wi-Fi data at limit</string>
|
||||||
|
|
||||||
<!-- Tab title for showing Wi-Fi data usage. [CHAR LIMIT=10] -->
|
<!-- Tab title for showing Wi-Fi data usage. [CHAR LIMIT=10] -->
|
||||||
<string name="data_usage_tab_wifi">Wi-Fi</string>
|
<string name="data_usage_tab_wifi">Wi-Fi</string>
|
||||||
@@ -3459,11 +3461,7 @@ found in the list of installed applications.</string>
|
|||||||
<!-- Title of dialog shown before user limits data usage. [CHAR LIMIT=48] -->
|
<!-- Title of dialog shown before user limits data usage. [CHAR LIMIT=48] -->
|
||||||
<string name="data_usage_limit_dialog_title">Limiting data usage</string>
|
<string name="data_usage_limit_dialog_title">Limiting data usage</string>
|
||||||
<!-- Body of dialog shown before user limits mobile data usage. [CHAR LIMIT=NONE] -->
|
<!-- Body of dialog shown before user limits mobile data usage. [CHAR LIMIT=NONE] -->
|
||||||
<string name="data_usage_limit_dialog_mobile">Your mobile data connection will be disabled when the specified limit is reached.\n\nTo avoid overage charges, consider using a reduced limit, as device and carrier accounting methods may vary.</string>
|
<string name="data_usage_limit_dialog">Your <xliff:g id="networks" example="mobile">%1$s</xliff:g> data connection will be disabled when the specified limit is reached.\n\nTo avoid overage charges, consider using a reduced limit, as device and carrier accounting methods may vary.</string>
|
||||||
<!-- Body of dialog shown before user limits 3G data usage. [CHAR LIMIT=NONE] -->
|
|
||||||
<string name="data_usage_limit_dialog_3g">Your 2G-3G data connection will be disabled when the specified limit is reached.\n\nTo avoid overage charges, consider using a reduced limit, as device and carrier accounting methods may vary.</string>
|
|
||||||
<!-- Body of dialog shown before user limits 4G data usage. [CHAR LIMIT=NONE] -->
|
|
||||||
<string name="data_usage_limit_dialog_4g">Your 4G data connection will be disabled when the specified limit is reached.\n\nTo avoid overage charges, consider using a reduced limit, as device and carrier accounting methods may vary.</string>
|
|
||||||
|
|
||||||
<!-- Title of dialog shown before user restricts background data usage. [CHAR LIMIT=48] -->
|
<!-- Title of dialog shown before user restricts background data usage. [CHAR LIMIT=48] -->
|
||||||
<string name="data_usage_restrict_background_title">Restrict background data?</string>
|
<string name="data_usage_restrict_background_title">Restrict background data?</string>
|
||||||
|
@@ -585,22 +585,8 @@ public class DataUsageSummary extends Fragment {
|
|||||||
|
|
||||||
if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
|
if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
|
||||||
|
|
||||||
if (TAB_WIFI.equals(currentTab)) {
|
mDataEnabledView.setVisibility(View.VISIBLE);
|
||||||
// wifi doesn't have any controls
|
mDisableAtLimitView.setVisibility(View.VISIBLE);
|
||||||
mDataEnabledView.setVisibility(View.GONE);
|
|
||||||
mDisableAtLimitView.setVisibility(View.GONE);
|
|
||||||
mTemplate = buildTemplateWifi();
|
|
||||||
|
|
||||||
} else if (TAB_ETHERNET.equals(currentTab)) {
|
|
||||||
// ethernet doesn't have any controls
|
|
||||||
mDataEnabledView.setVisibility(View.GONE);
|
|
||||||
mDisableAtLimitView.setVisibility(View.GONE);
|
|
||||||
mTemplate = buildTemplateEthernet();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
mDataEnabledView.setVisibility(View.VISIBLE);
|
|
||||||
mDisableAtLimitView.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TAB_MOBILE.equals(currentTab)) {
|
if (TAB_MOBILE.equals(currentTab)) {
|
||||||
setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
|
setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
|
||||||
@@ -619,6 +605,20 @@ public class DataUsageSummary extends Fragment {
|
|||||||
setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
|
setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
|
||||||
// TODO: bind mDataEnabled to 4G radio state
|
// TODO: bind mDataEnabled to 4G radio state
|
||||||
mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
|
mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
|
||||||
|
|
||||||
|
} else if (TAB_WIFI.equals(currentTab)) {
|
||||||
|
mDataEnabledView.setVisibility(View.GONE);
|
||||||
|
setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_wifi_limit);
|
||||||
|
mTemplate = buildTemplateWifi();
|
||||||
|
|
||||||
|
} else if (TAB_ETHERNET.equals(currentTab)) {
|
||||||
|
// ethernet doesn't have any controls
|
||||||
|
mDataEnabledView.setVisibility(View.GONE);
|
||||||
|
mDisableAtLimitView.setVisibility(View.GONE);
|
||||||
|
mTemplate = buildTemplateEthernet();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException("unknown tab: " + currentTab);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -847,7 +847,7 @@ public class DataUsageSummary extends Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
|
final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate, true);
|
||||||
if (isNetworkPolicyModifiable()) {
|
if (isNetworkPolicyModifiable()) {
|
||||||
mDisableAtLimitView.setVisibility(View.VISIBLE);
|
mDisableAtLimitView.setVisibility(View.VISIBLE);
|
||||||
mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
|
mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
|
||||||
@@ -1365,41 +1365,57 @@ public class DataUsageSummary extends Fragment {
|
|||||||
* {@link NetworkPolicy#limitBytes}.
|
* {@link NetworkPolicy#limitBytes}.
|
||||||
*/
|
*/
|
||||||
public static class ConfirmLimitFragment extends DialogFragment {
|
public static class ConfirmLimitFragment extends DialogFragment {
|
||||||
private static final String EXTRA_MESSAGE_ID = "messageId";
|
private static final String EXTRA_MESSAGE = "message";
|
||||||
private static final String EXTRA_LIMIT_BYTES = "limitBytes";
|
private static final String EXTRA_LIMIT_BYTES = "limitBytes";
|
||||||
|
|
||||||
public static void show(DataUsageSummary parent) {
|
public static void show(DataUsageSummary parent) {
|
||||||
final Bundle args = new Bundle();
|
final Resources res = parent.getResources();
|
||||||
|
|
||||||
|
final CharSequence message;
|
||||||
|
final long limitBytes;
|
||||||
|
|
||||||
// TODO: customize default limits based on network template
|
// TODO: customize default limits based on network template
|
||||||
final String currentTab = parent.mCurrentTab;
|
final String currentTab = parent.mCurrentTab;
|
||||||
if (TAB_3G.equals(currentTab)) {
|
if (TAB_3G.equals(currentTab)) {
|
||||||
args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g);
|
message = buildDialogMessage(res, R.string.data_usage_tab_3g);
|
||||||
args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
|
limitBytes = 5 * GB_IN_BYTES;
|
||||||
} else if (TAB_4G.equals(currentTab)) {
|
} else if (TAB_4G.equals(currentTab)) {
|
||||||
args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g);
|
message = buildDialogMessage(res, R.string.data_usage_tab_4g);
|
||||||
args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
|
limitBytes = 5 * GB_IN_BYTES;
|
||||||
} else if (TAB_MOBILE.equals(currentTab)) {
|
} else if (TAB_MOBILE.equals(currentTab)) {
|
||||||
args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile);
|
message = buildDialogMessage(res, R.string.data_usage_list_mobile);
|
||||||
args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
|
limitBytes = 5 * GB_IN_BYTES;
|
||||||
|
} else if (TAB_WIFI.equals(currentTab)) {
|
||||||
|
message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
|
||||||
|
limitBytes = 5 * GB_IN_BYTES;
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("unknown current tab: " + currentTab);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final Bundle args = new Bundle();
|
||||||
|
args.putCharSequence(EXTRA_MESSAGE, message);
|
||||||
|
args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
|
||||||
|
|
||||||
final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
|
final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
|
||||||
dialog.setArguments(args);
|
dialog.setArguments(args);
|
||||||
dialog.setTargetFragment(parent, 0);
|
dialog.setTargetFragment(parent, 0);
|
||||||
dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
|
dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static CharSequence buildDialogMessage(Resources res, int networkResId) {
|
||||||
|
return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
final Context context = getActivity();
|
final Context context = getActivity();
|
||||||
|
|
||||||
final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID);
|
final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
|
||||||
final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
|
final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
|
||||||
|
|
||||||
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||||
builder.setTitle(R.string.data_usage_limit_dialog_title);
|
builder.setTitle(R.string.data_usage_limit_dialog_title);
|
||||||
builder.setMessage(messageId);
|
builder.setMessage(message);
|
||||||
|
|
||||||
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
@@ -1421,7 +1437,7 @@ public class DataUsageSummary extends Fragment {
|
|||||||
private static final String EXTRA_CYCLE_DAY = "cycleDay";
|
private static final String EXTRA_CYCLE_DAY = "cycleDay";
|
||||||
|
|
||||||
public static void show(DataUsageSummary parent) {
|
public static void show(DataUsageSummary parent) {
|
||||||
final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
|
final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate, false);
|
||||||
final Bundle args = new Bundle();
|
final Bundle args = new Bundle();
|
||||||
args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
|
args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
|
||||||
|
|
||||||
|
@@ -31,6 +31,7 @@ import android.net.NetworkPolicy;
|
|||||||
import android.net.NetworkTemplate;
|
import android.net.NetworkTemplate;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
import android.text.format.Time;
|
||||||
|
|
||||||
import com.android.internal.util.Objects;
|
import com.android.internal.util.Objects;
|
||||||
import com.google.android.collect.Lists;
|
import com.google.android.collect.Lists;
|
||||||
@@ -92,35 +93,52 @@ public class NetworkPolicyEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasLimitedPolicy(NetworkTemplate template) {
|
public boolean hasLimitedPolicy(NetworkTemplate template) {
|
||||||
final NetworkPolicy policy = getPolicy(template);
|
final NetworkPolicy policy = getPolicy(template, false);
|
||||||
return policy != null && policy.limitBytes != LIMIT_DISABLED;
|
return policy != null && policy.limitBytes != LIMIT_DISABLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NetworkPolicy getPolicy(NetworkTemplate template) {
|
public NetworkPolicy getPolicy(NetworkTemplate template, boolean createDefault) {
|
||||||
for (NetworkPolicy policy : mPolicies) {
|
for (NetworkPolicy policy : mPolicies) {
|
||||||
if (policy.template.equals(template)) {
|
if (policy.template.equals(template)) {
|
||||||
return policy;
|
return policy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
if (createDefault) {
|
||||||
|
final NetworkPolicy policy = buildDefaultPolicy(template);
|
||||||
|
mPolicies.add(policy);
|
||||||
|
return policy;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static NetworkPolicy buildDefaultPolicy(NetworkTemplate template) {
|
||||||
|
// TODO: move this into framework to share with NetworkPolicyManagerService
|
||||||
|
final Time time = new Time();
|
||||||
|
time.setToNow();
|
||||||
|
final int cycleDay = time.monthDay;
|
||||||
|
|
||||||
|
return new NetworkPolicy(
|
||||||
|
template, cycleDay, WARNING_DISABLED, LIMIT_DISABLED, SNOOZE_NEVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPolicyCycleDay(NetworkTemplate template, int cycleDay) {
|
public void setPolicyCycleDay(NetworkTemplate template, int cycleDay) {
|
||||||
final NetworkPolicy policy = getPolicy(template);
|
final NetworkPolicy policy = getPolicy(template, true);
|
||||||
policy.cycleDay = cycleDay;
|
policy.cycleDay = cycleDay;
|
||||||
policy.lastSnooze = SNOOZE_NEVER;
|
policy.lastSnooze = SNOOZE_NEVER;
|
||||||
writeAsync();
|
writeAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPolicyWarningBytes(NetworkTemplate template, long warningBytes) {
|
public void setPolicyWarningBytes(NetworkTemplate template, long warningBytes) {
|
||||||
final NetworkPolicy policy = getPolicy(template);
|
final NetworkPolicy policy = getPolicy(template, true);
|
||||||
policy.warningBytes = warningBytes;
|
policy.warningBytes = warningBytes;
|
||||||
policy.lastSnooze = SNOOZE_NEVER;
|
policy.lastSnooze = SNOOZE_NEVER;
|
||||||
writeAsync();
|
writeAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPolicyLimitBytes(NetworkTemplate template, long limitBytes) {
|
public void setPolicyLimitBytes(NetworkTemplate template, long limitBytes) {
|
||||||
final NetworkPolicy policy = getPolicy(template);
|
final NetworkPolicy policy = getPolicy(template, true);
|
||||||
policy.limitBytes = limitBytes;
|
policy.limitBytes = limitBytes;
|
||||||
policy.lastSnooze = SNOOZE_NEVER;
|
policy.lastSnooze = SNOOZE_NEVER;
|
||||||
writeAsync();
|
writeAsync();
|
||||||
@@ -158,8 +176,8 @@ public class NetworkPolicyEditor {
|
|||||||
|
|
||||||
} else if (beforeSplit && !split) {
|
} else if (beforeSplit && !split) {
|
||||||
// combine, picking most restrictive policy
|
// combine, picking most restrictive policy
|
||||||
final NetworkPolicy policy3g = getPolicy(template3g);
|
final NetworkPolicy policy3g = getPolicy(template3g, false);
|
||||||
final NetworkPolicy policy4g = getPolicy(template4g);
|
final NetworkPolicy policy4g = getPolicy(template4g, false);
|
||||||
|
|
||||||
final NetworkPolicy restrictive = policy3g.compareTo(policy4g) < 0 ? policy3g
|
final NetworkPolicy restrictive = policy3g.compareTo(policy4g) < 0 ? policy3g
|
||||||
: policy4g;
|
: policy4g;
|
||||||
@@ -172,7 +190,7 @@ public class NetworkPolicyEditor {
|
|||||||
|
|
||||||
} else if (!beforeSplit && split) {
|
} else if (!beforeSplit && split) {
|
||||||
// duplicate existing policy into two rules
|
// duplicate existing policy into two rules
|
||||||
final NetworkPolicy policyAll = getPolicy(templateAll);
|
final NetworkPolicy policyAll = getPolicy(templateAll, false);
|
||||||
mPolicies.remove(policyAll);
|
mPolicies.remove(policyAll);
|
||||||
mPolicies.add(
|
mPolicies.add(
|
||||||
new NetworkPolicy(template3g, policyAll.cycleDay, policyAll.warningBytes,
|
new NetworkPolicy(template3g, policyAll.cycleDay, policyAll.warningBytes,
|
||||||
|
Reference in New Issue
Block a user