1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.development; 18 19 import android.content.Context; 20 import android.os.Flags; 21 import android.provider.Settings; 22 import android.text.format.Formatter; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.applications.ProcStatsData; 30 import com.android.settings.applications.ProcessStatsBase; 31 import com.android.settings.core.PreferenceControllerMixin; 32 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 33 import com.android.settingslib.utils.ThreadUtils; 34 35 public class MemoryUsagePreferenceController extends DeveloperOptionsPreferenceController implements 36 PreferenceControllerMixin { 37 38 private static final String MEMORY_USAGE_KEY = "memory"; 39 40 private ProcStatsData mProcStatsData; 41 MemoryUsagePreferenceController(Context context)42 public MemoryUsagePreferenceController(Context context) { 43 super(context); 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return MEMORY_USAGE_KEY; 49 } 50 51 @Override displayPreference(PreferenceScreen screen)52 public void displayPreference(PreferenceScreen screen) { 53 super.displayPreference(screen); 54 55 mProcStatsData = getProcStatsData(); 56 setDuration(); 57 } 58 59 @Override updateState(Preference preference)60 public void updateState(Preference preference) { 61 // This is posted on the background thread to speed up fragment launch time for dev options 62 // mProcStasData.refreshStats(true) takes ~20ms to run. 63 ThreadUtils.postOnBackgroundThread(() -> { 64 mProcStatsData.refreshStats(true); 65 final ProcStatsData.MemInfo memInfo = mProcStatsData.getMemInfo(); 66 final String usedResult = Formatter.formatShortFileSize(mContext, 67 (long) memInfo.realUsedRam); 68 final String totalResult = Formatter.formatShortFileSize(mContext, 69 (long) memInfo.realTotalRam); 70 boolean displayMemorySummary = !Flags.removeAppProfilerPssCollection(); 71 displayMemorySummary |= Settings.Global.getInt(mContext.getContentResolver(), 72 Settings.Global.FORCE_ENABLE_PSS_PROFILING, 0) == 1; 73 String summary = displayMemorySummary 74 ? mContext.getString(R.string.memory_summary, usedResult, totalResult) 75 : mContext.getString(R.string.pss_profiling_disabled); 76 ThreadUtils.postOnMainThread(() -> mPreference.setSummary(summary)); 77 }); 78 } 79 80 @VisibleForTesting setDuration()81 void setDuration() { 82 mProcStatsData.setDuration(ProcessStatsBase.sDurations[0] /* 3 hours */); 83 } 84 85 @VisibleForTesting getProcStatsData()86 ProcStatsData getProcStatsData() { 87 return new ProcStatsData(mContext, false); 88 } 89 } 90