Show Where You Are on Wordpress

By zxhmike, March 25, 2012 12:58 am

My friends in Shanghai are happy to know where I am in US. This can be done via Google Latitude.

Log onto Google Latitude, press the gear icon on the top right and choose "Application settings".

Choose the options and paste the code to your blog post. Like this:


You can also put the location badge on the text widget of Wordpress, since a text widget is actually a hyper text widget which you can insert HTML code into. Thus, this badge will appear on every post.

Buy Your Girlfriend A Unique Gift -- Android with Customized OOBE

By zxhmike, March 18, 2012 8:41 pm

What about a brand new Android phone as a birthday gift for your girlfriend? It is cool if she sees something different when she opens the box and turn on the device for the first time, isn't it? If you are happy with this idea, let's go.

WARNING: SOME MODIFICATIONS MADE TO YOUR PHONE IN THIS TUTORIAL MAY CAUSE YOU LOSE YOUR WARRANTY AND EVEN BRICK YOUR PHONE. DO IN YOUR OWN RISK.

Step 1. Prepare your phone for system modification.

For an HTC phone, S-OFF your phone to turn off the runtime system protection.
Ref: http://revolutionary.io/

At the end of the S-OFF procedure, you will be asked about flash the ClockworkMod recovery image. Good to do so to get a customized recovery program which is easy to use. Better "root" your phone to gain max control over your phone.
Ref: http://unrevoked.com/

Actually, the most significant step in the "rooting" procedure is installing the "su" command into the system. Your can simply download an "su" ARM binary from a trusted source or even compile your own, than copy it into the /system/bin directory. There are plenty of information about S-OFF and "rooting" your phone, so I don't want to spend more words here.

Step 2. Create your own OOBE app.

OOBE, or Out of Box Experience, is a run-once application to guide you get your phone provisioned. Have you mentioned that the first time you boot up your Android phone, you are asked to fill in information about your Google account and do some settings to customize your phone. It disappears after all set. What we wanna do here is to insert our own app before this OOBE procedure is started. Let's start with create a normal app first and then do some tricks on it to meet the requirement.

Tons of books are discussing Android application development today. Simply grasp any of them and you can easily create a very compact application with only one activity, showing a bunch of roses with some nice words over it. Simple but full of love. I created my own app for example.

Application Name: HappyBirthday
Package Name: com.zxhmike.happybirthday
Default Activity: HappyBirthdayActivity

Just do anything you want in the HappyBirthdayActivity. Usually some pictures are added to the related "main.xml" located in the /res/layout directory of the project.

Now here is the trick.

Add an intent to the HappybirthdayActivity in the file AndroidManifest.xml. (Line 17 - 21)


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zxhmike.happybirthday"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HappyBirthdayActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter android:priority="2">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.HOME"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

The intent filter [Action: Main Category:Launcher] (Line 15 - 18) is only necessary in debug. Delete it before uploading the app to the phone.

Add a button labeled "I Love You" or anything else to the "main.xml" under "res/layout" directory. This screen will disappear and "never" come back when your girlfriend clicks it. Add the following code to the activity.

package com.zxhmike.happybirthday;

import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HappyBirthdayActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final PackageManager pm = getPackageManager();
        final ComponentName cn = new ComponentName(getApplicationContext(), this.getClass());

        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				pm.setComponentEnabledSetting(cn, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
			}
		});
    }
}

Step 3. Compile your app and upload to your device.

Compile your app. If using Eclipse with ADT, the app will be automatically uploaded to your phone or emulator. When the "Home" button is pressed, a dialog will show to let you choose either switch to the launcher app or the "HappyBirthday" app. It is wield this stands out even our app is in a higher priority. This is because our app is not put into "/system/app" directory. Let's upload our app to this directory instead.

adb remount
adb push HappyBirthday.apk /system/app

Use the following code instead if the first command above doesn't work.

adb shell
su
mount -o remount,rw /system
exit

Step 4. Try it!

Time to reboot the phone. The highly customized OOBE will show. But it disappears "forever" once clicked the "I Love You" button.

TIP: use the "pm" command to get it back.

pm enable com.zxhmike.HappyBirthday/.HappyBirthdayActivity

