Get all contacts who have mobile phone numbers

For querying all the contacts, you can query the RawContacts.CONTENT_URI directly. For getting all the phone numbers, you can query the RawContactsEntity.CONTENT_URI. For getting exact contact with the fixed phone number, you can query from PhoneLookup filter URI.

But if you only want to get all the contacts who have mobile phone numbers, usually you will need to query all the mobile phone numbers, and filter that to remove duplicate contacts (some contacts may have more than 1 mobile phone number). Or you can query all the contacts, and filter that to remove the contacts who have no mobile phone numbers.

Here’s my code that work:

private void test() {
    Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
                 new String[]{"_id"}, "contact_id in (select raw_contact_id from data where mimetype_id=5"
                +" AND data2="+CommonDataKinds.Phone.TYPE_MOBILE
                +" )"
                , null, null);
    while (c.moveToNext()) {
        Log.e("Evan",c.getString(0));
    }
    c.close();
}

I think putting “distinct(_id)” in the column strings should also work, and it’s easier.

关于Palm Pre – webOS的开发

这个题目好像起得大了点儿。北京又下雨,闲来无事,找了treo8里提供的webOS开发的英文原版书出来看了下。o’Reilly出品,质量保证,在Palm开发者网站上有免费提供第一章。这书据说是网友自己买来的,我不确定是不是对外都能看到并下载。

书里讲的还是比较入门的,概括来说,只讲了三方面内容 UI控件(3章),数据存储(1章),还有系统服务的调用(2章)。如预想的一样,完完全全的html开发,加上mojo。不能说我完全没做过html的开发,连js/dom我都还算是知道并写过些代码的。不过毕竟不是专长,也不是喜欢的方向,所以像mojo这种框架,我也是知道webOS后才知道的。个人是不太喜欢写html的,我也说不上来原因,总觉得为了页面布局的,总要反反复复调,不如做其它程序的GUI时来得干脆。可行方式又太多,总让你想不停去优化它。不过webOS毕竟是手机用的,总还是有不同的,看了书里一点点示例代码,相比起一些网页程序,总觉得还是清爽许多。毕竟一屏撑死也就放得下十来行按钮而已,又没有网页左左右右,上上下下的拖拉,布局该是容易多的。控件都是用<div />标签来定义的,如<div id=”my-toggle” x-mojo-element=”ToggleButton”></div>这种方式指定控件类型。突然想到,这上面自己的扩展不少,不知道开发环境里的代码补全是否好用。现在的新平台看起来可真雷同。控件这些和Android好像大同小异,连List设置adapter这些都让我觉得大方向上差不多。对List里面的某行,设置模板,就好像android上对ListAdapter.getView时,一般从一个资源里直接创建,就和模板一样嘛。

