网上有一篇文章介绍了Android的属性系统(http://blog.csdn.net/tekkamanitachi/archive/2009/06/18/4280982.aspx),我现在对其进行补充:
1. android现在好像只有/ default.prop,/system/build.prop两个文件,别的两个文件找不到
2. /data/property下有4个prop文件:persist.sys.timezone,persist.sys.language,persist.sys.country,persist.sys.localevar,里面保存着属性名称以“persist.”开头的属性值。
3. libcutils的源码位于system\core\libcutils\下面,获取和设置属性的代码在properties.c里面,读取属性通过读共享内存得到,设置属性通过发送请求到property_service进行设置。
4. 相关的函数有property_set,property_get,property_list。
5. 属性系统的初始化通过__system_properties_init函数完成,__system_properties_init函数代码如下:
view plaincopy to clipboardprint?
int __system_properties_init(void)
{
prop_area *pa;
int s, fd;
unsigned sz;
char *env;
if(__system_property_area__ != ((void*) &dummy_props)) {
return 0;
}
env = getenv("ANDROID_PROPERTY_WORKSPACE");
if (!env) {
return -1;
}
fd = atoi(env);
env = strchr(env, ',');
if (!env) {
return -1;
}
sz = atoi(env + 1);
pa = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
if(pa == MAP_FAILED) {
return -1;
}
if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
munmap(pa, sz);
return -1;
}
__system_property_area__ = pa;
return 0;
}
int __system_properties_init(void)
{
prop_area *pa;
int s, fd;
unsigned sz;
char *env;
if(__system_property_area__ != ((void*) &dummy_props)) {
return 0;
}
env = getenv("ANDROID_PROPERTY_WORKSPACE");
if (!env) {
return -1;
}
fd = atoi(env);
env = strchr(env, ',');
if (!env) {
return -1;
}
sz = atoi(env + 1);
pa = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
if(pa == MAP_FAILED) {
return -1;
}
if((pa->magic != PROP_AREA_MAGIC) || (pa->version != PROP_AREA_VERSION)) {
munmap(pa, sz);
return -1;
}
__system_property_area__ = pa;
return 0;
}
可以看到首先从环境变量里面获取ANDROID_PROPERTY_WORKSPACE,ANDROID_PROPERTY_WORKSPACE的值为如下形式:ANDROID_PROPERTY_WORKSPACE=9,32768。然后获取共享内存的文件句柄和长度,最后对共享内存进行检查并设置全局变量__system_property_area__的值,property_get就是从__system_property_area__里面读取的属性值。环境变量的初始化在init.c的service_start函数里面。
6. /init.rc里面有设置属性的代码,例如:
# Define the oom_adj values for the classes of processes that can be
# killed by the kernel. These are used in ActivityManagerService.
setprop ro.FOREGROUND_APP_ADJ 0
setprop ro.VISIBLE_APP_ADJ 1
setprop ro.SECONDARY_SERVER_ADJ 2
setprop ro.HOME_APP_ADJ 4
setprop ro.HIDDEN_APP_MIN_ADJ 7
setprop ro.CONTENT_PROVIDER_ADJ 14
setprop ro.EMPTY_APP_ADJ 15
7. 通过property_list可以枚举所有的属性,下面是一段打印所有属性的代码。
view plaincopy to clipboardprint?
#include
#include
void print_prop(const char* key,const char* value,void* cookie)
{
printf("key=%s,value=%s\n",key,value);
}
int main()
{
property_list(print_prop,NULL);
}
#include
#include
void print_prop(const char* key,const char* value,void* cookie)
{
printf("key=%s,value=%s\n",key,value);
}
int main()
{
property_list(print_prop,NULL);
}
Android.mk文件:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
list_property.cpp \
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
LOCAL_MODULE:= list_prop
include $(BUILD_EXECUTABLE)
include $(call all-makefiles-under,$(LOCAL_PATH))
所有的属性如下:
key=ro.secure,value=0
key=ro.allow.mock.location,value=1
key=ro.debuggable,value=1
key=persist.service.adb.enable,value=1
key=ro.kernel.qemu,value=1
key=ro.kernel.console,value=ttyS0
key=ro.kernel.android.checkjni,value=1
key=ro.kernel.android.qemud,value=ttyS1
key=ro.factorytest,value=0
key=ro.serialno,value=
key=ro.bootmode,value=unknown
key=ro.baseband,value=unknown
key=ro.carrier,value=unknown
key=ro.bootloader,value=unknown
key=ro.hardware,value=goldfish
key=ro.revision,value=0
key=ro.build.id,value=CUPCAKE
key=ro.build.display.id,value=sdk-eng 1.5 CUPCAKE 148875 test-keys
key=ro.build.version.incremental,value=148875
key=ro.build.version.sdk,value=3
key=ro.build.version.release,value=1.5
key=ro.build.date,value=Thu May 14 17:29:49 PDT 2009
key=ro.build.date.utc,value=1242347389
key=ro.build.type,value=eng
key=ro.build.user,value=android-build
key=ro.build.host,value=e-honda.mtv.corp.google.com
key=ro.build.tags,value=test-keys
key=ro.product.model,value=sdk
key=ro.product.brand,value=generic
key=ro.product.name,value=sdk
key=ro.product.device,value=generic
key=ro.product.board,value=
key=ro.product.manufacturer,value=unknown
key=ro.product.locale.language,value=en
key=ro.product.locale.region,value=US
key=ro.board.platform,value=
key=ro.build.product,value=generic
key=ro.build.description,value=sdk-eng 1.5 CUPCAKE 148875 test-keys
key=ro.build.fingerprint,value=generic/sdk/generic/:1.5/CUPCAKE/148875:eng/test-keys
key=rild.libpath,value=/system/lib/libreference-ril.so
key=rild.libargs,value=-d /dev/ttyS0
key=ro.config.notification_sound,value=F1_New_SMS.ogg
key=xmpp.auto-presence,value=true
key=ro.config.nocheckin,value=yes
key=net.bt.name,value=Android
key=net.change,value=net.gprs.local-ip
key=dalvik.vm.stack-trace-file,value=/data/anr/traces.txt
key=persist.sys.timezone,value=GMT
key=persist.sys.language,value=en
key=persist.sys.country,value=US
key=persist.sys.localevar,value=
key=ro.FOREGROUND_APP_ADJ,value=0
key=ro.VISIBLE_APP_ADJ,value=1
key=ro.SECONDARY_SERVER_ADJ,value=2
key=ro.HOME_APP_ADJ,value=4
key=ro.HIDDEN_APP_MIN_ADJ,value=7
key=ro.CONTENT_PROVIDER_ADJ,value=14
key=ro.EMPTY_APP_ADJ,value=15
key=ro.FOREGROUND_APP_MEM,value=1536
key=ro.VISIBLE_APP_MEM,value=2048
key=ro.SECONDARY_SERVER_MEM,value=4096
key=ro.HOME_APP_MEM,value=4096
key=ro.HIDDEN_APP_MEM,value=5120
key=ro.CONTENT_PROVIDER_MEM,value=5632
key=ro.EMPTY_APP_MEM,value=6144
key=net.tcp.buffersize.default,value=4096,87380,110208,4096,16384,110208
key=net.tcp.buffersize.wifi,value=4095,87380,110208,4096,16384,110208
key=net.tcp.buffersize.umts,value=4094,87380,110208,4096,16384,110208
key=net.tcp.buffersize.edge,value=4093,26280,35040,4096,16384,35040
key=net.tcp.buffersize.gprs,value=4092,8760,11680,4096,8760,11680
key=init.svc.console,value=running
key=init.svc.servicemanager,value=running
key=init.svc.vold,value=running
key=init.svc.debuggerd,value=running
key=init.svc.ril-daemon,value=running
key=init.svc.zygote,value=running
key=init.svc.media,value=running
key=init.svc.installd,value=running
key=init.svc.flash_recovery,value=stopped
key=init.svc.goldfish-setup,value=stopped
key=init.svc.qemud,value=stopped
key=init.svc.goldfish-logcat,value=stopped
key=ARGH,value=ARGH
key=net.eth0.dns1,value=10.0.2.3
key=net.gprs.local-ip,value=10.0.2.15
key=ro.radio.use-ppp,value=no
key=status.battery.state,value=Slow
key=status.battery.level,value=5
key=status.battery.level_raw,value=50
key=status.battery.level_scale,value=9
key=ro.setupwizard.mode,value=EMULATOR
key=ro.com.google.locationfeatures,value=1
key=init.svc.adbd,value=running
key=ro.qemu.init.completed,value=1
key=hw.keyboards.65536.devname,value=qwerty2
key=sys.settings_secure_version,value=2
key=dev.bootcomplete,value=1
key=sys.settings_system_version,value=6
key=gsm.sim.operator.numeric,value=
key=gsm.sim.operator.alpha,value=
key=gsm.sim.operator.iso-country,value=
key=gsm.sim.state,value=UNKNOWN
key=adb.connected,value=1