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

Commit b1b4e25f authored by Tom Hsu's avatar Tom Hsu Committed by Android (Google) Code Review
Browse files

Merge "[Settings] Add data roaming observer" into udc-dev

parents d3b41aa9 499a2fa1
Loading
Loading
Loading
Loading
+53 −2
Original line number Diff line number Diff line
@@ -88,7 +88,7 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
    private List<MobileNetworkInfoEntity> mMobileNetworkInfoEntityList = new ArrayList<>();
    private Context mContext;
    private AirplaneModeObserver mAirplaneModeObserver;
    private Uri mAirplaneModeSettingUri;
    private DataRoamingObserver mDataRoamingObserver;
    private MetricsFeatureProvider mMetricsFeatureProvider;
    private Map<Integer, MobileDataContentObserver> mDataContentObserverMap = new HashMap<>();
    private int mPhysicalSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
@@ -127,10 +127,13 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
        mUiccInfoDao = mMobileNetworkDatabase.mUiccInfoDao();
        mMobileNetworkInfoDao = mMobileNetworkDatabase.mMobileNetworkInfoDao();
        mAirplaneModeObserver = new AirplaneModeObserver(new Handler(Looper.getMainLooper()));
        mAirplaneModeSettingUri = Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON);
        mDataRoamingObserver = new DataRoamingObserver(new Handler(Looper.getMainLooper()));
    }

    private class AirplaneModeObserver extends ContentObserver {
        private Uri mAirplaneModeSettingUri =
                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON);

        AirplaneModeObserver(Handler handler) {
            super(handler);
        }
@@ -155,6 +158,46 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
        }
    }

    private class DataRoamingObserver extends ContentObserver {
        private int mRegSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
        private String mBaseField = Settings.Global.DATA_ROAMING;

        DataRoamingObserver(Handler handler) {
            super(handler);
        }

        public void register(Context context, int subId) {
            mRegSubId = subId;
            String lastField = mBaseField;
            createTelephonyManagerBySubId(subId);
            TelephonyManager tm = mTelephonyManagerMap.get(subId);
            if (tm.getSimCount() != 1) {
                lastField += subId;
            }
            context.getContentResolver().registerContentObserver(
                    Settings.Global.getUriFor(lastField), false, this);
        }

        public void unRegister(Context context) {
            context.getContentResolver().unregisterContentObserver(this);
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            TelephonyManager tm = mTelephonyManagerMap.get(mRegSubId);
            if (tm == null) {
                return;
            }
            sExecutor.execute(() -> {
                insertMobileNetworkInfo(mContext, mRegSubId, tm);
            });
            boolean isDataRoamingEnabled = tm.isDataRoamingEnabled();
            for (MobileNetworkCallback callback : sCallbacks) {
                callback.onDataRoamingChanged(mRegSubId, isDataRoamingEnabled);
            }
        }
    }

    /**
     * Register all callbacks and listener.
     *
@@ -180,6 +223,7 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
        if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
            addRegisterBySubId(subId);
            createTelephonyManagerBySubId(subId);
            mDataRoamingObserver.register(mContext, subId);
        }
    }

@@ -251,6 +295,7 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
        if (sCallbacks.isEmpty()) {
            mSubscriptionManager.removeOnSubscriptionsChangedListener(this);
            mAirplaneModeObserver.unRegister(mContext);
            mDataRoamingObserver.unRegister(mContext);
            mDataContentObserverMap.forEach((id, observer) -> {
                observer.unRegister(mContext);
            });
@@ -709,6 +754,12 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
        default void onAirplaneModeChanged(boolean enabled) {
        }

        /**
         * Notify clients data roaming changed of subscription.
         */
        default void onDataRoamingChanged(int subId, boolean enabled) {
        }

        default void onCallStateChanged(int state) {
        }
    }
+10 −53
Original line number Diff line number Diff line
@@ -21,28 +21,23 @@ import static androidx.lifecycle.Lifecycle.Event.ON_STOP;

import android.content.Context;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.network.GlobalSettingsChangeListener;
import com.android.settings.network.MobileNetworkRepository;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.mobile.dataservice.MobileNetworkInfoEntity;
import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
import com.android.settingslib.mobile.dataservice.UiccInfoEntity;

import java.util.ArrayList;
import java.util.List;
@@ -52,7 +47,6 @@ import java.util.List;
 */
public class RoamingPreferenceController extends TelephonyTogglePreferenceController implements
        LifecycleObserver, MobileNetworkRepository.MobileNetworkCallback {

    private static final String TAG = "RoamingController";
    private static final String DIALOG_TAG = "MobileDataDialog";

@@ -63,15 +57,6 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
    protected LifecycleOwner mLifecycleOwner;
    private List<MobileNetworkInfoEntity> mMobileNetworkInfoEntityList = new ArrayList<>();

    /**
     * There're 2 listeners both activated at the same time.
     * For project that access DATA_ROAMING, only first listener is functional.
     * For project that access "DATA_ROAMING + subId", first listener will be stopped when receiving
     * any onChange from second listener.
     */
    private GlobalSettingsChangeListener mListener;
    private GlobalSettingsChangeListener mListenerForSubId;

    @VisibleForTesting
    FragmentManager mFragmentManager;
    MobileNetworkInfoEntity mMobileNetworkInfoEntity;
@@ -102,34 +87,11 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
    public void onStart() {
        mMobileNetworkRepository.addRegister(mLifecycleOwner, this, mSubId);
        mMobileNetworkRepository.updateEntity();
        if (mListener == null) {
            mListener = new GlobalSettingsChangeListener(mContext,
                    Settings.Global.DATA_ROAMING) {
                public void onChanged(String field) {
                    updateState(mSwitchPreference);
                }
            };
        }
        stopMonitorSubIdSpecific();

        if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
            return;
        }

        mListenerForSubId = new GlobalSettingsChangeListener(mContext,
                Settings.Global.DATA_ROAMING + mSubId) {
            public void onChanged(String field) {
                stopMonitor();
                updateState(mSwitchPreference);
            }
        };
    }

    @OnLifecycleEvent(ON_STOP)
    public void onStop() {
        mMobileNetworkRepository.removeRegister(this);
        stopMonitor();
        stopMonitorSubIdSpecific();
    }

    @Override
@@ -219,20 +181,6 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
        dialogFragment.show(mFragmentManager, DIALOG_TAG);
    }

    private void stopMonitor() {
        if (mListener != null) {
            mListener.close();
            mListener = null;
        }
    }

    private void stopMonitorSubIdSpecific() {
        if (mListenerForSubId != null) {
            mListenerForSubId.close();
            mListenerForSubId = null;
        }
    }

    @VisibleForTesting
    public void setMobileNetworkInfoEntity(MobileNetworkInfoEntity mobileNetworkInfoEntity) {
        mMobileNetworkInfoEntity = mobileNetworkInfoEntity;
@@ -251,4 +199,13 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
            }
        });
    }

    @Override
    public void onDataRoamingChanged(int subId, boolean enabled) {
        if (subId != mSubId) {
            Log.d(TAG, "onDataRoamingChanged - wrong subId : " + subId + " / " + enabled);
            return;
        }
        update();
    }
}