We are all set. Enjoy! If you are willing to know how this is happening behind the scene, follow up!

How could this happen?

Look back to the "AndroidManifest.xml"

The intent filter [Action: Main Category:Home, DEFAULT] is the same intent as the one to switch to your home screen. The system app "launcher", which is in charge of the home screen and widget loading, contains the same intent filter in its "AndroidManifest.xml".

When the phone first boots, the Android system doesn't know that this is the first time it sees the world, and it sends out the intent "intended" to launch the home screen. Unfortunately, the OOBE app takes the handle because it also holds the same intent filter [Action: Main Category:Home, DEFAULT] in its "AndroidManifest.xml" with higher priority of "1". After the phone is provisioned (That means you set the time, chose the language ...), the OOBE app disables itself. The next time the Android system sends out the intent [Action: Main Category:Home, DEFAULT], the home screen is showed instead, which is the usual case we see.

With a higher priority "2" in the intent filter for [Action: Main Category:Home, DEFAULT], our homemade OOBE app is launched even before the default OOBE app when the phone first starts. This is what the trick is.

Then, a few words on how to disable the app itself. In short, use the PackageManager API, as in the code.

Finally, why should the app be put into the "/system/app" directory? Because priority doesn't count if it is not a system app. See the source code of Android:

// ICS 4.0.1 PackageManagerService.java Line 4362
if (!systemApp &amp;&amp; intent.getPriority() &gt; 0 &amp;&amp; "activity".equals(type)) {
    intent.setPriority(0);
    Log.w(TAG, "Package " + a.info.applicationInfo.packageName + " has activity "
        + a.className + " with priority &gt; 0, forcing to 0");
}

The priority is forced to zero if it is not a system app which should be located in "/system/app" directory.

It evolves some Android source code reading and app reverse engineering to get there. Next time I will introduce basic reverse engineering of Android app. Like Linus said, "Read the f** source code!"

ENJOY!

Mike Zou

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Running Android in VirtualBox and More

By zxhmike, March 16, 2012 10:30 am

Playing with Android internals is fun.

Sometimes you read Android source code and want to confirm your digest. My approach is to add your own logging into the source codes and run it in an emulator. You will see the log in "adb logcat" if it is in the right path. Not only is it faster than flashing the image into a physical device, but also can we play with the newest Android distribution. However, booting up Android in the default emulator is really an annoying procedure, usually even slower than the incremental compilation after slight modification on the source code.

Recently, I discovered the "vbox" target when was about to compile Android. This opens a door to get faster experience, since VirtualBox is virtualization software rather than a processor emulator like QEMU, which the emulator in Android SDK is based on. Virtualization is much faster than emulation because it leverages host machine's X86 CPU directly. Thus, Android boots in just a few seconds in VirtualBox.

I can't wait to start now.

Version Info:

  • Host: Ubuntu 10.04 LTS X64
  • VirtualBox: 4.1.10
  • Android: ICS 4.0.1

Step 1. Get VirtualBox ready.

Ref: http://www.virtualbox.org/

Step 2. Download Android source.

Ref: http://source.android.com/source/downloading.html

Follow the instructions on the page and get the source code. Since Android source is huge, it takes a while to download and may be interrupted due to network connection issues, the following script may be useful to reconnect when this happens.

#!/bin/bash
PATH=./bin:$PATH
repo init -u git://android.git.kernel.org/platform/manifest.git -b master
repo sync
while [ $i = 1 ]; do
echo "==== sync failed, re-sync ===="
sleep 3
repo sync
done

Step 3. (Optional) Add mouse support.

The default configuration only supports touch screen device, so we have to add mouse support to gain more convenience to play with Android in VirtualBox. We build a new kernel in a new directory:

git clone https://android.googlesource.com/kernel/goldfish
cd goldfish
git checkout origin/android-goldfish-2.6.29
cp arch/x86/configs/vbox_defconfig .config
make ARCH=x86 menuconfig

To include mouse support:

  • Select/Expand ‘Device Drivers’
  • Select/Expand ‘Input Device Support’
  • Include ‘Mice’

Then build the kernel and copy it to the Android source directory.

