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