@Override
protected void onPause() {
super.onPause();
mWakeLock.release();
}
@Override
protected void onResume() {
super.onResume();
mWakeLock.acquire();
}
public void onDestroy() {
unregisterReceiver(mBatteryInfoReceiver);
if (mStart && mIsCosting && !mPausedCosting) {
mIsCosting = false;
stopCost();
}
super.onDestroy();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_button:
if (mStart) {
mStart = false;
startBtn.setText(R.string.start);
setEnableWidget(true);
} else {
mStart = true;
startBtn.setText(R.string.stop);
setEnableWidget(false);
}
checkCost(true);
break;
default:
Log.w(TAG, "Unknown click!");
break;
}
}
private void checkCost(boolean fromUI) {
if (mStart) {
if (mIsCosting) { // battery level: charge
if (mPausedCosting) { // temperature
if (mTemperature <= mResumeTemperature) {
mPausedCosting = false;
startCost();
}
} else {
if (mCurrentBatteryLevel <= mCostTargetBatteryLevel) {
mIsCosting = false;
stopCost();
charge();
Log.d(TAG, "mIsCosting:" + mIsCosting + ", mPausedCosting:"
+ mPausedCosting);
return;
}
if (mTemperature >= mProtectedTemperature) {
mPausedCosting = true;
stopCost();
}
}
} else {
if (fromUI) {
if (mCurrentBatteryLevel >= mCostTargetBatteryLevel) {
disCharge();
mIsCosting = true;
if (mTemperature < mProtectedTemperature) {
mPausedCosting = false;
startCost();
} else {
mPausedCosting = true;
// wait lower temperature
}
} else {
charge();
}
} else {
if (mCurrentBatteryLevel >= mChargeTargetBatteryLevel) {
disCharge();
mIsCosting = true;
if (mTemperature < mProtectedTemperature) {
mPausedCosting = false;
startCost();
} else {
mPausedCosting = true;
// wait lower temperature
}
}
}
}
} else {
if (mIsCosting && !mPausedCosting) {
mIsCosting = false;
stopCost();
}
}
}
private void setFlashlightEnabled(boolean isEnabled) {
try {
mCameraManager.setTorchMode("0", isEnabled);
} catch (Exception e) {
Log.e(TAG, "Fail to work on flashlight.");
e.printStackTrace();
}
}
private synchronized void startCost() {
if (brightnessCheckbox.isChecked()) {
startScreenBrightness();
}
if (cameraFlashLightCheckbox.isChecked()) {
setFlashlightEnabled(true);
}
if (vibratorCheckbox.isChecked()) {
// TODO: set the time
mVibrator.vibrate(new long[] { 1000L, 2000L, 1000L, 2000L }, 0);
}
if (cpuLoadCheckbox.isChecked()) {
mCostThread = new CostThread(mThreadNums);
mCostThread.start();
}
}
private synchronized void stopCost() {
Log.d(TAG, "stopCost()");
if (brightnessCheckbox.isChecked()) {
stopScreenBrightness();
}
if (cameraFlashLightCheckbox.isChecked()) {
setFlashlightEnabled(false);
}
if (vibratorCheckbox.isChecked()) {
mVibrator.cancel();
}
if (cpuLoadCheckbox.isChecked() && mCostThread != null) {
mCostThread.stopRunning();
}
}
private void startScreenBrightness() {
try {
int brightnessMode = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE,
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
mDefaultBrightness = localLayoutParams.screenBrightness;
localLayoutParams.screenBrightness = 1.0f;
getWindow().setAttributes(localLayoutParams);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
private void stopScreenBrightness() {
try {
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
localLayoutParams.screenBrightness = mDefaultBrightness;
getWindow().setAttributes(localLayoutParams);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
private void saveKey(String key, int value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}