2011-12-22 06:09:41 -08:00
|
|
|
#filter substitution
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-22 06:09:41 -08:00
|
|
|
|
|
|
|
package @ANDROID_PACKAGE_NAME@;
|
|
|
|
|
2012-03-22 10:36:20 -07:00
|
|
|
import java.io.BufferedOutputStream;
|
2011-12-22 06:09:41 -08:00
|
|
|
import java.io.BufferedReader;
|
2012-03-22 10:36:20 -07:00
|
|
|
import java.io.DataOutputStream;
|
2011-12-22 06:09:41 -08:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileWriter;
|
2012-03-22 10:36:20 -07:00
|
|
|
import java.io.FileOutputStream;
|
2011-12-22 06:09:41 -08:00
|
|
|
import java.io.IOException;
|
2012-03-22 10:36:20 -07:00
|
|
|
import java.io.OutputStream;
|
2012-03-27 13:16:13 -07:00
|
|
|
import java.io.PrintWriter;
|
2012-01-30 19:46:13 -08:00
|
|
|
import java.nio.IntBuffer;
|
2011-12-22 06:09:41 -08:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.lang.Class;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.Proxy;
|
|
|
|
import java.lang.reflect.InvocationHandler;
|
|
|
|
import java.lang.Long;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2012-01-30 19:46:13 -08:00
|
|
|
import android.opengl.GLSurfaceView;
|
2011-12-22 06:09:41 -08:00
|
|
|
import android.view.View;
|
2012-02-08 06:32:42 -08:00
|
|
|
import android.util.Log;
|
2011-12-22 06:09:41 -08:00
|
|
|
|
|
|
|
import org.json.*;
|
|
|
|
|
|
|
|
import com.jayway.android.robotium.solo.Solo;
|
|
|
|
|
|
|
|
public class FennecNativeDriver implements Driver {
|
2012-05-03 11:59:10 -07:00
|
|
|
private static final int FRAME_TIME_THRESHOLD = 25; // allow 25ms per frame (40fps)
|
2012-03-23 08:58:03 -07:00
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
// Map of IDs to element names.
|
|
|
|
private HashMap mLocators = null;
|
|
|
|
private Activity mActivity;
|
|
|
|
private Solo mSolo;
|
|
|
|
|
|
|
|
private static String mLogFile = null;
|
2012-04-10 12:20:46 -07:00
|
|
|
private static LogLevel mLogLevel = LogLevel.INFO;
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
// Objects for reflexive access of fennec classes.
|
|
|
|
private ClassLoader mClassLoader;
|
|
|
|
private Class mGel;
|
|
|
|
private Class mGe;
|
|
|
|
private Class mGas;
|
|
|
|
private Method mRegisterGEL;
|
|
|
|
private Method mUnregisterGEL;
|
|
|
|
private Method mSendGE;
|
|
|
|
private Method _startFrameRecording;
|
|
|
|
private Method _stopFrameRecording;
|
|
|
|
private Method _startCheckerboardRecording;
|
|
|
|
private Method _stopCheckerboardRecording;
|
|
|
|
private Method _getPixels;
|
|
|
|
|
|
|
|
public enum LogLevel {
|
2012-04-10 12:20:46 -07:00
|
|
|
DEBUG(1),
|
|
|
|
INFO(2),
|
|
|
|
WARN(3),
|
|
|
|
ERROR(4);
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
private int mValue;
|
|
|
|
LogLevel(int value) {
|
|
|
|
mValue = value;
|
|
|
|
}
|
|
|
|
public boolean isEnabled(LogLevel configuredLevel) {
|
|
|
|
return mValue >= configuredLevel.getValue();
|
|
|
|
}
|
|
|
|
private int getValue() {
|
|
|
|
return mValue;
|
|
|
|
}
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
public FennecNativeDriver(Activity activity, Solo robocop) {
|
|
|
|
mActivity = activity;
|
|
|
|
mSolo = robocop;
|
2011-12-22 06:09:41 -08:00
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
// Set up table of fennec_ids.
|
|
|
|
mLocators = convertTextToTable(getFile("/mnt/sdcard/fennec_ids.txt"));
|
|
|
|
|
|
|
|
// Set up reflexive access of java classes and methods.
|
|
|
|
try {
|
|
|
|
mClassLoader = activity.getClassLoader();
|
|
|
|
mGel = mClassLoader.loadClass("org.mozilla.gecko.GeckoEventListener");
|
|
|
|
mGe = mClassLoader.loadClass("org.mozilla.gecko.GeckoEvent");
|
|
|
|
mGas = mClassLoader.loadClass("org.mozilla.gecko.GeckoAppShell");
|
|
|
|
Class [] parameters = new Class[2];
|
|
|
|
parameters[0] = String.class;
|
|
|
|
parameters[1] = mGel;
|
|
|
|
mRegisterGEL = mGas.getMethod("registerGeckoEventListener", parameters);
|
|
|
|
mUnregisterGEL = mGas.getMethod("unregisterGeckoEventListener", parameters);
|
|
|
|
parameters = new Class[1];
|
|
|
|
parameters[0] = mGe;
|
|
|
|
mSendGE = mGas.getMethod("sendEventToGecko", parameters);
|
|
|
|
|
|
|
|
Class gfx = mClassLoader.loadClass("org.mozilla.gecko.gfx.PanningPerfAPI");
|
|
|
|
_startFrameRecording = gfx.getDeclaredMethod("startFrameTimeRecording");
|
|
|
|
_stopFrameRecording = gfx.getDeclaredMethod("stopFrameTimeRecording");
|
|
|
|
_startCheckerboardRecording = gfx.getDeclaredMethod("startCheckerboardRecording");
|
|
|
|
_stopCheckerboardRecording = gfx.getDeclaredMethod("stopCheckerboardRecording");
|
|
|
|
|
|
|
|
Class layerView = mClassLoader.loadClass("org.mozilla.gecko.gfx.LayerView");
|
|
|
|
_getPixels = layerView.getDeclaredMethod("getPixels");
|
2012-04-10 12:20:55 -07:00
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
log(LogLevel.ERROR, e);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
log(LogLevel.ERROR, e);
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
log(LogLevel.ERROR, e);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
log(LogLevel.ERROR, e);
|
|
|
|
}
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
//Information on the location of the Gecko Frame.
|
|
|
|
private boolean mGeckoInfo = false;
|
|
|
|
private int mGeckoTop = 100;
|
|
|
|
private int mGeckoLeft = 0;
|
|
|
|
private int mGeckoHeight= 700;
|
|
|
|
private int mGeckoWidth = 1024;
|
|
|
|
|
|
|
|
private void getGeckoInfo() {
|
|
|
|
View geckoLayout = mActivity.findViewById(Integer.decode((String)mLocators.get("gecko_layout")));
|
|
|
|
if (geckoLayout != null) {
|
|
|
|
int[] pos = new int[2];
|
|
|
|
geckoLayout.getLocationOnScreen(pos);
|
|
|
|
mGeckoTop = pos[1];
|
|
|
|
mGeckoLeft = pos[0];
|
|
|
|
mGeckoWidth = geckoLayout.getWidth();
|
|
|
|
mGeckoHeight = geckoLayout.getHeight();
|
|
|
|
mGeckoInfo = true;
|
|
|
|
} else {
|
|
|
|
throw new RoboCopException("Unable to find view gecko_layout");
|
|
|
|
}
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
public int getGeckoTop() {
|
|
|
|
if (!mGeckoInfo) {
|
|
|
|
getGeckoInfo();
|
|
|
|
}
|
|
|
|
return mGeckoTop;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
public int getGeckoLeft() {
|
|
|
|
if (!mGeckoInfo) {
|
|
|
|
getGeckoInfo();
|
|
|
|
}
|
|
|
|
return mGeckoLeft;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
public int getGeckoHeight() {
|
|
|
|
if (!mGeckoInfo) {
|
|
|
|
getGeckoInfo();
|
|
|
|
}
|
|
|
|
return mGeckoHeight;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
public int getGeckoWidth() {
|
|
|
|
if (!mGeckoInfo) {
|
|
|
|
getGeckoInfo();
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
return mGeckoWidth;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2012-05-22 16:25:30 -07:00
|
|
|
/** Find the named element in the list of known Fennec views.
|
|
|
|
* @return An Element representing the view, or null if the view is not found.
|
|
|
|
*/
|
2012-02-15 06:56:20 -08:00
|
|
|
public Element findElement(Activity activity, String name) {
|
|
|
|
if (name == null) {
|
2012-05-22 16:25:30 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
|
|
|
|
"Can not findElements when passed a null");
|
|
|
|
return null;
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
if (mLocators.containsKey(name)) {
|
|
|
|
return new FennecNativeElement(Integer.decode((String)mLocators.get(name)), activity, mSolo);
|
|
|
|
}
|
2012-05-22 16:25:30 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
|
|
|
|
"findElement: Element '"+name+"' does not exist in the list");
|
|
|
|
return null;
|
2012-02-02 01:02:49 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
public void startFrameRecording() {
|
|
|
|
try {
|
|
|
|
Object [] params = null;
|
|
|
|
_startFrameRecording.invoke(null, params);
|
|
|
|
} catch (IllegalAccessException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InvocationTargetException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-02-02 01:02:49 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
public int stopFrameRecording() {
|
|
|
|
Class [] parameters = new Class[1];
|
|
|
|
parameters[0] = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Object [] params = null;
|
2012-03-23 08:58:03 -07:00
|
|
|
List<Long> frames = (List<Long>)_stopFrameRecording.invoke(null, params);
|
2012-05-03 11:59:10 -07:00
|
|
|
int badness = 0;
|
2012-03-23 08:58:03 -07:00
|
|
|
for (int i = 1; i < frames.size(); i++) {
|
2012-05-03 11:59:10 -07:00
|
|
|
long frameTime = frames.get(i) - frames.get(i - 1);
|
|
|
|
int delay = (int)(frameTime - FRAME_TIME_THRESHOLD);
|
|
|
|
// for each frame we miss, add the square of the delay. This
|
|
|
|
// makes large delays much worse than small delays.
|
|
|
|
if (delay > 0) {
|
|
|
|
badness += delay * delay;
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 11:59:10 -07:00
|
|
|
// Don't do any averaging of the numbers because really we want to
|
|
|
|
// know how bad the jank was at its worst
|
|
|
|
return badness;
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (IllegalAccessException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InvocationTargetException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-02-02 01:02:49 -08:00
|
|
|
|
2012-05-03 11:59:10 -07:00
|
|
|
// higher values are worse, and the test failing is the worst!
|
|
|
|
return Integer.MAX_VALUE;
|
2012-01-30 19:46:13 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
public void startCheckerboardRecording() {
|
|
|
|
try {
|
|
|
|
Object [] params = null;
|
|
|
|
_startCheckerboardRecording.invoke(null, params);
|
|
|
|
} catch (IllegalAccessException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InvocationTargetException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-01-30 19:46:13 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
public float stopCheckerboardRecording() {
|
|
|
|
Class [] parameters = new Class[1];
|
|
|
|
parameters[0] = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Object [] params = null;
|
2012-03-23 08:58:03 -07:00
|
|
|
List<Float> checkerboard = (List<Float>)_stopCheckerboardRecording.invoke(null, params);
|
2012-04-26 11:24:10 -07:00
|
|
|
float total = 0;
|
2012-03-23 08:58:03 -07:00
|
|
|
for (float val : checkerboard) {
|
2012-04-26 11:24:10 -07:00
|
|
|
total += val;
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-04-26 11:24:10 -07:00
|
|
|
return total * 100.0f;
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (IllegalAccessException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InvocationTargetException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0f;
|
2012-01-30 19:46:13 -08:00
|
|
|
}
|
|
|
|
|
2012-03-22 10:35:19 -07:00
|
|
|
private View getSurfaceView() {
|
|
|
|
try {
|
|
|
|
Class c = Class.forName("org.mozilla.gecko.gfx.LayerView");
|
|
|
|
for (View v : mSolo.getCurrentViews()) {
|
|
|
|
if (c.isInstance(v)) {
|
|
|
|
return v;
|
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-03-22 10:35:19 -07:00
|
|
|
} catch (ClassNotFoundException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
return null;
|
2012-01-30 19:46:13 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
2012-03-06 12:08:45 -08:00
|
|
|
public PaintedSurface getPaintedSurface() {
|
2012-03-22 10:35:19 -07:00
|
|
|
View view = getSurfaceView();
|
2012-02-15 06:56:20 -08:00
|
|
|
if (view == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
IntBuffer pixelBuffer;
|
|
|
|
try {
|
|
|
|
pixelBuffer = (IntBuffer)_getPixels.invoke(view);
|
|
|
|
} catch (Exception e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
return null;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
// now we need to (1) flip the image, because GL likes to do things up-side-down,
|
|
|
|
// and (2) rearrange the bits from AGBR-8888 to ARGB-8888.
|
|
|
|
int w = view.getWidth();
|
|
|
|
int h = view.getHeight();
|
|
|
|
pixelBuffer.position(0);
|
2012-03-06 12:08:45 -08:00
|
|
|
String mapFile = "/mnt/sdcard/pixels.map";
|
|
|
|
|
|
|
|
FileOutputStream fos = null;
|
2012-03-22 10:36:20 -07:00
|
|
|
BufferedOutputStream bos = null;
|
2012-03-06 12:08:45 -08:00
|
|
|
DataOutputStream dos = null;
|
|
|
|
try {
|
|
|
|
fos = new FileOutputStream(mapFile);
|
2012-03-22 10:36:20 -07:00
|
|
|
bos = new BufferedOutputStream(fos);
|
|
|
|
dos = new DataOutputStream(bos);
|
2012-03-06 12:08:45 -08:00
|
|
|
|
|
|
|
for (int y = h - 1; y >= 0; y--) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
int agbr = pixelBuffer.get();
|
|
|
|
dos.writeInt((agbr & 0xFF00FF00) | ((agbr >> 16) & 0x000000FF) | ((agbr << 16) & 0x00FF0000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RoboCopException("exception with pixel writer on file: " + mapFile);
|
|
|
|
} finally {
|
|
|
|
try {
|
2012-03-22 10:36:20 -07:00
|
|
|
if (dos != null) {
|
2012-03-06 12:08:45 -08:00
|
|
|
dos.flush();
|
|
|
|
dos.close();
|
2012-03-22 10:36:20 -07:00
|
|
|
}
|
|
|
|
// closing dos automatically closes bos
|
|
|
|
if (fos != null) {
|
|
|
|
fos.flush();
|
2012-03-06 12:08:45 -08:00
|
|
|
fos.close();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-03-06 12:08:45 -08:00
|
|
|
throw new RoboCopException("exception closing pixel writer on file: " + mapFile);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
}
|
2012-03-22 10:36:20 -07:00
|
|
|
return new PaintedSurface(mapFile, w, h);
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
public int mHeight=0;
|
|
|
|
public int mScrollHeight=0;
|
|
|
|
public int mPageHeight=10;
|
|
|
|
|
|
|
|
class scrollHandler implements InvocationHandler {
|
|
|
|
public scrollHandler(){};
|
|
|
|
public Object invoke(Object proxy, Method method, Object[] args) {
|
|
|
|
try {
|
|
|
|
// Disect the JSON object into the appropriate variables
|
|
|
|
JSONObject jo = ((JSONObject)args[1]);
|
|
|
|
mScrollHeight = jo.getInt("y");
|
|
|
|
mHeight = jo.getInt("cheight");
|
|
|
|
// We don't want a height of 0. That means it's a bad response.
|
|
|
|
if (mHeight > 0) {
|
|
|
|
mPageHeight = jo.getInt("height");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch( Throwable e) {
|
2012-04-10 12:20:46 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
|
2012-02-15 06:56:20 -08:00
|
|
|
"WARNING: ScrollReceived, but read wrong!");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
public int getScrollHeight() {
|
|
|
|
return mScrollHeight;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
public int getPageHeight() {
|
|
|
|
return mPageHeight;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
public int getHeight() {
|
|
|
|
return mHeight;
|
2012-02-08 06:32:42 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
public void setupScrollHandling() {
|
|
|
|
//Setup scrollHandler to catch "robocop:scroll" events.
|
|
|
|
try {
|
|
|
|
Class [] interfaces = new Class[1];
|
|
|
|
interfaces[0] = mGel;
|
|
|
|
Object[] finalParams = new Object[2];
|
|
|
|
finalParams[0] = "robocop:scroll";
|
|
|
|
finalParams[1] = Proxy.newProxyInstance(mClassLoader, interfaces, new scrollHandler());
|
|
|
|
mRegisterGEL.invoke(null, finalParams);
|
|
|
|
} catch (IllegalAccessException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InvocationTargetException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-02-08 06:32:42 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
/**
|
|
|
|
* Takes a filename, loads the file, and returns a string version of the entire file.
|
|
|
|
*/
|
|
|
|
public static String getFile(String filename)
|
|
|
|
{
|
|
|
|
StringBuilder text = new StringBuilder();
|
|
|
|
|
|
|
|
BufferedReader br = null;
|
2012-02-08 06:32:42 -08:00
|
|
|
try {
|
2012-02-15 06:56:20 -08:00
|
|
|
br = new BufferedReader(new FileReader(filename));
|
|
|
|
String line;
|
|
|
|
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
text.append(line);
|
|
|
|
text.append('\n');
|
|
|
|
}
|
2012-04-10 12:20:55 -07:00
|
|
|
} catch (IOException e) {
|
|
|
|
log(LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
br.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
}
|
2012-02-08 06:32:42 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
return text.toString();
|
2012-02-08 06:32:42 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
/**
|
|
|
|
* Takes a string of "key=value" pairs split by \n and creates a hash table.
|
|
|
|
*/
|
|
|
|
public static HashMap convertTextToTable(String data)
|
|
|
|
{
|
|
|
|
HashMap retVal = new HashMap();
|
|
|
|
|
|
|
|
String[] lines = data.split("\n");
|
|
|
|
for (int i = 0; i < lines.length; i++) {
|
|
|
|
String[] parts = lines[i].split("=");
|
|
|
|
retVal.put(parts[0].trim(), parts[1].trim());
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the filename used for logging. If the file already exists, delete it
|
|
|
|
* as a safe-guard against accidentally appending to an old log file.
|
|
|
|
*/
|
|
|
|
public static void setLogFile(String filename) {
|
|
|
|
mLogFile = filename;
|
|
|
|
File file = new File(mLogFile);
|
|
|
|
if (file.exists()) {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setLogLevel(LogLevel level) {
|
|
|
|
mLogLevel = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void log(LogLevel level, String message) {
|
2012-03-27 13:16:13 -07:00
|
|
|
log(level, message, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void log(LogLevel level, Throwable t) {
|
|
|
|
log(level, null, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void log(LogLevel level, String message, Throwable t) {
|
2012-02-15 06:56:20 -08:00
|
|
|
if (mLogFile == null) {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level.isEnabled(mLogLevel)) {
|
2012-03-27 13:16:13 -07:00
|
|
|
PrintWriter pw = null;
|
2012-02-15 06:56:20 -08:00
|
|
|
try {
|
2012-03-27 13:16:13 -07:00
|
|
|
pw = new PrintWriter(new FileWriter(mLogFile, true));
|
|
|
|
if (message != null) {
|
|
|
|
pw.println(message);
|
|
|
|
}
|
|
|
|
if (t != null) {
|
|
|
|
t.printStackTrace(pw);
|
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
2012-02-15 06:56:20 -08:00
|
|
|
Log.e("Robocop", "exception with file writer on: " + mLogFile);
|
|
|
|
} finally {
|
2012-03-27 13:16:13 -07:00
|
|
|
pw.close();
|
|
|
|
}
|
|
|
|
// PrintWriter doesn't throw IOE but sets an error flag instead,
|
|
|
|
// so check for that
|
|
|
|
if (pw.checkError()) {
|
|
|
|
Log.e("Robocop", "exception with file writer on: " + mLogFile);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 12:20:46 -07:00
|
|
|
if (level == LogLevel.INFO) {
|
2012-03-27 13:16:13 -07:00
|
|
|
Log.i("Robocop", message, t);
|
2012-04-10 12:20:46 -07:00
|
|
|
} else if (level == LogLevel.DEBUG) {
|
2012-03-27 13:16:13 -07:00
|
|
|
Log.d("Robocop", message, t);
|
2012-04-10 12:20:46 -07:00
|
|
|
} else if (level == LogLevel.WARN) {
|
2012-03-27 13:16:13 -07:00
|
|
|
Log.w("Robocop", message, t);
|
2012-04-10 12:20:46 -07:00
|
|
|
} else if (level == LogLevel.ERROR) {
|
2012-03-27 13:16:13 -07:00
|
|
|
Log.e("Robocop", message, t);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-02-08 06:32:42 -08:00
|
|
|
}
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|