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.settings.SettingsEnums;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.text.format.Formatter;
22 import android.text.format.Formatter.BytesResult;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.Preference.OnPreferenceClickListener;
26 
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 
33 public class ProcessStatsSummary extends ProcessStatsBase implements OnPreferenceClickListener {
34 
35     private static final String KEY_STATUS_HEADER = "status_header";
36 
37     private static final String KEY_PERFORMANCE = "performance";
38     private static final String KEY_TOTAL_MEMORY = "total_memory";
39     private static final String KEY_AVERAGY_USED = "average_used";
40     private static final String KEY_FREE = "free";
41     private static final String KEY_APP_LIST = "apps_list";
42 
43     private SummaryPreference mSummaryPref;
44 
45     private Preference mPerformance;
46     private Preference mTotalMemory;
47     private Preference mAverageUsed;
48     private Preference mFree;
49     private Preference mAppListPreference;
50 
51     @Override
onCreate(Bundle icicle)52     public void onCreate(Bundle icicle) {
53         super.onCreate(icicle);
54 
55         addPreferencesFromResource(R.xml.process_stats_summary);
56         mSummaryPref = (SummaryPreference) findPreference(KEY_STATUS_HEADER);
57         mPerformance = findPreference(KEY_PERFORMANCE);
58         mTotalMemory = findPreference(KEY_TOTAL_MEMORY);
59         mAverageUsed = findPreference(KEY_AVERAGY_USED);
60         mFree = findPreference(KEY_FREE);
61         mAppListPreference = findPreference(KEY_APP_LIST);
62         mAppListPreference.setOnPreferenceClickListener(this);
63     }
64 
65     @Override
refreshUi()66     public void refreshUi() {
67         Context context = getContext();
68 
69         MemInfo memInfo = mStatsManager.getMemInfo();
70 
71         double usedRam = memInfo.realUsedRam;
72         double totalRam = memInfo.realTotalRam;
73         double freeRam = memInfo.realFreeRam;
74         BytesResult usedResult = Formatter.formatBytes(context.getResources(), (long) usedRam,
75                 Formatter.FLAG_SHORTER);
76         String totalString = Formatter.formatShortFileSize(context, (long) totalRam);
77         String freeString = Formatter.formatShortFileSize(context, (long) freeRam);
78         CharSequence memString;
79         CharSequence[] memStatesStr = getResources().getTextArray(R.array.ram_states);
80         int memState = mStatsManager.getMemState();
81         if (memState >= 0 && memState < memStatesStr.length - 1) {
82             memString = memStatesStr[memState];
83         } else {
84             memString = memStatesStr[memStatesStr.length - 1];
85         }
86         mSummaryPref.setAmount(usedResult.value);
87         mSummaryPref.setUnits(usedResult.units);
88         float usedRatio = (float)(usedRam / (freeRam + usedRam));
89         mSummaryPref.setRatios(usedRatio, 0, 1 - usedRatio);
90 
91         mPerformance.setSummary(memString);
92         mTotalMemory.setSummary(totalString);
93         mAverageUsed.setSummary(Utils.formatPercentage((long) usedRam, (long) totalRam));
94         mFree.setSummary(freeString);
95         String durationString = getString(sDurationLabels[mDurationIndex]);
96         int numApps = mStatsManager.getEntries().size();
97         mAppListPreference.setSummary(getResources().getQuantityString(
98                 R.plurals.memory_usage_apps_summary, numApps, numApps, durationString));
99     }
100 
101     @Override
getMetricsCategory()102     public int getMetricsCategory() {
103         return SettingsEnums.PROCESS_STATS_SUMMARY;
104     }
105 
106     @Override
getHelpResource()107     public int getHelpResource() {
108         return R.string.help_uri_process_stats_summary;
109     }
110 
111     @Override
onPreferenceClick(Preference preference)112     public boolean onPreferenceClick(Preference preference) {
113         if (preference == mAppListPreference) {
114             final Bundle args = new Bundle();
115             args.putBoolean(ARG_TRANSFER_STATS, true);
116             args.putInt(ARG_DURATION_INDEX, mDurationIndex);
117             mStatsManager.xferStats();
118             new SubSettingLauncher(getContext())
119                     .setDestination(ProcessStatsUi.class.getName())
120                     .setTitleRes(R.string.memory_usage_apps)
121                     .setArguments(args)
122                     .setSourceMetricsCategory(getMetricsCategory())
123                     .launch();
124             return true;
125         }
126         return false;
127     }
128 }
129