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

Commit 2de3d281 authored by jackqdyulei's avatar jackqdyulei
Browse files

Refactor CdmaApnPreference

Bug: 114749736
Test: RunSettingsRoboTests
Change-Id: I3d0203b7b5fb732e2814fb741761af4371924e9a
parent 37f2e60b
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -17,17 +17,6 @@

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

    <!--We want separate APN setting from reset of settings because-->
    <!--we want user to change it with caution.-->
    <PreferenceCategory
        android:key="category_cdma_apn_key">
        <!-- The launching Intent will be defined thru code as we need to pass some Extra -->
        <com.android.settingslib.RestrictedPreference
            android:key="button_cdma_apn_key"
            android:title="@string/apn_settings"
            android:persistent="false"/>
    </PreferenceCategory>

    <Preference
        android:key="carrier_settings_key"
        android:title="@string/carrier_settings_title">
+10 −0
Original line number Diff line number Diff line
@@ -109,4 +109,14 @@
        android:dialogTitle="@string/cdma_subscription_dialogtitle"
        settings:controller="com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController"/>

    <!--We want separate APN setting from reset of settings because we want user to change it with caution-->
    <PreferenceCategory
        android:key="category_cdma_apn_key">
        <com.android.settingslib.RestrictedPreference
            android:key="button_cdma_apn_key"
            android:title="@string/apn_settings"
            android:persistent="false"
            settings:controller="com.android.settings.network.telephony.cdma.CdmaApnPreferenceController"/>
    </PreferenceCategory>

</PreferenceScreen>
+2 −59
Original line number Diff line number Diff line
@@ -16,25 +16,15 @@

package com.android.settings.network.telephony;

import android.content.Intent;
import android.os.PersistableBundle;
import android.os.SystemProperties;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.R;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;

/**
 * List of Phone-specific settings screens.
@@ -43,21 +33,18 @@ public class CdmaOptions {
    private static final String LOG_TAG = "CdmaOptions";

    private CarrierConfigManager mCarrierConfigManager;
    private RestrictedPreference mButtonAPNExpand;
    private Preference mCategoryAPNExpand;
    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 static final String BUTTON_APN_EXPAND_KEY = "button_cdma_apn_key";
    private static final String CATEGORY_APN_EXPAND_KEY = "category_cdma_apn_key";

    private PreferenceFragmentCompat mPrefFragment;
    private PreferenceScreen mPrefScreen;
    private int mSubId;

    public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen, int subId) {
    public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen,
            int subId) {
        mPrefFragment = prefFragment;
        mPrefScreen = prefScreen;
        mPrefFragment.addPreferencesFromResource(R.xml.cdma_options);
@@ -65,20 +52,14 @@ public class CdmaOptions {

        // Initialize preferences.
        mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
        mButtonAPNExpand = (RestrictedPreference) mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY);
        mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY);

        updateSubscriptionId(subId);
    }

    protected void updateSubscriptionId(int subId) {
        mSubId = subId;
        int phoneType = TelephonyManager.from(mPrefFragment.getContext())
                .createForSubscriptionId(mSubId).getPhoneType();

        PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
        // Some CDMA carriers want the APN settings.
        boolean addAPNExpand = shouldAddApnExpandPreference(phoneType, carrierConfig);
        // Read platform settings for carrier settings
        boolean addCarrierSettings =
                carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL);
@@ -86,34 +67,6 @@ public class CdmaOptions {
        // 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 (addAPNExpand) {
            log("update: addAPNExpand");
            mButtonAPNExpand.setDisabledByAdmin(
                    MobileNetworkUtils.isDpcApnEnforced(mButtonAPNExpand.getContext())
                            ? RestrictedLockUtilsInternal.getDeviceOwner(
                                    mButtonAPNExpand.getContext())
                            : null);
            mButtonAPNExpand.setOnPreferenceClickListener(
                    new Preference.OnPreferenceClickListener() {
                        @Override
                        public boolean onPreferenceClick(Preference preference) {
                            MetricsLogger.action(mButtonAPNExpand.getContext(),
                                    MetricsEvent.ACTION_MOBILE_NETWORK_APN_SETTINGS);
                            // We need to build the Intent by hand as the Preference Framework
                            // does not allow to add an Intent with some extras into a Preference
                            // XML file
                            final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
                            // This will setup the Home and Search affordance
                            intent.putExtra(":settings:show_fragment_as_subsetting", true);
                            intent.putExtra("sub_id", mSubId);
                            mPrefFragment.startActivity(intent);
                            return true;
                        }
                    });
            mPrefScreen.addPreference(mCategoryAPNExpand);
        } else {
            mPrefScreen.removePreference(mCategoryAPNExpand);
        }

        if (addCarrierSettings) {
            mPrefScreen.addPreference(mButtonCarrierSettings);
@@ -122,16 +75,6 @@ public class CdmaOptions {
        }
    }

    /**
     * Return whether we should add the APN expandable preference based on the phone type and
     * carrier config
     */
    @VisibleForTesting
    public static boolean shouldAddApnExpandPreference(int phoneType, PersistableBundle config) {
        return phoneType == PhoneConstants.PHONE_TYPE_CDMA
                && config.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);
    }

    public boolean preferenceTreeClick(Preference preference) {
        if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
            log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true");
+2 −0
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.CdmaApnPreferenceController;
import com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController;
import com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController;
import com.android.settings.search.BaseSearchIndexProvider;
@@ -399,6 +400,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
                SubscriptionManager.INVALID_SUBSCRIPTION_ID);

        use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
        use(CdmaApnPreferenceController.class).init(mSubId);

        mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
        mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);
+87 −0
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.cdma;

import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.SettingsActivity;
import com.android.settings.network.ApnSettings;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;

/**
 * Preference controller for "CDMA Apn"
 */
public class CdmaApnPreferenceController extends CdmaBasePreferenceController {

    private static final String CATEGORY_KEY = "category_cdma_apn_key";
    @VisibleForTesting
    CarrierConfigManager mCarrierConfigManager;

    public CdmaApnPreferenceController(Context context, String key) {
        super(context, key);
        mCarrierConfigManager = new CarrierConfigManager(context);
    }

    @Override
    public int getAvailabilityStatus() {
        final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);

        return carrierConfig != null
                && carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL)
                && MobileNetworkUtils.isCdmaOptions(mContext, mSubId)
                ? AVAILABLE
                : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        if (isAvailable()) {
            ((RestrictedPreference) mPreference).setDisabledByAdmin(
                    MobileNetworkUtils.isDpcApnEnforced(mContext)
                            ? RestrictedLockUtilsInternal.getDeviceOwner(mContext)
                            : null);
        } else {
            screen.findPreference(CATEGORY_KEY).setVisible(false);
        }
    }

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (getPreferenceKey().equals(preference.getKey())) {
            // This activity runs in phone process, we must use intent to start
            final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
            // This will setup the Home and Search affordance
            intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true);
            intent.putExtra(ApnSettings.SUB_ID, mSubId);
            mContext.startActivity(intent);
            return true;
        }

        return false;
    }
}
Loading