Refactor DataUsagePreference

Move the logic to PreferenceController

Bug: 114749736
Test: RunSettingsRoboTests
Change-Id: I7a0845c0cbc3f33e23257831d0d4ed97e7a5cc04
This commit is contained in:
jackqdyulei
2018-10-15 13:35:24 -07:00
parent 1d133e3f04
commit 5680477b8b
6 changed files with 211 additions and 92 deletions

View File

@@ -10259,6 +10259,9 @@
<!-- Mobile network settings screen, message asking the user to check their pricing with their Carrier, when enabling Data roaming. [CHAR LIMIT=NONE] --> <!-- Mobile network settings screen, message asking the user to check their pricing with their Carrier, when enabling Data roaming. [CHAR LIMIT=NONE] -->
<string name="roaming_check_price_warning">Check with your network provider for pricing.</string> <string name="roaming_check_price_warning">Check with your network provider for pricing.</string>
<!-- Title for mobile data preference, to display the mobile data usage for each app. [CHAR LIMIT=NONE]-->
<string name="mobile_data_usage_title">App data usage</string>
<!-- Available networks screen, summary when button disallowed due to permanent automatic mode [CHAR LIMIT=NONE] --> <!-- Available networks screen, summary when button disallowed due to permanent automatic mode [CHAR LIMIT=NONE] -->
<string name="manual_mode_disallowed_summary">Unavailable when connected to <xliff:g id="carrier" example="verizon">%1$s</xliff:g></string> <string name="manual_mode_disallowed_summary">Unavailable when connected to <xliff:g id="carrier" example="verizon">%1$s</xliff:g></string>
</resources> </resources>

View File

@@ -38,9 +38,10 @@
android:summaryOn="@string/roaming_enable" android:summaryOn="@string/roaming_enable"
android:summaryOff="@string/roaming_disable"/> android:summaryOff="@string/roaming_disable"/>
<com.android.settings.network.telephony.DataUsagePreference <Preference
android:key="data_usage_summary" android:key="data_usage_summary"
android:title="@string/data_usage_title" /> android:title="@string/mobile_data_usage_title"
settings:controller="com.android.settings.network.telephony.DataUsagePreferenceController"/>
<SwitchPreference <SwitchPreference
android:key="enhanced_4g_lte" android:key="enhanced_4g_lte"

View File

@@ -1,79 +0,0 @@
/*
* Copyright (C) 2018 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.network.telephony;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkTemplate;
import androidx.preference.Preference;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.AttributeSet;
import com.android.settings.R;
import com.android.settingslib.net.DataUsageController;
/**
* The preference that shows mobile data usage summary and
* leads to mobile data usage list page.
*/
public class DataUsagePreference extends Preference {
private NetworkTemplate mTemplate;
private int mSubId;
public DataUsagePreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* After creating this preference, this functions needs to be called to
* initialize which subID it connects to.
*/
public void initialize(int subId) {
final Context context = getContext();
mSubId = subId;
mTemplate = getNetworkTemplate(context, subId);
DataUsageController controller = new DataUsageController(context);
DataUsageController.DataUsageInfo usageInfo = controller.getDataUsageInfo(mTemplate);
setSummary(context.getString(R.string.data_usage_template,
Formatter.formatFileSize(context, usageInfo.usageLevel), usageInfo.period));
setIntent(getIntent());
}
@Override
public Intent getIntent() {
Intent intent = new Intent(Settings.ACTION_MOBILE_DATA_USAGE);
intent.putExtra(Settings.EXTRA_NETWORK_TEMPLATE, mTemplate);
intent.putExtra(Settings.EXTRA_SUB_ID, mSubId);
return intent;
}
private NetworkTemplate getNetworkTemplate(Context context, int subId) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
tm.getSubscriberId(subId));
return NetworkTemplate.normalize(mobileAll,
tm.getMergedSubscriberIds());
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright (C) 2018 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.network.telephony;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkTemplate;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.text.format.Formatter;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.net.DataUsageController;
/**
* Preference controller for "Data usage"
*/
public class DataUsagePreferenceController extends BasePreferenceController {
private NetworkTemplate mTemplate;
private DataUsageController.DataUsageInfo mDataUsageInfo;
private Intent mIntent;
private int mSubId;
public DataUsagePreferenceController(Context context, String key) {
super(context, key);
}
@Override
public int getAvailabilityStatus() {
return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
? AVAILABLE
: AVAILABLE_UNSEARCHABLE;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
mContext.startActivity(mIntent);
return true;
}
return false;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
final boolean enabled = mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID;
preference.setEnabled(enabled);
if (enabled) {
preference.setSummary(mContext.getString(R.string.data_usage_template,
Formatter.formatFileSize(mContext, mDataUsageInfo.usageLevel),
mDataUsageInfo.period));
}
}
public void init(int subId) {
mSubId = subId;
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
mTemplate = getNetworkTemplate(mContext, subId);
final DataUsageController controller = new DataUsageController(mContext);
mDataUsageInfo = controller.getDataUsageInfo(mTemplate);
mIntent = new Intent(Settings.ACTION_MOBILE_DATA_USAGE);
mIntent.putExtra(Settings.EXTRA_NETWORK_TEMPLATE, mTemplate);
mIntent.putExtra(Settings.EXTRA_SUB_ID, mSubId);
}
}
private NetworkTemplate getNetworkTemplate(Context context, int subId) {
final TelephonyManager tm = TelephonyManager.from(context).createForSubscriptionId(subId);
NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(tm.getSubscriberId());
return NetworkTemplate.normalize(mobileAll, tm.getMergedSubscriberIds());
}
}

View File

