OSDN Git Service

Refactor carrier settings
authorjackqdyulei <jackqdyulei@google.com>
Fri, 12 Oct 2018 20:50:28 +0000 (13:50 -0700)
committerjackqdyulei <jackqdyulei@google.com>
Tue, 16 Oct 2018 22:33:20 +0000 (15:33 -0700)
Only show it when:
1. It is in CDMA or GSM mode
2. CarrierConfig tell settings to show it

Bug: 114749736
Test: RunSettingsRoboTests

Change-Id: I45ecbc86c793ebec602142be208058e2043a2ba7

res/xml/cdma_options.xml [deleted file]
res/xml/gsm_umts_options.xml
res/xml/network_setting_fragment.xml
src/com/android/settings/network/telephony/CarrierPreferenceController.java [new file with mode: 0644]
src/com/android/settings/network/telephony/CdmaOptions.java
src/com/android/settings/network/telephony/MobileNetworkFragment.java
src/com/android/settings/network/telephony/MobileNetworkUtils.java
tests/robotests/src/com/android/settings/network/telephony/CarrierPreferenceControllerTest.java [new file with mode: 0644]

diff --git a/res/xml/cdma_options.xml b/res/xml/cdma_options.xml
deleted file mode 100644 (file)
index facc1d3..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-    Copyright (C) 2008 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.
--->
-
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <Preference
-        android:key="carrier_settings_key"
-        android:title="@string/carrier_settings_title">
-        <!-- b/114749736, create a preference controller to build intent -->
-    </Preference>
-
-</PreferenceScreen>
index f5439a4..1d1d4f3 100644 (file)
@@ -27,7 +27,7 @@
             android:title="@string/select_automatically"
             android:persistent="false"/>
 
-        <com.android.settings.mobilenetwork.NetworkSelectListPreference
+        <com.android.settings.network.telephony.NetworkSelectListPreference
             android:key="button_network_select_key"
             android:title="@string/network_select_title"
             android:persistent="false"/>
index 1a011b0..1de3ef7 100644 (file)
             settings:controller="com.android.settings.network.telephony.cdma.CdmaApnPreferenceController"/>
     </PreferenceCategory>
 
+    <Preference
+        android:key="carrier_settings_key"
+        android:title="@string/carrier_settings_title"
+        settings:controller="com.android.settings.network.telephony.CarrierPreferenceController">
+    </Preference>
+
 </PreferenceScreen>
diff --git a/src/com/android/settings/network/telephony/CarrierPreferenceController.java b/src/com/android/settings/network/telephony/CarrierPreferenceController.java
new file mode 100644 (file)
index 0000000..957eaaa
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * 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.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.Preference;
+
+import com.android.settings.core.BasePreferenceController;
+
+/**
+ * Preference controller for "Carrier Settings"
+ */
+public class CarrierPreferenceController extends BasePreferenceController {
+
+    @VisibleForTesting
+    CarrierConfigManager mCarrierConfigManager;
+    private int mSubId;
+
+    public CarrierPreferenceController(Context context, String key) {
+        super(context, key);
+        mCarrierConfigManager = new CarrierConfigManager(context);
+    }
+
+    public void init(int subId) {
+        mSubId = subId;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
+
+        // Return available if it is in CDMA or GSM mode, and the flag is on
+        return carrierConfig != null
+                && carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL)
+                && (MobileNetworkUtils.isCdmaOptions(mContext, mSubId)
+                || MobileNetworkUtils.isGsmOptions(mContext, mSubId))
+                ? AVAILABLE
+                : CONDITIONALLY_UNAVAILABLE;
+    }
+
+    @Override
+    public boolean handlePreferenceTreeClick(Preference preference) {
+        if (getPreferenceKey().equals(preference.getKey())) {
+            //TODO(b/117651939): start carrier settings activity
+            return true;
+        }
+
+        return false;
+    }
+}
index 5017466..c573128 100644 (file)
 
 package com.android.settings.network.telephony;
 
-import android.os.PersistableBundle;
-import android.telephony.CarrierConfigManager;
-import android.telephony.TelephonyManager;
-
 import androidx.preference.Preference;
 import androidx.preference.PreferenceFragmentCompat;
 import androidx.preference.PreferenceScreen;
