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.Activity;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.support.v7.preference.Preference;
22 import android.support.v7.preference.Preference.OnPreferenceClickListener;
23 import android.text.format.Formatter;
24 import android.text.format.Formatter.BytesResult;
25 import com.android.internal.logging.MetricsProto.MetricsEvent;
26 import com.android.settings.R;
27 import com.android.settings.SummaryPreference;
28 import com.android.settings.Utils;
29 import com.android.settings.applications.ProcStatsData.MemInfo;
30 import com.android.settings.dashboard.SummaryLoader;
31 
32 public 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 SummaryPreference mSummaryPref;
43 
44     private Preference mPerformance;
45     private Preference mTotalMemory;
46     private Preference mAverageUsed;
47     private Preference mFree;
48     private Preference mAppListPreference;
49 
50     @Override
onCreate(Bundle icicle)51     public void onCreate(Bundle icicle) {
52         super.onCreate(icicle);
53 
54         addPreferencesFromResource(R.xml.process_stats_summary);
55         mSummaryPref = (SummaryPreference) findPreference(KEY_STATUS_HEADER);
56         int memColor = getContext().getColor(R.color.running_processes_apps_ram);
57         mSummaryPref.setColors(memColor, memColor,
58                 getContext().getColor(R.color.running_processes_free_ram));
59 
60         mPerformance = findPreference(KEY_PERFORMANCE);
61         mTotalMemory = findPreference(KEY_TOTAL_MEMORY);
62         mAverageUsed = findPreference(KEY_AVERAGY_USED);
63         mFree = findPreference(KEY_FREE);
64         mAppListPreference = findPreference(KEY_APP_LIST);
65         mAppListPreference.setOnPreferenceClickListener(this);
66     }
67 
68     @Override
refreshUi()69     public void refreshUi() {
70         Context context = getContext();
71 
72         MemInfo memInfo = mStatsManager.getMemInfo();
73 
74         double usedRam = memInfo.realUsedRam;
75         double totalRam = memInfo.realTotalRam;
76         double freeRam = memInfo.realFreeRam;
77         BytesResult usedResult = Formatter.formatBytes(context.getResources(), (long) usedRam,
78                 Formatter.FLAG_SHORTER);
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         mSummaryPref.setAmount(usedResult.value);
90         mSummaryPref.setUnits(usedResult.units);
91         float usedRatio = (float)(usedRam / (freeRam + usedRam));
92         mSummaryPref.setRatios(usedRatio, 0, 1 - usedRatio);
93 
94         mPerformance.setSummary(memString);
95         mTotalMemory.setSummary(totalString);
96         mAverageUsed.setSummary(Utils.formatPercentage((long) usedRam, (long) totalRam));
97         mFree.setSummary(freeString);
98         String durationString = getString(sDurationLabels[mDurationIndex]);
99         int numApps = mStatsManager.getEntries().size();
100         mAppListPreference.setSummary(getResources().getQuantityString(
101                 R.plurals.memory_usage_apps_summary, numApps, numApps, durationString));
102     }
103 
104     @Override
getMetricsCategory()105     protected int getMetricsCategory() {
106         return MetricsEvent.PROCESS_STATS_SUMMARY;
107     }
108 
109     @Override
onPreferenceClick(Preference preference)110     public boolean onPreferenceClick(Preference preference) {
111         if (preference == mAppListPreference) {
112             Bundle args = new Bundle();
113             args.putBoolean(ARG_TRANSFER_STATS, true);
114             args.putInt(ARG_DURATION_INDEX, mDurationIndex);
115             mStatsManager.xferStats();
116             startFragment(this, ProcessStatsUi.class.getName(), R.string.app_list_memory_use, 0,
117                     args);
118             return true;
119         }
120         return false;
121     }
122 
123     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
124 
125         private final Context mContext;
126         private final SummaryLoader mSummaryLoader;
127 
SummaryProvider(Context context, SummaryLoader summaryLoader)128         public SummaryProvider(Context context, SummaryLoader summaryLoader) {
129             mContext = context;
130             mSummaryLoader = summaryLoader;
131         }
132 
133         @Override
setListening(boolean listening)134         public void setListening(boolean listening) {
135             if (listening) {
136                 ProcStatsData statsManager = new ProcStatsData(mContext, false);
137                 statsManager.setDuration(sDurations[0]);
138                 MemInfo memInfo = statsManager.getMemInfo();
139                 String usedResult = Formatter.formatShortFileSize(mContext,
140                         (long) memInfo.realUsedRam);
141                 String totalResult = Formatter.formatShortFileSize(mContext,
142                         (long) memInfo.realTotalRam);
143                 mSummaryLoader.setSummary(this, mContext.getString(R.string.memory_summary,
144                         usedResult, totalResult));
145             }
146         }
147     }
148 
149     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
150             = new SummaryLoader.SummaryProviderFactory() {
151         @Override
152         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
153                                                                    SummaryLoader summaryLoader) {
154             return new SummaryProvider(activity, summaryLoader);
155         }
156     };
157 
158 }
159