make ARCH=x86
cp /PATH/TO/KERNEL/SOURCE/goldfish/arch/x86/boot/bzImage /PATH/TO/ANDROID/SOURCE/prebuilt/android-x86/kernel/kernel-vbox
cp /PATH/TO/KERNEL/SOURCE/goldfish/vmlinux /PATH/TO/ANDROID/SOURCE/prebuilt/android-x86/kernel/vmlinux-vbox

Step 4. (Optional) Add busybox support.

Default equipped "toolbox" in Android is too simple to play with. We try more advanced "Swiss knife" busybox instead. Download pre-build X86 binary from busybox official site and include it into Android source.

mkdir -p /PATH/TO/ANDROID/SOURCE/prebuilt/android-x86/busybox
cd /PATH/TO/ANDROID/SOURCE/prebuilt/android-x86/busybox
wget http://www.busybox.net/downloads/binaries/latest/busybox-i686
mv busybox-i686 busybox

Create an Android.mk file in the directory so the build system will acknowledge a new component being added.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := busybox
LOCAL_MODULE := busybox
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_MODULE_TAGS := eng
include $(BUILD_PREBUILT)

Step 5. Compile the source.

Change to the root directory of the source code. Switch to the vbox target and compile.

source build/envsetup.sh
lunch vbox_x86-eng
make android_disk_vdi

To "make android_disk_vdi" will involve VirtualBox command "VBoxManage", so be sure to install VirtualBox in advance. It takes hours to build the image depending on your machine.

Step 6. Create a new virtual machine in VirtualBox. Create a new virtual machine in VirtualBox with the following suggested settings:

  • OS: Linux/Other Linux
  • Memory: 512 MB
  • Processor: Enable PAE/NX
  • Network: Bridged adapter (Optional. Choose other connection types as you wish.)
  • Hard Disk: choose the vdi file just created. (See the output of the last step of compiling.)
  • Serial Port: Create pipe: /PATH/TO/HOME/ics_pipe

There you go! A freshly installed, fast responding Android running in VirtualBox is ready to enjoy. You may debug the system via either virtual serial port or TCP/IP network.

socat unix-client:/PATH/TO/HOME/.ics_pipe stdout

or

ADBHOST=1.2.3.4 adb shell

We are almost there. After that, each time modifications are made in source and compiled by "make android_disk_vdi" command, a vdi file is generated. However, it is not automatically updated in the virtual machine created before, because a virtual machine will stamp a unique UUID for each storage device attached to it to make sure the disk is used exclusively. Thus, we need to acknowledge the virtual machine the update of the virtual disk manually.

VBoxManage storageattach VIRTUAL_MACHINE_NAME --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /PATH/TO/ANDROID/SOURCE/out/target/product/vbox_x86/android_disk.vdi

Time to start the machine...

ENJOY!

Mike Zou

References:

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

ZZ: 沪闵路的历史

By zxhmike, December 27, 2011 3:02 am

沪闵路,上海徐家汇至闵行的一条重要道路。这条道路历经八十载,其中的故事真不少呢!

李英石集资建造沪闵路

