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
17package com.android.settings.applications;
18
19import android.content.Context;
20import android.content.pm.PackageManager;
21import android.graphics.drawable.ColorDrawable;
22import android.text.TextUtils;
23import android.text.format.Formatter;
24
25import com.android.settings.AppProgressPreference;
26
27public class ProcessStatsPreference extends AppProgressPreference {
28
29    private ProcStatsPackageEntry mEntry;
30
31    public ProcessStatsPreference(Context context) {
32        super(context, null);
33    }
34
35    public void init(ProcStatsPackageEntry entry, PackageManager pm, double maxMemory,
36            double weightToRam, double totalScale, boolean avg) {
37        mEntry = entry;
38        setTitle(TextUtils.isEmpty(entry.mUiLabel) ? entry.mPackage : entry.mUiLabel);
39        if (entry.mUiTargetApp != null) {
40            setIcon(entry.mUiTargetApp.loadIcon(pm));
41        } else {
42            setIcon(new ColorDrawable(0));
43        }
44        boolean statsForeground = entry.mRunWeight > entry.mBgWeight;
45        double amount = avg ? (statsForeground ? entry.mRunWeight : entry.mBgWeight) * weightToRam
46                : (statsForeground ? entry.mMaxRunMem : entry.mMaxBgMem) * totalScale * 1024;
47        setSummary(Formatter.formatShortFileSize(getContext(), (long) amount));
48        setProgress((int) (100 * amount / maxMemory));
49    }
50
51    public ProcStatsPackageEntry getEntry() {
52        return mEntry;
53    }
54}
55