数据存储可以用cookie,Depot和HTML 5 storage方式,cookie和Depot都是存简单数据的,虽然上面说Depot可以存5M的数据。比较有意思的是,Depot存取数据时是异步的,这个好像我还没见过。文档里面对HTML 5 storage只提了一个openDatabase API,甚至连怎么建表都没提。不过给了个链接(http://dev.w3.org/html5/webstorage/#databases),这东西我也之后再琢磨吧。看起来也是sqlite之类的实现。不知道性能怎么样。数据访问这章还提及了Ajax来和web server交换数据。

系统服务这些比如提了怎么调用phone程序打电话,怎么查看一个联系人,怎么出个联系人选择对话框。本来我也觉得和Android上的Intent方式看起来挺像。不过有意思的是,在调用时,可以设置onSuccess和onFailure的回调函数。至少从结构上来看,方式是不同的。Android上用onActivityResult来处理调用后的结果。这两章还提及了像系统alarm service怎么用一类的。还有”Cloud Service”,很有噱头的名字。里面有个palm service叫palm://com.palm.pubsubservice,看起来和Apple家那个notification center好像是类似的主意吧。我不知道大家是同时都想到了呢,还是抄的呢?

很可惜的是,没有其它更深入的一些资源,比如没提及过native code怎么写。像classic模拟器那种自绘的View怎么做,难道用HTML代码生绘出来的?我可不信。。更加没有提及有没有输入法框架一类的。

期待Palm Pre SDK出来,到时再来给大家提供新情报。

Howto change the startup animation of Android OS

The statup animation means the one you see before the launcher application comes out.
It was a line with a red point goes from left to right again and again before, in Android 1.0. Currently, on cupcake, it’s a text string, “ANDROID”, with shine goes from left to right.
I found someone add an advertisement logo to that animation in a customized rom. So I studied how to remove that, or make a new one by myself.

Finally, I found it out. There are 2 pictures under frameworks/base/core/res/assets/images:
android-logo-mask.png
android-logo-shine.png

The 1st one is the background. Then, you can edit it with picture editor, like add your own name on it.
Well, after edited it, you need to build framework-res out, with command “make framework-res”. Then, you will get a new framework-res.apk. But usually, you can not use this file to replace the one in the rom/firmware for real devices, like G1, G2. Since, some resources are not under the same version.
What I do is pull out the original framework-res.apk from the rom. Unzip both this framework-res.apk and the one I made by myself. Replace the 2 pictures under assests/images with the one I made before. Notice, you can not replace the files with the PNG files you edited, but not compiled.
Then zip out framework-res.apk again, and sign with SignApk tool. Now you can use the new framework-res.apk to replace the original one.

Android platform study tips

Belows are some tips for getting/building/patching android source code.

1. Getting source
I’m not sure if you could download source code fast, if yes, skip this. But it took me more than 10 hours to finish getting the source code. The main git server limits to 3 connections from 1 IP address, so you can repo sync 3 projects together. For me, I start “repo sync”, “repo sync kernel” and “repo sync prebuilt” together, for the kernel and prebuilt projects are larger, and they could finish before repo sync touch them. Well, you can also start “repo sync external/webkit” first, for it’s not from the git server.

2. Building source

Added on 11-03, 2008. Today I installed a clean Ubuntu Linux 8.10, and did these steps to prepare the installation
$ sudo apt-get install git-core gnupg
$ sudo apt-get install flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl
$ sudo apt-get install zlib1g-dev
$ sudo apt-get install valgrind
(this step is optional, its size is about 22M)
$ sudo apt-get install python2.5
$ sudo apt-get install sun-java5-jdk
Warning: Do not use sun-java6-jdk,or you will meet below error when you do “make sdk”
Docs droiddoc: out/target/common/docs/dx
javadoc: error – In doclet class DroidDoc, method start has thrown an
exception java.lang.reflect.InvocationTargetException
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for
sun.util.resources.OpenListResourceBundle not found

In the document, it says run “make” in the root path of the android code. The result of “make” is generating system.img. For getting the whole runnable sdk, you can run “make sdk”. The sdk will be putted in out/host/YOUR_OS/sdk/. By the way, it’s said “make -j2″ may make it faster.
You can enable CCACHE with
export USE_CCACHE=1
Pls also set a cache dir with “export CCACHE_DIR=YOUR_CACHE_DIR”

After once make the whole projects, if you modify some code, you do not need to re-compile the whole projects with “make”. Run
$. build/envsetup.sh
in the android source code dir. And you can use “mmm” command to compile a project. Like “mmm packages/apps/Contacts”. Well, if you change some code like frameworks/base/core/java/android/widget/ListView.java, you need to run “mmm frameworks/base”, for only frameworks/base is a project, others are only part of this project. There would be a file named “Android.mk” under the root of a project.

Today, I tried to fix some bug in Contacts application. For testing the changes, if you run “make && make sdk” again, it would take a long time. You can easily do as I did:
1. Modify code
2. $mmm packages/apps/Contacts
If there’re compiling errors, go back to 1
3. make snod
With this command, it will build a new system.img very quickly.
4. $cp out/target/product/generic/system.img out/host/YOUR_OS/sdk/YOUR_SDK_PATH/tools/lib/images/
5. $out/host/YOUR_OS/sdk/YOUR_SDK_PATH/tools/emulator -wipe-data

It took about 2 minutes for me on my macbook pro with 2GHz CPU.
Well, you cannot use “make snod” for all the situations. It would not check the dependences. If you change some code in the framework which will effect other applications, please use a clean make.

3. Project layout
Most of the classes you see in the android sdk reference are in frameworks/base/core/java
And the resources like android.R.layout.simple_list_item_1 is defined in frameworks/base/core/res/res/layout

Build in applications are in packages/apps

To be continued..

Android源码研究Tips

从去年12月开始写Android程序开始,都差不多快一年了。上月Google终于公开了Android的较完整的源码。最近几天,闲来无事时就给他们提交一些补丁。

这里讲一些开发中的技巧,或许对大家有帮助,转载请注明出处:
下载源码
首先一个问题是http://source.android.com/在国内访问有时,甚至是大多时候,是有问题的。那么怎么翻墙就需要大家自己解决了。其实只是为了看源码的话,问题并不大,源码更新的站点还是可以正常连接的,但是如果有打算要提交patch,那么用tor都有些麻烦,我是找了个VPN出去的。获取源码请参照

http://source.android.com/download

嗯,我这里也可以给一些简单的步骤介绍吧:
1. 你的系统需要是Linux或者是Mac OS X。需要先安装git-core和gnupg,在Debian/Ubuntu Linux上,可以用apt-get获取,Mac上使用Macports吧。额外的,在mac上你需要先安装XCode 2.4以上版本,另外再用macports安装gmake, libsdl。 事实上编译期还会有问题,这在http://source.android.com/download页文档中没有提及,缺失ncursor和zlib库,也需要单独安装(这个在mail list里有讨论,或许我晚些可以补全些)。

2008-11-03 补 正好干净安装了Ubuntu Linux 8.10记录一下需要的东西
注意可能blog中会自动折行,请以$来区分新行
$ sudo apt-get install git-core gnupg (gnupg实际已自带)
$ sudo apt-get install flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl
$ sudo apt-get install zlib1g-dev
$ sudo apt-get install valgrind (可选,有21M大,我觉得一般人是用不到的)
$ sudo apt-get install python2.5 (实际上不用装,Ubuntu 8.10已经自带)
$ sudo apt-get install sun-java5-jdk
注意,不要用 sun-java6-jdk, 不然在make sdk,具体来说是make doc这一步中,遇到这个错误:
Docs droiddoc: out/target/common/docs/dx
javadoc: error – In doclet class DroidDoc, method start has thrown an
exception java.lang.reflect.InvocationTargetException
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for
sun.util.resources.OpenListResourceBundle not found

2. 下载repo脚本,放到/bin目录下,加上可执行权限
$ curl http://android.git.kernel.org/repo > repo
$ sudo mv repo /bin
$ sudo chmod a+x /bin/repo
其实文档中是把这个放在个人用户的~/bin目录下,但是要改PATH,我嫌麻烦而已就放/bin下了

3. 创建一个放置源码的文件夹,如叫myandroid。这里要注意下,需要区分大小写的分区,Linux下一般是用ext3的,是符合要求的,而mac下默认的分区是不区分大小写的,请自行创建分区。但是事实上,我以前测试过,在非大小写敏感分区上,直接make会报错,但修改Makefile后直接跳过检查这一步,最终是可以成功编译的。

4. 在myandroid目录中执行
$ repo init -u git://android.git.kernel.org/platform/manifest.git
中间会提示输入电子邮件什么的,如果你打算要提交patch的话,用google accounts注册过的邮箱

5. 在myandroid中执行 repo sync就可以开始下载源码了
但是这一步非常慢,这里有些文档中没有的技巧了:
测试发现,主站点是限三线程连接的,也就是说你可以同时开三个窗口做不同部分下载,
我建议是第一个窗口直接执行repo sync,第二个窗口执行repo sync kernel,第三个窗口执行repo sync prebuilt
因为kernel和prebuilt这两个工程比较大。
有时因为多次频繁连接,会有服务器端拒绝连接的错误,此时等两三分钟再重试就好了
repo的使用请看repo help。如果只是要学习源码的话,那只用repo sync一个命令就行。repo sync不带参数的话会更新所有子项目,可以repo sync project_path来指定更新项目。 那么project_path在哪可以找到呢?myandroid/.repo/manifests/default.xml (你至少需要先repo init过才有这些东西)

编译源码
当repo sync全部完毕时,进入myandroid目录,首先执行make,大约会耗时一个小时左右(在我的一代macbook pro上大概是这时间)。make最后会生成system.img。看到mail list里有人问这个干嘛用,其实是文档里没有完全写清楚。一般来说,我想普通人还需要的是make sdk这一步,会在myandroid/out/host/YOUR_OS/sdk中生成sdk。和从google官方下载下来的sdk差不多,可以直接运行了

在进行过一次完整的make后,以后对一些程序的修改,大可不必重新make sdk,因为make sdk实在太慢了。
先在myandroid目录下执行
$ . build/envsetup.sh
然后你就会多出几个可用的命令。在改了Contacts项目后,可以简单的执行mmm packages/apps/Contacts/来单独编译这个部分。为了可以直接测试改动,需要生成新的system.img,在myandroid目录下执行
make snod即可
当然,如果你改动的是emulator或者其它外围相关的,而非系统内部的东西,就不只是要重新生成system.img了

修改测试
这个周末,我提交了大约四到五个patch。主要是联系人程序相关的一些bug修正。如果你是想修改build in的程序,像联系人这样的,最好先在编译前把Contacts包删去,这样编译出来的system.img不带Contacts程序会方便很多,可以直接用eclipse开发Contacts程序,在这个模拟器上调试。像一般情况下,Contacts是带在system.img内的,对程序修改后想测试,比较麻烦,需要先运行emulator,然后adb uninstall掉原先的程序,再安装自己的,并且如果数据没卸干净,会有permission出错或uid不否而报错的情况。最最麻烦的是,每次重启emulator,build in的程序在被adb uninstall后又会出现,这个问题在真机上不会出现。所以我建议你直接去掉Contacts再编译个system.img出来,不过今天的几个修改都比较简单,我全部都是改一点就用mmm重新编译Contacts程序,然后用make snod来出一个新的system.img,覆盖掉sdk目录下旧有的文件,来测试。这个过程熟练了一般两分多钟可以测一把。

ZDic on sf.net move to svn source control

I found that I’m not familiar with cvs operation. Bobgreen@hi-pda had developed lots to improve the zdic. I guess it would be better to accept the patches to make this project alive.

Anyone who’d like to get ZDic source code, pls use a svn client to checkout source code from https://zdic.svn.sourceforge.net/svnroot/zdic/trunk/
In command line, you can use:
svn co https://zdic.svn.sourceforge.net/svnroot/zdic/trunk/
You will need both ZDic and ZDicFont project to compile the whole project.
ZDic with skin is putted in https://zdic.svn.sourceforge.net/svnroot/zdic/branches/ZDic_Skined

Hope it works for you.

Howto set FULL SCREEN on Android

If you only want to remove the title bar, add this line to onCreate() method of your activity:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

If you want to set your activity to use the whole display screen, which means also remove the status bar, you need to add one more line:

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NO_STATUS_BAR,
WindowManager.LayoutParams.FLAG_NO_STATUS_BAR);

