12a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani/*
22a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * Copyright (C) 2009 The Android Open Source Project
32a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani *
42a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * Licensed under the Apache License, Version 2.0 (the "License");
52a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * you may not use this file except in compliance with the License.
62a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * You may obtain a copy of the License at
72a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani *
82a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani *      http://www.apache.org/licenses/LICENSE-2.0
92a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani *
102a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * Unless required by applicable law or agreed to in writing, software
112a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * distributed under the License is distributed on an "AS IS" BASIS,
122a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * See the License for the specific language governing permissions and
142a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani * limitations under the License.
152a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani */
162a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
172a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasanipackage com.android.settings.fuelgauge;
182a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
192a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasaniimport android.content.Context;
2052e56a24904389ca8f7e67e8a883f353b887a4afDianne Hackbornimport android.graphics.drawable.ColorDrawable;
212a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasaniimport android.graphics.drawable.Drawable;
222a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasaniimport android.preference.Preference;
232a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasaniimport android.view.View;
2428130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkeyimport android.widget.ProgressBar;
2578fd96a1c5dd7b03fa09d8b3116243cc9661c5e6Amith Yamasaniimport android.widget.TextView;
262a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
272a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasaniimport com.android.settings.R;
282a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
292a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani/**
3028130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey * Custom preference for displaying power consumption as a bar and an icon on
3128130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey * the left for the subsystem/app type.
322a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani */
332a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasanipublic class PowerGaugePreference extends Preference {
347f6aa6283ae759c5b013c142be036617cf79f725Amith Yamasani    private BatterySipper mInfo;
3528130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey    private int mProgress;
3628130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey    private CharSequence mProgressText;
372a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
387f6aa6283ae759c5b013c142be036617cf79f725Amith Yamasani    public PowerGaugePreference(Context context, Drawable icon, BatterySipper info) {
392a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani        super(context);
4028130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        setLayoutResource(R.layout.app_percentage_item);
4152e56a24904389ca8f7e67e8a883f353b887a4afDianne Hackborn        setIcon(icon != null ? icon : new ColorDrawable(0));
427f6aa6283ae759c5b013c142be036617cf79f725Amith Yamasani        mInfo = info;
432a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani    }
442a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
4528130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey    public void setPercent(double percentOfMax, double percentOfTotal) {
4628130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        mProgress = (int) Math.ceil(percentOfMax);
4728130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        mProgressText = getContext().getResources().getString(
4828130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey                R.string.percentage, (int) Math.ceil(percentOfTotal));
4928130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        notifyChanged();
5078fd96a1c5dd7b03fa09d8b3116243cc9661c5e6Amith Yamasani    }
5178fd96a1c5dd7b03fa09d8b3116243cc9661c5e6Amith Yamasani
527f6aa6283ae759c5b013c142be036617cf79f725Amith Yamasani    BatterySipper getInfo() {
537f6aa6283ae759c5b013c142be036617cf79f725Amith Yamasani        return mInfo;
547f6aa6283ae759c5b013c142be036617cf79f725Amith Yamasani    }
557f6aa6283ae759c5b013c142be036617cf79f725Amith Yamasani
562a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani    @Override
572a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani    protected void onBindView(View view) {
582a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani        super.onBindView(view);
592a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
6028130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
6128130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        progress.setProgress(mProgress);
622a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani
6328130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        final TextView text1 = (TextView) view.findViewById(android.R.id.text1);
6428130d96385d7d7b17992b45fb5d124836d85880Jeff Sharkey        text1.setText(mProgressText);
652a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani    }
662a2daf960c43a2c97b1c6da430138e08e9caa54fAmith Yamasani}
67