ProcessStatsSummary.java revision beb171d2e50f93b5fb78d73b372a4981e13e04ff
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 */
16package com.android.settings.applications;
17
18import android.content.Context;
19import android.os.Bundle;
20import android.preference.Preference;
21import android.preference.Preference.OnPreferenceClickListener;
22import android.text.format.Formatter;
23import android.widget.TextView;
24
25import com.android.settings.InstrumentedFragment;
26import com.android.settings.R;
27import com.android.settings.Utils;
28import com.android.settings.applications.ProcStatsData.MemInfo;
29
30public class ProcessStatsSummary extends ProcessStatsBase implements OnPreferenceClickListener {
31
32    private static final String KEY_STATUS_HEADER = "status_header";
33
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";
39
40    private LinearColorBar mColors;
41    private LayoutPreference mHeader;
42    private TextView mMemStatus;
43
44    private Preference mPerformance;
45    private Preference mTotalMemory;
46    private Preference mAverageUsed;
47    private Preference mFree;
48    private Preference mAppListPreference;
49
50    @Override
51    public void onCreate(Bundle icicle) {
52        super.onCreate(icicle);
53
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);
58
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);
65    }
66
67    @Override
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));
72
73        MemInfo memInfo = mStatsManager.getMemInfo();
74
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];
86        } else {
87            memString = memStatesStr[memStatesStr.length - 1];
88        }
89        mMemStatus.setText(usedString);
90        float usedRatio = (float)(usedRam / (freeRam + usedRam));
91        mColors.setRatios(usedRatio, 0, 1 - usedRatio);
92
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));
101    }
102
103    @Override
104    protected int getMetricsCategory() {
105        return InstrumentedFragment.PROCESS_STATS_SUMMARY;
106    }
107
108    @Override
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);
116            return true;
117        }
118        return false;
119    }
120
121}
122