Archive

Archive for December, 2011

Android XML Adventure – Parsing XML using XPath

December 24, 2011 4 comments

Article Series: Android XML Adventure

Author: Pete Houston (aka. `xjaphx`)

TABLE OF CONTENTS

  1. What is the “Thing” called XML?
  2. Parsing XML Data w/ SAXParser
  3. Parsing XML Data w/ DOMParser
  4. Parsing XML Data w/ XMLPullParser
  5. Create & Write XML Data
  6. Compare: XML Parsers
  7. Parsing XML using XPath
  8. Parsing HTML using HtmlCleaner
  9. Parsing HTML using JSoup
  10. Sample Project 1: RSS Parser – using SAXParser
  11. Sample Project 1: RSS Parser – using DOM Parser
  12. Sample Project 1: RSS Parser – using XMLPullParser
  13. Sample Project 2: HTML Parser – using HtmlCleaner
  14. Sample Project 2: HTML Parser – using JSoup
  15. Finalization on the “Thing” called XML!

=========================================

XPath is a syntax to query directly to the specified tag by name or id or using any pre-defined function to detect the nodes. It’s really useful when it’s coming to parse a lot of data in the form of an array.

You might want to study some about XPath first: W3Schools – XPath Tutorials

Specifically, we will apply XPath in Android platform, there’s a library also named XPath, pre-package in Android framework. Reference to Android XPath Library.

The usage of XPath is pretty much simple:

1. Create a `InputSource` object, from a `String`, from a `InputStream`, from `Resources`, from `Assets` ….

2. Create a `XPath` object

3. Define your XPath expression, which is a `String`.

4. Evaluate above expression from `InputSource` created at step 1.

5. Query data retrieved from evaluation.

That’s it. Here go for an example, I’ll have the following XML file, `data.xml`, and put into `/res/raw` folder.

<?xml version="1.0" encoding="UTF-8"?>
<sample>
	<info>
		<title>Using XPath to parse XML</title>
		<author>Pete Houston</author>
	<list>
		<person id="1">
			<name>Pete Houston</name>
			<age>28</age>
		</person>

		<person id="2">
			<name>Nina Jones</name>
			<age>27</age>
		</person>

		<person id="3">
			<name>Yumin Hanazuki</name>
			<age>22</age>
		</person>
	</list>
	</info>
</sample>

Following above 5 steps, including display data to UI.

package pete.android.tutorial.xml.xpath;

import java.util.ArrayList;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class XPathStudyActivity extends ListActivity {
    // data
	ArrayList<String> mPeople = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
        	parseData();
        } catch(Exception ex) {
        	Toast.makeText(this, "Exception: " + ex.getMessage(), Toast.LENGTH_LONG).show();
        }

        // pass adapter w/ data queried through XPath to ListView
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPeople);
        setListAdapter(adapter);
    }

    private void parseData() throws Exception {
    	// create an InputSource object from /res/raw
    	InputSource inputSrc = new InputSource(getResources().openRawResource(R.raw.data));
    	// query XPath instance, this is the parser
    	XPath xpath = XPathFactory.newInstance().newXPath();
    	// specify the xpath expression
    	String expression = "//name";
    	// list of nodes queried
    	NodeList nodes = (NodeList)xpath.evaluate(expression, inputSrc, XPathConstants.NODESET);

    	Toast.makeText(this, "count: " + String.valueOf(nodes.getLength()),Toast.LENGTH_SHORT).show();
    	// if node found
    	if(nodes != null && nodes.getLength() > 0) {
    		mPeople.clear();
    		int len = nodes.getLength();
    		for(int i = 0; i < len; ++i) {
    			// query value
    			Node node = nodes.item(i);
    			mPeople.add(node.getTextContent());
    		}
    	}
    }
}

The above sample, I’ve tried to query all `name` XML tag from `data.xml` file and display on the list. Very simple usage w/ XPath library!

Have fun,
Pete Houston

