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