1 /* 2 * Copyright (C) 2013 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.content.Context; 20 import android.content.pm.PackageManager; 21 import android.text.TextUtils; 22 import android.text.format.Formatter; 23 import android.util.Log; 24 import android.view.View; 25 import android.widget.ProgressBar; 26 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 import com.android.settingslib.widget.AppPreference; 31 32 public class ProcessStatsPreference extends AppPreference { 33 static final String TAG = "ProcessStatsPreference"; 34 35 private ProcStatsPackageEntry mEntry; 36 private int mProgress; 37 private boolean mProgressVisible; 38 ProcessStatsPreference(Context context)39 public ProcessStatsPreference(Context context) { 40 super(context, null); 41 setLayoutResource(R.layout.preference_process_stats); 42 } 43 init(ProcStatsPackageEntry entry, PackageManager pm, double maxMemory, double weightToRam, double totalScale, boolean avg)44 public void init(ProcStatsPackageEntry entry, PackageManager pm, double maxMemory, 45 double weightToRam, double totalScale, boolean avg) { 46 mEntry = entry; 47 String title = TextUtils.isEmpty(entry.mUiLabel) ? entry.mPackage : entry.mUiLabel; 48 setTitle(title); 49 if (TextUtils.isEmpty(title)) { 50 Log.d(TAG, "PackageEntry contained no package name or uiLabel"); 51 } 52 if (entry.mUiTargetApp != null) { 53 setIcon(entry.mUiTargetApp.loadIcon(pm)); 54 } else { 55 setIcon(pm.getDefaultActivityIcon()); 56 } 57 boolean statsForeground = entry.mRunWeight > entry.mBgWeight; 58 double amount = avg ? (statsForeground ? entry.mRunWeight : entry.mBgWeight) * weightToRam 59 : (statsForeground ? entry.mMaxRunMem : entry.mMaxBgMem) * totalScale * 1024; 60 setSummary(Formatter.formatShortFileSize(getContext(), (long) amount)); 61 setProgress((int) (100 * amount / maxMemory)); 62 } 63 getEntry()64 public ProcStatsPackageEntry getEntry() { 65 return mEntry; 66 } 67 68 /** 69 * Sets the current progress. 70 * @param amount the current progress 71 * 72 * @see ProgressBar#setProgress(int) 73 */ setProgress(int amount)74 public void setProgress(int amount) { 75 mProgress = amount; 76 mProgressVisible = true; 77 notifyChanged(); 78 } 79 80 @Override onBindViewHolder(PreferenceViewHolder view)81 public void onBindViewHolder(PreferenceViewHolder view) { 82 super.onBindViewHolder(view); 83 84 final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress); 85 if (mProgressVisible) { 86 progress.setProgress(mProgress); 87 progress.setVisibility(View.VISIBLE); 88 } else { 89 progress.setVisibility(View.GONE); 90 } 91 } 92 } 93