@@ -161,7 +161,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
private Preference mWiFiCallingPref; private Preference mWiFiCallingPref;
private SwitchPreference mVideoCallingPref; private SwitchPreference mVideoCallingPref;
private NetworkSelectListPreference mButtonNetworkSelect; private NetworkSelectListPreference mButtonNetworkSelect;
private DataUsagePreference mDataUsagePref;
private CdmaSystemSelectPreferenceController mCdmaSystemSelectPreferenceController; private CdmaSystemSelectPreferenceController mCdmaSystemSelectPreferenceController;
private CdmaSubscriptionPreferenceController mCdmaSubscriptionPreferenceController; private CdmaSubscriptionPreferenceController mCdmaSubscriptionPreferenceController;
@@ -307,8 +306,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
Intent intent = new Intent(EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS); Intent intent = new Intent(EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS);
startActivity(intent); startActivity(intent);
return true; return true;
} else if (preference == mWiFiCallingPref || preference == mVideoCallingPref } else if (preference == mWiFiCallingPref || preference == mVideoCallingPref) {
|| preference == mDataUsagePref) {
return false; return false;
} else { } else {
// if the button is anything but the simple toggle preference, // if the button is anything but the simple toggle preference,
@@ -402,6 +400,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId); use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
use(CdmaApnPreferenceController.class).init(mSubId); use(CdmaApnPreferenceController.class).init(mSubId);
use(CarrierPreferenceController.class).init(mSubId); use(CarrierPreferenceController.class).init(mSubId);
use(DataUsagePreferenceController.class).init(mSubId);
mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class); mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId); mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);
@@ -433,7 +432,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
mCallingCategory = (PreferenceCategory) findPreference(CATEGORY_CALLING_KEY); mCallingCategory = (PreferenceCategory) findPreference(CATEGORY_CALLING_KEY);
mWiFiCallingPref = findPreference(BUTTON_WIFI_CALLING_KEY); mWiFiCallingPref = findPreference(BUTTON_WIFI_CALLING_KEY);
mVideoCallingPref = (SwitchPreference) findPreference(BUTTON_VIDEO_CALLING_KEY); mVideoCallingPref = (SwitchPreference) findPreference(BUTTON_VIDEO_CALLING_KEY);
mDataUsagePref = (DataUsagePreference) findPreference(BUTTON_DATA_USAGE_KEY);
try { try {
Context con = context.createPackageContext("com.android.systemui", 0); Context con = context.createPackageContext("com.android.systemui", 0);
@@ -583,14 +581,10 @@ public class MobileNetworkFragment extends DashboardFragment implements
} }
prefSet.addPreference(mButtonDataRoam); prefSet.addPreference(mButtonDataRoam);
prefSet.addPreference(mDataUsagePref);
mButtonDataRoam.setEnabled(hasActiveSubscriptions); mButtonDataRoam.setEnabled(hasActiveSubscriptions);
mDataUsagePref.setEnabled(hasActiveSubscriptions);
if (hasActiveSubscriptions) { if (hasActiveSubscriptions) {
// Customized preferences needs to be initialized with subId.
mDataUsagePref.initialize(phoneSubId);
// Initialize states of mButtonDataRoam. // Initialize states of mButtonDataRoam.
mButtonDataRoam.setChecked(mTelephonyManager.isDataRoamingEnabled()); mButtonDataRoam.setChecked(mTelephonyManager.isDataRoamingEnabled());
@@ -1701,7 +1695,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
// For ListPreferences, we log it here without a value, only indicating it's clicked to // For ListPreferences, we log it here without a value, only indicating it's clicked to
// open the list dialog. When a value is chosen, another MetricsEvent is logged with // open the list dialog. When a value is chosen, another MetricsEvent is logged with
// new value in onPreferenceChange. // new value in onPreferenceChange.
if (preference == mLteDataServicePref || preference == mDataUsagePref if (preference == mLteDataServicePref
|| preference == mEuiccSettingsPref || preference == mEuiccSettingsPref
|| preference == mWiFiCallingPref || preference == mButtonPreferredNetworkMode || preference == mWiFiCallingPref || preference == mButtonPreferredNetworkMode
|| preference == mButtonEnabledNetworks || preference == mButtonEnabledNetworks
@@ -1742,8 +1736,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
return MetricsProto.MetricsEvent.VIEW_UNKNOWN; return MetricsProto.MetricsEvent.VIEW_UNKNOWN;
} else if (preference == mButtonDataRoam) { } else if (preference == mButtonDataRoam) {
return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_DATA_ROAMING_TOGGLE; return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_DATA_ROAMING_TOGGLE;
} else if (preference == mDataUsagePref) {
return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_DATA_USAGE;
} else if (preference == mLteDataServicePref) { } else if (preference == mLteDataServicePref) {
return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_SET_UP_DATA_SERVICE; return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_SET_UP_DATA_SERVICE;
} else if (preference == mButton4glte) { } else if (preference == mButton4glte) {

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2018 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.network.telephony;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.SwitchPreference;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class DataUsagePreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private TelephonyManager mTelephonyManager;
private DataUsagePreferenceController mController;
private SwitchPreference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
mPreference = new SwitchPreference(mContext);
mController = new DataUsagePreferenceController(mContext, "data_usage");
mController.init(SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_validSubId_returnAvailable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_invalidSubId_returnUnsearchable() {
mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
}
@Test
public void handlePreferenceTreeClick_needDialog_showDialog() {
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
mController.handlePreferenceTreeClick(mPreference);
verify(mContext).startActivity(captor.capture());
final Intent intent = captor.getValue();
assertThat(intent.getAction()).isEqualTo(Settings.ACTION_MOBILE_DATA_USAGE);
assertThat(intent.getIntExtra(Settings.EXTRA_SUB_ID, 0)).isEqualTo(SUB_ID);
}
@Test
public void updateState_invalidSubId_disabled() {
mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
}