Data Usage summary creates a dialog to change data warning level

Clicking the Data usage summary preference will open the same dialog
that billing cycle does to modify data warning level. The Dialog
was abstracted to be used outside of the Billing Cycle settings
fragment.

Test: Robo tests in next cherry-pick.

Change-Id: I70398d4656b6da4233b2bfa0da6ffa7402ead69b
Fixes: 26932938
(cherry picked from commit 9fad289ca9)
This commit is contained in:
Matthew Fritze
2016-08-12 09:37:43 -07:00
parent ed765a4817
commit 25437d7c61
3 changed files with 115 additions and 28 deletions

View File

@@ -17,6 +17,7 @@ package com.android.settings.datausage;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.app.DialogFragment; import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
@@ -44,7 +45,7 @@ import static android.net.TrafficStats.GB_IN_BYTES;
import static android.net.TrafficStats.MB_IN_BYTES; import static android.net.TrafficStats.MB_IN_BYTES;
public class BillingCycleSettings extends DataUsageBase implements public class BillingCycleSettings extends DataUsageBase implements
Preference.OnPreferenceChangeListener { Preference.OnPreferenceChangeListener, DataUsageEditController {
private static final String TAG = "BillingCycleSettings"; private static final String TAG = "BillingCycleSettings";
private static final boolean LOGD = false; private static final boolean LOGD = false;
@@ -147,33 +148,52 @@ public class BillingCycleSettings extends DataUsageBase implements
updatePrefs(); updatePrefs();
} }
@Override
public NetworkPolicyEditor getNetworkPolicyEditor() {
return services.mPolicyEditor;
}
@Override
public NetworkTemplate getNetworkTemplate() {
return mNetworkTemplate;
}
@Override
public void updateDataUsage() {
updatePrefs();
}
/** /**
* Dialog to edit {@link NetworkPolicy#warningBytes}. * Dialog to edit {@link NetworkPolicy#warningBytes}.
*/ */
public static class BytesEditorFragment extends DialogFragment public static class BytesEditorFragment extends DialogFragment
implements DialogInterface.OnClickListener{ implements DialogInterface.OnClickListener {
private static final String EXTRA_TEMPLATE = "template"; private static final String EXTRA_TEMPLATE = "template";
private static final String EXTRA_LIMIT = "limit"; private static final String EXTRA_LIMIT = "limit";
private View mView; private View mView;
public static void show(BillingCycleSettings parent, boolean isLimit) { public static void show(DataUsageEditController parent, boolean isLimit) {
if (!parent.isAdded()) return; if (!(parent instanceof Fragment)) {
return;
}
Fragment targetFragment = (Fragment) parent;
if (!targetFragment.isAdded()) {
return;
}
final Bundle args = new Bundle(); final Bundle args = new Bundle();
args.putParcelable(EXTRA_TEMPLATE, parent.mNetworkTemplate); args.putParcelable(EXTRA_TEMPLATE, parent.getNetworkTemplate());
args.putBoolean(EXTRA_LIMIT, isLimit); args.putBoolean(EXTRA_LIMIT, isLimit);
final BytesEditorFragment dialog = new BytesEditorFragment(); final BytesEditorFragment dialog = new BytesEditorFragment();
dialog.setArguments(args); dialog.setArguments(args);
dialog.setTargetFragment(parent, 0); dialog.setTargetFragment(targetFragment, 0);
dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR); dialog.show(targetFragment.getFragmentManager(), TAG_WARNING_EDITOR);
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity(); final Context context = getActivity();
final LayoutInflater dialogInflater = LayoutInflater.from(context); final LayoutInflater dialogInflater = LayoutInflater.from(context);
final boolean isLimit = getArguments().getBoolean(EXTRA_LIMIT); final boolean isLimit = getArguments().getBoolean(EXTRA_LIMIT);
mView = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false); mView = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
@@ -188,8 +208,8 @@ public class BillingCycleSettings extends DataUsageBase implements
} }
private void setupPicker(EditText bytesPicker, Spinner type) { private void setupPicker(EditText bytesPicker, Spinner type) {
final BillingCycleSettings target = (BillingCycleSettings) getTargetFragment(); final DataUsageEditController target = (DataUsageEditController) getTargetFragment();
final NetworkPolicyEditor editor = target.services.mPolicyEditor; final NetworkPolicyEditor editor = target.getNetworkPolicyEditor();
final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
final boolean isLimit = getArguments().getBoolean(EXTRA_LIMIT); final boolean isLimit = getArguments().getBoolean(EXTRA_LIMIT);
@@ -222,8 +242,8 @@ public class BillingCycleSettings extends DataUsageBase implements
if (which != DialogInterface.BUTTON_POSITIVE) { if (which != DialogInterface.BUTTON_POSITIVE) {
return; return;
} }
final BillingCycleSettings target = (BillingCycleSettings) getTargetFragment(); final DataUsageEditController target = (DataUsageEditController) getTargetFragment();
final NetworkPolicyEditor editor = target.services.mPolicyEditor; final NetworkPolicyEditor editor = target.getNetworkPolicyEditor();
final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
final boolean isLimit = getArguments().getBoolean(EXTRA_LIMIT); final boolean isLimit = getArguments().getBoolean(EXTRA_LIMIT);
@@ -241,7 +261,7 @@ public class BillingCycleSettings extends DataUsageBase implements
} else { } else {
editor.setPolicyWarningBytes(template, bytes); editor.setPolicyWarningBytes(template, bytes);
} }
target.updatePrefs(); target.updateDataUsage();
} }
} }
@@ -249,7 +269,7 @@ public class BillingCycleSettings extends DataUsageBase implements
* Dialog to edit {@link NetworkPolicy#cycleDay}. * Dialog to edit {@link NetworkPolicy#cycleDay}.
*/ */
public static class CycleEditorFragment extends DialogFragment implements public static class CycleEditorFragment extends DialogFragment implements
DialogInterface.OnClickListener{ DialogInterface.OnClickListener {
private static final String EXTRA_TEMPLATE = "template"; private static final String EXTRA_TEMPLATE = "template";
private NumberPicker mCycleDayPicker; private NumberPicker mCycleDayPicker;
@@ -268,8 +288,8 @@ public class BillingCycleSettings extends DataUsageBase implements
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity(); final Context context = getActivity();
final BillingCycleSettings target = (BillingCycleSettings) getTargetFragment(); final DataUsageEditController target = (DataUsageEditController) getTargetFragment();
final NetworkPolicyEditor editor = target.services.mPolicyEditor; final NetworkPolicyEditor editor = target.getNetworkPolicyEditor();
final AlertDialog.Builder builder = new AlertDialog.Builder(context); final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
@@ -294,8 +314,8 @@ public class BillingCycleSettings extends DataUsageBase implements
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
final BillingCycleSettings target = (BillingCycleSettings) getTargetFragment(); final DataUsageEditController target = (DataUsageEditController) getTargetFragment();
final NetworkPolicyEditor editor = target.services.mPolicyEditor; final NetworkPolicyEditor editor = target.getNetworkPolicyEditor();
// clear focus to finish pending text edits // clear focus to finish pending text edits
mCycleDayPicker.clearFocus(); mCycleDayPicker.clearFocus();
@@ -303,7 +323,7 @@ public class BillingCycleSettings extends DataUsageBase implements
final int cycleDay = mCycleDayPicker.getValue(); final int cycleDay = mCycleDayPicker.getValue();
final String cycleTimezone = new Time().timezone; final String cycleTimezone = new Time().timezone;
editor.setPolicyCycleDay(template, cycleDay, cycleTimezone); editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
target.updatePrefs(); target.updateDataUsage();
} }
} }
@@ -312,7 +332,7 @@ public class BillingCycleSettings extends DataUsageBase implements
* {@link NetworkPolicy#limitBytes}. * {@link NetworkPolicy#limitBytes}.
*/ */
public static class ConfirmLimitFragment extends DialogFragment implements public static class ConfirmLimitFragment extends DialogFragment implements
DialogInterface.OnClickListener{ DialogInterface.OnClickListener {
private static final String EXTRA_MESSAGE = "message"; 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 final float FLOAT = 1.2f; public static final float FLOAT = 1.2f;

View File

@@ -0,0 +1,38 @@
/*
* 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.net.NetworkTemplate;
import com.android.settingslib.NetworkPolicyEditor;
/**
* Used to create a dialog that modifies the Cellular data warning amount.
*/
public interface DataUsageEditController {
/**
* @return NetworkPolicyEditor to update the values of the data warning and usage limits.
*/
NetworkPolicyEditor getNetworkPolicyEditor();
/**
* @return NetworkTemplate to get the currently set values of the data warning and usage limits.
*/
NetworkTemplate getNetworkTemplate();
/**
* Callback to update the UI and values changed by the Dialog.
*/
void updateDataUsage();
}

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.INetworkStatsSession; import android.net.INetworkStatsSession;
import android.net.NetworkPolicy;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.net.TrafficStats; import android.net.TrafficStats;
import android.os.Bundle; import android.os.Bundle;
@@ -48,6 +49,7 @@ import com.android.settings.Utils;
import com.android.settings.dashboard.SummaryLoader; import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable; import com.android.settings.search.Indexable;
import com.android.settingslib.NetworkPolicyEditor;
import com.android.settingslib.net.DataUsageController; import com.android.settingslib.net.DataUsageController;
import java.util.ArrayList; import java.util.ArrayList;
@@ -56,7 +58,7 @@ import java.util.List;
import static android.net.ConnectivityManager.TYPE_ETHERNET; import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
public class DataUsageSummary extends DataUsageBase implements Indexable { public class DataUsageSummary extends DataUsageBase implements Indexable, DataUsageEditController {
private static final String TAG = "DataUsageSummary"; private static final String TAG = "DataUsageSummary";
static final boolean LOGD = false; static final boolean LOGD = false;
@@ -87,15 +89,13 @@ public class DataUsageSummary extends DataUsageBase implements Indexable {
hasMobileData = false; hasMobileData = false;
} }
mDefaultTemplate = getDefaultTemplate(getContext(), defaultSubId); mDefaultTemplate = getDefaultTemplate(getContext(), defaultSubId);
if (hasMobileData) { mSummaryPreference = (SummaryPreference) findPreference(KEY_STATUS_HEADER);
mLimitPreference = findPreference(KEY_LIMIT_SUMMARY);
} else {
removePreference(KEY_LIMIT_SUMMARY);
}
if (!hasMobileData || !isAdmin()) { if (!hasMobileData || !isAdmin()) {
removePreference(KEY_RESTRICT_BACKGROUND); removePreference(KEY_RESTRICT_BACKGROUND);
} }
if (hasMobileData) { if (hasMobileData) {
mLimitPreference = findPreference(KEY_LIMIT_SUMMARY);
List<SubscriptionInfo> subscriptions = List<SubscriptionInfo> subscriptions =
services.mSubscriptionManager.getActiveSubscriptionInfoList(); services.mSubscriptionManager.getActiveSubscriptionInfoList();
if (subscriptions == null || subscriptions.size() == 0) { if (subscriptions == null || subscriptions.size() == 0) {
@@ -104,6 +104,10 @@ public class DataUsageSummary extends DataUsageBase implements Indexable {
for (int i = 0; subscriptions != null && i < subscriptions.size(); i++) { for (int i = 0; subscriptions != null && i < subscriptions.size(); i++) {
addMobileSection(subscriptions.get(i).getSubscriptionId()); addMobileSection(subscriptions.get(i).getSubscriptionId());
} }
mSummaryPreference.setSelectable(true);
} else {
removePreference(KEY_LIMIT_SUMMARY);
mSummaryPreference.setSelectable(false);
} }
boolean hasWifiRadio = hasWifiRadio(getContext()); boolean hasWifiRadio = hasWifiRadio(getContext());
if (hasWifiRadio) { if (hasWifiRadio) {
@@ -116,7 +120,6 @@ public class DataUsageSummary extends DataUsageBase implements Indexable {
: hasWifiRadio ? R.string.wifi_data_template : hasWifiRadio ? R.string.wifi_data_template
: R.string.ethernet_data_template; : R.string.ethernet_data_template;
mSummaryPreference = (SummaryPreference) findPreference(KEY_STATUS_HEADER);
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
@@ -142,6 +145,15 @@ public class DataUsageSummary extends DataUsageBase implements Indexable {
return false; 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);
}
private void addMobileSection(int subId) { private void addMobileSection(int subId) {
TemplatePreferenceCategory category = (TemplatePreferenceCategory) TemplatePreferenceCategory category = (TemplatePreferenceCategory)
inflatePreferences(R.xml.data_usage_cellular); inflatePreferences(R.xml.data_usage_cellular);
@@ -224,6 +236,8 @@ public class DataUsageSummary extends DataUsageBase implements Indexable {
DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo( DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo(
mDefaultTemplate); mDefaultTemplate);
Context context = getContext(); Context context = getContext();
NetworkPolicy policy = services.mPolicyEditor.getPolicy(mDefaultTemplate);
info.warningLevel = policy.warningBytes;
if (mSummaryPreference != null) { if (mSummaryPreference != null) {
mSummaryPreference.setTitle( mSummaryPreference.setTitle(
formatTitle(context, getString(mDataUsageTemplate), info.usageLevel)); formatTitle(context, getString(mDataUsageTemplate), info.usageLevel));
@@ -258,6 +272,21 @@ public class DataUsageSummary extends DataUsageBase implements Indexable {
return MetricsEvent.DATA_USAGE_SUMMARY; return MetricsEvent.DATA_USAGE_SUMMARY;
} }
@Override
public NetworkPolicyEditor getNetworkPolicyEditor() {
return services.mPolicyEditor;
}
@Override
public NetworkTemplate getNetworkTemplate() {
return mDefaultTemplate;
}
@Override
public void updateDataUsage() {
updateState();
}
/** /**
* Test if device has an ethernet network connection. * Test if device has an ethernet network connection.
*/ */