Home | History | Annotate | Download | only in batterywaster
      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.batterywaster;
     18 
     19 import android.app.Activity;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.os.BatteryManager;
     25 import android.os.Bundle;
     26 import android.os.PowerManager;
     27 import android.view.View;
     28 import android.view.WindowManager;
     29 import android.widget.CheckBox;
     30 import android.widget.TextView;
     31 
     32 import java.text.DateFormat;
     33 import java.util.Date;
     34 
     35 /**
     36  * So you thought sync used up your battery life.
     37  */
     38 public class BatteryWaster extends Activity {
     39     TextView mLog;
     40     DateFormat mDateFormat;
     41     IntentFilter mFilter;
     42     PowerManager.WakeLock mPartialWakeLock;
     43     SpinThread mThread;
     44 
     45     boolean mWasting, mWaking;
     46 
     47     @Override
     48     public void onCreate(Bundle savedInstanceState) {
     49         super.onCreate(savedInstanceState);
     50 
     51         // Set the layout for this activity.  You can find it
     52         // in res/layout/hello_activity.xml
     53         setContentView(R.layout.battery_waster);
     54 
     55         findViewById(R.id.checkbox).setOnClickListener(mClickListener);
     56         findViewById(R.id.checkbox_wake).setOnClickListener(mWakeClickListener);
     57         mLog = (TextView)findViewById(R.id.log);
     58 
     59         mDateFormat = DateFormat.getInstance();
     60 
     61         mFilter = new IntentFilter();
     62         mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
     63         mFilter.addAction(Intent.ACTION_BATTERY_LOW);
     64         mFilter.addAction(Intent.ACTION_BATTERY_OKAY);
     65         mFilter.addAction(Intent.ACTION_POWER_CONNECTED);
     66 
     67         PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
     68         mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "BatteryWaster");
     69         mPartialWakeLock.setReferenceCounted(false);
     70     }
     71 
     72     @Override
     73     public void onResume() {
     74         super.onResume();
     75         if (((CheckBox)findViewById(R.id.checkbox)).isChecked()) {
     76             startRunning();
     77         }
     78         if (((CheckBox)findViewById(R.id.checkbox_wake)).isChecked()) {
     79             mWaking = true;
     80             updateWakeLock();
     81         }
     82     }
     83 
     84     @Override
     85     public void onDestroy() {
     86         super.onDestroy();
     87         stopRunning();
     88         if (mPartialWakeLock.isHeld()) {
     89             mPartialWakeLock.release();
     90         }
     91     }
     92 
     93     View.OnClickListener mClickListener = new View.OnClickListener() {
     94         public void onClick(View v) {
     95             CheckBox checkbox = (CheckBox)v;
     96             if (checkbox.isChecked()) {
     97                 startRunning();
     98             } else {
     99                 stopRunning();
    100             }
    101         }
    102     };
    103 
    104     View.OnClickListener mWakeClickListener = new View.OnClickListener() {
    105         public void onClick(View v) {
    106             CheckBox checkbox = (CheckBox)v;
    107             if (checkbox.isChecked()) {
    108                 mWaking = true;
    109                 updateWakeLock();
    110             } else {
    111                 mWaking = false;
    112                 updateWakeLock();
    113             }
    114         }
    115     };
    116 
    117     void startRunning() {
    118         if (!mWasting) {
    119             log("Start");
    120             registerReceiver(mReceiver, mFilter);
    121             mWasting = true;
    122             updateWakeLock();
    123             if (mThread == null) {
    124                 mThread = new SpinThread();
    125                 mThread.start();
    126             }
    127         }
    128     }
    129 
    130     void stopRunning() {
    131         if (mWasting) {
    132             log("Stop");
    133             unregisterReceiver(mReceiver);
    134             mWasting = false;
    135             updateWakeLock();
    136             if (mThread != null) {
    137                 mThread.quit();
    138                 mThread = null;
    139             }
    140         }
    141     }
    142 
    143     void updateWakeLock() {
    144         if (mWasting) {
    145             getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    146         } else {
    147             getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    148         }
    149         if (mWaking) {
    150             if (!mPartialWakeLock.isHeld()) {
    151                 mPartialWakeLock.acquire();
    152             }
    153         } else {
    154             if (mPartialWakeLock.isHeld()) {
    155                 mPartialWakeLock.release();
    156             }
    157         }
    158     }
    159 
    160     void log(String s) {
    161         mLog.setText(mLog.getText() + "\n" + mDateFormat.format(new Date()) + ": " + s);
    162     }
    163 
    164     BroadcastReceiver mReceiver = new BroadcastReceiver() {
    165         public void onReceive(Context context, Intent intent) {
    166             String action = intent.getAction();
    167             String title = action;
    168             int index = title.lastIndexOf('.');
    169             if (index >= 0) {
    170                 title = title.substring(index + 1);
    171             }
    172             if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
    173                 int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    174                 log(title + ": level=" + level);
    175             } else {
    176                 log(title);
    177             }
    178         }
    179     };
    180 
    181     class SpinThread extends Thread {
    182         private boolean mStop;
    183 
    184         public void quit() {
    185             synchronized (this) {
    186                 mStop = true;
    187             }
    188         }
    189 
    190         public void run() {
    191             while (true) {
    192                 synchronized (this) {
    193                     if (mStop) {
    194                         return;
    195                     }
    196                 }
    197             }
    198         }
    199     }
    200 }
    201 
    202 
    203