@@ -32,50 +28,18 @@ import com.android.settings.R;
 public class CdmaOptions {
     private static final String LOG_TAG = "CdmaOptions";
 
-    private CarrierConfigManager mCarrierConfigManager;
-    private Preference mButtonCarrierSettings;
-
     private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
     private static final String BUTTON_CDMA_SUBSCRIPTION_KEY = "cdma_subscription_key";
-    private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
 
     private PreferenceFragmentCompat mPrefFragment;
-    private PreferenceScreen mPrefScreen;
-    private int mSubId;
 
     public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen,
             int subId) {
         mPrefFragment = prefFragment;
-        mPrefScreen = prefScreen;
-        mPrefFragment.addPreferencesFromResource(R.xml.cdma_options);
-        mCarrierConfigManager = new CarrierConfigManager(prefFragment.getContext());
-
-        // Initialize preferences.
-        mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
-
-        updateSubscriptionId(subId);
-    }
-
-    protected void updateSubscriptionId(int subId) {
-        mSubId = subId;
-
-        PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
-        // Read platform settings for carrier settings
-        boolean addCarrierSettings =
-                carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL);
-
-        // Making no assumptions of whether they are added or removed at this point.
-        // Calling add or remove explicitly to make sure they are updated.
-
-
-        if (addCarrierSettings) {
-            mPrefScreen.addPreference(mButtonCarrierSettings);
-        } else {
-            mPrefScreen.removePreference(mButtonCarrierSettings);
-        }
     }
 
     public boolean preferenceTreeClick(Preference preference) {
+        //TODO(b/114749736): handle it in preferenceController and remove this file
         if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
             log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true");
             return true;
index 91b351e..518e59b 100644 (file)
@@ -401,6 +401,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
 
         use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
         use(CdmaApnPreferenceController.class).init(mSubId);
+        use(CarrierPreferenceController.class).init(mSubId);
 
         mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
         mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);
@@ -1798,8 +1799,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
         // the open dialog gets dismissed or detached after pause / resume.
         if (mCdmaOptions == null) {
             mCdmaOptions = new CdmaOptions(prefFragment, prefScreen, subId);
-        } else {
-            mCdmaOptions.updateSubscriptionId(subId);
         }
     }
 
index 666db1b..8950178 100644 (file)
@@ -275,6 +275,20 @@ public class MobileNetworkUtils {
         return false;
     }
 
+    public static boolean isGsmOptions(Context context, int subId) {
+        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+            return false;
+        }
+        final TelephonyManager telephonyManager = TelephonyManager.from(context)
+                .createForSubscriptionId(subId);
+
+        if (telephonyManager.getPhoneType() == PhoneConstants.PHONE_TYPE_GSM) {
+            return true;
+        }
+
+        return false;
+    }
+
     /**
      * Return {@code true} if it is world mode, and we may show advanced options in telephony
      * settings
diff --git a/tests/robotests/src/com/android/settings/network/telephony/CarrierPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/CarrierPreferenceControllerTest.java
new file mode 100644 (file)
index 0000000..e119e89
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * 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.android.settings.core.BasePreferenceController.AVAILABLE;
+import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import com.android.internal.telephony.PhoneConstants;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.RestrictedPreference;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class CarrierPreferenceControllerTest {
+    private static final int SUB_ID = 2;
+
+    @Mock
+    private TelephonyManager mTelephonyManager;
+    @Mock
+    private TelephonyManager mInvalidTelephonyManager;
+    @Mock
+    private SubscriptionManager mSubscriptionManager;
+    @Mock
+    private CarrierConfigManager mCarrierConfigManager;
+
+    private CarrierPreferenceController mController;
+    private RestrictedPreference mPreference;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(RuntimeEnvironment.application);
+        doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
+        doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
+        doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+        doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+
+        mPreference = new RestrictedPreference(mContext);
+        mController = new CarrierPreferenceController(mContext, "mobile_data");
+        mController.init(SUB_ID);
+        mController.mCarrierConfigManager = mCarrierConfigManager;
+        mPreference.setKey(mController.getPreferenceKey());
+    }
+
+    @Test
+    public void getAvailabilityStatus_cdmaWithFlagOff_returnUnavailable() {
+        doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
+        final PersistableBundle bundle = new PersistableBundle();
+        bundle.putBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL, false);
+        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_cdmaWithFlagOnreturnAvailable() {
+        doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
+        final PersistableBundle bundle = new PersistableBundle();
+        bundle.putBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL, true);
+        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_gsmWithFlagOnreturnAvailable() {
+        doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
+        final PersistableBundle bundle = new PersistableBundle();
+        bundle.putBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL, true);
+        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+}