1 /* 2 * Copyright (C) 2015 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 package com.android.settings.applications; 17 18 import android.app.Activity; 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.Preference.OnPreferenceClickListener; 23 import android.text.format.Formatter; 24 import android.text.format.Formatter.BytesResult; 25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 26 import com.android.settings.R; 27 import com.android.settings.SummaryPreference; 28 import com.android.settings.Utils; 29 import com.android.settings.applications.ProcStatsData.MemInfo; 30 import com.android.settings.dashboard.SummaryLoader; 31 32 public class ProcessStatsSummary extends ProcessStatsBase implements OnPreferenceClickListener { 33 34 private static final String KEY_STATUS_HEADER = "status_header"; 35 36 private static final String KEY_PERFORMANCE = "performance"; 37 private static final String KEY_TOTAL_MEMORY = "total_memory"; 38 private static final String KEY_AVERAGY_USED = "average_used"; 39 private static final String KEY_FREE = "free"; 40 private static final String KEY_APP_LIST = "apps_list"; 41 42 private SummaryPreference mSummaryPref; 43 44 private Preference mPerformance; 45 private Preference mTotalMemory; 46 private Preference mAverageUsed; 47 private Preference mFree; 48 private Preference mAppListPreference; 49 50 @Override onCreate(Bundle icicle)51 public void onCreate(Bundle icicle) { 52 super.onCreate(icicle); 53 54 addPreferencesFromResource(R.xml.process_stats_summary); 55 mSummaryPref = (SummaryPreference) findPreference(KEY_STATUS_HEADER); 56 mPerformance = findPreference(KEY_PERFORMANCE); 57 mTotalMemory = findPreference(KEY_TOTAL_MEMORY); 58 mAverageUsed = findPreference(KEY_AVERAGY_USED); 59 mFree = findPreference(KEY_FREE); 60 mAppListPreference = findPreference(KEY_APP_LIST); 61 mAppListPreference.setOnPreferenceClickListener(this); 62 } 63 64 @Override refreshUi()65 public void refreshUi() { 66 Context context = getContext(); 67 68 MemInfo memInfo = mStatsManager.getMemInfo(); 69 70 double usedRam = memInfo.realUsedRam; 71 double totalRam = memInfo.realTotalRam; 72 double freeRam = memInfo.realFreeRam; 73 BytesResult usedResult = Formatter.formatBytes(context.getResources(), (long) usedRam, 74 Formatter.FLAG_SHORTER); 75 String totalString = Formatter.formatShortFileSize(context, (long) totalRam); 76 String freeString = Formatter.formatShortFileSize(context, (long) freeRam); 77 CharSequence memString; 78 CharSequence[] memStatesStr = getResources().getTextArray(R.array.ram_states); 79 int memState = mStatsManager.getMemState(); 80 if (memState >= 0 && memState < memStatesStr.length - 1) { 81 memString = memStatesStr[memState]; 82 } else { 83 memString = memStatesStr[memStatesStr.length - 1]; 84 } 85 mSummaryPref.setAmount(usedResult.value); 86 mSummaryPref.setUnits(usedResult.units); 87 float usedRatio = (float)(usedRam / (freeRam + usedRam)); 88 mSummaryPref.setRatios(usedRatio, 0, 1 - usedRatio); 89 90 mPerformance.setSummary(memString); 91 mTotalMemory.setSummary(totalString); 92 mAverageUsed.setSummary(Utils.formatPercentage((long) usedRam, (long) totalRam)); 93 mFree.setSummary(freeString); 94 String durationString = getString(sDurationLabels[mDurationIndex]); 95 int numApps = mStatsManager.getEntries().size(); 96 mAppListPreference.setSummary(getResources().getQuantityString( 97 R.plurals.memory_usage_apps_summary, numApps, numApps, durationString)); 98 } 99 100 @Override getMetricsCategory()101 public int getMetricsCategory() { 102 return MetricsEvent.PROCESS_STATS_SUMMARY; 103 } 104 105 @Override getHelpResource()106 protected int getHelpResource() { 107 return R.string.help_uri_process_stats_summary; 108 } 109 110 @Override onPreferenceClick(Preference preference)111 public boolean onPreferenceClick(Preference preference) { 112 if (preference == mAppListPreference) { 113 Bundle args = new Bundle(); 114 args.putBoolean(ARG_TRANSFER_STATS, true); 115 args.putInt(ARG_DURATION_INDEX, mDurationIndex); 116 mStatsManager.xferStats(); 117 startFragment(this, ProcessStatsUi.class.getName(), R.string.memory_usage_apps, 0, 118 args); 119 return true; 120 } 121 return false; 122 } 123 124 private static class SummaryProvider implements SummaryLoader.SummaryProvider { 125 126 private final Context mContext; 127 private final SummaryLoader mSummaryLoader; 128 SummaryProvider(Context context, SummaryLoader summaryLoader)129 public SummaryProvider(Context context, SummaryLoader summaryLoader) { 130 mContext = context; 131 mSummaryLoader = summaryLoader; 132 } 133 134 @Override setListening(boolean listening)135 public void setListening(boolean listening) { 136 if (listening) { 137 ProcStatsData statsManager = new ProcStatsData(mContext, false); 138 statsManager.setDuration(sDurations[0]); 139 MemInfo memInfo = statsManager.getMemInfo(); 140 String usedResult = Formatter.formatShortFileSize(mContext, 141 (long) memInfo.realUsedRam); 142 String totalResult = Formatter.formatShortFileSize(mContext, 143 (long) memInfo.realTotalRam); 144 mSummaryLoader.setSummary(this, mContext.getString(R.string.memory_summary, 145 usedResult, totalResult)); 146 } 147 } 148 } 149 150 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY 151 = new SummaryLoader.SummaryProviderFactory() { 152 @Override 153 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity, 154 SummaryLoader summaryLoader) { 155 return new SummaryProvider(activity, summaryLoader); 156 } 157 }; 158 159 } 160