Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions f...
If you think the Android project Dual-Battery-Widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2011 Artiom Chilaru (http://flexlabs.org)
*/*fromwww.java2s.com*/
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package org.flexlabs.widgets.dualbattery;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import java.util.Date;
publicclass BatteryLevel {
privatestatic BatteryLevel _instance;
publicstatic Date dockLastConnected = null, lastCharged = null;
publicstaticInteger lastDockLevel = null;
privateboolean _dockFriendly;
privateint _status;
privateint _level;
privateint _dock_status;
privateint _dock_level;
private BatteryLevel() { }
publicstaticvoid update(BatteryLevel level) { _instance = level; }
publicstatic BatteryLevel getCurrent() {
if (_instance == null) {
Intent intent = BatteryApplication.getInstance().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
_instance = parse(intent.getExtras());
}
return _instance;
}
publicstatic BatteryLevel parse(Bundle extras) {
if (extras == null)
return null;
BatteryLevel level = new BatteryLevel();
level._status = extras.getInt("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
level._level = extras.getInt("level");
level._dock_status = extras.getInt("dock_status", Constants.DOCK_STATE_UNKNOWN);
level._dock_level = extras.getInt("dock_level", -1);
level._dockFriendly = extras.containsKey("dock_level");
return level;
}
publicboolean isDifferent(BatteryLevel level) {
return
level == null ||
level._level != _level ||
level._status != _status ||
level._dock_status != _dock_status ||
level._dock_level != _dock_level;
}
publicboolean is_dockFriendly() {
return _dockFriendly;
}
publicboolean is_dockConnected() {
return (_dockFriendly && _dock_status >= Constants.DOCK_STATE_CHARGING && _dock_level >= 0);
}
publicint get_status() {
return _status;
}
publicint get_level() {
return _level;
}
publicint get_dock_status() {
if (!_dockFriendly)
return Constants.DOCK_STATE_UNKNOWN;
return _dock_status;
}
publicInteger get_dock_level() {
if (is_dockConnected())
return _dock_level;
return null;
}
publicvoid undock() {
_dock_status = Constants.DOCK_STATE_UNDOCKED;
}
publicvoid dock(int dock_level) {
_dock_status = Constants.DOCK_STATE_DOCKED;
_dock_level = dock_level;
}
}