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 17package com.android.batterywaster; 18 19import android.app.Activity; 20import android.content.BroadcastReceiver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.os.BatteryManager; 25import android.os.Bundle; 26import android.os.PowerManager; 27import android.view.View; 28import android.widget.CheckBox; 29import android.widget.TextView; 30 31import java.text.DateFormat; 32import java.util.Date; 33 34/** 35 * So you thought sync used up your battery life. 36 */ 37public class BatteryWaster extends Activity { 38 TextView mLog; 39 DateFormat mDateFormat; 40 IntentFilter mFilter; 41 PowerManager.WakeLock mWakeLock; 42 SpinThread mThread; 43 44 @Override 45 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 // Set the layout for this activity. You can find it 49 // in res/layout/hello_activity.xml 50 setContentView(R.layout.battery_waster); 51 52 findViewById(R.id.checkbox).setOnClickListener(mClickListener); 53 mLog = (TextView)findViewById(R.id.log); 54 55 mDateFormat = DateFormat.getInstance(); 56 57 mFilter = new IntentFilter(); 58 mFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 59 mFilter.addAction(Intent.ACTION_BATTERY_LOW); 60 mFilter.addAction(Intent.ACTION_BATTERY_OKAY); 61 mFilter.addAction(Intent.ACTION_POWER_CONNECTED); 62 63 PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); 64 mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "BatteryWaster"); 65 mWakeLock.setReferenceCounted(false); 66 } 67 68 @Override 69 public void onPause() { 70 stopRunning(); 71 } 72 73 View.OnClickListener mClickListener = new View.OnClickListener() { 74 public void onClick(View v) { 75 CheckBox checkbox = (CheckBox)v; 76 if (checkbox.isChecked()) { 77 startRunning(); 78 } else { 79 stopRunning(); 80 } 81 } 82 }; 83 84 void startRunning() { 85 log("Start"); 86 registerReceiver(mReceiver, mFilter); 87 mWakeLock.acquire(); 88 if (mThread == null) { 89 mThread = new SpinThread(); 90 mThread.start(); 91 } 92 } 93 94 void stopRunning() { 95 log("Stop"); 96 unregisterReceiver(mReceiver); 97 mWakeLock.release(); 98 if (mThread != null) { 99 mThread.quit(); 100 mThread = null; 101 } 102 } 103 104 void log(String s) { 105 mLog.setText(mLog.getText() + "\n" + mDateFormat.format(new Date()) + ": " + s); 106 } 107 108 BroadcastReceiver mReceiver = new BroadcastReceiver() { 109 public void onReceive(Context context, Intent intent) { 110 String action = intent.getAction(); 111 String title = action; 112 int index = title.lastIndexOf('.'); 113 if (index >= 0) { 114 title = title.substring(index + 1); 115 } 116 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 117 int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 118 log(title + ": level=" + level); 119 } else { 120 log(title); 121 } 122 } 123 }; 124 125 class SpinThread extends Thread { 126 private boolean mStop; 127 128 public void quit() { 129 synchronized (this) { 130 mStop = true; 131 } 132 } 133 134 public void run() { 135 while (true) { 136 synchronized (this) { 137 if (mStop) { 138 return; 139 } 140 } 141 } 142 } 143 } 144} 145 146 147