Set preferred network types on background thread

Also migrate deprecated method to setAllowedNetworkTypesForReason.

Bug: 325549769
Test: manual - on Mobile Settings
Change-Id: If377915ec4ea6267d7b2b2f36f3cb30ba4bab8fe
This commit is contained in:
Chaohui Wang
2024-02-27 17:03:24 +08:00
parent 8c169da968
commit fa1764baad
2 changed files with 51 additions and 9 deletions

View File

@@ -19,6 +19,8 @@ package com.android.settings.network.telephony;
import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import static com.android.settings.network.telephony.EnabledNetworkModePreferenceControllerHelperKt.setAllowedNetworkTypes;
import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
@@ -28,10 +30,12 @@ import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.ListPreference;
import androidx.preference.ListPreferenceDialogFragmentCompat;
@@ -72,6 +76,7 @@ public class EnabledNetworkModePreferenceController extends
private int mCallState = TelephonyManager.CALL_STATE_IDLE;
private PhoneCallStateTelephonyCallback mTelephonyCallback;
private FragmentManager mFragmentManager;
private LifecycleOwner mViewLifecycleOwner;
public EnabledNetworkModePreferenceController(Context context, String key) {
super(context, key);
@@ -169,18 +174,15 @@ public class EnabledNetworkModePreferenceController extends
}
@Override
public boolean onPreferenceChange(Preference preference, Object object) {
public boolean onPreferenceChange(@NonNull Preference preference, Object object) {
final int newPreferredNetworkMode = Integer.parseInt((String) object);
final ListPreference listPreference = (ListPreference) preference;
mBuilder.setPreferenceValueAndSummary(newPreferredNetworkMode);
listPreference.setValue(Integer.toString(mBuilder.getSelectedEntryValue()));
listPreference.setSummary(mBuilder.getSummary());
if (mTelephonyManager.setPreferredNetworkTypeBitmask(
MobileNetworkUtils.getRafFromNetworkType(newPreferredNetworkMode))) {
mBuilder.setPreferenceValueAndSummary(newPreferredNetworkMode);
listPreference.setValue(Integer.toString(mBuilder.getSelectedEntryValue()));
listPreference.setSummary(mBuilder.getSummary());
return true;
}
return false;
setAllowedNetworkTypes(mTelephonyManager, mViewLifecycleOwner, newPreferredNetworkMode);
return true;
}
void init(int subId, FragmentManager fragmentManager) {
@@ -201,6 +203,11 @@ public class EnabledNetworkModePreferenceController extends
}
}
@Override
public void onViewCreated(@NonNull LifecycleOwner viewLifecycleOwner) {
mViewLifecycleOwner = viewLifecycleOwner;
}
private void updatePreference() {
if (mPreferenceScreen != null) {
displayPreference(mPreferenceScreen);

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2024 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.telephony.TelephonyManager
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
fun TelephonyManager.setAllowedNetworkTypes(
viewLifecycleOwner: LifecycleOwner,
newPreferredNetworkMode: Int,
) {
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Default) {
setAllowedNetworkTypesForReason(
TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
MobileNetworkUtils.getRafFromNetworkType(newPreferredNetworkMode),
)
}
}