Archive

Archive for September, 2011

Get SIM card number

September 29, 2011 1 comment

A tip for retrieving SIM card number:

TelephonyManager teleManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String simNo = teleManager .getSimSerialNumber();

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , ,

Set Phone Ringer Mode

September 28, 2011 Leave a comment

A quick snippet for setting phone ringer mode 🙂

AudioManager audioMnger = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

audioMnger .setRingerMode(AudioManager.RINGER_MODE_NORMAL); // NORMAL
audioMnger .setRingerMode(AudioManager.RINGER_MODE_SILENT); // SILENT
audioMnger .setRingerMode(AudioManager.RINGER_MODE_VIBRATE); // VIBRATE

Just select one of those pre-supported mode.

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , ,

Image Processing – Pixel Color Replacement

September 28, 2011 3 comments

Some buddies has mailed me asking about replacing a pixel color by another.

For sample, of what I’ve done, this is how it looks.

Image - The Original

Image - The Original

The next one replaces all the 0xFFFFFFFF pixel into 0xFF0B0C0D color:

Image - The Replaced

Image - The Replaced

Not so beautiful, well, I’ve just picked random color….lol..

This is what I’ve done for it.

package pete.android.study;

import android.graphics.Bitmap;

public class ImageProcessor {
	Bitmap mImage;
	boolean mIsError = false;

	public ImageProcessor(final Bitmap image) {
		mImage = image.copy(image.getConfig(), image.isMutable());
		if(mImage == null) {
			mIsError = true;
		}
	}

	public boolean isError() {
		return mIsError;
	}

	public void setImage(final Bitmap image) {
		mImage = image.copy(image.getConfig(), image.isMutable());
		if(mImage == null) {
			mIsError = true;
		} else {
			mIsError = false;
		}
	}

	public Bitmap getImage() {
		if(mImage == null){
			return null;
		}
		return mImage.copy(mImage.getConfig(), mImage.isMutable());
	}

	public void free() {
		if(mImage != null && !mImage.isRecycled()) {
			mImage.recycle();
			mImage = null;
		}
	}

	public Bitmap replaceColor(int fromColor, int targetColor) {
		if(mImage == null) {
			return null;
		}

		int width = mImage.getWidth();
		int height = mImage.getHeight();
		int[] pixels = new int[width * height];
		mImage.getPixels(pixels, 0, width, 0, 0, width, height);

		for(int x = 0; x < pixels.length; ++x) {
			pixels[x] = (pixels[x] == fromColor) ? targetColor : pixels[x];
		}

		Bitmap newImage = Bitmap.createBitmap(width, height, mImage.getConfig());
		newImage.setPixels(pixels, 0, width, 0, 0, width, height);

		return newImage;
	}
}

Guess you already knew the basic concepts of image processing in all of my previous articles.
Hope you get something cool from it!

Cheers,
Pete Houston

Categories: Tutorials Tags: , , , ,

A day of good learnings and gains

September 21, 2011 Leave a comment

I’ve taken the course of Risk Management today at my company Training Center for career improvement, or something like that. There was a lot of fun, especially about Risk itself.

By default, I always have in mind that Risk is something negative and should be avoided, but actually it is not. In facts, Risk includes both the positives and negatives, in which the positives refer to the opportunities or chances met or triggered under some conditions, and the negatives are things already known.

In life, we tend to find a way to meet the positive risks, but we don’t know how to do or to manage it well.

The Risk Management coure today really gives me something valuable and I hope I can manage it well in the future.

“Avoid the negatives, Trigger the positives!”

Some keywords related to the Risk concept: opportunity, mitigation, impact, contingency.

 

Colorizing the Title Bar

September 20, 2011 2 comments

It’s very interesting if we can do some customizing over title bar on screen, like this:

Colorizing Title Bar

Colorizing Title Bar

Here the tricks:

package pete.android.study;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewParent;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // load title bar from Android layout
        TextView titleBar = (TextView)getWindow().findViewById(android.R.id.title);
        if (titleBar != null) {
            // set text color, YELLOW as sample
            titleBar.setTextColor(Color.YELLOW);
            // find parent view
            ViewParent parent = titleBar.getParent();
            if (parent != null && (parent instanceof View)) {
                // set background on parent, BRICK as sample
                View parentView = (View)parent;
                parentView.setBackgroundColor(Color.rgb(0x88, 0x33, 0x33));
            }
        }
    }
}

If you want to do some customization on default Android screen layout by re-construct the framework, then refer to this file: platforms/android-2.3.3/data/res/layout/screen.xml, whereas replace 2.3.3 by your version.

Have fun 😀

 

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: , , ,