Advertisements
Categories: Tutorials Tags: , , , , , ,

Retrieve all secrect codes from your Android devices

December 21, 2011 Leave a comment

In my previous post about list of Google Android Secret Codes, you may find them not working in every Android devices.

So the question is how to know the secrect codes in a specific Android phone?

After searching for a while, I’ve found the could-be answer, at the following link: XDA-Developers – Android’s secret codes

1. First, root your phone

2. Secondly, create this bash shell script, name it, maybe: “secrets.sh

#!/bin/bash
echo -n &gt; apks.txt
echo -n &gt; output.txt
for x in `find . -name "*.apk"`
do
unzip -p $x | strings | grep -i "android.provider.Telephony.SECRET_CODE"
if [ $? -eq 0 ]; then
echo $x &gt;&gt; apks.txt
fi
done
for x in `cat apks.txt`
do
mkdir "$x.folder"
mv $x $x.folder
done
for x in `cat apks.txt`
do
unzip -d $x.folder $x.folder/$x
done
for x in `cat apks.txt`
do
bin2xml $x.folder/AndroidManifest.xml &gt; $x.folder/AndroidManifest.decoded.xml
done
for x in `find . -name "AndroidManifest.decoded.xml"`
do
echo "$x:" &gt;&gt; output.txt
cat $x | grep -i host | sed 's/android:host=\"/*#*#/' | sed 's/\" \/&gt;/#*#*/' | sed 's/\t\t\t\t\t//' &gt;&gt; output.txt
done

3. Download this script: http://android-random.googlecode.com/files/axml2xml.pl

and rename it to “bin2xml.pl“, then put somewhere in your Android phone $PATH.

4. Run the following commands:

chmod a+x secrets.sh
./secrets.sh
cat output.txt

5. Read your output and try it out!

 

Have fun!

Pete Houston

Categories: Tricks & Tips Tags: , , , ,

Google Android Phone Secret Codes

December 21, 2011 2 comments

A really interesting discovery, I’ve found on this page:

Justin.My – Google Android Phone Secret Codes

and

AskVG – Android Phone Secret Codes

These codes have been obtained by decoding various .apk files present in the phone firmware.

*#*#4636#*#*


This code can be used to get some interesting information about your phone and battery. It shows following 4 menus on screen:

  • Phone information
  • Battery information
  • Battery history
  • Usage statistics

*#*#7780#*#*


This code can be used for a factory data reset. It’ll remove following things:

  • Google account settings stored in your phone
  • System and application data and settings
  • Downloaded applications

It’ll NOT remove:

  • Current system software and bundled applications
  • SD card files e.g. photos, music files, etc.

PS: Once you give this code, you get a prompt screen asking you to click on “Reset phone” button. So you get a chance to cancel your operation.

*2767*3855#

Think before you give this code. This code is used for factory format. It’ll remove all files and settings including the internal memory storage. It’ll also reinstall the phone firmware.
PS: Once you give this code, there is no way to cancel the operation unless you remove the battery from the phone. So think twice before giving this code.

*#*#34971539#*#*

This code is used to get information about phone camera. It shows following 4 menus:

  • Update camera firmware in image (Don’t try this option)
  • Update camera firmware in SD card
  • Get camera firmware version
  • Get firmware update count

WARNING: Never use the first option otherwise your phone camera will stop working and you’ll need to take your phone to service center to reinstall camera firmware.

*#*#7594#*#*

This one is my favorite one. This code can be used to change the “End Call / Power” button action in your phone. Be default, if you long press the button, it shows a screen asking you to select any option from Silent mode, Airplane mode and Power off.
You can change this action using this code. You can enable direct power off on this button so you don’t need to waste your time in selecting the option.

*#*#273283*255*663282*#*#*

This code opens a File copy screen where you can backup your media files e.g. Images, Sound, Video and Voice memo.

*#*#197328640#*#*

