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

Commit d0007632 authored by tom hsu's avatar tom hsu
Browse files

[Satellite settings] Fix can not search Satellite connectivity

Flag: EXEMPT bug fix
Fix: b/411274868
Fix: b/410774243
Test: Manual test
Change-Id: I7976dfa906cbd36e323fbe648bc50ec0aa2d20d5
parent dafdb83c
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANG
import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;

import android.annotation.Nullable;
import android.annotation.TestApi;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -164,14 +163,14 @@ public class CarrierConfigCache {
     * @return A {@link PersistableBundle} containing the config for the given subId, or default
     * values for an invalid subId.
     */
    public @Nullable PersistableBundle getSpecificConfigsForSubId(int subId,
    public @NonNull PersistableBundle getSpecificConfigsForSubId(int subId,
            @NonNull String... keys) {
        if (sCarrierConfigManager == null) return null;
        if (sCarrierConfigManager == null) return PersistableBundle.EMPTY;

        final PersistableBundle config = sCarrierConfigManager.getConfigForSubId(subId, keys);
        if (config == null) {
            Log.e(TAG, "Could not get carrier config, subId:" + subId);
            return null;
            return PersistableBundle.EMPTY;
        }
        return config;
    }
+54 −2
Original line number Diff line number Diff line
@@ -18,12 +18,19 @@ package com.android.settings.network

import android.content.Context
import android.os.OutcomeReceiver
import android.telephony.NetworkRegistrationInfo
import android.telephony.SignalStrength
import android.telephony.TelephonyCallback
import android.telephony.TelephonyCallback.CarrierRoamingNtnListener
import android.telephony.satellite.SatelliteManager
import android.telephony.satellite.SatelliteManager.SatelliteException
import android.telephony.satellite.SatelliteModemStateCallback
import android.util.Log
import androidx.annotation.VisibleForTesting
import androidx.concurrent.futures.CallbackToFutureAdapter
import com.android.internal.telephony.flags.Flags
import com.android.settings.core.BasePreferenceController
import com.android.settings.network.telephony.telephonyCallbackFlow
import com.google.common.util.concurrent.Futures.immediateFuture
import com.google.common.util.concurrent.ListenableFuture
import java.util.concurrent.Executor
@@ -63,13 +70,13 @@ open class SatelliteRepository(
        return CallbackToFutureAdapter.getFuture { completer ->
            try {
                satelliteManager.requestIsEnabled(executor,
                    object : OutcomeReceiver<Boolean, SatelliteManager.SatelliteException> {
                    object : OutcomeReceiver<Boolean, SatelliteException> {
                        override fun onResult(result: Boolean) {
                            Log.i(TAG, "Satellite modem enabled status: $result")
                            completer.set(result)
                        }

                        override fun onError(error: SatelliteManager.SatelliteException) {
                        override fun onError(error: SatelliteException) {
                            super.onError(error)
                            Log.w(TAG, "Can't get satellite modem enabled status", error)
                            completer.set(false)
@@ -216,6 +223,51 @@ open class SatelliteRepository(
        return emptyList()
    }

    /** Gets result of [CarrierRoamingNtnListener.onCarrierRoamingNtnAvailableServicesChanged]. */
    fun carrierRoamingNtnAvailableServicesChangedFlow(subId: Int): Flow<Boolean> =
        context.telephonyCallbackFlow(subId) {
            object : TelephonyCallback(), CarrierRoamingNtnListener {
                override fun onCarrierRoamingNtnAvailableServicesChanged(availableServices: IntArray) {
                    val isSmsAvailable: Boolean = availableServices.any { it ->
                        Log.d(TAG, "availableServices : $it")
                        it == NetworkRegistrationInfo.SERVICE_TYPE_SMS
                    }
                    Log.d(TAG, "isSmsAvailable : $isSmsAvailable")
                    trySend(isSmsAvailable)
                }

                override fun onCarrierRoamingNtnModeChanged(active: Boolean) {}
            }
        }

    /** Gets result of [SatelliteManager.requestIsSupported]. */
    fun requestIsSupportedFlow(): Flow<Boolean>  {
        val satelliteManager: SatelliteManager? =
            context.getSystemService(SatelliteManager::class.java)
        if (satelliteManager == null) {
            Log.w(TAG, "SatelliteManager is null")
            return flowOf(false)
        }
        return callbackFlow {
                val callback =
                    object : OutcomeReceiver<Boolean, SatelliteException> {
                        override fun onError(error: SatelliteException) {
                            super.onError(error)
                            trySend(false)
                        }

                        override fun onResult(result: Boolean) {
                            Log.w(TAG, "requestIsSupported : Result is $result")
                            trySend(result)
                        }
                    }

                satelliteManager.requestIsSupported(Dispatchers.Default.asExecutor(), callback)

                awaitClose {}
            }.flowOn(Dispatchers.Default)
        }

    companion object {
        private const val TAG: String = "SatelliteRepository"

+2 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import com.android.settings.network.telephony.NrAdvancedCallingPreferenceControl
import com.android.settings.network.telephony.RoamingPreferenceController.Companion.RoamingSearchItem
import com.android.settings.network.telephony.VideoCallingPreferenceController.Companion.VideoCallingSearchItem
import com.android.settings.network.telephony.WifiCallingPreferenceController.Companion.WifiCallingSearchItem
import com.android.settings.network.telephony.satellite.SatelliteSettingPreferenceController.Companion.SatelliteConnectivitySearchItem
import com.android.settingslib.spa.search.SpaSearchIndexableItem
import com.android.settingslib.spa.search.SpaSearchIndexablePage
import com.android.settingslib.spa.search.SpaSearchLanding.BundleValue
@@ -117,6 +118,7 @@ class MobileNetworkSettingsSearchIndex(
                RoamingSearchItem(context),
                VideoCallingSearchItem(context),
                WifiCallingSearchItem(context),
                SatelliteConnectivitySearchItem(context),
            )
    }
}
+0 −295
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_AUTOMATIC;
import static android.telephony.CarrierConfigManager.KEY_CARRIER_ROAMING_NTN_CONNECT_TYPE_INT;
import static android.telephony.CarrierConfigManager.KEY_SATELLITE_ATTACH_SUPPORTED_BOOL;
import static android.telephony.CarrierConfigManager.KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL;
import static android.telephony.NetworkRegistrationInfo.SERVICE_TYPE_DATA;
import static android.telephony.NetworkRegistrationInfo.SERVICE_TYPE_SMS;

import android.content.Context;
import android.content.Intent;
import android.os.OutcomeReceiver;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
import android.telephony.satellite.NtnSignalStrength;
import android.telephony.satellite.SatelliteAccessConfiguration;
import android.telephony.satellite.SatelliteManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.network.CarrierConfigCache;
import com.android.settings.network.telephony.TelephonyBasePreferenceController;

import java.util.Arrays;
import java.util.List;

/**
 * Preference controller for "Satellite Setting"
 */
public class SatelliteSettingPreferenceController extends
        TelephonyBasePreferenceController implements DefaultLifecycleObserver {
    private static final String TAG = "SatelliteSettingPreferenceController";
    @VisibleForTesting
    final CarrierRoamingNtnModeCallback mCarrierRoamingNtnModeCallback =
            new CarrierRoamingNtnModeCallback(this);
    CarrierConfigCache mCarrierConfigCache;
    SatelliteManager mSatelliteManager;
    private TelephonyManager mTelephonyManager;
    @Nullable
    private Boolean mIsSatelliteEligible = null;
    private PersistableBundle mCarrierConfigs = new PersistableBundle();
    private PreferenceScreen mPreferenceScreen;
    @VisibleForTesting
    @Nullable
    List<Integer> mTagIds = null;

    public SatelliteSettingPreferenceController(@NonNull Context context, @NonNull String key) {
        super(context, key);
        mCarrierConfigCache = CarrierConfigCache.getInstance(mContext);
        mSatelliteManager = mContext.getSystemService(SatelliteManager.class);
        mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
        if (mSatelliteManager != null) {
            mSatelliteManager.requestSatelliteAccessConfigurationForCurrentLocation(
                    context.getMainExecutor(),
                    new OutcomeReceiver<>() {
                        @Override
                        public void onResult(@Nullable SatelliteAccessConfiguration result) {
                            if (result == null) {
                                return;
                            }
                            mTagIds = result.getTagIds();
                        }
                    });
        }
    }

    /**
     * Set subId for Satellite Settings page.
     *
     * @param subId subscription ID.
     */
    public void initialize(int subId) {
        logd("initialize(), subId=" + subId);
        mSubId = subId;
        if (mTelephonyManager != null) {
            mTelephonyManager = mTelephonyManager.createForSubscriptionId(subId);
        }
        if (mCarrierConfigs == null) {
            return;
        }
        mCarrierConfigs = mCarrierConfigCache.getSpecificConfigsForSubId(
                subId, KEY_SATELLITE_ATTACH_SUPPORTED_BOOL,
                KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL,
                KEY_CARRIER_ROAMING_NTN_CONNECT_TYPE_INT);
    }

    @Override
    public int getAvailabilityStatus(int subId) {
        if (mSatelliteManager == null) {
            return UNSUPPORTED_ON_DEVICE;
        }

        if (mCarrierConfigs.isEmpty()) {
            // for search
            mCarrierConfigs = mCarrierConfigCache.getSpecificConfigsForSubId(
                    subId,
                    KEY_SATELLITE_ATTACH_SUPPORTED_BOOL,
                    KEY_CARRIER_ROAMING_NTN_CONNECT_TYPE_INT);
        }

        if (!mCarrierConfigs.getBoolean(KEY_SATELLITE_ATTACH_SUPPORTED_BOOL, false)) {
            return CONDITIONALLY_UNAVAILABLE;
        }

        if (isCarrierRoamingNtnConnectedTypeManual()) {
            return mCarrierRoamingNtnModeCallback.isSatelliteSmsAvailable()
                    ? AVAILABLE
                    : CONDITIONALLY_UNAVAILABLE;
        } else {
            return AVAILABLE;
        }
    }

    @Override
    public void onResume(@NonNull LifecycleOwner owner) {
        if (mTelephonyManager != null) {
            mTelephonyManager.registerTelephonyCallback(mContext.getMainExecutor(),
                    mCarrierRoamingNtnModeCallback);
        }
    }

    @Override
    public void onPause(@NonNull LifecycleOwner owner) {
        if (mTelephonyManager != null) {
            mTelephonyManager.unregisterTelephonyCallback(mCarrierRoamingNtnModeCallback);
        }
    }

    @Override
    public void displayPreference(@NonNull PreferenceScreen screen) {
        super.displayPreference(screen);
        if (mPreferenceScreen == null) {
            mPreferenceScreen = screen;
        }
        updateState(screen.findPreference(getPreferenceKey()));
    }

    void displayPreference() {
        if (mPreferenceScreen != null) {
            displayPreference(mPreferenceScreen);
        }
    }

    @Override
    public void updateState(@Nullable Preference preference) {
        super.updateState(preference);
        if (preference != null && preference.getKey().equals(getPreferenceKey())) {
            updateSummary(preference);
            preference.setEnabled(
                    SatelliteCarrierSettingUtils.isCarrierSatelliteRegionSupported(mContext,
                            mTagIds, mTelephonyManager.getSimSpecificCarrierId()));
        }

    }

    @Override
    public boolean handlePreferenceTreeClick(@NonNull 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_SATELLITE_SETTING)
                    .setPackage(mContext.getPackageName());
            // This will setup the Home and Search affordance
            intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true);
            intent.putExtra(SatelliteSetting.SUB_ID, mSubId);
            mContext.startActivity(intent);
            return true;
        }

        return false;
    }

    private void updateSummary(Preference preference) {
        if (preference == null) {
            logd("updateSummary - no Preference");
            return;
        }
        if (mSatelliteManager == null) {
            logd("updateSummary - no SatelliteManager");
            return;
        }

        if (isCarrierRoamingNtnConnectedTypeManual()) {
            preference.setSummary(
                    mCarrierRoamingNtnModeCallback.isSatelliteSmsAvailable()
                            ? R.string.satellite_setting_enabled_summary
                            : R.string.satellite_setting_disabled_summary);
        } else {
            if (!mCarrierConfigs.getBoolean(KEY_SATELLITE_ENTITLEMENT_SUPPORTED_BOOL)) {
                preference.setSummary(R.string.satellite_setting_summary_without_entitlement);
                return;
            }

            try {
                boolean isSatelliteEligible =
                        SatelliteCarrierSettingUtils.isSatelliteAccountEligible(
                                mContext, mSubId);
                if (mIsSatelliteEligible == null || mIsSatelliteEligible != isSatelliteEligible) {
                    mIsSatelliteEligible = isSatelliteEligible;
                    String summary = mContext.getString(
                            mIsSatelliteEligible ? R.string.satellite_setting_enabled_summary
                                    : R.string.satellite_setting_disabled_summary);
                    preference.setSummary(summary);
                }
            } catch (SecurityException | IllegalStateException | IllegalArgumentException ex) {
                loge(ex.toString());
                preference.setSummary(R.string.satellite_setting_disabled_summary);
            }
        }
    }

    private boolean isCarrierRoamingNtnConnectedTypeManual() {
        return CARRIER_ROAMING_NTN_CONNECT_AUTOMATIC != mCarrierConfigs.getInt(
                KEY_CARRIER_ROAMING_NTN_CONNECT_TYPE_INT,
                CARRIER_ROAMING_NTN_CONNECT_AUTOMATIC);
    }

    private static void logd(String message) {
        Log.d(TAG, message);
    }

    private static void loge(String message) {
        Log.e(TAG, message);
    }

    @VisibleForTesting
    static class CarrierRoamingNtnModeCallback extends TelephonyCallback implements
            TelephonyCallback.CarrierRoamingNtnListener {
        SatelliteSettingPreferenceController mSatelliteSettingPreferenceController;
        private boolean mIsSatelliteSmsAvailable = false;

        CarrierRoamingNtnModeCallback(
                SatelliteSettingPreferenceController satelliteSettingPreferenceController) {
            mSatelliteSettingPreferenceController = satelliteSettingPreferenceController;
        }

        boolean isSatelliteSmsAvailable() {
            return mIsSatelliteSmsAvailable;
        }

        @Override
        public void onCarrierRoamingNtnAvailableServicesChanged(int[] availableServices) {
            CarrierRoamingNtnListener.super.onCarrierRoamingNtnAvailableServicesChanged(
                    availableServices);
            List<Integer> availableServicesList = Arrays.stream(availableServices).boxed().toList();
            boolean isSmsAvailable = availableServicesList.contains(SERVICE_TYPE_SMS);
            boolean isDataAvailable = availableServicesList.contains(SERVICE_TYPE_DATA);
            logd("isSmsAvailable : " + isSmsAvailable
                    + " / isDataAvailable " + isDataAvailable);
            mIsSatelliteSmsAvailable = isSmsAvailable;
            mSatelliteSettingPreferenceController.displayPreference();
        }

        @Override
        public void onCarrierRoamingNtnEligibleStateChanged(boolean eligible) {
            // Do nothing
        }

        @Override
        public void onCarrierRoamingNtnModeChanged(boolean active) {
            // Do nothing
        }

        @Override
        public void onCarrierRoamingNtnSignalStrengthChanged(NtnSignalStrength ntnSignalStrength) {
            // Do nothing
        }
    }
}
+197 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading