ProcessStatsPreference.java revision 1de522323fc2b20b86f59819d819708cafdbd2ab
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.preference.Preference;
23import android.text.TextUtils;
24import android.util.AttributeSet;
25import android.util.Log;
26import android.view.View;
27
28import com.android.settings.R;
29
30public class ProcessStatsPreference extends Preference {
31
32    private ProcStatsPackageEntry mEntry;
33    private final int mAvgColor;
34    private final int mMaxColor;
35    private final int mRemainingColor;
36    private float mAvgRatio;
37    private float mMaxRatio;
38    private float mRemainingRatio;
39
40    public ProcessStatsPreference(Context context) {
41        this(context, null);
42    }
43
44    public ProcessStatsPreference(Context context, AttributeSet attrs) {
45        this(context, attrs, 0);
46    }
47
48    public ProcessStatsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
49        this(context, attrs, defStyleAttr, 0);
50    }
51
52    public ProcessStatsPreference(Context context, AttributeSet attrs, int defStyleAttr,
53            int defStyleRes) {
54        super(context, attrs, defStyleAttr, defStyleRes);
55        setLayoutResource(R.layout.app_item_linear_color);
56        mAvgColor = context.getColor(R.color.memory_avg_use);
57        mMaxColor = context.getColor(R.color.memory_max_use);
58        mRemainingColor = context.getColor(R.color.memory_remaining);
59    }
60
61    public void init(ProcStatsPackageEntry entry, PackageManager pm, float maxMemory) {
62        mEntry = entry;
63        setTitle(TextUtils.isEmpty(entry.mUiLabel) ? entry.mPackage : entry.mUiLabel);
64        if (entry.mUiTargetApp != null) {
65            setIcon(entry.mUiTargetApp.loadIcon(pm));
66        } else {
67            setIcon(new ColorDrawable(0));
68        }
69        boolean statsForeground = entry.mRunWeight > entry.mBgWeight;
70        setSummary(entry.mRunDuration > entry.mBgDuration ? entry.getRunningFrequency(getContext())
71                : entry.getBackgroundFrequency(getContext()));
72        mAvgRatio = (statsForeground ? entry.mAvgRunMem : entry.mAvgBgMem) / maxMemory;
73        mMaxRatio = (statsForeground ? entry.mMaxRunMem : entry.mMaxBgMem) / maxMemory - mAvgRatio;
74        mRemainingRatio = 1 - mAvgRatio - mMaxRatio;
75    }
76
77    public ProcStatsPackageEntry getEntry() {
78        return mEntry;
79    }
80
81    @Override
82    protected void onBindView(View view) {
83        super.onBindView(view);
84
85        LinearColorBar linearColorBar = (LinearColorBar) view.findViewById(R.id.linear_color_bar);
86        linearColorBar.setColors(mAvgColor, mMaxColor, mRemainingColor);
87        linearColorBar.setRatios(mAvgRatio, mMaxRatio, mRemainingRatio);
88    }
89}
90