李英石(1882——1933)清末毕业于日本东京陆军士官学校。辛亥革命期间,他参加光复起义,率700名敢死队员攻下江南制造局和上海道台衙门;上海光复后,任沪军都督府军务部长兼沪防水陆军统领。1912年孙中山就任临时大总统时任命他为南京警备司令。
因受国民D内一些人的排挤,他愤而退官回家乡闵行隐居。 李英石回到老家后,有人向他提出,闵行交通不便,光靠黄浦江水路运输无法把上海的大商业吸引过来,李英石大胆设想,从闵行到上海建造一条汽车路。众人一听,七嘴八舌地议论开了,资金哪来?……就在大家一筹莫展时,一位曾在上海钱庄做过帐房先生的老叟胸有成竹地说:“凭阿显(李英石小名)的名望,只须晓之以理,动之以情,发动民众,有钱出钱,有力出力,何愁筑不成路!”这位族叔的话,使李英石开了窍。于是,他从闵行到徐家汇,为募集筑路资金而奔忙着。经过几个月的奔走,终于筹措到开工筑路的资金,沿路乡绅们还无偿地捐献了筑路的土地。 经过艰苦努力,这条从闵行黄浦江渡口开始途经北桥、颛桥、钱粮庙、朱家行、梅家弄、漕河泾、土山湾全长23公里的汽车路,到1922年12月2日终于筑成。众人提议,把这条路命名为“英石路”。但李英石不允,说:“我一不为名,二不为利,用我的名字作路名不妥,还是叫‘沪闵路’为好。” 沪郊最早通公交 沪闵路筑好了,申请“沪闵长途公共汽车公司”的执照也批下来了,但由上海华商证券公司出面向英国史蒂文工厂订购的12辆公共汽车,要等待半年以后才能到达。事有凑巧,这时,上海有家杏生汽车公司歇业。杏生汽车公司的老板是宁波籍的上海房地巨商董杏生,他于1922年6月向公共租界工部局申请静安寺到兆丰公园(今中山公园)的公共汽车获准,这是上海最早的公共汽车,但运行时只有两辆汽车。当时静安寺到兆丰公园往返的人不多,由于乘客少,入不敷出,加上董杏生主业是房地产,办公共汽车只是小试牛刀。因而,运行不到半年就停驶了。 李英石打听到董杏生有两辆公共汽车闲置着,心想,何不租借过来,沪闵长途汽车就可通车了。董杏生听了李英石集资筑沪闵路和创办沪闵长途汽车公司而眼前急需公共汽车时,双手一拱说:“这样吧,这两辆公共汽车反正闲着没用,干脆捐献给沪闵长途汽车公司,算是董某的一份礼物!” 于是,沪闵长途公共汽车提前在1922年12月30日通车。它是上海郊区最早通公共汽车的日子。
Continue reading 'ZZ: 沪闵路的历史'»

Install Java from Sun

By zxhmike, December 15, 2011 12:41 pm

It is easy to install JDK in Debian Linux by simply typing apt-get commands, but to get the latest version, it is recommended to download it from Sun website.

The downloaded file is a bin executable. Run it as root and JDK will be installed. However, when type "java -version", you will find it is still in the old version. You have to "update-alternative" manually, which is an annoying task. On the other hand, we can go to the sun-java6-jdk deb file for help. Open the "postinst" file in it and all commands are seen. It is here after a little tweak. Next time you install a new version of JDK, run the code!

priority=1061
basedir=/opt/jdk1.6.0_29
basediralias=/opt/jdk1.6.0_29
mandir=/opt/jdk1.6.0_29/man
jdiralias=jdk1.6.0_29
srcext=1
dstext=1
jdk_tools='appletviewer apt extcheck idlj jar jarsigner javac javadoc javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc'
 
for i in $jdk_tools; do
        unset slave1 slave2 || true
if [ -e $mandir/man1/$i.$srcext ]; then
         slave1="--slave
                /usr/share/man/man1/$i.$dstext
$i.$dstext
$mandir/man1/$i.$srcext"
        fi
if false &amp;&amp; [ -e $mandir/ja/man1/$i.$srcext ]; then
         slave2="--slave
                /usr/share/man/ja/man1/$i.$dstext
${i}_ja.$dstext
$mandir/ja/man1/$i.$srcext"
        fi
update-alternatives
--install
/usr/bin/$i
$i
$basediralias/bin/$i
$priority
         $slave1 $slave2
done

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

在Wordpress上贴代码之sed脚本

By zxhmike, December 7, 2011 7:26 am

研究了一下Wordpress的贴代码功能,将代码放在<code>标签下就可以了,但是效果并不理想,于是写了下面这个脚本:
从标准输入读取代码进行转换然后拷贝到剪贴板,并在/tmp下做个备份。这样,代码就可以直接贴到博客中,格式也不错。注意要贴到HTML视图中,而不是Visual视图中。

sed 's/&gt;/\&amp;gt\;/g; s/&lt;/\&amp;lt\;/g; s/\t/\&amp;nbsp\;\&amp;nbsp\;\&amp;nbsp\;\&amp;nbsp\;\&amp;nbsp\;\&amp;nbsp;\&amp;nbsp\;\&amp;nbsp\;/g; s/^\s*$/\&lt;br\&gt;/g; 1s/\(.*\)/\&lt;code\&gt;\1/; $s/\(.*\)/\1\&lt;\/code\&gt;/;' | tee /tmp/wordpress.out | xsel -bi

