Home | History | Annotate | Download | only in fuelgauge
      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.util.AttributeSet;
     24 import android.view.View;
     25 import android.widget.ImageView;
     26 import android.widget.LinearLayout;
     27 import android.widget.TextView;
     28 
     29 import com.android.settings.R;
     30 import com.android.settings.TintablePreference;
     31 import com.android.settings.Utils;
     32 
     33 /**
     34  * Custom preference for displaying battery usage info as a bar and an icon on
     35  * the left for the subsystem/app type.
     36  *
     37  * The battery usage info could be usage percentage or usage time. The preference
     38  * won't show any icon if it is null.
     39  */
     40 public class PowerGaugePreference extends TintablePreference {
     41     private final int mIconSize;
     42 
     43     private BatteryEntry mInfo;
     44     private CharSequence mContentDescription;
     45     private CharSequence mProgress;
     46     private boolean mShowAnomalyIcon;
     47 
     48     public PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription,
     49             BatteryEntry info) {
     50         this(context, null, icon, contentDescription, info);
     51     }
     52 
     53     public PowerGaugePreference(Context context) {
     54         this(context, null, null, null, null);
     55     }
     56 
     57     public PowerGaugePreference(Context context, AttributeSet attrs) {
     58         this(context, attrs, null, null, null);
     59     }
     60 
     61     private PowerGaugePreference(Context context, AttributeSet attrs, Drawable icon,
     62             CharSequence contentDescription, BatteryEntry info) {
     63         super(context, attrs);
     64         setIcon(icon != null ? icon : new ColorDrawable(0));
     65         setWidgetLayoutResource(R.layout.preference_widget_summary);
     66         mInfo = info;
     67         mContentDescription = contentDescription;
     68         mIconSize = context.getResources().getDimensionPixelSize(R.dimen.app_icon_size);
     69         mShowAnomalyIcon = false;
     70     }
     71 
     72     public void setContentDescription(String name) {
     73         mContentDescription = name;
     74         notifyChanged();
     75     }
     76 
     77     public void setPercent(double percentOfTotal) {
     78         mProgress = Utils.formatPercentage(percentOfTotal, true);
     79         notifyChanged();
     80     }
     81 
     82     public String getPercent() {
     83         return mProgress.toString();
     84     }
     85 
     86     public void setSubtitle(CharSequence subtitle) {
     87         mProgress = subtitle;
     88         notifyChanged();
     89     }
     90 
     91     public CharSequence getSubtitle() {
     92         return mProgress;
     93     }
     94 
     95     public void shouldShowAnomalyIcon(boolean showAnomalyIcon) {
     96         mShowAnomalyIcon = showAnomalyIcon;
     97         notifyChanged();
     98     }
     99 
    100     public boolean showAnomalyIcon() {
    101         return mShowAnomalyIcon;
    102     }
    103 
    104     BatteryEntry getInfo() {
    105         return mInfo;
    106     }
    107 
    108     @Override
    109     public void onBindViewHolder(PreferenceViewHolder view) {
    110         super.onBindViewHolder(view);
    111         ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
    112         icon.setLayoutParams(new LinearLayout.LayoutParams(mIconSize, mIconSize));
    113 
    114         final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary);
    115         subtitle.setText(mProgress);
    116         if (mShowAnomalyIcon) {
    117             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_warning_24dp, 0,
    118                     0, 0);
    119         } else {
    120             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
    121         }
    122         if (mContentDescription != null) {
    123             final TextView titleView = (TextView) view.findViewById(android.R.id.title);
    124             titleView.setContentDescription(mContentDescription);
    125         }
    126     }
    127 }
    128