2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.applications;
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.preference.Preference;
21 import android.preference.Preference.OnPreferenceClickListener;
22 import android.text.format.Formatter;
23 import android.widget.TextView;
25 import com.android.settings.InstrumentedFragment;
26 import com.android.settings.R;
27 import com.android.settings.Utils;
28 import com.android.settings.applications.ProcStatsData.MemInfo;
30 public class ProcessStatsSummary extends ProcessStatsBase implements OnPreferenceClickListener {
32 private static final String KEY_STATUS_HEADER = "status_header";
34 private static final String KEY_PERFORMANCE = "performance";
35 private static final String KEY_TOTAL_MEMORY = "total_memory";
36 private static final String KEY_AVERAGY_USED = "average_used";
37 private static final String KEY_FREE = "free";
38 private static final String KEY_APP_LIST = "apps_list";
40 private LinearColorBar mColors;
41 private LayoutPreference mHeader;
42 private TextView mMemStatus;
44 private Preference mPerformance;
45 private Preference mTotalMemory;
46 private Preference mAverageUsed;
47 private Preference mFree;
48 private Preference mAppListPreference;
51 public void onCreate(Bundle icicle) {
52 super.onCreate(icicle);
54 addPreferencesFromResource(R.xml.process_stats_summary);
55 mHeader = (LayoutPreference) findPreference(KEY_STATUS_HEADER);
56 mMemStatus = (TextView) mHeader.findViewById(R.id.memory_state);
57 mColors = (LinearColorBar) mHeader.findViewById(R.id.color_bar);
59 mPerformance = findPreference(KEY_PERFORMANCE);
60 mTotalMemory = findPreference(KEY_TOTAL_MEMORY);
61 mAverageUsed = findPreference(KEY_AVERAGY_USED);
62 mFree = findPreference(KEY_FREE);
63 mAppListPreference = findPreference(KEY_APP_LIST);
64 mAppListPreference.setOnPreferenceClickListener(this);
68 public void refreshUi() {
69 Context context = getContext();
70 int memColor = context.getColor(R.color.running_processes_apps_ram);
71 mColors.setColors(memColor, memColor, context.getColor(R.color.running_processes_free_ram));
73 MemInfo memInfo = mStatsManager.getMemInfo();
75 double usedRam = memInfo.realUsedRam;
76 double totalRam = memInfo.realTotalRam;
77 double freeRam = memInfo.realFreeRam;
78 String usedString = Formatter.formatShortFileSize(context, (long) usedRam);
79 String totalString = Formatter.formatShortFileSize(context, (long) totalRam);
80 String freeString = Formatter.formatShortFileSize(context, (long) freeRam);
81 CharSequence memString;
82 CharSequence[] memStatesStr = getResources().getTextArray(R.array.ram_states);
83 int memState = mStatsManager.getMemState();
84 if (memState >= 0 && memState < memStatesStr.length - 1) {
85 memString = memStatesStr[memState];
87 memString = memStatesStr[memStatesStr.length - 1];
89 mMemStatus.setText(usedString);
90 float usedRatio = (float)(usedRam / (freeRam + usedRam));
91 mColors.setRatios(usedRatio, 0, 1 - usedRatio);
93 mPerformance.setSummary(memString);
94 mTotalMemory.setSummary(totalString);
95 mAverageUsed.setSummary(Utils.formatPercentage((long) usedRam, (long) totalRam));
96 mFree.setSummary(freeString);
97 String durationString = getString(sDurationLabels[mDurationIndex]);
98 int numApps = mStatsManager.getEntries().size();
99 mAppListPreference.setSummary(getResources().getQuantityString(
100 R.plurals.memory_usage_apps_summary, numApps, numApps, durationString));
104 protected int getMetricsCategory() {
105 return InstrumentedFragment.PROCESS_STATS_SUMMARY;
109 public boolean onPreferenceClick(Preference preference) {
110 if (preference == mAppListPreference) {
111 Bundle args = new Bundle();
112 args.putBoolean(ARG_TRANSFER_STATS, true);
113 args.putInt(ARG_DURATION_INDEX, mDurationIndex);
114 mStatsManager.xferStats();
115 startFragment(this, ProcessStatsUi.class.getName(), R.string.app_memory_use, 0, args);