Logging class for Android

Technorati Tags: ,,
Many times we need to print and log debug information that is used while we are debugging our program or code is in development stages. And this grows and grows and it piles up when your project is near to publish phase.

IO is very costly so we have to remove all the logs and it’s really a pains taking task, donkey work. I wrote a class that uses Android Log class that is less costly than System.out, just put it in your project and you can turn debugging on and off by simple flag, even you can control the level of debugging like

Information

Verbose

Debug

Warning e.t.c

   
/**
* @author AZ
*/

import android.content.Context;
import android.util.Log;
import android.widget.Toast;


public class Logger implements com.newsreader.IConstants {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

private static String sTag = DEBUGTAG;
private static String sDebugUser = "";
private static DebugLevel sDebugLevel = DebugLevel.VERBOSE;

// ===========================================================
// Constructors
// ===========================================================

// ===========================================================
// Getter & Setter
// ===========================================================

public static String getTag() {
return Logger.sTag;
}

public static void setTag(final String pTag) {
Logger.sTag = pTag;
}

public static DebugLevel getDebugLevel() {
return Logger.sDebugLevel;
}

public static void setDebugLevel(final DebugLevel pDebugLevel) {
if (pDebugLevel == null) {
throw new IllegalArgumentException("pDebugLevel must not be null!");
}
Logger.sDebugLevel = pDebugLevel;
}

public static void setDebugUser(final String pDebugUser) {
if (pDebugUser == null) {
throw new IllegalArgumentException("pDebugUser must not be null!");
}
Logger.sDebugUser = pDebugUser;
}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

public static void log(final DebugLevel pDebugLevel, final String pMessage) {
switch (pDebugLevel) {
case NONE:
return;
case VERBOSE:
Logger.v(pMessage);
return;
case INFO:
Logger.i(pMessage);
return;
case DEBUG:
Logger.d(pMessage);
return;
case WARNING:
Logger.w(pMessage);
return;
case ERROR:
Logger.e(pMessage);
return;
}
}

public static void log(final String pMessage, final Throwable pThrowable) {
Logger.log(DebugLevel.ERROR, pMessage, pThrowable);
}

public static void log(final DebugLevel pDebugLevel, final String pMessage, final Throwable pThrowable) {
switch (pDebugLevel) {
case NONE:
return;
case VERBOSE:
Logger.v(pMessage, pThrowable);
return;
case INFO:
Logger.i(pMessage, pThrowable);
return;
case DEBUG:
Logger.d(pMessage, pThrowable);
return;
case WARNING:
Logger.w(pMessage, pThrowable);
return;
case ERROR:
Logger.e(pMessage, pThrowable);
return;
}
}

public static void log(final DebugLevel pDebugLevel, final String pTag, final String pMessage) {
switch (pDebugLevel) {
case NONE:
return;
case VERBOSE:
Logger.v(pTag, pMessage);
return;
case INFO:
Logger.i(pTag, pMessage);
return;
case DEBUG:
Logger.d(pTag, pMessage);
return;
case WARNING:
Logger.w(pTag, pMessage);
return;
case ERROR:
Logger.e(pTag, pMessage);
return;
}
}

public static void log(final DebugLevel pDebugLevel, final String pTag, final String pMessage, final Throwable pThrowable) {
switch (pDebugLevel) {
case NONE:
return;
case VERBOSE:
Logger.v(pTag, pMessage, pThrowable);
return;
case INFO:
Logger.i(pTag, pMessage, pThrowable);
return;
case DEBUG:
Logger.d(pTag, pMessage, pThrowable);
return;
case WARNING:
Logger.w(pTag, pMessage, pThrowable);
return;
case ERROR:
Logger.e(pTag, pMessage, pThrowable);
return;
}
}

public static void v(final String pMessage) {
Logger.v(Logger.sTag, pMessage, null);
}

public static void v(final String pMessage, final Throwable pThrowable) {
Logger.v(Logger.sTag, pMessage, pThrowable);
}

public static void v(final String pTag, final String pMessage) {
Logger.v(pTag, pMessage, null);
}

public static void v(final String pTag, final String pMessage, final Throwable pThrowable) {
if (Logger.sDebugLevel.isSameOrLessThan(DebugLevel.VERBOSE)) {
if (pThrowable == null) {
Log.v(pTag, pMessage);
} else {
Log.v(pTag, pMessage, pThrowable);
}
}
}

public static void vUser(final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.v(pMessage);
}
}

