package com.em.batterywidget;
import com.em.batterywidget.preferences.*;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
private TextView mStatusView;
private TextView mPlugView;
private TextView mLevelView;
private TextView mScaleView;
private TextView mVoltageView;
private TextView mTemperatureView;
private TextView mTechnologyView;
private TextView mHealthView;
private Button mSummaryButton;
private Button mSettingsButton;
private Button mHistoryButton;
private Preferences mBatteryInfo;
private Preferences mPrefSettings;
private Intent mIntent;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
getWindow().setBackgroundDrawable(new ColorDrawable(0x7f000000));
setContentView(R.layout.battery_info_view);
mStatusView = (TextView) findViewById(R.id.state);
mPlugView = (TextView) findViewById(R.id.plug);
mLevelView = (TextView) findViewById(R.id.level);
mScaleView = (TextView) findViewById(R.id.scale);
mVoltageView = (TextView) findViewById(R.id.voltage);
mTemperatureView = (TextView) findViewById(R.id.temperature);
mTechnologyView = (TextView) findViewById(R.id.technology);
mHealthView = (TextView) findViewById(R.id.health);
mSummaryButton = (Button) findViewById(R.id.summaryButton);
mSettingsButton = (Button) findViewById(R.id.settingsButton);
mHistoryButton = (Button) findViewById(R.id.historyButton);
mSummaryButton.setOnClickListener(this);
mSettingsButton.setOnClickListener(this);
mHistoryButton.setOnClickListener(this);
}
@Override
super.onResume();
registerReceiver();
}
@Override
super.onPause();
unregisterReceiver(batteryEventListener);
}
switch (view.getId()) {
case R.id.summaryButton:
mIntent = new Intent(Constants.BATTERY_USAGE);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
this.startActivity(mIntent);
break;
case R.id.historyButton:
mIntent = new Intent(getApplicationContext(),HistoryViewManager.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
this.startActivity(mIntent);
break;
case R.id.settingsButton:
mIntent = new Intent(getApplicationContext(),OptionsManager.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
this.startActivity(mIntent);
break;
default:
break;
}
}
try {
mBatteryInfo = new Preferences(Constants.BATTERY_INFO,getApplicationContext());
mStatusView.setText(getBatteryStatus());
mPlugView.setText(getBatteryPlug());
mTemperatureView.setText(getBatteryTemperature());
mHealthView.setText(getBatteryHealth());
mLevelView.setText(mBatteryInfo.getValue(Constants.LEVEL, 0)+this.getString(R.string.batteryLevelSymbol));
mScaleView.setText (mBatteryInfo.getValue(Constants.SCALE, 0)+this.getString(R.string.Empty));
mVoltageView.setText(mBatteryInfo.getValue(Constants.VOLTAGE, 0)+this.getString(R.string.batteryVoltSymbol));
mTechnologyView.setText(mBatteryInfo.getValue(Constants.TECHNOLOGY,"")+this.getString(R.string.Empty));
} catch (Exception e) {
}
}
int status = mBatteryInfo.getValue(Constants.STATUS, 0);
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
return R.string.Unknown;
case BatteryManager.BATTERY_STATUS_CHARGING:
return R.string.batteryStatusCharging;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
return R.string.batteryStatusDischarging;
case BatteryManager.BATTERY_STATUS_FULL:
return R.string.batteryStatusFull;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
return R.string.batteryStatusNotCharging;
default:
return R.string.Unknown;
}
}
int plug = mBatteryInfo.getValue(Constants.PLUG, 0);
switch (plug) {
case BatteryManager.BATTERY_PLUGGED_AC:
return R.string.batteryPlugAC;
case BatteryManager.BATTERY_PLUGGED_USB:
return R.string.batteryPlugUSB;
default:
return R.string.batteryPlugNone;
}
}
int health = mBatteryInfo.getValue(Constants.HEALTH, 0);
switch (health) {
case BatteryManager.BATTERY_HEALTH_DEAD:
return R.string.batteryHealthDead;
case BatteryManager.BATTERY_HEALTH_GOOD:
return R.string.batteryHealthGood;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
return R.string.batteryHealthOverVoltage;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
return R.string.batteryHealthOverheat;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
return R.string.batteryHealthUnspecifiedFailure;
default:
return R.string.Unknown;
}
}
int iTempValue = mBatteryInfo.getValue(Constants.TEMPERATURE, 0);
mPrefSettings = new Preferences(Constants.BATTERY_SETTINGS, this);
String unit = mPrefSettings.getValue(Constants.TEMPERATURE_SETTINGS,
Constants.DEFAULT_TEMEPERATURE);
if (unit.equalsIgnoreCase(this.getString(R.string.batteryFahrenheitSymbol))) {
iTempValue = iTempValue * 9 / 5 + 320;
}
float fTempValue = iTempValue / (float) 10;
return fTempValue + mPrefSettings.getValue(Constants.TEMPERATURE_SETTINGS,
Constants.DEFAULT_TEMEPERATURE);
}
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
this.registerReceiver(batteryEventListener, filter);
updateBatteryInfoView();
}
final private BroadcastReceiver batteryEventListener = new BroadcastReceiver() {
@Override
public void onReceive(Context context,Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
updateBatteryInfoView();
}
}
};
}