Home | History | Annotate | Download | only in tips
      1 /*
      2  * Copyright (C) 2018 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.batterytip.tips;
     18 
     19 import android.content.Context;
     20 import android.os.Parcel;
     21 
     22 import com.android.internal.logging.nano.MetricsProto;
     23 import com.android.settings.R;
     24 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
     25 
     26 /**
     27  * Tip to show early warning if battery couldn't make to usual charging time
     28  */
     29 public class EarlyWarningTip extends BatteryTip {
     30     private boolean mPowerSaveModeOn;
     31 
     32     public EarlyWarningTip(@StateType int state, boolean powerSaveModeOn) {
     33         super(TipType.BATTERY_SAVER, state, false /* showDialog */);
     34         mPowerSaveModeOn = powerSaveModeOn;
     35     }
     36 
     37     public EarlyWarningTip(Parcel in) {
     38         super(in);
     39         mPowerSaveModeOn = in.readBoolean();
     40     }
     41 
     42     @Override
     43     public CharSequence getTitle(Context context) {
     44         return context.getString(
     45                 mState == StateType.HANDLED
     46                         ? R.string.battery_tip_early_heads_up_done_title
     47                         : R.string.battery_tip_early_heads_up_title);
     48     }
     49 
     50     @Override
     51     public CharSequence getSummary(Context context) {
     52         return context.getString(
     53                 mState == StateType.HANDLED
     54                         ? R.string.battery_tip_early_heads_up_done_summary
     55                         : R.string.battery_tip_early_heads_up_summary);
     56     }
     57 
     58     @Override
     59     public int getIconId() {
     60         return mState == StateType.HANDLED
     61                 ? R.drawable.ic_battery_status_maybe_24dp
     62                 : R.drawable.ic_battery_status_bad_24dp;
     63     }
     64 
     65     @Override
     66     public void updateState(BatteryTip tip) {
     67         final EarlyWarningTip earlyWarningTip = (EarlyWarningTip) tip;
     68         if (earlyWarningTip.mState == StateType.NEW) {
     69             // Display it if there is early warning
     70             mState = StateType.NEW;
     71         } else if (mState == StateType.NEW && earlyWarningTip.mState == StateType.INVISIBLE) {
     72             // If powerSaveMode is really on, show it as handled, otherwise just dismiss it.
     73             mState = earlyWarningTip.mPowerSaveModeOn ? StateType.HANDLED : StateType.INVISIBLE;
     74         } else {
     75             mState = earlyWarningTip.getState();
     76         }
     77         mPowerSaveModeOn = earlyWarningTip.mPowerSaveModeOn;
     78     }
     79 
     80     @Override
     81     public void log(Context context, MetricsFeatureProvider metricsFeatureProvider) {
     82         metricsFeatureProvider.action(context, MetricsProto.MetricsEvent.ACTION_EARLY_WARNING_TIP,
     83                 mState);
     84     }
     85 
     86     @Override
     87     public void writeToParcel(Parcel dest, int flags) {
     88         super.writeToParcel(dest, flags);
     89         dest.writeBoolean(mPowerSaveModeOn);
     90     }
     91 
     92     public boolean isPowerSaveModeOn() {
     93         return mPowerSaveModeOn;
     94     }
     95 
     96     public static final Creator CREATOR = new Creator() {
     97         public BatteryTip createFromParcel(Parcel in) {
     98             return new EarlyWarningTip(in);
     99         }
    100 
    101         public BatteryTip[] newArray(int size) {
    102             return new EarlyWarningTip[size];
    103         }
    104     };
    105 }
    106