Reference: http://groups.google.com/group/android-developers/msg/1d7497e5626896a7
Note: It’s for SDK-M3, the flag name changed in SDK-M5.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
 
public class FullScreenActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NO_STATUS_BAR,
        		WindowManager.LayoutParams.FLAG_NO_STATUS_BAR);
        setContentView(R.layout.main);
    }
}

Thanks to Ace’s comment. The code should be changed to

1
2
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                    WindowManager.LayoutParams.FLAG_FULLSCREEN );

移除小方块

再次贡献代码

QQ昵称里老有人喜欢加ascii 09之类以图排序在前面,可是手机上显示会是方块的,还有一些虽然也是GBK字符,可是在手机上显示不了,亦或者用户用的是GB,显示不了GBK扩展的内容,就移除好了

void RemoveUnprintChars(char * str,Boolean removeNewLine)
{
UInt16 i,len;
UInt8 chW;
WChar ch;
char * p=str;
if (str==NULL)
return;
len=StrLen(str);
i=0;
while (i < len) {
chW= TxtGetNextChar(str, i, &ch);
if (TxtCharIsPrint(ch)
|| (!removeNewLine && ch!='\n') )
{
if (chW==1)
*p++=ch;
else
{
*p++=ch/256;
*p++=ch%256;
}
}
i+=chW;
}
 
*p=0;
}