public static void vUser(final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.v(pMessage, pThrowable);
}
}

public static void vUser(final String pTag, final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.v(pTag, pMessage);
}
}

public static void vUser(final String pTag, final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.v(pTag, pMessage, pThrowable);
}
}

public static void d(final String pMessage) {
Logger.d(Logger.sTag, pMessage, null);
}

public static void d(final String pMessage, final Throwable pThrowable) {
Logger.d(Logger.sTag, pMessage, pThrowable);
}

public static void d(final String pTag, final String pMessage) {
Logger.d(pTag, pMessage, null);
}

public static void d(final String pTag, final String pMessage, final Throwable pThrowable) {
if (Logger.sDebugLevel.isSameOrLessThan(DebugLevel.DEBUG)) {
if (pThrowable == null) {
Log.d(pTag, pMessage);
} else {
Log.d(pTag, pMessage, pThrowable);
}
}
}

public static void dUser(final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.d(pMessage);
}
}

public static void dUser(final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.d(pMessage, pThrowable);
}
}

public static void dUser(final String pTag, final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.d(pTag, pMessage);
}
}

public static void dUser(final String pTag, final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.d(pTag, pMessage, pThrowable);
}
}

public static void i(final String pMessage) {
Logger.i(Logger.sTag, pMessage, null);
}

public static void i(final String pMessage, final Throwable pThrowable) {
Logger.i(Logger.sTag, pMessage, pThrowable);
}

public static void i(final String pTag, final String pMessage) {
Logger.i(pTag, pMessage, null);
}

public static void i(final String pTag, final String pMessage, final Throwable pThrowable) {
if (Logger.sDebugLevel.isSameOrLessThan(DebugLevel.INFO)) {
if (pThrowable == null) {
Log.i(pTag, pMessage);
} else {
Log.i(pTag, pMessage, pThrowable);
}
}
}

public static void iUser(final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.i(pMessage);
}
}

public static void iUser(final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.i(pMessage, pThrowable);
}
}

public static void iUser(final String pTag, final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.i(pTag, pMessage);
}
}

public static void iUser(final String pTag, final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.i(pTag, pMessage, pThrowable);
}
}

public static void w(final String pMessage) {
Logger.w(Logger.sTag, pMessage, null);
}

public static void w(final Throwable pThrowable) {
Logger.w(Logger.sTag, pThrowable);
}

public static void w(final String pMessage, final Throwable pThrowable) {
Logger.w(Logger.sTag, pMessage, pThrowable);
}

public static void w(final String pTag, final String pMessage) {
Logger.w(pTag, pMessage, null);
}

public static void w(final String pTag, final String pMessage, final Throwable pThrowable) {
if (Logger.sDebugLevel.isSameOrLessThan(DebugLevel.WARNING)) {
if (pThrowable == null) {
Log.w(pTag, pMessage);
} else {
Log.w(pTag, pMessage, pThrowable);
}
}
}

public static void wUser(final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.w(pMessage);
}
}

public static void wUser(final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.w(pThrowable);
}
}

public static void wUser(final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.w(pMessage, pThrowable);
}
}

public static void wUser(final String pTag, final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.w(pTag, pMessage);
}
}

public static void wUser(final String pTag, final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.w(pTag, pMessage, pThrowable);
}
}

public static void e(final String pMessage) {
Logger.e(Logger.sTag, pMessage, null);
}

public static void e(final Throwable pThrowable) {
Logger.e(Logger.sTag, pThrowable);
}

public static void e(final String pMessage, final Throwable pThrowable) {
Logger.e(Logger.sTag, pMessage, pThrowable);
}

