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

Commit 1bb0fe12 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[Satellite] Refactor code to controller base." into main

parents 4a048a36 f0efc2e3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -32,7 +32,8 @@

    <PreferenceCategory
        android:key="key_category_your_satellite_plan"
        android:title="@string/category_title_your_satellite_plan">
        android:title="@string/category_title_your_satellite_plan"
        settings:controller="com.android.settings.network.telephony.satellite.SatelliteSettingAccountInfoController">

        <Preference
            android:key="key_your_satellite_plan"
+7 −75
Original line number Diff line number Diff line
@@ -28,9 +28,6 @@ import android.app.Activity;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.os.UserManager;
@@ -38,10 +35,6 @@ import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.satellite.SatelliteManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;

@@ -53,7 +46,6 @@ import androidx.preference.PreferenceCategory;
import com.android.settings.R;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settingslib.HelpUtils;
import com.android.settingslib.Utils;
import com.android.settingslib.widget.FooterPreference;

import java.util.Set;
@@ -61,12 +53,7 @@ import java.util.Set;
/** Handle Satellite Setting Preference Layout. */
public class SatelliteSetting extends RestrictedDashboardFragment {
    private static final String TAG = "SatelliteSetting";
    private static final String PREF_KEY_CATEGORY_YOUR_SATELLITE_PLAN =
            "key_category_your_satellite_plan";
    private static final String PREF_KEY_YOUR_SATELLITE_PLAN = "key_your_satellite_plan";
    private static final String PREF_KEY_CATEGORY_HOW_IT_WORKS = "key_category_how_it_works";
    private static final String PREF_KEY_YOUR_SATELLITE_DATA_PLAN = "key_your_satellite_data_plan";
    private static final String PREF_KEY_CATEGORY_ABOUT_SATELLITE = "key_category_about_satellite";
    private static final String KEY_FOOTER_PREFERENCE = "satellite_setting_extra_info_footer_pref";
    private static final String KEY_SATELLITE_CONNECTION_GUIDE = "key_satellite_connection_guide";
    private static final String KEY_SUPPORTED_SERVICE = "key_supported_service";
@@ -99,9 +86,15 @@ public class SatelliteSetting extends RestrictedDashboardFragment {
        mActivity = getActivity();
        mSubId = mActivity.getIntent().getIntExtra(SUB_ID,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        mConfigBundle = fetchCarrierConfigData(mSubId);
        mIsServiceDataType = getIntent().getBooleanExtra(EXTRA_IS_SERVICE_DATA_TYPE, false);
        mIsSmsAvailableForManualType = getIntent().getBooleanExtra(
                EXTRA_IS_SMS_AVAILABLE_FOR_MANUAL_TYPE, false);

        use(SatelliteAppListCategoryController.class).init();
        use(SatelliteSettingAboutContentController.class).init(mSubId);
        use(SatelliteSettingAccountInfoController.class).init(mSubId, mConfigBundle,
                mIsSmsAvailableForManualType, mIsServiceDataType);
    }

    @Override
@@ -114,8 +107,6 @@ public class SatelliteSetting extends RestrictedDashboardFragment {
            return;
        }

        mConfigBundle = fetchCarrierConfigData(mSubId);

        if (!isSatelliteAttachSupported(mSubId)) {
            Log.d(TAG, "SatelliteSettings: KEY_SATELLITE_ATTACH_SUPPORTED_BOOL is false, "
                    + "do nothing.");
@@ -123,9 +114,6 @@ public class SatelliteSetting extends RestrictedDashboardFragment {
            return;
        }

        mIsServiceDataType = getIntent().getBooleanExtra(EXTRA_IS_SERVICE_DATA_TYPE, false);
        mIsSmsAvailableForManualType = getIntent().getBooleanExtra(
                EXTRA_IS_SMS_AVAILABLE_FOR_MANUAL_TYPE, false);
        mSimOperatorName = getSystemService(TelephonyManager.class).getSimOperatorName(mSubId);
    }

@@ -133,7 +121,6 @@ public class SatelliteSetting extends RestrictedDashboardFragment {
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        boolean isSatelliteEligible = isSatelliteEligible();
        updateMobilePlan(isSatelliteEligible);
        updateHowItWorksContent(isSatelliteEligible);
        updateFooterContent();
    }
@@ -148,61 +135,6 @@ public class SatelliteSetting extends RestrictedDashboardFragment {
        return R.xml.satellite_setting;
    }

    private void updateMobilePlan(boolean isSatelliteEligible) {
        PreferenceCategory prefCategory = findPreference(PREF_KEY_CATEGORY_YOUR_SATELLITE_PLAN);
        if (prefCategory == null || !mConfigBundle.getBoolean(
                KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL)) {
            prefCategory.setVisible(false);
            return;
        }

        // Your mobile plan
        prefCategory.setTitle(getResources().getString(R.string.category_title_your_satellite_plan,
                mSimOperatorName));
        Preference messagingPreference = findPreference(PREF_KEY_YOUR_SATELLITE_PLAN);

        Drawable icon = getContext().getDrawable(R.drawable.ic_check_circle_24px);
        if (isSatelliteEligible) {
            /* In case satellite is allowed by carrier's entitlement server, the page will show
               the check icon with guidance that satellite is included in user's mobile plan */
            messagingPreference.setTitle(R.string.title_have_satellite_plan);
            if (com.android.settings.flags.Flags.satelliteOemSettingsUxMigration()) {
                if (mIsServiceDataType) {
                    Preference connectivityPreference = findPreference(
                            PREF_KEY_YOUR_SATELLITE_DATA_PLAN);
                    connectivityPreference.setTitle(R.string.title_have_satellite_data_plan);
                    connectivityPreference.setIcon(icon);
                    connectivityPreference.setVisible(true);
                }
            }
        } else {
            /* Or, it will show the blocked icon with the guidance that satellite is not included
               in user's mobile plan */
            messagingPreference.setTitle(R.string.title_no_satellite_plan);
            /* And, the link url provides more information via web page will be shown */
            SpannableString spannable = new SpannableString(
                    getResources().getString(R.string.summary_add_satellite_setting));
            spannable.setSpan(new UnderlineSpan(), 0, spannable.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, spannable.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            messagingPreference.setSummary(spannable);
            /* The link will lead users to a guide page */
            messagingPreference.setOnPreferenceClickListener(pref -> {
                String url = readSatelliteMoreInfoString();
                if (!url.isEmpty()) {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
                return true;
            });
            icon = getResources().getDrawable(R.drawable.ic_block_24px, null);
        }
        icon.setTintList(Utils.getColorAttr(getContext(), android.R.attr.textColorPrimary));
        messagingPreference.setIcon(icon);
    }

    private void updateHowItWorksContent(boolean isSatelliteEligible) {
        /* Composes "How it works" section, which guides how users can use satellite messaging, when
           satellite messaging is included in user's mobile plan, or it'll will be grey out. */
@@ -233,7 +165,7 @@ public class SatelliteSetting extends RestrictedDashboardFragment {

            final String[] link = new String[1];
            link[0] = readSatelliteMoreInfoString();
            if (true) {
            if (link[0] != null && !link[0].isEmpty()) {
                footerPreference.setLearnMoreAction(view -> {
                    if (!link[0].isEmpty()) {
                        Intent helpIntent = HelpUtils.getHelpIntent(mActivity, link[0],
+178 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.satellite;

import static android.telephony.CarrierConfigManager.CARRIER_ROAMING_NTN_CONNECT_MANUAL;
import static android.telephony.CarrierConfigManager.KEY_CARRIER_ROAMING_NTN_CONNECT_TYPE_INT;
import static android.telephony.CarrierConfigManager.KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL;
import static android.telephony.CarrierConfigManager.KEY_SATELLITE_INFORMATION_REDIRECT_URL_STRING;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.PersistableBundle;
import android.telephony.TelephonyManager;
import android.telephony.satellite.SatelliteManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;

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

import com.android.settings.R;
import com.android.settings.network.telephony.TelephonyBasePreferenceController;
import com.android.settingslib.Utils;

import java.util.Set;

/** A controller to control content of "Your mobile plan". */
public class SatelliteSettingAccountInfoController extends TelephonyBasePreferenceController {
    private static final String TAG = "SatelliteSettingAccountInfoController";
    @VisibleForTesting
    static final String PREF_KEY_CATEGORY_YOUR_SATELLITE_PLAN =
            "key_category_your_satellite_plan";
    @VisibleForTesting
    static final String PREF_KEY_YOUR_SATELLITE_PLAN = "key_your_satellite_plan";
    @VisibleForTesting
    static final String PREF_KEY_YOUR_SATELLITE_DATA_PLAN = "key_your_satellite_data_plan";

    private PreferenceScreen mScreen;
    private String mSimOperatorName;
    private boolean mIsSmsAvailable;
    private boolean mIsDataAvailable;
    private boolean mIsSatelliteEligible;
    private PersistableBundle mConfigBundle = new PersistableBundle();

    public SatelliteSettingAccountInfoController(@NonNull Context context,
            @NonNull String preferenceKey) {
        super(context, preferenceKey);
    }

    /** Initialize the UI settings. */
    public void init(int subId, @NonNull PersistableBundle configBundle, boolean isSmsAvailable,
            boolean isDataAvailable) {
        mSubId = subId;
        mConfigBundle = configBundle;
        mSimOperatorName = mContext.getSystemService(TelephonyManager.class).getSimOperatorName(
                mSubId);
        mIsSmsAvailable = isSmsAvailable;
        mIsDataAvailable = isDataAvailable;
        mIsSatelliteEligible = isSatelliteEligible();
    }

    @Override
    public void displayPreference(@NonNull PreferenceScreen screen) {
        mScreen = screen;
        super.displayPreference(screen);
        PreferenceCategory prefCategory = screen.findPreference(
                PREF_KEY_CATEGORY_YOUR_SATELLITE_PLAN);
        // Your mobile plan
        prefCategory.setTitle(mContext.getString(R.string.category_title_your_satellite_plan,
                mSimOperatorName));

        if (mIsSatelliteEligible) {
            handleEligibleUI();
            return;
        }
        handleIneligibleUI();
    }

    @Override
    public int getAvailabilityStatus(int subId) {
        return mConfigBundle.getBoolean(KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL)
                ? AVAILABLE
                : CONDITIONALLY_UNAVAILABLE;
    }

    private void handleEligibleUI() {
        Preference messagingPreference = mScreen.findPreference(PREF_KEY_YOUR_SATELLITE_PLAN);
        Drawable icon = mContext.getDrawable(R.drawable.ic_check_circle_24px);
        /* In case satellite is allowed by carrier's entitlement server, the page will show
               the check icon with guidance that satellite is included in user's mobile plan */
        messagingPreference.setTitle(R.string.title_have_satellite_plan);
        if (com.android.settings.flags.Flags.satelliteOemSettingsUxMigration()) {
            if (mIsDataAvailable) {
                Preference connectivityPreference = mScreen.findPreference(
                        PREF_KEY_YOUR_SATELLITE_DATA_PLAN);
                connectivityPreference.setTitle(R.string.title_have_satellite_data_plan);
                connectivityPreference.setIcon(icon);
                connectivityPreference.setVisible(true);
            }
        }
        icon.setTintList(Utils.getColorAttr(mContext, android.R.attr.textColorPrimary));
        messagingPreference.setIcon(icon);
    }

    private void handleIneligibleUI() {
        Preference messagingPreference = mScreen.findPreference(PREF_KEY_YOUR_SATELLITE_PLAN);
        /* Or, it will show the blocked icon with the guidance that satellite is not included
               in user's mobile plan */
        messagingPreference.setTitle(R.string.title_no_satellite_plan);
        String url = mConfigBundle.getString(KEY_SATELLITE_INFORMATION_REDIRECT_URL_STRING, "");
        if (!url.isEmpty()) {
            /* And, the link url provides more information via web page will be shown */
            SpannableString spannable = new SpannableString(
                    mContext.getString(R.string.summary_add_satellite_setting));
            spannable.setSpan(new UnderlineSpan(), 0, spannable.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, spannable.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            messagingPreference.setSummary(spannable);
            /* The link will lead users to a guide page */
            messagingPreference.setOnPreferenceClickListener(pref -> {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                mContext.startActivity(intent);
                return true;
            });
        }

        Drawable icon = mContext.getDrawable(R.drawable.ic_block_24px);
        icon.setTintList(Utils.getColorAttr(mContext, android.R.attr.textColorPrimary));
        messagingPreference.setIcon(icon);
    }

    @VisibleForTesting
    protected boolean isSatelliteEligible() {
        if (mConfigBundle.getInt(KEY_CARRIER_ROAMING_NTN_CONNECT_TYPE_INT)
                == CARRIER_ROAMING_NTN_CONNECT_MANUAL) {
            return mIsSmsAvailable;
        }
        SatelliteManager satelliteManager = mContext.getSystemService(SatelliteManager.class);
        if (satelliteManager == null) {
            Log.d(TAG, "SatelliteManager is null.");
            return false;
        }
        try {
            Set<Integer> restrictionReason =
                    satelliteManager.getAttachRestrictionReasonsForCarrier(mSubId);
            return !restrictionReason.contains(
                    SatelliteManager.SATELLITE_COMMUNICATION_RESTRICTION_REASON_ENTITLEMENT);
        } catch (SecurityException | IllegalStateException | IllegalArgumentException ex) {
            Log.d(TAG, "Error to getAttachRestrictionReasonsForCarrier : " + ex.toString());
            return false;
        }
    }
}
+210 −0

File added.

Preview size limit exceeded, changes collapsed.