1 /* 2 * Copyright (C) 2009 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.fuelgauge; 18 19 import android.content.Context; 20 import android.graphics.drawable.ColorDrawable; 21 import android.graphics.drawable.Drawable; 22 import android.support.v7.preference.PreferenceViewHolder; 23 import android.widget.ImageView; 24 import android.widget.LinearLayout; 25 import android.widget.TextView; 26 import com.android.settings.R; 27 import com.android.settings.TintablePreference; 28 import com.android.settings.Utils; 29 30 /** 31 * Custom preference for displaying power consumption as a bar and an icon on 32 * the left for the subsystem/app type. 33 */ 34 public class PowerGaugePreference extends TintablePreference { 35 private final int mIconSize; 36 37 private BatteryEntry mInfo; 38 private CharSequence mContentDescription; 39 private CharSequence mProgress; 40 41 public PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription, 42 BatteryEntry info) { 43 super(context, null); 44 setIcon(icon != null ? icon : new ColorDrawable(0)); 45 setWidgetLayoutResource(R.layout.preference_widget_summary); 46 mInfo = info; 47 mContentDescription = contentDescription; 48 mIconSize = context.getResources().getDimensionPixelSize(R.dimen.app_icon_size); 49 } 50 51 public void setContentDescription(String name) { 52 mContentDescription = name; 53 notifyChanged(); 54 } 55 56 public void setPercent(double percentOfMax, double percentOfTotal) { 57 mProgress = Utils.formatPercentage((int) (percentOfTotal + 0.5)); 58 notifyChanged(); 59 } 60 61 BatteryEntry getInfo() { 62 return mInfo; 63 } 64 65 @Override 66 public void onBindViewHolder(PreferenceViewHolder view) { 67 super.onBindViewHolder(view); 68 ImageView icon = (ImageView) view.findViewById(android.R.id.icon); 69 icon.setLayoutParams(new LinearLayout.LayoutParams(mIconSize, mIconSize)); 70 71 ((TextView) view.findViewById(R.id.widget_summary)).setText(mProgress); 72 if (mContentDescription != null) { 73 final TextView titleView = (TextView) view.findViewById(android.R.id.title); 74 titleView.setContentDescription(mContentDescription); 75 } 76 } 77 } 78