Settings: Fix total RAM calculation

- Showing 11GB for 12GB RAM variant device
This commit is contained in:
SuperDroidBond
2024-02-20 21:42:36 +05:30
committed by Joey
parent 62c569e201
commit 7dab125134

View File

@@ -16,25 +16,16 @@
package com.android.settings.deviceinfo.hardwareinfo; package com.android.settings.deviceinfo.hardwareinfo;
import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
import android.text.format.Formatter;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.applications.ProcStatsData;
import com.android.settings.applications.ProcessStatsBase;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.slices.Sliceable;
import com.android.settingslib.utils.ThreadUtils;
public class TotalRAMPreferenceController extends BasePreferenceController implements import java.text.DecimalFormat;
PreferenceControllerMixin {
private ProcStatsData mProcStatsData; public class TotalRAMPreferenceController extends BasePreferenceController {
private PreferenceScreen mPreferenceScreen;
public TotalRAMPreferenceController(Context context, String preferenceKey) { public TotalRAMPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
@@ -47,35 +38,42 @@ public class TotalRAMPreferenceController extends BasePreferenceController imple
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public boolean useDynamicSliceSummary() {
super.displayPreference(screen); return true;
mProcStatsData = getProcStatsData();
mPreferenceScreen = screen;
setDuration();
} }
@Override @Override
public void updateState(Preference preference) { public boolean isSliceable() {
// This is posted on the background thread to speed up fragment launch time for dev options return true;
// mProcStasData.refreshStats(true) takes ~20ms to run.
ThreadUtils.postOnBackgroundThread(() -> {
mProcStatsData.refreshStats(true);
final ProcStatsData.MemInfo memInfo = mProcStatsData.getMemInfo();
final String totalResult = Formatter.formatShortFileSize(mContext,
(long) memInfo.realTotalRam);
ThreadUtils.postOnMainThread(
() -> mPreferenceScreen.findPreference(mPreferenceKey).setSummary(totalResult));
});
} }
@VisibleForTesting @Override
void setDuration() { public CharSequence getSummary() {
mProcStatsData.setDuration(ProcessStatsBase.sDurations[0] /* 3 hours */); ActivityManager actManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totRam = memInfo.totalMem;
double gb = (double) totRam / 1073741824.0;
String aproxRam;
if (gb > 0 && gb <= 2) {
aproxRam = "2";
} else if (gb <= 3) {
aproxRam = "3";
} else if (gb <= 4) {
aproxRam = "4";
} else if (gb <= 6) {
aproxRam = "6";
} else if (gb <= 8) {
aproxRam = "8";
} else if (gb <= 12) {
aproxRam = "12";
} else {
aproxRam = "12+";
} }
@VisibleForTesting String actualRam = aproxRam.concat(" GB");
ProcStatsData getProcStatsData() { return actualRam;
return new ProcStatsData(mContext, false);
} }
} }