Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37f2e60b authored by jackqdyulei's avatar jackqdyulei
Browse files

Refactor CdmaSubscriptionListPreference

Extract CdmaBasePreferenceController that can be shared by all CDMA
PreferenceController. Also create new subscription controller to use
it.

Bug: 114749736
Test: RunSettingsRoboTests
Change-Id: I7cfc27ffd0704d09dc02e49fbf5641ff8b90642d
parent b402a56f
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -17,14 +17,6 @@

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <com.android.settings.network.telephony.CdmaSubscriptionListPreference
        android:key="cdma_subscription_key"
        android:title="@string/cdma_subscription_title"
        android:summary="@string/cdma_subscription_summary"
        android:entries="@array/cdma_subscription_choices"
        android:entryValues="@array/cdma_subscription_values"
        android:dialogTitle="@string/cdma_subscription_dialogtitle" />

    <!--We want separate APN setting from reset of settings because-->
    <!--we want user to change it with caution.-->
    <PreferenceCategory
+9 −0
Original line number Diff line number Diff line
@@ -100,4 +100,13 @@
        android:dialogTitle="@string/cdma_system_select_dialogtitle"
        settings:controller="com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController"/>

    <ListPreference
        android:key="cdma_subscription_key"
        android:title="@string/cdma_subscription_title"
        android:summary="@string/cdma_subscription_summary"
        android:entries="@array/cdma_subscription_choices"
        android:entryValues="@array/cdma_subscription_values"
        android:dialogTitle="@string/cdma_subscription_dialogtitle"
        settings:controller="com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController"/>

</PreferenceScreen>
+0 −46
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ public class CdmaOptions {
    private static final String LOG_TAG = "CdmaOptions";

    private CarrierConfigManager mCarrierConfigManager;
    private CdmaSubscriptionListPreference mButtonCdmaSubscription;
    private RestrictedPreference mButtonAPNExpand;
    private Preference mCategoryAPNExpand;
    private Preference mButtonCarrierSettings;
@@ -65,8 +64,6 @@ public class CdmaOptions {
        mCarrierConfigManager = new CarrierConfigManager(prefFragment.getContext());

        // Initialize preferences.
        mButtonCdmaSubscription = (CdmaSubscriptionListPreference) mPrefScreen
                .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY);
        mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
        mButtonAPNExpand = (RestrictedPreference) mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY);
        mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY);
@@ -82,8 +79,6 @@ public class CdmaOptions {
        PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
        // Some CDMA carriers want the APN settings.
        boolean addAPNExpand = shouldAddApnExpandPreference(phoneType, carrierConfig);
        boolean addCdmaSubscription =
                deviceSupportsNvAndRuim();
        // Read platform settings for carrier settings
        boolean addCarrierSettings =
                carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL);
@@ -120,15 +115,6 @@ public class CdmaOptions {
            mPrefScreen.removePreference(mCategoryAPNExpand);
        }

        if (addCdmaSubscription) {
            log("Both NV and Ruim supported, ENABLE subscription type selection");
            mPrefScreen.addPreference(mButtonCdmaSubscription);
            mButtonCdmaSubscription.setEnabled(true);
        } else {
            log("Both NV and Ruim NOT supported, REMOVE subscription type selection");
            mPrefScreen.removePreference(mButtonCdmaSubscription);
        }

        if (addCarrierSettings) {
            mPrefScreen.addPreference(mButtonCarrierSettings);
        } else {
@@ -146,32 +132,6 @@ public class CdmaOptions {
                && config.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);
    }

    private boolean deviceSupportsNvAndRuim() {
        // retrieve the list of subscription types supported by device.
        String subscriptionsSupported = SystemProperties.get("ril.subscription.types");
        boolean nvSupported = false;
        boolean ruimSupported = false;

        log("deviceSupportsnvAnRum: prop=" + subscriptionsSupported);
        if (!TextUtils.isEmpty(subscriptionsSupported)) {
            // Searches through the comma-separated list for a match for "NV"
            // and "RUIM" to update nvSupported and ruimSupported.
            for (String subscriptionType : subscriptionsSupported.split(",")) {
                subscriptionType = subscriptionType.trim();
                if (subscriptionType.equalsIgnoreCase("NV")) {
                    nvSupported = true;
                }
                if (subscriptionType.equalsIgnoreCase("RUIM")) {
                    ruimSupported = true;
                }
            }
        }

        log("deviceSupportsnvAnRum: nvSupported=" + nvSupported +
                " ruimSupported=" + ruimSupported);
        return (nvSupported && ruimSupported);
    }

    public boolean preferenceTreeClick(Preference preference) {
        if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
            log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true");
@@ -184,12 +144,6 @@ public class CdmaOptions {
        return false;
    }

    public void showDialog(Preference preference) {
        if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) {
            mButtonCdmaSubscription.showDialog(null);
        }
    }

    protected void log(String s) {
        android.util.Log.d(LOG_TAG, s);
    }
