Merge "[Settings] Code Refactor for performance"
This commit is contained in:
@@ -109,7 +109,9 @@ public class MyDeviceInfoFragment extends DashboardFragment
|
||||
|
||||
final ExecutorService executor = (fragment == null) ? null :
|
||||
Executors.newSingleThreadExecutor();
|
||||
final SlotSimStatus slotSimStatus = new SlotSimStatus(context, executor);
|
||||
androidx.lifecycle.Lifecycle lifecycleObject = (fragment == null) ? null :
|
||||
fragment.getLifecycle();
|
||||
final SlotSimStatus slotSimStatus = new SlotSimStatus(context, executor, lifecycleObject);
|
||||
|
||||
controllers.add(new IpAddressPreferenceController(context, lifecycle));
|
||||
controllers.add(new WifiMacAddressPreferenceController(context, lifecycle));
|
||||
|
||||
@@ -17,12 +17,14 @@
|
||||
package com.android.settings.deviceinfo.simstatus;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.os.UserManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
@@ -40,16 +42,12 @@ public class SimStatusPreferenceController extends BasePreferenceController {
|
||||
|
||||
private static final String KEY_PREFERENCE_CATEGORY = "device_detail_category";
|
||||
|
||||
private final SubscriptionManager mSubscriptionManager;
|
||||
private final List<Preference> mPreferenceList = new ArrayList<>();
|
||||
|
||||
private Fragment mFragment;
|
||||
private SlotSimStatus mSlotSimStatus;
|
||||
private Observer mSimChangeObserver;
|
||||
|
||||
public SimStatusPreferenceController(Context context, String prefKey) {
|
||||
super(context, prefKey);
|
||||
|
||||
mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,26 +101,36 @@ public class SimStatusPreferenceController extends BasePreferenceController {
|
||||
// Add additional preferences for each sim in the device
|
||||
for (int simSlotNumber = 0; simSlotNumber < mSlotSimStatus.size(); simSlotNumber++) {
|
||||
final Preference multiSimPreference = createNewPreference(screen.getContext());
|
||||
multiSimPreference.setCopyingEnabled(true);
|
||||
multiSimPreference.setOrder(mSlotSimStatus.getPreferenceOrdering(simSlotNumber));
|
||||
multiSimPreference.setKey(mSlotSimStatus.getPreferenceKey(simSlotNumber));
|
||||
category.addPreference(multiSimPreference);
|
||||
mPreferenceList.add(multiSimPreference);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
for (int simSlotNumber = 0; simSlotNumber < mPreferenceList.size(); simSlotNumber++) {
|
||||
final Preference simStatusPreference = mPreferenceList.get(simSlotNumber);
|
||||
simStatusPreference.setTitle(getPreferenceTitle(simSlotNumber /* sim slot */));
|
||||
simStatusPreference.setSummary(getCarrierName(simSlotNumber /* sim slot */));
|
||||
final int simSlot = getSimSlotIndex();
|
||||
if (mSimChangeObserver == null) {
|
||||
mSimChangeObserver = x -> updateStateBySlot(preference, simSlot);
|
||||
mSlotSimStatus.observe(mFragment.getViewLifecycleOwner(), mSimChangeObserver);
|
||||
}
|
||||
updateStateBySlot(preference, simSlot);
|
||||
}
|
||||
|
||||
protected void updateStateBySlot(Preference preference, int simSlot) {
|
||||
SubscriptionInfo subInfo = getSubscriptionInfo(simSlot);
|
||||
preference.setEnabled(subInfo != null);
|
||||
preference.setCopyingEnabled(subInfo != null);
|
||||
preference.setTitle(getPreferenceTitle(simSlot));
|
||||
preference.setSummary(getCarrierName(simSlot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
final int simSlot = mPreferenceList.indexOf(preference);
|
||||
if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
|
||||
return false;
|
||||
}
|
||||
final int simSlot = getSimSlotIndex();
|
||||
if (simSlot == -1) {
|
||||
return false;
|
||||
}
|
||||
@@ -138,16 +146,7 @@ public class SimStatusPreferenceController extends BasePreferenceController {
|
||||
}
|
||||
|
||||
private SubscriptionInfo getSubscriptionInfo(int simSlot) {
|
||||
final List<SubscriptionInfo> subscriptionInfoList =
|
||||
mSubscriptionManager.getActiveSubscriptionInfoList();
|
||||
if (subscriptionInfoList != null) {
|
||||
for (SubscriptionInfo info : subscriptionInfoList) {
|
||||
if (info.getSimSlotIndex() == simSlot) {
|
||||
return info;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return (mSlotSimStatus == null) ? null : mSlotSimStatus.getSubscriptionInfo(simSlot);
|
||||
}
|
||||
|
||||
private CharSequence getCarrierName(int simSlot) {
|
||||
|
||||
@@ -17,24 +17,43 @@
|
||||
package com.android.settings.deviceinfo.simstatus;
|
||||
|
||||
import android.content.Context;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.android.settings.network.SubscriptionsChangeListener;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Phaser;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* A class for showing a summary of status of sim slots.
|
||||
*/
|
||||
public class SlotSimStatus {
|
||||
public class SlotSimStatus extends LiveData<Long>
|
||||
implements DefaultLifecycleObserver,
|
||||
SubscriptionsChangeListener.SubscriptionsChangeListenerClient {
|
||||
|
||||
private static final String TAG = "SlotSimStatus";
|
||||
|
||||
private final AtomicInteger mNumberOfSlots = new AtomicInteger(0);
|
||||
private final ConcurrentHashMap<Integer, SubscriptionInfo> mSubscriptionMap =
|
||||
new ConcurrentHashMap<Integer, SubscriptionInfo>();
|
||||
private final Phaser mBlocker = new Phaser(1);
|
||||
private final AtomicLong mDataVersion = new AtomicLong(0);
|
||||
|
||||
private Context mContext;
|
||||
private int mBasePreferenceOrdering;
|
||||
private SubscriptionsChangeListener mSubscriptionsChangeListener;
|
||||
|
||||
private static final String KEY_SIM_STATUS = "sim_status";
|
||||
|
||||
@@ -43,28 +62,71 @@ public class SlotSimStatus {
|
||||
* @param context Context
|
||||
*/
|
||||
public SlotSimStatus(Context context) {
|
||||
this(context, null);
|
||||
this(context, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct of class.
|
||||
* @param context Context
|
||||
* @param executor executor for offload to thread
|
||||
* @param lifecycle Lifecycle
|
||||
*/
|
||||
public SlotSimStatus(Context context, Executor executor) {
|
||||
public SlotSimStatus(Context context, Executor executor, Lifecycle lifecycle) {
|
||||
mContext = context;
|
||||
if (executor == null) {
|
||||
queryRecords(context);
|
||||
} else {
|
||||
executor.execute(() -> queryRecords(context));
|
||||
executor.execute(() -> asyncQueryRecords(context));
|
||||
}
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
mSubscriptionsChangeListener = new SubscriptionsChangeListener(context, this);
|
||||
mSubscriptionsChangeListener.start();
|
||||
}
|
||||
}
|
||||
|
||||
protected void queryRecords(Context context) {
|
||||
queryDetails(context);
|
||||
setValue(mDataVersion.incrementAndGet());
|
||||
mBlocker.arrive();
|
||||
}
|
||||
|
||||
protected void asyncQueryRecords(Context context) {
|
||||
queryDetails(context);
|
||||
postValue(mDataVersion.incrementAndGet());
|
||||
mBlocker.arrive();
|
||||
}
|
||||
|
||||
protected void updateRecords() {
|
||||
queryDetails(mContext);
|
||||
setValue(mDataVersion.incrementAndGet());
|
||||
}
|
||||
|
||||
protected void queryDetails(Context context) {
|
||||
TelephonyManager telMgr = context.getSystemService(TelephonyManager.class);
|
||||
if (telMgr != null) {
|
||||
mNumberOfSlots.set(telMgr.getPhoneCount());
|
||||
}
|
||||
mBlocker.arrive();
|
||||
|
||||
SubscriptionManager subMgr = context.getSystemService(SubscriptionManager.class);
|
||||
if (subMgr == null) {
|
||||
mSubscriptionMap.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
List<SubscriptionInfo> subInfoList = subMgr.getActiveSubscriptionInfoList();
|
||||
if ((subInfoList == null) || (subInfoList.size() <= 0)) {
|
||||
mSubscriptionMap.clear();
|
||||
Log.d(TAG, "No active SIM.");
|
||||
return;
|
||||
}
|
||||
|
||||
mSubscriptionMap.clear();
|
||||
subInfoList.forEach(subInfo -> {
|
||||
int slotIndex = subInfo.getSimSlotIndex();
|
||||
mSubscriptionMap.put(slotIndex, subInfo);
|
||||
});
|
||||
Log.d(TAG, "Number of active SIM: " + subInfoList.size());
|
||||
}
|
||||
|
||||
protected void waitForResult() {
|
||||
@@ -109,6 +171,19 @@ public class SlotSimStatus {
|
||||
return KEY_SIM_STATUS + (1 + slotIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription based on slot index.
|
||||
* @param slotIndex index of slot (starting from 0)
|
||||
* @return SubscriptionInfo based on index of slot.
|
||||
* {@code null} means no subscription on slot.
|
||||
*/
|
||||
public SubscriptionInfo getSubscriptionInfo(int slotIndex) {
|
||||
if (slotIndex >= size()) {
|
||||
return null;
|
||||
}
|
||||
return mSubscriptionMap.get(slotIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get slot index based on Preference key
|
||||
* @param prefKey is the preference key
|
||||
@@ -124,4 +199,28 @@ public class SlotSimStatus {
|
||||
}
|
||||
return simSlotIndex - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAirplaneModeChanged(boolean airplaneModeEnabled) {
|
||||
if (airplaneModeEnabled) {
|
||||
/**
|
||||
* Only perform update when airplane mode ON.
|
||||
* Relay on #onSubscriptionsChanged() when airplane mode OFF.
|
||||
*/
|
||||
updateRecords();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscriptionsChanged() {
|
||||
updateRecords();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy(LifecycleOwner lifecycleOwner) {
|
||||
if (mSubscriptionsChangeListener != null) {
|
||||
mSubscriptionsChangeListener.stop();
|
||||
}
|
||||
lifecycleOwner.getLifecycle().removeObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user