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.text.format.Formatter;
21 
22 import androidx.annotation.VisibleForTesting;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.applications.ProcStatsData;
28 import com.android.settings.applications.ProcessStatsBase;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
31 import com.android.settingslib.utils.ThreadUtils;
32 
33 public class MemoryUsagePreferenceController extends DeveloperOptionsPreferenceController implements
34         PreferenceControllerMixin {
35 
36     private static final String MEMORY_USAGE_KEY = "memory";
37 
38     private ProcStatsData mProcStatsData;
39 
MemoryUsagePreferenceController(Context context)40     public MemoryUsagePreferenceController(Context context) {
41         super(context);
42     }
43 
44     @Override
getPreferenceKey()45     public String getPreferenceKey() {
46         return MEMORY_USAGE_KEY;
47     }
48 
49     @Override
displayPreference(PreferenceScreen screen)50     public void displayPreference(PreferenceScreen screen) {
51         super.displayPreference(screen);
52 
53         mProcStatsData = getProcStatsData();
54         setDuration();
55     }
56 
57     @Override
updateState(Preference preference)58     public void updateState(Preference preference) {
59         // This is posted on the background thread to speed up fragment launch time for dev options
60         // mProcStasData.refreshStats(true) takes ~20ms to run.
61         ThreadUtils.postOnBackgroundThread(() -> {
62             mProcStatsData.refreshStats(true);
63             final ProcStatsData.MemInfo memInfo = mProcStatsData.getMemInfo();
64             final String usedResult = Formatter.formatShortFileSize(mContext,
65                     (long) memInfo.realUsedRam);
66             final String totalResult = Formatter.formatShortFileSize(mContext,
67                     (long) memInfo.realTotalRam);
68             ThreadUtils.postOnMainThread(
69                     () -> mPreference.setSummary(mContext.getString(R.string.memory_summary,
70                             usedResult, totalResult)));
71         });
72     }
73 
74     @VisibleForTesting
setDuration()75     void setDuration() {
76         mProcStatsData.setDuration(ProcessStatsBase.sDurations[0] /* 3 hours */);
77     }
78 
79     @VisibleForTesting
getProcStatsData()80     ProcStatsData getProcStatsData() {
81         return new ProcStatsData(mContext, false);
82     }
83 }
84