FileDocCategorySizeDatePackage
BatteryWaster.javaAPI DocAndroid 5.1 API5905Thu Mar 12 22:22:42 GMT 2015com.android.batterywaster

BatteryWaster

public class BatteryWaster extends android.app.Activity
So you thought sync used up your battery life.

Fields Summary
android.widget.TextView
mLog
DateFormat
mDateFormat
android.content.IntentFilter
mFilter
PowerManager.WakeLock
mPartialWakeLock
SpinThread
mThread
boolean
mWasting
boolean
mWaking
View.OnClickListener
mClickListener
View.OnClickListener
mWakeClickListener
android.content.BroadcastReceiver
mReceiver
Constructors Summary
Methods Summary
voidlog(java.lang.String s)

        mLog.setText(mLog.getText() + "\n" + mDateFormat.format(new Date()) + ": " + s);
    
public voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);

        // Set the layout for this activity.  You can find it
        // in res/layout/hello_activity.xml
        setContentView(R.layout.battery_waster);

        findViewById(R.id.checkbox).setOnClickListener(mClickListener);
        findViewById(R.id.checkbox_wake).setOnClickListener(mWakeClickListener);
        mLog = (TextView)findViewById(R.id.log);

        mDateFormat = DateFormat.getInstance();

        mFilter = new IntentFilter();
        mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        mFilter.addAction(Intent.ACTION_BATTERY_LOW);
        mFilter.addAction(Intent.ACTION_BATTERY_OKAY);
        mFilter.addAction(Intent.ACTION_POWER_CONNECTED);

        PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
        mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "BatteryWaster");
        mPartialWakeLock.setReferenceCounted(false);
    
public voidonDestroy()

        super.onDestroy();
        stopRunning();
        if (mPartialWakeLock.isHeld()) {
            mPartialWakeLock.release();
        }
    
public voidonResume()

        super.onResume();
        if (((CheckBox)findViewById(R.id.checkbox)).isChecked()) {
            startRunning();
        }
        if (((CheckBox)findViewById(R.id.checkbox_wake)).isChecked()) {
            mWaking = true;
            updateWakeLock();
        }
    
voidstartRunning()


      
        if (!mWasting) {
            log("Start");
            registerReceiver(mReceiver, mFilter);
            mWasting = true;
            updateWakeLock();
            if (mThread == null) {
                mThread = new SpinThread();
                mThread.start();
            }
        }
    
voidstopRunning()

        if (mWasting) {
            log("Stop");
            unregisterReceiver(mReceiver);
            mWasting = false;
            updateWakeLock();
            if (mThread != null) {
                mThread.quit();
                mThread = null;
            }
        }
    
voidupdateWakeLock()

        if (mWasting) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        if (mWaking) {
            if (!mPartialWakeLock.isHeld()) {
                mPartialWakeLock.acquire();
            }
        } else {
            if (mPartialWakeLock.isHeld()) {
                mPartialWakeLock.release();
            }
        }