public static void e(final String pTag, final String pMessage) {
Logger.e(pTag, pMessage, null);
}

public static void e(final String pTag, final String pMessage, final Throwable pThrowable) {
if (Logger.sDebugLevel.isSameOrLessThan(DebugLevel.ERROR)) {
if (pThrowable == null) {
Log.e(pTag, pMessage);
} else {
Log.e(pTag, pMessage, pThrowable);
}
}
}

public static void eUser(final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.e(pMessage);
}
}

public static void eUser(final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.e(pThrowable);
}
}

public static void eUser(final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.e(pMessage, pThrowable);
}
}

public static void eUser(final String pTag, final String pMessage, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.e(pTag, pMessage);
}
}

public static void eUser(final String pTag, final String pMessage, final Throwable pThrowable, final String pDebugUser) {
if (Logger.sDebugUser.equals(pDebugUser)) {
Logger.e(pTag, pMessage, pThrowable);
}
}

/**
* Call from <b>UI Thread</b>
*/
public static void showToast(final Context pContext, final String pMessage, final int pDuration) {
Toast.makeText(pContext, pMessage, pDuration).show();
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

public static enum DebugLevel implements Comparable<DebugLevel> {
NONE, ERROR, WARNING, INFO, DEBUG, VERBOSE;

public static DebugLevel ALL = DebugLevel.VERBOSE;

public boolean isSameOrLessThan(final DebugLevel pDebugLevel) {
return this.compareTo(pDebugLevel) >= 0;
}
}
}

