Merge "Update the mobile activity"
diff --git a/src/com/android/settings/network/telephony/MobileNetworkFragment.java b/src/com/android/settings/network/telephony/MobileNetworkFragment.java
index df6c7ab..e6db51c 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkFragment.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkFragment.java
@@ -22,6 +22,7 @@
import android.os.Bundle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
+import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
@@ -106,7 +107,7 @@
@Override
public void onAttach(Context context) {
super.onAttach(context);
- mSubId = getArguments().getInt(MobileSettingsActivity.KEY_SUBSCRIPTION_ID,
+ mSubId = getArguments().getInt(Settings.EXTRA_SUB_ID,
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
diff --git a/src/com/android/settings/network/telephony/MobileSettingsActivity.java b/src/com/android/settings/network/telephony/MobileSettingsActivity.java
index d712515..b2bceb9 100644
--- a/src/com/android/settings/network/telephony/MobileSettingsActivity.java
+++ b/src/com/android/settings/network/telephony/MobileSettingsActivity.java
@@ -17,10 +17,15 @@
package com.android.settings.network.telephony;
import android.app.ActionBar;
+import android.content.BroadcastReceiver;
+import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.os.Bundle;
+import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
+import android.util.Log;
import android.view.Menu;
import android.view.View;
@@ -30,62 +35,61 @@
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
+import com.android.internal.telephony.TelephonyIntents;
import com.android.internal.util.CollectionUtils;
import com.android.settings.R;
import com.android.settings.core.SettingsBaseActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
+import java.util.Objects;
public class MobileSettingsActivity extends SettingsBaseActivity {
+ private static final String TAG = "MobileSettingsActivity";
@VisibleForTesting
static final String MOBILE_SETTINGS_TAG = "mobile_settings:";
- public static final String KEY_SUBSCRIPTION_ID = "key_subscription_id";
- public static final String KEY_CUR_SUBSCRIPTION_ID = "key_cur_subscription_id";
+ @VisibleForTesting
+ static final int SUB_ID_NULL = Integer.MIN_VALUE;
- private SubscriptionManager mSubscriptionManager;
@VisibleForTesting
- Integer mCurSubscriptionId;
+ SubscriptionManager mSubscriptionManager;
@VisibleForTesting
- List<SubscriptionInfo> mSubscriptionInfos;
+ int mCurSubscriptionId;
+ @VisibleForTesting
+ List<SubscriptionInfo> mSubscriptionInfos = new ArrayList<>();
+ private PhoneChangeReceiver mPhoneChangeReceiver;
private final SubscriptionManager.OnSubscriptionsChangedListener
mOnSubscriptionsChangeListener
= new SubscriptionManager.OnSubscriptionsChangedListener() {
@Override
public void onSubscriptionsChanged() {
- updateSubscriptions(null);
+ if (!Objects.equals(mSubscriptionInfos,
+ mSubscriptionManager.getActiveSubscriptionInfoList())) {
+ updateSubscriptions(null);
+ }
}
};
@Override
- protected void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- //TODO(b/114749736): update fragment by new intent, or at least make sure this page shows
- // current tab for sim card
- }
-
- @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
-
- //TODO(b/114749736): add phone change receiver here: ACTION_RADIO_TECHNOLOGY_CHANGED
-
setContentView(R.layout.mobile_settings_container);
setActionBar(findViewById(R.id.mobile_action_bar));
+ mPhoneChangeReceiver = new PhoneChangeReceiver();
mSubscriptionManager = getSystemService(SubscriptionManager.class);
mSubscriptionInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
mCurSubscriptionId = savedInstanceState != null
- ? savedInstanceState.getInt(KEY_CUR_SUBSCRIPTION_ID)
- : null;
-
- mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+ ? savedInstanceState.getInt(Settings.EXTRA_SUB_ID, SUB_ID_NULL)
+ : SUB_ID_NULL;
final ActionBar actionBar = getActionBar();
if (actionBar != null) {
- // android.R.id.home will be triggered in onOptionsItemSelected()
actionBar.setDisplayHomeAsUpEnabled(true);
}
@@ -93,6 +97,22 @@
}
@Override
+ protected void onStart() {
+ super.onStart();
+ final IntentFilter intentFilter = new IntentFilter(
+ TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
+ registerReceiver(mPhoneChangeReceiver, intentFilter);
+ mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+ }
+
+ @Override
+ protected void onStop() {
+ super.onStop();
+ unregisterReceiver(mPhoneChangeReceiver);
+ mSubscriptionManager.removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+ }
+
+ @Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
saveInstanceState(outState);
@@ -100,24 +120,42 @@
@VisibleForTesting
void saveInstanceState(@NonNull Bundle outState) {
- outState.putInt(KEY_CUR_SUBSCRIPTION_ID, mCurSubscriptionId);
+ outState.putInt(Settings.EXTRA_SUB_ID, mCurSubscriptionId);
}
@VisibleForTesting
void updateSubscriptions(Bundle savedInstanceState) {
- //TODO(b/114749736): Sort it by phoneId
mSubscriptionInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
- final int subId = CollectionUtils.isEmpty(mSubscriptionInfos)
- ? SubscriptionManager.INVALID_SUBSCRIPTION_ID
- : mSubscriptionInfos.get(0).getSubscriptionId();
updateBottomNavigationView();
if (savedInstanceState == null) {
- switchFragment(new MobileNetworkFragment(), subId);
+ switchFragment(new MobileNetworkFragment(), getSubscriptionId());
}
}
+ /**
+ * Get the current subId to display. First check whether intent has {@link
+ * Settings#EXTRA_SUB_ID}. If not, just display first one in list
+ * since it is already sorted by sim slot.
+ */
+ @VisibleForTesting
+ int getSubscriptionId() {
+ final Intent intent = getIntent();
+ if (intent != null) {
+ final int subId = intent.getIntExtra(Settings.EXTRA_SUB_ID, SUB_ID_NULL);
+ if (subId != SUB_ID_NULL && mSubscriptionManager.isActiveSubscriptionId(subId)) {
+ return subId;
+ }
+ }
+
+ if (CollectionUtils.isEmpty(mSubscriptionInfos)) {
+ return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+ }
+
+ return mSubscriptionInfos.get(0).getSubscriptionId();
+ }
+
@VisibleForTesting
void updateBottomNavigationView() {
final BottomNavigationView navigation = findViewById(R.id.bottom_nav);
@@ -130,7 +168,8 @@
for (int i = 0, size = mSubscriptionInfos.size(); i < size; i++) {
final SubscriptionInfo subscriptionInfo = mSubscriptionInfos.get(i);
menu.add(0, subscriptionInfo.getSubscriptionId(), i,
- subscriptionInfo.getDisplayName());
+ subscriptionInfo.getDisplayName())
+ .setIcon(R.drawable.ic_settings_sim);
}
navigation.setOnNavigationItemSelectedListener(item -> {
switchFragment(new MobileNetworkFragment(), item.getItemId());
@@ -141,30 +180,23 @@
@VisibleForTesting
void switchFragment(Fragment fragment, int subscriptionId) {
- if (mCurSubscriptionId != null && subscriptionId == mCurSubscriptionId) {
+ switchFragment(fragment, subscriptionId, false /* forceUpdate */);
+ }
+
+ @VisibleForTesting
+ void switchFragment(Fragment fragment, int subscriptionId, boolean forceUpdate) {
+ if (mCurSubscriptionId != SUB_ID_NULL && subscriptionId == mCurSubscriptionId
+ && !forceUpdate) {
return;
}
final FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final Bundle bundle = new Bundle();
- bundle.putInt(KEY_SUBSCRIPTION_ID, subscriptionId);
+ bundle.putInt(Settings.EXTRA_SUB_ID, subscriptionId);
- if (mCurSubscriptionId != null) {
- final Fragment hideFragment = fragmentManager.findFragmentByTag(
- buildFragmentTag(mCurSubscriptionId));
- if (hideFragment != null) {
- fragmentTransaction.hide(hideFragment);
- }
- }
-
- Fragment showFragment = fragmentManager.findFragmentByTag(buildFragmentTag(subscriptionId));
- if (showFragment == null) {
- fragment.setArguments(bundle);
- fragmentTransaction.add(R.id.main_content, fragment, buildFragmentTag(subscriptionId));
- } else {
- showFragment.setArguments(bundle);
- fragmentTransaction.show(showFragment);
- }
+ fragment.setArguments(bundle);
+ fragmentTransaction.replace(R.id.main_content, fragment,
+ buildFragmentTag(subscriptionId));
fragmentTransaction.commit();
mCurSubscriptionId = subscriptionId;
}
@@ -172,4 +204,16 @@
private String buildFragmentTag(int subscriptionId) {
return MOBILE_SETTINGS_TAG + subscriptionId;
}
+
+ private class PhoneChangeReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ // When the radio changes (ex: CDMA->GSM), refresh the fragment.
+ // This is very rare to happen.
+ if (mCurSubscriptionId != SUB_ID_NULL) {
+ switchFragment(new MobileNetworkFragment(), mCurSubscriptionId,
+ true /* forceUpdate */);
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/src/com/android/settings/network/telephony/NetworkSelectSettings.java b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
index 3e42c70..dbab674 100644
--- a/src/com/android/settings/network/telephony/NetworkSelectSettings.java
+++ b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
@@ -23,6 +23,7 @@
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
+import android.provider.Settings;
import android.telephony.AccessNetworkConstants;
import android.telephony.CarrierConfigManager;
import android.telephony.CellIdentity;
@@ -93,7 +94,7 @@
mUseNewApi = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI);
- mSubId = getArguments().getInt(MobileSettingsActivity.KEY_SUBSCRIPTION_ID);
+ mSubId = getArguments().getInt(Settings.EXTRA_SUB_ID);
mConnectedPreferenceCategory =
(PreferenceCategory) findPreference(PREF_KEY_CONNECTED_NETWORK_OPERATOR);
diff --git a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
index 72f69cd..d5c704a 100644
--- a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
@@ -19,6 +19,7 @@
import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
+import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -96,7 +97,7 @@
== TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
} else {
final Bundle bundle = new Bundle();
- bundle.putInt(MobileSettingsActivity.KEY_SUBSCRIPTION_ID, mSubId);
+ bundle.putInt(Settings.EXTRA_SUB_ID, mSubId);
new SubSettingLauncher(mContext)
.setDestination(NetworkSelectSettings.class.getName())
.setSourceMetricsCategory(MetricsProto.MetricsEvent.MOBILE_NETWORK_SELECT)
diff --git a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
index 9ea977c..df5e151 100644
--- a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
@@ -18,6 +18,7 @@
import android.content.Context;
import android.os.Bundle;
+import android.provider.Settings;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -84,7 +85,7 @@
public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
final Bundle bundle = new Bundle();
- bundle.putInt(MobileSettingsActivity.KEY_SUBSCRIPTION_ID, mSubId);
+ bundle.putInt(Settings.EXTRA_SUB_ID, mSubId);
new SubSettingLauncher(mContext)
.setDestination(NetworkSelectSettings.class.getName())
.setSourceMetricsCategory(MetricsProto.MetricsEvent.MOBILE_NETWORK_SELECT)
diff --git a/tests/robotests/src/com/android/settings/network/telephony/MobileSettingsActivityTest.java b/tests/robotests/src/com/android/settings/network/telephony/MobileSettingsActivityTest.java
index c5ca427..ea184fd 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/MobileSettingsActivityTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/MobileSettingsActivityTest.java
@@ -23,9 +23,12 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import android.content.Context;
+import android.content.Intent;
import android.os.Bundle;
+import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.view.Menu;
@@ -69,6 +72,8 @@
@Mock
private SubscriptionInfo mSubscriptionInfo;
@Mock
+ private SubscriptionInfo mSubscriptionInfo2;
+ @Mock
private FragmentManager mFragmentManager;
@Mock
private FragmentTransaction mFragmentTransaction;
@@ -85,6 +90,9 @@
mShowFragment = new Fragment();
mHideFragment = new Fragment();
mMobileSettingsActivity.mSubscriptionInfos = mSubscriptionInfos;
+ mMobileSettingsActivity.mSubscriptionManager = mSubscriptionManager;
+ when(mSubscriptionInfo.getSubscriptionId()).thenReturn(PREV_SUB_ID);
+ when(mSubscriptionInfo2.getSubscriptionId()).thenReturn(CURRENT_SUB_ID);
doReturn(mSubscriptionManager).when(mMobileSettingsActivity).getSystemService(
SubscriptionManager.class);
@@ -121,23 +129,32 @@
}
@Test
- public void switchFragment_hidePreviousFragment() {
+ public void switchFragment_newFragment_replaceIt() {
mMobileSettingsActivity.mCurSubscriptionId = PREV_SUB_ID;
mMobileSettingsActivity.switchFragment(mShowFragment, CURRENT_SUB_ID);
- verify(mFragmentTransaction).hide(mHideFragment);
+ verify(mFragmentTransaction).replace(R.id.main_content, mShowFragment,
+ MOBILE_SETTINGS_TAG + CURRENT_SUB_ID);
}
@Test
- public void switchFragment_fragmentExist_showItWithArguments() {
- mMobileSettingsActivity.mCurSubscriptionId = PREV_SUB_ID;
+ public void getSubscriptionId_hasIntent_getIdFromIntent() {
+ final Intent intent = new Intent();
+ intent.putExtra(Settings.EXTRA_SUB_ID, CURRENT_SUB_ID);
+ doReturn(intent).when(mMobileSettingsActivity).getIntent();
+ doReturn(true).when(mSubscriptionManager).isActiveSubscriptionId(CURRENT_SUB_ID);
- mMobileSettingsActivity.switchFragment(mShowFragment, CURRENT_SUB_ID);
+ assertThat(mMobileSettingsActivity.getSubscriptionId()).isEqualTo(CURRENT_SUB_ID);
+ }
- assertThat(mShowFragment.getArguments().getInt(
- MobileSettingsActivity.KEY_SUBSCRIPTION_ID)).isEqualTo(CURRENT_SUB_ID);
- verify(mFragmentTransaction).show(mShowFragment);
+ @Test
+ public void getSubscriptionId_noIntent_firstIdInList() {
+ doReturn(null).when(mMobileSettingsActivity).getIntent();
+ mSubscriptionInfos.add(mSubscriptionInfo);
+ mSubscriptionInfos.add(mSubscriptionInfo2);
+
+ assertThat(mMobileSettingsActivity.getSubscriptionId()).isEqualTo(PREV_SUB_ID);
}
@Test
@@ -149,7 +166,7 @@
mMobileSettingsActivity.saveInstanceState(bundle);
- assertThat(bundle.getInt(MobileSettingsActivity.KEY_CUR_SUBSCRIPTION_ID)).isEqualTo(
+ assertThat(bundle.getInt(Settings.EXTRA_SUB_ID)).isEqualTo(
PREV_SUB_ID);
}
}