Home | History | Annotate | Download | only in conditional
      1 /*
      2  * Copyright (C) 2015 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 package com.android.settings.dashboard.conditional;
     17 
     18 import android.content.Intent;
     19 import android.graphics.drawable.Drawable;
     20 import android.os.PowerManager;
     21 
     22 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     23 import com.android.settings.R;
     24 import com.android.settings.core.SubSettingLauncher;
     25 import com.android.settings.fuelgauge.BatterySaverDrawable;
     26 import com.android.settings.fuelgauge.BatterySaverReceiver;
     27 import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
     28 import com.android.settingslib.fuelgauge.BatterySaverUtils;
     29 
     30 public class BatterySaverCondition extends Condition implements
     31         BatterySaverReceiver.BatterySaverListener {
     32 
     33     private final BatterySaverReceiver mReceiver;
     34 
     35     public BatterySaverCondition(ConditionManager manager) {
     36         super(manager);
     37 
     38         mReceiver = new BatterySaverReceiver(manager.getContext());
     39         mReceiver.setBatterySaverListener(this);
     40     }
     41 
     42     @Override
     43     public void refreshState() {
     44         PowerManager powerManager = mManager.getContext().getSystemService(PowerManager.class);
     45         setActive(powerManager.isPowerSaveMode());
     46     }
     47 
     48     @Override
     49     public Drawable getIcon() {
     50         return mManager.getContext().getDrawable(R.drawable.ic_battery_saver_accent_24dp);
     51     }
     52 
     53     @Override
     54     public CharSequence getTitle() {
     55         return mManager.getContext().getString(R.string.condition_battery_title);
     56     }
     57 
     58     @Override
     59     public CharSequence getSummary() {
     60         return mManager.getContext().getString(R.string.condition_battery_summary);
     61     }
     62 
     63     @Override
     64     public CharSequence[] getActions() {
     65         return new CharSequence[]{mManager.getContext().getString(R.string.condition_turn_off)};
     66     }
     67 
     68     @Override
     69     public void onPrimaryClick() {
     70         new SubSettingLauncher(mManager.getContext())
     71                 .setDestination(BatterySaverSettings.class.getName())
     72                 .setSourceMetricsCategory(MetricsEvent.DASHBOARD_SUMMARY)
     73                 .setTitle(R.string.battery_saver)
     74                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
     75                 .launch();
     76     }
     77 
     78     @Override
     79     public void onActionClick(int index) {
     80         if (index == 0) {
     81             BatterySaverUtils.setPowerSaveMode(mManager.getContext(), false,
     82                     /*needFirstTimeWarning*/ false);
     83             refreshState();
     84         } else {
     85             throw new IllegalArgumentException("Unexpected index " + index);
     86         }
     87     }
     88 
     89     @Override
     90     public int getMetricsConstant() {
     91         return MetricsEvent.SETTINGS_CONDITION_BATTERY_SAVER;
     92     }
     93 
     94     @Override
     95     public void onResume() {
     96         mReceiver.setListening(true);
     97     }
     98 
     99     @Override
    100     public void onPause() {
    101         mReceiver.setListening(false);
    102     }
    103 
    104     @Override
    105     public void onPowerSaveModeChanged() {
    106         ConditionManager.get(mManager.getContext()).getCondition(BatterySaverCondition.class)
    107                         .refreshState();
    108     }
    109 
    110     @Override
    111     public void onBatteryChanged(boolean pluggedIn) {
    112         // do nothing
    113     }
    114 }
    115