带报错的atoi

好久没写东西了,随便填上一段代码

这个QQ烦死了,我自己用了一个钟头,不停在后台运行聊天,都一点问题没有。不过还是有不少人有重启问题,不得不把各种检查都做细致来

上一段代码替换StrAToI,不过里面有对%1a这种处理,这是QQ协议中带的,一般大家用不到就去了这一段吧,那其实result可以换个变量名:)

 
Int32 myAToI(const Char *str)
{
UInt8 result;
if (str==NULL || StrLen(str)==0)
{
ErrDisplay("严重异常:转换空数字串,请退出");
return 0;
}
if (str[0]=='%')
{
if (StrLen(str)!=3)
{
ErrDisplay("严重异常:数字转换出错%,请退出");
return 0;
}
if (str[1]>='a')
result=str[1]-'a'+10;
else
result=str[1]-'0';
result*=16;
if (str[2]>='a')
result+=str[2]-'a'+10;
else
result+=str[2]-'0';
return result;
}
if (!TxtCharIsDigit(str[0]) && str[0]!='-')
{
ErrDisplay("严重异常:数字转换出错1,请退出");
return 0;
}
for (result=1;result<StrLen(str);result++)
if (!TxtCharIsDigit(str[result]))
{
ErrDisplay("严重异常:数字转换出错2,请退出");
return 0;
}
return StrAToI(str);
}

Palm Dev 随笔(2.11)

接下来会常有这种小文章了,可惜国内开发者不多,不然我估计会有不少人有兴趣的,哈哈:P 自我感觉良好一下

嗯,今晚的要收工。写点刚才一个钟头里遇到的一些东西:

1. 打开网络设置面板

LaunchWithCommand(sysFileTPanel, sysFileCNetworkPanel,sysAppLaunchCmdPanelCalledFromApp, NULL);

2.断开网络连接

HsNetworkDropConnection();

3.退出当前程序

这个我以前试过直接调AppStop()似乎也可以,就是FrmCloseAllForms()啦,不过我觉得逻辑上有点说不通,因为AppEventLoop()没道理退出啊。

现在一般我就加个全局变量ForceStop,默认为false,

改AppEventLoop那行为

while (event.eType != appStopEvent && !ForceStop);

有时不能用全局量,或者某些特定情况,直接自己加个appStopEvent到事件对列就好了

竟然才三点,其它的没什么好写了。哦,还有把680模拟器中文化。。。嗯,这个懒得说了

Next Page »