1 /*
2  * Copyright (C) 2014 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 
17 package com.android.settings.applications;
18 
19 import android.app.Fragment;
20 import android.os.Bundle;
21 import android.text.format.Formatter;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ProgressBar;
26 import android.widget.TextView;
27 import com.android.internal.app.ProcessStats;
28 import com.android.settings.R;
29 
30 import static com.android.settings.Utils.prepareCustomPreferencesList;
31 
32 public class ProcessStatsMemDetail extends Fragment {
33     public static final String EXTRA_MEM_TIMES = "mem_times";
34     public static final String EXTRA_MEM_STATE_WEIGHTS = "mem_state_weights";
35     public static final String EXTRA_MEM_CACHED_WEIGHT = "mem_cached_weight";
36     public static final String EXTRA_MEM_FREE_WEIGHT = "mem_free_weight";
37     public static final String EXTRA_MEM_ZRAM_WEIGHT = "mem_zram_weight";
38     public static final String EXTRA_MEM_KERNEL_WEIGHT = "mem_kernel_weight";
39     public static final String EXTRA_MEM_NATIVE_WEIGHT = "mem_native_weight";
40     public static final String EXTRA_MEM_TOTAL_WEIGHT = "mem_total_weight";
41     public static final String EXTRA_USE_USS = "use_uss";
42     public static final String EXTRA_TOTAL_TIME = "total_time";
43 
44     long[] mMemTimes;
45     double[] mMemStateWeights;
46     double mMemCachedWeight;
47     double mMemFreeWeight;
48     double mMemZRamWeight;
49     double mMemKernelWeight;
50     double mMemNativeWeight;
51     double mMemTotalWeight;
52     boolean mUseUss;
53     long mTotalTime;
54 
55     private View mRootView;
56     private ViewGroup mMemStateParent;
57     private ViewGroup mMemUseParent;
58 
59     @Override
onCreate(Bundle icicle)60     public void onCreate(Bundle icicle) {
61         super.onCreate(icicle);
62         final Bundle args = getArguments();
63         mMemTimes = args.getLongArray(EXTRA_MEM_TIMES);
64         mMemStateWeights = args.getDoubleArray(EXTRA_MEM_STATE_WEIGHTS);
65         mMemCachedWeight = args.getDouble(EXTRA_MEM_CACHED_WEIGHT);
66         mMemFreeWeight = args.getDouble(EXTRA_MEM_FREE_WEIGHT);
67         mMemZRamWeight = args.getDouble(EXTRA_MEM_ZRAM_WEIGHT);
68         mMemKernelWeight = args.getDouble(EXTRA_MEM_KERNEL_WEIGHT);
69         mMemNativeWeight = args.getDouble(EXTRA_MEM_NATIVE_WEIGHT);
70         mMemTotalWeight = args.getDouble(EXTRA_MEM_TOTAL_WEIGHT);
71         mUseUss = args.getBoolean(EXTRA_USE_USS);
72         mTotalTime = args.getLong(EXTRA_TOTAL_TIME);
73     }
74 
75     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)76     public View onCreateView(
77             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
78         final View view = inflater.inflate(R.layout.process_stats_mem_details, container, false);
79         prepareCustomPreferencesList(container, view, view, false);
80 
81         mRootView = view;
82         createDetails();
83         return view;
84     }
85 
86     @Override
onPause()87     public void onPause() {
88         super.onPause();
89     }
90 
createDetails()91     private void createDetails() {
92         mMemStateParent = (ViewGroup)mRootView.findViewById(R.id.mem_state);
93         mMemUseParent = (ViewGroup)mRootView.findViewById(R.id.mem_use);
94 
95         fillMemStateSection();
96         fillMemUseSection();
97     }
98 
addDetailsItem(ViewGroup parent, CharSequence title, float level, CharSequence value)99     private void addDetailsItem(ViewGroup parent, CharSequence title,
100             float level, CharSequence value) {
101         LayoutInflater inflater = getActivity().getLayoutInflater();
102         ViewGroup item = (ViewGroup) inflater.inflate(R.layout.app_percentage_item,
103                 null);
104         parent.addView(item);
105         item.findViewById(android.R.id.icon).setVisibility(View.GONE);
106         TextView titleView = (TextView) item.findViewById(android.R.id.title);
107         TextView valueView = (TextView) item.findViewById(android.R.id.text1);
108         titleView.setText(title);
109         valueView.setText(value);
110         ProgressBar progress = (ProgressBar) item.findViewById(android.R.id.progress);
111         progress.setProgress(Math.round(level*100));
112     }
113 
fillMemStateSection()114     private void fillMemStateSection() {
115         CharSequence[] labels = getResources().getTextArray(R.array.proc_stats_memory_states);
116         for (int i=0; i<ProcessStats.ADJ_MEM_FACTOR_COUNT; i++) {
117             if (mMemTimes[i] > 0) {
118                 float level = ((float)mMemTimes[i])/mTotalTime;
119                 addDetailsItem(mMemStateParent, labels[i], level,
120                         Formatter.formatShortElapsedTime(getActivity(), mMemTimes[i]));
121             }
122         }
123     }
124 
addMemUseDetailsItem(ViewGroup parent, CharSequence title, double weight)125     private void addMemUseDetailsItem(ViewGroup parent, CharSequence title, double weight) {
126         if (weight > 0) {
127             float level = (float)(weight/mMemTotalWeight);
128             String value = Formatter.formatShortFileSize(getActivity(),
129                     (long)((weight * 1024) / mTotalTime));
130             addDetailsItem(parent, title, level, value);
131         }
132     }
133 
fillMemUseSection()134     private void fillMemUseSection() {
135         CharSequence[] labels = getResources().getTextArray(R.array.proc_stats_process_states);
136         addMemUseDetailsItem(mMemUseParent,
137                 getResources().getText(R.string.mem_use_kernel_type), mMemKernelWeight);
138         addMemUseDetailsItem(mMemUseParent,
139                 getResources().getText(R.string.mem_use_zram_type), mMemZRamWeight);
140         addMemUseDetailsItem(mMemUseParent,
141                 getResources().getText(R.string.mem_use_native_type), mMemNativeWeight);
142         for (int i=0; i<ProcessStats.STATE_COUNT; i++) {
143             addMemUseDetailsItem(mMemUseParent, labels[i], mMemStateWeights[i]);
144         }
145         addMemUseDetailsItem(mMemUseParent,
146                 getResources().getText(R.string.mem_use_kernel_cache_type), mMemCachedWeight);
147         addMemUseDetailsItem(mMemUseParent,
148                 getResources().getText(R.string.mem_use_free_type), mMemFreeWeight);
149         addMemUseDetailsItem(mMemUseParent,
150                 getResources().getText(R.string.mem_use_total), mMemTotalWeight);
151     }
152 }
153