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 
25 import com.android.settingslib.widget.apppreference.AppPreference;
26 
27 public class ProcessStatsPreference extends AppPreference {
28     static final String TAG = "ProcessStatsPreference";
29 
30     private ProcStatsPackageEntry mEntry;
31 
ProcessStatsPreference(Context context)32     public ProcessStatsPreference(Context context) {
33         super(context, null);
34     }
35 
init(ProcStatsPackageEntry entry, PackageManager pm, double maxMemory, double weightToRam, double totalScale, boolean avg)36     public void init(ProcStatsPackageEntry entry, PackageManager pm, double maxMemory,
37             double weightToRam, double totalScale, boolean avg) {
38         mEntry = entry;
39         String title = TextUtils.isEmpty(entry.mUiLabel) ? entry.mPackage : entry.mUiLabel;
40         setTitle(title);
41         if (TextUtils.isEmpty(title)) {
42             Log.d(TAG, "PackageEntry contained no package name or uiLabel");
43         }
44         if (entry.mUiTargetApp != null) {
45             setIcon(entry.mUiTargetApp.loadIcon(pm));
46         } else {
47             setIcon(pm.getDefaultActivityIcon());
48         }
49         boolean statsForeground = entry.mRunWeight > entry.mBgWeight;
50         double amount = avg ? (statsForeground ? entry.mRunWeight : entry.mBgWeight) * weightToRam
51                 : (statsForeground ? entry.mMaxRunMem : entry.mMaxBgMem) * totalScale * 1024;
52         setSummary(Formatter.formatShortFileSize(getContext(), (long) amount));
53         setProgress((int) (100 * amount / maxMemory));
54     }
55 
getEntry()56     public ProcStatsPackageEntry getEntry() {
57         return mEntry;
58     }
59 }
60