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