The less-known over screen orientation

September 19, 2011 Leave a comment

I’ve just found a really interesting problem today at work, it’s about screen orientation.

I was trying to create a very simple application, which:

+ contains one button to change screen orientation state: PORTRAIT to LANDSCAPE, and vice versa.

+ the text on button determines the action going to be done if click. If the current is PORTRAIT then the text should be LANDSCAPE, and vice versa.

OK, so here is one very simple implementation, easy-read, easy-understood:

package pete.android.study;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    // the constant strings defining text to display on button
	static final String ORIENT_PORTRAIT = "Set Portrait";
	static final String ORIENT_LANDSCAPE = "Set Landscape";
	// determine screen orientation state changed
	boolean mState = false;
	// the main button
	Button mBtnSet = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // load button from layout
        mBtnSet = (Button)findViewById(R.id.btnSet);
        // set default text display
        mBtnSet.setText(ORIENT_LANDSCAPE);
        // handle click event
        mBtnSet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// state FALSE: switch to LANDSCAPE
				if(!mState) {
					setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
					mBtnSet.setText(ORIENT_PORTRAIT);
				}
				// state TRUE: switch to PORTRAIT
				else {
					setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
					mBtnSet.setText(ORIENT_LANDSCAPE);
				}
				// update state
				mState = !mState;
			}
		});
    }
}

However, there has a problem inside. First, try to build and run it.

The Initial Screen

The Initial Screen

By default, the screen is set as PORTRAIT, and the text is “Set Landscape“, which is correct.

Next, try to click on button, this will happen:

After First Click on Button

After First Click on Button

The screen orientation is changed to LANDSCAPE, however, the text on button doesn’t change at all. That’s so surprising!

And keep clicking on button one more time:

The 2nd Click

The 2nd Click

Now, the text changes to “Set Portrait“, well, the orientation doesn’t change, but on the third click, the screen interface backs to the first initial state.

It’s interesting, isn’t it?

The question in mind is why it is…

First, reading this one on Google Android References – Activity#ConfigurationChanges

Reading clearly, you’ll find that, everytime the screen orientation changes, the Activity is auto destroyed and recreated again, which means the text “Set Landscape” is reset as the initial state, a re-call of onCreate() method.

That’s the problem!

The solution is very simple, by adding the attribute – android:configChanges=”orientation” – , it will avoid re-create the activity and keep the current state.

Note: if you always to handle screen orientation without attribute of handling orientation, sometimes you might get exception, or errors in your application as well, even crash-out and freeze. So always keep this in mind :).

Hope you having fun with this little tips!

 

Cheers,

Pete Houston

 

 

 

Using WebView as a Image Zoom View control

September 18, 2011 12 comments

Well, the built-in Google Android has supported for developers lots of things; however, many still don’t know how to make uses the best out of it. One example is about WebView control.

Reference on WebView control on Google Android Developers’ Page

Most of developers just know “WebView is a control that supports the functionality to browse and surf webs.” Yes, that’s totally true! But, hang on…what does it mean specifically?

Think of it! Just like any other browsers, Firefox, IE, Opera or Safari.. what are things  could be displayed on a web browser? – I say “HTML code“, that’s right, for developers. What are the things wrapped around by HTML code? – I say “lots of text and images“.

Hey dude, stop right there. “Text and Images” are things can be displayed seperatedly on a web browser. Hence, for an open and creative minds, by using WebView control, developers can create a simple application like: Text Reader or Image Viewer. That’s true! So for simple application, you don’t have to customize or create any complicated control, WebView control is a good option.

Just for a fun introduction, let’s head to our goal, make use of WebView as an Image Zoom View control.

The WebView control has supported the built-in functionality of zoom, so we don’t have to re-create an already-existed on.

WebView as Image Zoom View

WebView as Image Zoom View

By using a simple property of WebView, we make use of it:

package pete.study.android;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity {
    // declare a WebView object first
	WebView mWebView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // load view
        mWebView = (WebView)findViewById(R.id.webView);
        // (*) this line make uses of the Zoom control
        mWebView.getSettings().setBuiltInZoomControls(true);
        // simply, just load an image
        mWebView.loadUrl("http://www.fanseries.fr/wp-content/gallery/allison-mack/allison-mack-01.jpg");
    }
}

It’s cool, isn’t it?

What I want to share with you all actually is that, sometimes we think too complicated while the solution is very simple right in front of our eyes. Just to see it with a clear point of view, you’ll find the best solution!

Hope you have a lot of fun in this article >^o^<

 

Cheers,

Pete Houston

 

Categories: Tutorials Tags: , , ,