[Settings] Code refactor - apply BasePreferenceController
Apply BasePreferenceController for better compatible with existing softawre architecture. Bug: 260540995 Test: local Change-Id: I6d69ff878a062299b4d8478f6c0341b37a28532d
This commit is contained in:
@@ -100,7 +100,7 @@
|
||||
settings:keywords="@string/keywords_sim_status"
|
||||
android:summary="@string/summary_placeholder"
|
||||
settings:isPreferenceVisible="@bool/config_show_sim_info"
|
||||
settings:enableCopying="true"/>
|
||||
settings:controller="com.android.settings.deviceinfo.simstatus.SimStatusPreferenceController"/>
|
||||
|
||||
<!-- Model & hardware -->
|
||||
<Preference
|
||||
|
@@ -104,13 +104,14 @@ public class MyDeviceInfoFragment extends DashboardFragment
|
||||
Context context, MyDeviceInfoFragment fragment, Lifecycle lifecycle) {
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
|
||||
String simStatusKey = SimStatusPreferenceController.KEY_SIM_STATUS;
|
||||
SimStatusPreferenceController defaultRecord =
|
||||
new SimStatusPreferenceController(context, fragment);
|
||||
new SimStatusPreferenceController(context, simStatusKey);
|
||||
|
||||
for (int slotIndex = 0; slotIndex < defaultRecord.getSimSlotSize(); slotIndex ++) {
|
||||
SimStatusPreferenceController slotRecord =
|
||||
new SimStatusPreferenceController(context, fragment);
|
||||
slotRecord.setSimSlotStatus(slotIndex);
|
||||
new SimStatusPreferenceController(context, simStatusKey + slotIndex + 1);
|
||||
slotRecord.init(fragment, slotIndex);
|
||||
controllers.add(slotRecord);
|
||||
}
|
||||
|
||||
|
@@ -20,6 +20,7 @@ import android.content.Context;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.fragment.app.Fragment;
|
||||
@@ -28,48 +29,39 @@ import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.network.SubscriptionUtil;
|
||||
import com.android.settingslib.deviceinfo.AbstractSimStatusImeiInfoPreferenceController;
|
||||
import com.android.settingslib.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SimStatusPreferenceController extends
|
||||
AbstractSimStatusImeiInfoPreferenceController implements PreferenceControllerMixin {
|
||||
public class SimStatusPreferenceController extends BasePreferenceController {
|
||||
|
||||
private static final String KEY_SIM_STATUS = "sim_status";
|
||||
public static final String KEY_SIM_STATUS = "sim_status";
|
||||
private static final String KEY_PREFERENCE_CATEGORY = "device_detail_category";
|
||||
|
||||
private final TelephonyManager mTelephonyManager;
|
||||
private final SubscriptionManager mSubscriptionManager;
|
||||
private final Fragment mFragment;
|
||||
private final List<Preference> mPreferenceList = new ArrayList<>();
|
||||
|
||||
private Fragment mFragment;
|
||||
private int mSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
|
||||
|
||||
public SimStatusPreferenceController(Context context, Fragment fragment) {
|
||||
super(context);
|
||||
public SimStatusPreferenceController(Context context, String prefKey) {
|
||||
super(context, prefKey);
|
||||
|
||||
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
mSubscriptionManager = (SubscriptionManager) context.getSystemService(
|
||||
Context.TELEPHONY_SUBSCRIPTION_SERVICE);
|
||||
mFragment = fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
if (mSlotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
|
||||
return KEY_SIM_STATUS + mSlotIndex;
|
||||
}
|
||||
return KEY_SIM_STATUS;
|
||||
mTelephonyManager = context.getSystemService(TelephonyManager.class);
|
||||
mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the index of slot for this subscription.
|
||||
* Initialize this preference controller.
|
||||
* @param fragment parent fragment
|
||||
* @param slotIndex index of slot
|
||||
*/
|
||||
public void setSimSlotStatus(int slotIndex) {
|
||||
public void init(Fragment fragment, int slotIndex) {
|
||||
mFragment = fragment;
|
||||
mSlotIndex = slotIndex;
|
||||
}
|
||||
|
||||
@@ -90,9 +82,14 @@ public class SimStatusPreferenceController extends
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return SubscriptionUtil.isSimHardwareVisible(mContext) &&
|
||||
super.isAvailable();
|
||||
public int getAvailabilityStatus() {
|
||||
if (getSimSlotIndex() == SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
|
||||
return UNSUPPORTED_ON_DEVICE;
|
||||
}
|
||||
boolean isAvailable = SubscriptionUtil.isSimHardwareVisible(mContext) &&
|
||||
mContext.getSystemService(UserManager.class).isAdminUser() &&
|
||||
!Utils.isWifiOnly(mContext);
|
||||
return isAvailable ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
@@ -35,6 +36,7 @@ import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -81,21 +83,20 @@ public class SimStatusPreferenceControllerTest {
|
||||
when(mContext.getResources()).thenReturn(mResources);
|
||||
when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
|
||||
|
||||
doReturn(mTelephonyManager).when(mContext)
|
||||
.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
mockService(Context.TELEPHONY_SERVICE, TelephonyManager.class, mTelephonyManager);
|
||||
|
||||
doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
|
||||
mockService(Context.USER_SERVICE, UserManager.class, mUserManager);
|
||||
final List<Preference> preferencePool = new ArrayList<Preference>();
|
||||
preferencePool.add(mFirstSimPreference);
|
||||
preferencePool.add(mSecondSimPreference);
|
||||
|
||||
mController = spy(new SimStatusPreferenceController(mContext, mFragment) {
|
||||
mController = spy(new SimStatusPreferenceController(mContext, "sim_status") {
|
||||
@Override
|
||||
public Preference createNewPreference(Context context) {
|
||||
return preferencePool.remove(0);
|
||||
}
|
||||
});
|
||||
doReturn(true).when(mController).isAvailable();
|
||||
doReturn(BasePreferenceController.AVAILABLE).when(mController).getAvailabilityStatus();
|
||||
when(mScreen.getContext()).thenReturn(mContext);
|
||||
final String categoryKey = "device_detail_category";
|
||||
when(mScreen.findPreference(categoryKey)).thenReturn(mCategory);
|
||||
@@ -105,7 +106,7 @@ public class SimStatusPreferenceControllerTest {
|
||||
when(mPreference.getKey()).thenReturn(prefKey);
|
||||
when(mPreference.isVisible()).thenReturn(true);
|
||||
|
||||
mController.setSimSlotStatus(-1);
|
||||
mController.init(mFragment, SubscriptionManager.INVALID_SIM_SLOT_INDEX);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -154,4 +155,9 @@ public class SimStatusPreferenceControllerTest {
|
||||
|
||||
verify(mFragment).getChildFragmentManager();
|
||||
}
|
||||
|
||||
private <T> void mockService(String serviceName, Class<T> serviceClass, T service) {
|
||||
when(mContext.getSystemServiceName(serviceClass)).thenReturn(serviceName);
|
||||
when(mContext.getSystemService(serviceName)).thenReturn(service);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user