+0 −115
Original line number Diff line number Diff line
/*
 * 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.Bundle;
import androidx.preference.ListPreference;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.AttributeSet;
import android.util.Log;

import com.android.internal.telephony.Phone;
import com.android.settingslib.utils.ThreadUtils;

public class CdmaSubscriptionListPreference extends ListPreference {

    private static final String LOG_TAG = "CdmaSubListPref";

    // Used for CDMA subscription mode
    private static final int CDMA_SUBSCRIPTION_RUIM_SIM = 0;
    private static final int CDMA_SUBSCRIPTION_NV = 1;

    //preferredSubscriptionMode  0 - RUIM/SIM, preferred
    //                           1 - NV
    static final int preferredSubscriptionMode = Phone.PREFERRED_CDMA_SUBSCRIPTION;

    private TelephonyManager mTelephonyManager;

    public CdmaSubscriptionListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        mTelephonyManager = TelephonyManager.from(context);
        setCurrentCdmaSubscriptionModeValue();
    }

    private void setCurrentCdmaSubscriptionModeValue() {
        int cdmaSubscriptionMode = Settings.Global.getInt(getContext().getContentResolver(),
                Settings.Global.CDMA_SUBSCRIPTION_MODE, preferredSubscriptionMode);
        setValue(Integer.toString(cdmaSubscriptionMode));
    }

    public CdmaSubscriptionListPreference(Context context) {
        this(context, null);
    }

    /**
     * Sets the subscription id associated with this preference.
     *
     * @param subId the subscription id.
     */
    public void setSubscriptionId(int subId) {
        mTelephonyManager = TelephonyManager.from(getContext()).createForSubscriptionId(subId);
    }

    //TODO(b/114749736): move this logic to preference controller
    protected void showDialog(Bundle state) {
        setCurrentCdmaSubscriptionModeValue();
    }

    //TODO(b/114749736): move this logic to preference controller
    protected void onDialogClosed(boolean positiveResult) {
        if (!positiveResult) {
            //The button was dismissed - no need to set new value
            return;
        }

        int buttonCdmaSubscriptionMode = Integer.parseInt(getValue());
        Log.d(LOG_TAG, "Setting new value " + buttonCdmaSubscriptionMode);
        int statusCdmaSubscriptionMode;
        switch(buttonCdmaSubscriptionMode) {
            case CDMA_SUBSCRIPTION_NV:
                statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_NV;
                break;
            case CDMA_SUBSCRIPTION_RUIM_SIM:
                statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_RUIM_SIM;
                break;
            default:
                statusCdmaSubscriptionMode = Phone.PREFERRED_CDMA_SUBSCRIPTION;
        }

        // Set the CDMA subscription mode, when mode has been successfully changed, update the
        // mode to the global setting.
        ThreadUtils.postOnBackgroundThread(() -> {
            // The subscription mode selected by user.
            int cdmaSubscriptionMode = Integer.parseInt(getValue());

            boolean isSuccessed = mTelephonyManager.setCdmaSubscriptionMode(
                    statusCdmaSubscriptionMode);

            // Update the global settings if successed.
            if (isSuccessed) {
                Settings.Global.putInt(getContext().getContentResolver(),
                        Settings.Global.CDMA_SUBSCRIPTION_MODE,
                        cdmaSubscriptionMode);
            } else {
                Log.e(LOG_TAG, "Setting Cdma subscription source failed");
            }
        });
    }
}
+9 −15
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.TelephonyIntents;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController;
import com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
@@ -162,6 +163,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
    private DataUsagePreference mDataUsagePref;

    private CdmaSystemSelectPreferenceController mCdmaSystemSelectPreferenceController;
    private CdmaSubscriptionPreferenceController mCdmaSubscriptionPreferenceController;

    private static final String iface = "rmnet0"; //TODO: this will go away
    private List<SubscriptionInfo> mActiveSubInfos;
@@ -400,6 +402,8 @@ public class MobileNetworkFragment extends DashboardFragment implements

        mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
        mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);
        mCdmaSubscriptionPreferenceController = use(CdmaSubscriptionPreferenceController.class);
        mCdmaSubscriptionPreferenceController.init(getPreferenceManager(), mSubId);
    }

    @Override
@@ -724,15 +728,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
            }
        }

        /**
         * Listen to extra preference changes that need as Metrics events logging.
         */

        if (prefSet.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY) != null) {
            prefSet.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY)
                    .setOnPreferenceChangeListener(this);
        }

        // Get the networkMode from Settings.System and displays it
        mButtonEnabledNetworks.setValue(Integer.toString(settingsNetworkMode));
        mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
@@ -1093,9 +1088,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
                mVideoCallingPref.setEnabled(false);
                return false;
            }
        } else if (preference == getPreferenceScreen()
                .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY)) {
            return true;
        }

        updateBody();
@@ -1431,11 +1423,13 @@ public class MobileNetworkFragment extends DashboardFragment implements
                        EXTRA_EXIT_ECM_RESULT, false);
                if (isChoiceYes) {
                    // If the phone exits from ECM mode, show the CDMA Options
                    if (TextUtils.equals(mClickedPreference.getKey(),
                    final String key = mClickedPreference.getKey();
                    if (TextUtils.equals(key,
                            mCdmaSystemSelectPreferenceController.getPreferenceKey())) {
                        mCdmaSystemSelectPreferenceController.showDialog();
                    } else {
                        mCdmaOptions.showDialog(mClickedPreference);
                    } else if (TextUtils.equals(key,
                            mCdmaSubscriptionPreferenceController.getPreferenceKey())) {
                        mCdmaSubscriptionPreferenceController.showDialog();
                    }
                } else {
                    // do nothing
Loading