/*******************************************************************************
* Copyright (c) 2009 Ferenc Hechler - [email protected]
*
* This file is part of the Android Battery Dog
*
* The Android Battery Dog is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any later version.
*
* The Android Battery Dog is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Android Battery Dog;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*******************************************************************************/
package net.sf.andbatdog.batterydog;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
public class BatteryGraph extends Activity {
private final static String TAG = "BATDOG.graph";
private final static int MENU_4H = 1;
private final static int MENU_8H = 2;
private final static int MENU_24H = 3;
private final static int MENU_3DAYS = 4;
private final static int MENU_7DAYS = 5;
private final static int MENU_ALL = 6;
private final static int margXLeft = 5;
private final static int margXRight = 5;
private final static int margYTop = 60;
private final static int margYBottom = 5;
private long width = 300;
private long height = 300;
private long w = width - margXLeft - margXRight;
private long h = height - margYTop - margYBottom;
private long msecPerHour = 1000*60*60;
private long mDeltaTime = 24*msecPerHour;
private long mOffset = 0;
private GraphView mGraphView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGraphView = new GraphView(this);
setContentView(mGraphView);
}
/**
* Called when your activity's options menu needs to be created.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_4H, Menu.NONE, "4h");
menu.add(Menu.NONE, MENU_8H, Menu.NONE, "8h");
menu.add(Menu.NONE, MENU_24H, Menu.NONE, "24h");
menu.add(Menu.NONE, MENU_3DAYS, Menu.NONE, "3 days");
menu.add(Menu.NONE, MENU_7DAYS, Menu.NONE, "7 days");
menu.add(Menu.NONE, MENU_ALL, Menu.NONE, "all");
return true;
}
/**
* Called when a menu item is selected.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == MENU_4H) {
mDeltaTime = 4*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_8H) {
mDeltaTime = 8*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_24H) {
mDeltaTime = 24*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_3DAYS) {
mDeltaTime = 3*24*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_7DAYS) {
mDeltaTime = 7*24*60*60*1000;
mOffset = 0;
mGraphView.invalidate();
}
else if (item.getItemId() == MENU_ALL) {
mDeltaTime = 0;
mOffset = 0;
mGraphView.invalidate();
}
mGraphView.readRecords(); // update from service
return true;
}
@Override
public boolean onTrackballEvent(MotionEvent event) {
super.onTrackballEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mOffset = 0;
mGraphView.readRecords(); // update from service
mGraphView.invalidate();
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float x = event.getRawX();
if (x < 0) {
mOffset -= mDeltaTime/5;
mGraphView.invalidate();
}
else if (x>0) {
mOffset += mDeltaTime/5;
if (mOffset > 0)
mOffset = 0;
mGraphView.invalidate();
}
}
return true;
}
private class GraphView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private BatteryRecord[] mRecords;
private float mLastX;
public void readRecords() {
try {
mRecords = readLog();
}
catch (Exception e) {
Log.e(TAG,e.getMessage(), e);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mLastX = event.getRawX();
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
// Log.i(TAG, event.toString()+" - " + event.getHistorySize());
float x = event.getRawX();
float dx = x-mLastX;
mLastX = x;
long ldx = (long)(mDeltaTime*dx/width);
mOffset -= ldx;
if (mOffset > 0)
mOffset = 0;
mGraphView.invalidate();
}
return true;
}
public GraphView(Context context) {
super(context);
readRecords();
Display display = ((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
w = width - margXLeft - margXRight;
h = height - margYTop - margYBottom;
}
@Override protected void onDraw(Canvas canvas) {
Paint paint = mPaint;
paint.setStrokeWidth(1);
Paint paintP = new Paint(Paint.ANTI_ALIAS_FLAG);
paintP.setStrokeWidth(2);
paintP.setColor(Color.YELLOW);
Paint paintV = new Paint();
paintV.setStrokeWidth(0);
paintV.setColor(Color.RED);
Paint paintT = new Paint();
paintT.setStrokeWidth(0);
paintT.setColor(Color.GREEN);
Paint paintD = new Paint(); // discharge rate
paintD.setStrokeWidth(0);
paintD.setColor(Color.CYAN);
canvas.drawColor(Color.BLACK);
if ((mRecords == null) || (mRecords.length == 0)) {
paint.setColor(Color.WHITE);
canvas.drawText("no data found", 10, 50, paint);
return;
}
drawMarker(canvas, paintP, paintV, paintT, paintD);
int maxRec = mRecords.length;
long minTime = mRecords[0].timestamp;
long maxTime = mRecords[maxRec-1].timestamp;
long dTime = maxTime-minTime;
if (mDeltaTime != 0) {
// make minTime, maxTime be min and max time on screen
dTime = mDeltaTime;
minTime = maxTime-dTime+mOffset;
maxTime = minTime+mDeltaTime;
}
BatteryRecord rec;
BatteryRecord oldRec;
for (int i = 0; i <= maxRec; i++) {
if (i == 0)
oldRec = mRecords[0];
else
oldRec = mRecords[i-1];
if (i == maxRec)
rec = mRecords[maxRec-1];
else
rec =
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android监视电池.zip (51个子文件)
Android监视电池
Android监视电池
andbatdog监视电池
tags
0_1_0
AndroidBatteryDog
res
drawable
battery_dog_icon.png 5KB
values
strings.xml 180B
layout
battery_dog.xml 2KB
default.properties 960B
src
net
sf
andbatdog
batterydog
BatteryDog.java 7KB
BatteryGraph.java 11KB
BatteryDog_Service.java 6KB
Log.java 455B
.project 853B
.classpath 280B
AndroidManifest.xml 940B
v0.1.0
AndroidBatteryDog
res
drawable
battery_dog_icon.png 5KB
values
strings.xml 180B
layout
battery_dog.xml 2KB
default.properties 960B
src
net
sf
andbatdog
batterydog
BatteryDog.java 7KB
BatteryGraph.java 11KB
BatteryDog_Service.java 6KB
Log.java 455B
.project 853B
.classpath 280B
AndroidManifest.xml 940B
trunk
AndroidBatteryDog
AndroidBatteryDog
res
drawable
battery_dog_icon.png 5KB
values
strings.xml 180B
layout
battery_dog.xml 2KB
default.properties 960B
src
net
sf
andbatdog
batterydog
BatteryDog.java 7KB
BatteryGraph.java 16KB
BatteryDog_Service.java 6KB
Log.java 455B
.project 853B
.classpath 280B
AndroidManifest.xml 940B
.svn
pristine
98
98a13557122e962c423ed181a04abaf5e615d997.svn-base 2KB
be
be8283b6be42711d12cb6d2feca9150178d12639.svn-base 6KB
a8
a8e1ff0f16051da24df26a73aff0369fe2401e0b.svn-base 960B
a6
a6f27b57fff3b1f7b0b50edad74ba894d49c3bff.svn-base 280B
2a
2aeaf042e00ebd0deb02631af9cf48279715174c.svn-base 5KB
76
76046ed18052cd0a57911bd0acb56e1c8072c9aa.svn-base 16KB
b2
b2cdcef08835323c5d2e4f5c77d17db0ef3d4222.svn-base 455B
a1
a10711fe7c3141f131286dd6aa5c80d2bc31b226.svn-base 960B
cb
cbef9e187a29a6267c5a050d68b78efa74e67a7d.svn-base 11KB
fe
fedfadc44a04792c4ca97d61d5708b1fddfeaee0.svn-base 940B
c6
c6ae6bd6c0f5516851755f1f30f7fb423fd5e2d2.svn-base 180B
8d
8dbe68e3842bded48762763287bc4bc22a65f656.svn-base 853B
af
af17836108cfb74275bdcbb9c3e10c24f24c7cf4.svn-base 2KB
93
9390fcb60ccf3517e11e44976fc75f2f7f31f12f.svn-base 940B
b4
b4482bdba6412272aae7eace6beddcc86f9971db.svn-base 7KB
wc.db 87KB
entries 3B
format 3B
共 51 条
- 1
资源评论
yxkfw
- 粉丝: 76
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功