This code can be used to enter into Service mode. You can run various tests and change settings in the service mode.
WLAN, GPS and Bluetooth Test Codes:
*#*#232339#*#* OR *#*#526#*#* OR *#*#528#*#* – WLAN test (Use “Menu” button to start various tests)
*#*#232338#*#* – Shows WiFi MAC address
*#*#1472365#*#* – GPS test
*#*#1575#*#* – Another GPS test
*#*#232331#*#* – Bluetooth test
*#*#232337#*# – Shows Bluetooth device address

*#*#8255#*#*

This code can be used to launch GTalk Service Monitor.

Codes to get Firmware version information:

*#*#4986*2650468#*#* – PDA, Phone, H/W, RFCallDate
*#*#1234#*#* – PDA and Phone
*#*#1111#*#* – FTA SW Version
*#*#2222#*#* – FTA HW Version
*#*#44336#*#* – PDA, Phone, CSC, Build Time, Changelist number

Codes to launch various Factory Tests:
*#*#0283#*#* – Packet Loopback
*#*#0*#*#* – LCD test
*#*#0673#*#* OR *#*#0289#*#* – Melody test
*#*#0842#*#* – Device test (Vibration test and BackLight test)
*#*#2663#*#* – Touch screen version
*#*#2664#*#* – Touch screen test
*#*#0588#*#* – Proximity sensor test
*#*#3264#*#* – RAM version
NOTE: All above codes have been checked on Google Android phone Samsung Galaxy I7500 only but they should also work in other Google Android phones.

Also, some additional secret codes, found on Android-DLS – Secret Codes

  1. Display IMEI:
    *#06#
  2. Voice Dialer Logging Enabled
    *#*#8351#*#*
  3. Voice Dialer Logging Disabled
    *#*#8350#*#*
  4. Phone Setting
    *#*#4636#*#*
  5. FieldTest
    *#*#7262626#*#*
  6. PUK Unlock (from emergency dial screen)
    **05*<PUK Code>*<enter a new pin>*<confirm the new pin>#

Some secret code specifically for Samsung Galaxy SII: *#0*#

 

Enjoy!

Pete Houston

Categories: Tricks & Tips Tags: , , , ,

Android Graphics True Facts

December 12, 2011 Leave a comment

I’ve just got something interesting about Android Graphics performance, it’s worthy to read it.

– From Andrew Munn, an intern student at Google Android.

– From Dianne Hackborn,

 

Categories: Of Diary

Launch camera programmatically

December 7, 2011 Leave a comment

If you’ve ever wanted to launch camera programmatically, this snippet will do.

Intent i = new Intent();
i.setAction(Intent.ACTION_CAMERA_BUTTON);
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_CAMERA));
sendOrderedBroadcast(i, null);

Have fun,
Pete Houston

Categories: Tricks & Tips Tags: ,

QuickCam – A Quick Shortcut to Record Video

December 7, 2011 Leave a comment

This is a tool for quickly opening recording mode on Android phones, if you’ve ever gotten tired of launching camera and switching to recording manually everytime.

[Application Information]

Application: QuickCam

Latest version:  v1.1

Compatibility: 1.5 or higher

Description:

+ Launch quickly to recording camera mode

+ A widget size 2×1

Download:

Click me to get from Repo

or Scanning QR Code

Get QuickCam to Your Phone Now!

QuickCam Download

Screenshots:

QuickCam Widget

QuickCam Widget

Changelog:

[2011-Dec-07]

+ first release

+ add shortcut to application screen

+ add widget (size 2×1)

Enjoy,

Pete Houston

Categories: Applications Tags: , , , ,

Get an on-hand device: Samsung Galasy SII

December 7, 2011 Leave a comment

After long time waiting, I’ve had my chance to take own of the one I desire, the Samsung Galaxy S II – I9100.

Just testing and using it for several days, it’s really working great beyond my imagination.

Certainly, there will be more tutorials and articles on my blog from now on.

Categories: Of Diary Tags: , , , , ,