1. <> 替换为 &lt; &gr;
2. TAB替换为8个空格
3. 空行替换为<br>
4. 头尾加<code></code>标签

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

成功转移博客到七十二松

By zxhmike, December 4, 2011 1:15 pm

感谢老同学ZJY从Linkedin顺藤摸瓜光临zxhmike.wordpress.com.cn,并指出该空间中的图片均无法显示。我查看以后发觉确实所有图片均丢失,看他人博客听说可能是由于网站方数据迁移中的失误。还好我有备份数据的习惯,而且有一个小脚本自动下载博客中的图片。所以,通过wordpress.com.cn导出的xml文件,并用sed将其中的图片地址改为新地址后导入72pines,完成迁移。顺便将以前Yo2上的博文也迁于此。

七十二松是一个优秀的Wordpress博客平台。相对于前两者,名字更有诗意,使用更清新,对网站内容导入导出有很好的支持。当然,也感谢wordpress.com.cn和Yo2先前为我提供免费的博客空间。

新的博客地址为http://zxhmike.72pines.com/
也可访问http://www.zxhmike.com/
但这就是一个301重定向而已。我的域名服务商美橙不支持域名转发,并且七十二松的域名绑定确实不稳定,姑且只能这样。

最终我还是回到了Wordpress平台的博客。微博有字数限制,人人过于嘈杂,还是这里有更大的自由能够创出一片天地。欢迎大家到新博客来坐坐,和我聊聊。也请提供宝贵的意见和建议。

[Solved] USB mount error under Debian Squeeze

By zxhmike, June 25, 2011 12:55 pm

When I insert my 250 GB hard drive into my netbook running Debian Squeeze, I was indicated:

Error mounting: mount exited with exit code 1: helper failed with:
mount: wrong fs type, bad option, bad superblock on /dev/sdb1,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail  or so

This won't happen on my laptop, whose Debian was installed with a CD.

However, the Debian on the netbook was installed with a USB disk, so there was a line in /etc/fstab reads:

/dev/sdb1 /media/cdrom0 bla bla bla

This is used while installation to redirect the installer from reading the CD-ROM to the USB disk, but it was not deleted automatically after installation, so it will override the behavior of gnome nautilus and auto-mounting will fail.

So, simply delete this line and everything will work fine.

For your information if you are faced with the same problem.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

学习GNU development tools

By zxhmike, April 5, 2011 7:44 pm

Rabbit项目已经开始垒码,需要进行项目管理。今天在看这方面书籍,发现还是GNU官网上的指南最直接了当,特此推荐。
http://autotoolset.sourceforge.net/tutorial.html

试讨论解决小游戏《钉子户大战拆迁队》的算法

By zxhmike, March 22, 2011 3:31 am

今天手贱玩了个小游戏《钉子户大战拆迁队》。简单说就是有6个人物保卫家园,他们各有攻击速度和攻击数值,还可以花钱升级,攻击数值增加但是速度不 变。拆迁队也有很多人物,也有自己的速度和生命值,打死一个可以获得一笔钱。阳台上最多只能站4个人进攻拆迁队。试求一种算法,根据当前来的拆迁队决策选 择加入合适人物或者升级某个人物。可以用贪心或者动态规划吗,或者是个NP完全问题吗?如果可以知道以后有哪些拆迁队的人来,这还是个NP完全问题吗?如 果是,有什么近似算法吗?太晚了,我稍微想想,暂时没有什么思路。

注:为简化规则,不允许使用丁老爸丢燃烧瓶和丁小没丢爆竹,因为这是多人伤害,会导致问题过于复杂。过前六关是用不着这两个人的。经观察,钉子户总是先攻击拆迁队第一个人。

可以去玩玩,对这个问题提出你的想法:

http://www.yx8.cn/11/12697.html

我是Ubuntu,没有显示有病毒。

dingzihu_fighting

Panorama Theme by Themocracy