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