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