Andengine Sprite Relative Rotation

  1: package com.example.andenginetest;
  2: 
  3: import org.andengine.engine.camera.Camera;
  4: import org.andengine.engine.options.EngineOptions;
  5: import org.andengine.engine.options.ScreenOrientation;
  6: import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
  7: import org.andengine.entity.scene.IOnSceneTouchListener;
  8: import org.andengine.entity.scene.Scene;
  9: import org.andengine.entity.sprite.Sprite;
 10: import org.andengine.input.touch.TouchEvent;
 11: import org.andengine.opengl.texture.TextureOptions;
 12: import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
 13: import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
 14: import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
 15: import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
 16: import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
 17: import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
 18: import org.andengine.opengl.texture.region.ITextureRegion;
 19: import org.andengine.ui.activity.BaseGameActivity;
 20: import org.andengine.util.math.MathUtils;
 21: 
 22: public class MainActivity extends BaseGameActivity implements IOnSceneTouchListener {
 23: 
 24: 	// ====================================================
 25: 	// CONSTANTS
 26: 	// ====================================================
 27: 	public static final int WIDTH = 800;
 28: 	public static final int HEIGHT = 480;
 29: 	public static final int SPRITE_DEFAULT_ROTATION = -90;
 30: 
 31: 	// ====================================================
 32: 	// VARIABLES
 33: 	// ====================================================
 34: 	private Scene mScene;
 35: 	private Camera mCamera;
 36: 
 37: 	private Sprite mArrowSprite;
 38: 	private Sprite mMarbleSprite;
 39: 
 40: 	private ITextureRegion mArrowTextureRegion;
 41: 	private ITextureRegion mMarbleTextureRegion;
 42: 
 43: 	// ====================================================
 44: 	// CREATE ENGINE OPTIONS
 45: 	// ====================================================
 46: 	@Override
 47: 	public EngineOptions onCreateEngineOptions() {
 48: 		mCamera = new Camera(0, 0, WIDTH, HEIGHT);
 49: 
 50: 		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
 51: 
 52: 		return engineOptions;
 53: 	}
 54: 
 55: 	// ====================================================
 56: 	// CREATE RESOURCES
 57: 	// ====================================================
 58: 	@Override
 59: 	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) {
 60: 		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
 61: 
 62: 		BuildableBitmapTextureAtlas mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
 63: 
 64: 		mArrowTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "arrow.png");
 65: 		mMarbleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "marble.png");
 66: 
 67: 		try {
 68: 			mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1));
 69: 			mBitmapTextureAtlas.load();
 70: 		} catch (TextureAtlasBuilderException e) {
 71: 			e.printStackTrace();
 72: 		}
 73: 
 74: 		pOnCreateResourcesCallback.onCreateResourcesFinished();
 75: 	}
 76: 
 77: 	// ====================================================
 78: 	// CREATE SCENE
 79: 	// ====================================================
 80: 	@Override
 81: 	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
 82: 		mScene = new Scene();
 83: 
 84: 		// Allow our scene to register this activities touch events
 85: 		mScene.setOnSceneTouchListener(this);
 86: 
 87: 		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
 88: 	}
 89: 
 90: 	// ====================================================
 91: 	// POPULATE SCENE
 92: 	// ====================================================
 93: 	@Override
 94: 	public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) {
 95: 
 96: 		// Create our marble sprite in the top left corner of the screen
 97: 		// initially.
 98: 		mMarbleSprite = new Sprite(0, 0, mMarbleTextureRegion, mEngine.getVertexBufferObjectManager());
 99: 		mScene.attachChild(mMarbleSprite);
100: 
101: 		// Set our arrow sprite's position to the center of the screen
102: 		final float x = (WIDTH / 2) - (mArrowTextureRegion.getWidth() / 2);
103: 		final float y = (HEIGHT / 2) - (mArrowTextureRegion.getHeight() / 2);
104: 
105: 		// Create and attach our arrow sprite to the scene
106: 		mArrowSprite = new Sprite(x, y, mArrowTextureRegion, mEngine.getVertexBufferObjectManager());
107: 		mScene.attachChild(mArrowSprite);
108: 
109: 		pOnPopulateSceneCallback.onPopulateSceneFinished();
110: 	}
111: 
112: 	// We can override the onSceneTouchEvent() method since our activity
113: 	// now implements the IOnSceneTouchListener
114: 	@Override
115: 	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
116: 		// If a user moves their finger on the device
117: 		if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE) {
118: 
119: 			// Set our marble's position to the touched area on the screen
120: 			mMarbleSprite.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());			
121: 
122: 			final float dX = (mArrowSprite.getX() - mMarbleSprite.getX());
123: 			final float dY = (mArrowSprite.getY() - mMarbleSprite.getY());
124: 
125: 			// We can use the atan2 function to find the angle
126: 			// Additionally, OpenGL works with degrees so we must convert
127: 			// from radians
128: 			final float rotation = MathUtils.radToDeg((float) Math.atan2(-dY, dX)) + SPRITE_DEFAULT_ROTATION;
129: 
130: 			// Set the new rotation for the arrow
131: 			mArrowSprite.setRotation(rotation);
132: 			return true;
133: 		}
134: 		return false;
135: 	}
136: 
137: }
138: 

Serialize, De-serialize Android SDcard

public static void writeObject(String filePath, DamageAssessmentFormPojo pojo) throws IOException {

            filePath += DefaultReportName;
            FileOutputStream fos = new FileOutputStream(filePath);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(pojo);

        }

        public static Object readObject(String filePath) throws Exception {
            filePath += DefaultReportName;
            FileInputStream fis = new FileInputStream(filePath);
            ObjectInputStream ois = new ObjectInputStream(fis);
            return (ois.readObject());
        }

Android Super Fast Emulator

Blazing fast android emulator-x86 on ICS

In this article I am going to show android developers how Intel’s hypervisor with Intel Linux 2.6 kernel outperforms native ARM hardware performance.

First download “Intel Atom System Image”

Next, to create a new AVD we perform the following steps:

  1. Click on "Tools", "Manage AVDs" and "New.."
  2. Name the AVD "x86_4_0_3"
  3. Select "Android 4.0.3 - API Level 15" as Target
  4. Choose "Intel Atom (x86)" as CPU/ABI
  5. Click "Create AVD"

Tags:

Android fast emulator x86 intel atom hypervision

get height width or inflated layout

Hi,

its simple and easy :)

public Bitmap getBitmapFromView(RelativeLayout v) {
v.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.RGB_565);

Canvas c = new Canvas(b);
v.draw(c);
return b;
}