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.nano.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         mPerformance = findPreference(KEY_PERFORMANCE);
57         mTotalMemory = findPreference(KEY_TOTAL_MEMORY);
58         mAverageUsed = findPreference(KEY_AVERAGY_USED);
59         mFree = findPreference(KEY_FREE);
60         mAppListPreference = findPreference(KEY_APP_LIST);
61         mAppListPreference.setOnPreferenceClickListener(this);
62     }
63 
64     @Override
refreshUi()65     public void refreshUi() {
66         Context context = getContext();
67 
68         MemInfo memInfo = mStatsManager.getMemInfo();
69 
70         double usedRam = memInfo.realUsedRam;
71         double totalRam = memInfo.realTotalRam;
72         double freeRam = memInfo.realFreeRam;
73         BytesResult usedResult = Formatter.formatBytes(context.getResources(), (long) usedRam,
74                 Formatter.FLAG_SHORTER);
75         String totalString = Formatter.formatShortFileSize(context, (long) totalRam);
76         String freeString = Formatter.formatShortFileSize(context, (long) freeRam);
77         CharSequence memString;
78         CharSequence[] memStatesStr = getResources().getTextArray(R.array.ram_states);
79         int memState = mStatsManager.getMemState();
80         if (memState >= 0 && memState < memStatesStr.length - 1) {
81             memString = memStatesStr[memState];
82         } else {
83             memString = memStatesStr[memStatesStr.length - 1];
84         }
85         mSummaryPref.setAmount(usedResult.value);
86         mSummaryPref.setUnits(usedResult.units);
87         float usedRatio = (float)(usedRam / (freeRam + usedRam));
88         mSummaryPref.setRatios(usedRatio, 0, 1 - usedRatio);
89 
90         mPerformance.setSummary(memString);
91         mTotalMemory.setSummary(totalString);
92         mAverageUsed.setSummary(Utils.formatPercentage((long) usedRam, (long) totalRam));
93         mFree.setSummary(freeString);
94         String durationString = getString(sDurationLabels[mDurationIndex]);
95         int numApps = mStatsManager.getEntries().size();
96         mAppListPreference.setSummary(getResources().getQuantityString(
97                 R.plurals.memory_usage_apps_summary, numApps, numApps, durationString));
98     }
99 
100     @Override
getMetricsCategory()101     public int getMetricsCategory() {
102         return MetricsEvent.PROCESS_STATS_SUMMARY;
103     }
104 
105     @Override
onPreferenceClick(Preference preference)106     public boolean onPreferenceClick(Preference preference) {
107         if (preference == mAppListPreference) {
108             Bundle args = new Bundle();
109             args.putBoolean(ARG_TRANSFER_STATS, true);
110             args.putInt(ARG_DURATION_INDEX, mDurationIndex);
111             mStatsManager.xferStats();
112             startFragment(this, ProcessStatsUi.class.getName(), R.string.app_list_memory_use, 0,
113                     args);
114             return true;
115         }
116         return false;
117     }
118 
119     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
120 
121         private final Context mContext;
122         private final SummaryLoader mSummaryLoader;
123 
SummaryProvider(Context context, SummaryLoader summaryLoader)124         public SummaryProvider(Context context, SummaryLoader summaryLoader) {
125             mContext = context;
126             mSummaryLoader = summaryLoader;
127         }
128 
129         @Override
setListening(boolean listening)130         public void setListening(boolean listening) {
131             if (listening) {
132                 ProcStatsData statsManager = new ProcStatsData(mContext, false);
133                 statsManager.setDuration(sDurations[0]);
134                 MemInfo memInfo = statsManager.getMemInfo();
135                 String usedResult = Formatter.formatShortFileSize(mContext,
136                         (long) memInfo.realUsedRam);
137                 String totalResult = Formatter.formatShortFileSize(mContext,
138                         (long) memInfo.realTotalRam);
139                 mSummaryLoader.setSummary(this, mContext.getString(R.string.memory_summary,
140                         usedResult, totalResult));
141             }
142         }
143     }
144 
145     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
146             = new SummaryLoader.SummaryProviderFactory() {
147         @Override
148         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
149                                                                    SummaryLoader summaryLoader) {
150             return new SummaryProvider(activity, summaryLoader);
151         }
152     };
153 
154 }
155