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