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.TextUtils;
23import android.text.format.Formatter;
24import android.text.format.Formatter.BytesResult;
25import android.widget.TextView;
26
27import com.android.internal.logging.MetricsLogger;
28import com.android.settings.R;
29import com.android.settings.Utils;
30import com.android.settings.applications.ProcStatsData.MemInfo;
31
32public 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 LinearColorBar mColors;
43    private LayoutPreference mHeader;
44    private TextView mMemStatus;
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        mHeader = (LayoutPreference) findPreference(KEY_STATUS_HEADER);
58        mMemStatus = (TextView) mHeader.findViewById(R.id.memory_state);
59        mColors = (LinearColorBar) mHeader.findViewById(R.id.color_bar);
60
61        mPerformance = findPreference(KEY_PERFORMANCE);
62        mTotalMemory = findPreference(KEY_TOTAL_MEMORY);
63        mAverageUsed = findPreference(KEY_AVERAGY_USED);
64        mFree = findPreference(KEY_FREE);
65        mAppListPreference = findPreference(KEY_APP_LIST);
66        mAppListPreference.setOnPreferenceClickListener(this);
67    }
68
69    @Override
70    public void refreshUi() {
71        Context context = getContext();
72        int memColor = context.getColor(R.color.running_processes_apps_ram);
73        mColors.setColors(memColor, memColor, context.getColor(R.color.running_processes_free_ram));
74
75        MemInfo memInfo = mStatsManager.getMemInfo();
76
77        double usedRam = memInfo.realUsedRam;
78        double totalRam = memInfo.realTotalRam;
79        double freeRam = memInfo.realFreeRam;
80        BytesResult usedResult = Formatter.formatBytes(context.getResources(), (long) usedRam,
81                Formatter.FLAG_SHORTER);
82        String totalString = Formatter.formatShortFileSize(context, (long) totalRam);
83        String freeString = Formatter.formatShortFileSize(context, (long) freeRam);
84        CharSequence memString;
85        CharSequence[] memStatesStr = getResources().getTextArray(R.array.ram_states);
86        int memState = mStatsManager.getMemState();
87        if (memState >= 0 && memState < memStatesStr.length - 1) {
88            memString = memStatesStr[memState];
89        } else {
90            memString = memStatesStr[memStatesStr.length - 1];
91        }
92        mMemStatus.setText(TextUtils.expandTemplate(getText(R.string.storage_size_large),
93                usedResult.value, usedResult.units));
94        float usedRatio = (float)(usedRam / (freeRam + usedRam));
95        mColors.setRatios(usedRatio, 0, 1 - usedRatio);
96
97        mPerformance.setSummary(memString);
98        mTotalMemory.setSummary(totalString);
99        mAverageUsed.setSummary(Utils.formatPercentage((long) usedRam, (long) totalRam));
100        mFree.setSummary(freeString);
101        String durationString = getString(sDurationLabels[mDurationIndex]);
102        int numApps = mStatsManager.getEntries().size();
103        mAppListPreference.setSummary(getResources().getQuantityString(
104                R.plurals.memory_usage_apps_summary, numApps, numApps, durationString));
105    }
106
107    @Override
108    protected int getMetricsCategory() {
109        return MetricsLogger.PROCESS_STATS_SUMMARY;
110    }
111
112    @Override
113    public boolean onPreferenceClick(Preference preference) {
114        if (preference == mAppListPreference) {
115            Bundle args = new Bundle();
116            args.putBoolean(ARG_TRANSFER_STATS, true);
117            args.putInt(ARG_DURATION_INDEX, mDurationIndex);
118            mStatsManager.xferStats();
119            startFragment(this, ProcessStatsUi.class.getName(), R.string.app_list_memory_use, 0,
120                    args);
121            return true;
122        }
123        return false;
124    }
125
126}
127