[Settings] Code Refactor for performance
Reduce number of SubscriptionManager API queries. Bug: 260540995 Test: local and auto Change-Id: Ib05660d9ade7f352a7cc82d4065974aec396714a
This commit is contained in:
@@ -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