Android属性系统分析

android property system

Property system is an important feature on android. It runs as a service and manages system configurations and status. All these configurations and status are properties. A property is a key/value pair, both of which are of string type.
From the sense of function, it's very similar to windows registry. Many android applications and libraries directly or indirectly relies on this feature to determine their runtime behavior. For example, adbd process queries property service to check if it's running in emulator. Another example is the java.io.File.pathSeparator returns the value stored in property service.
How property system works
The high level architecture of property system is shown as following.
android prop sys arch
In the figure, there are three processes, a group of persistent property files and a shared memory block. The shared memory block is the container of all property records. Only the property service process can write to the shared memory block. It'll load property records from persistent the save them in the shared memory.
The consumer process loads the shared memory in its own virtual space and access properties directly. The setter process also loads the shared memory in its virtual space, but it can't write to the memory directly. When the setter tries to add or update a property, it sends the property to property service via unix domain socket. The property service will write the property to shared memory on behalf of the setter process, as well as to the persistent file.
Property service runs inside init process. The init process first creates a shared memory region and stores a fd to the region. Then init process maps the region into its virtual space with mmap with MAP_SHARED flag, as a result, any updates to this area can be seen by all processes. This fd and region size are saved in a environment variable named "ANDROID_PROPERTY_WORKSPACE". Any other processes like consumer and setter will use this environment variable to get the fd and size, so that they can mmap this region into its own virtual space. The layout of the shared memory is shown below.
androi prop mem layout
After that, init process will load properties from following files:
/default.prop
/system/build.prop
/system/default.prop
/data/local.prop

The next step is start property service. In this step, a unix domain socket server is created. This socket's pathname is "/dev/socket/property_service" which is well known to other client processes.
Finally, init process calls poll to wait for connect event on the socket.
On the consumer side, when it initializes libc(bionic/libc/bionic/libc_common.c __libc_init_common function). It will retrieve the fd and size from environment variable, and map the shared memory into its own space(bionic/libc/bionic/system_properties.c __system_properties_init function). After that, libcutils can read property just as normal memory for the consumer.
Currently, properties can't be removed. That's to say, once a property has been added, it can't be removed, neither can its key be changed.
How to get/set properties
There are three main means to get/set properies on android.
1. native code
When writing native applications, property_get and property_set APIs can be used to get/set properties. To use them, we need to include cutils/properties.h and link against libcutils.
2. java code
Android also provides System.getProperty and System.setProperty functions in java library, our java application can use them to get/set properties.

But it's important to note that although these java APIs are semantically equal to native version, java version store data in a totally different place. Actually, a hashtable is employed by dalvik VM to store properties. So, java properties are separated, it can't get or set native properties, and neither vice versa.
Update: Andrew mentioned that android.os.SystemProperties class can manipulate native properties, though it's intended for internal usage only. It calls through jni into native property library to get/set properties.
3. shell script
Android provides getprop and setprop command line tool to retrieve and update properties. They can be used in shell script. They are implemented on top of libcutils.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Launcher是Android系统中的一个应用程序,它是用户与设备交互的主要界面。它提供了桌面、应用程序列表、小部件等功能,用户可以通过它来启动应用程序、查看通知、管理设备等。 Android Launcher的源码分析主要包括以下几个方面: 1. 桌面布局:Android Launcher的桌面布局是通过GridView实现的,它可以显示应用程序图标和小部件。在源码中,可以看到GridView的相关代码,包括布局、适配器等。 2. 应用程序列表:Android Launcher的应用程序列表是通过ListView实现的,它可以显示所有安装的应用程序。在源码中,可以看到ListView的相关代码,包括布局、适配器等。 3. 搜索功能:Android Launcher提供了搜索功能,用户可以通过输入关键字来搜索应用程序、联系人等。在源码中,可以看到搜索框的相关代码,包括布局、事件处理等。 4. 桌面小部件:Android Launcher支持桌面小部件,用户可以在桌面上添加各种小部件,如天气、时钟、日历等。在源码中,可以看到小部件的相关代码,包括布局、事件处理等。 5. 动画效果:Android Launcher提供了各种动画效果,如应用程序图标的放大缩小、桌面的滑动等。在源码中,可以看到动画效果的相关代码,包括属性动画、插值器等。 总之,Android Launcher的源码分析涉及到很多方面,需要深入研究才能掌握。 ### 回答2: Android launcher是一个重要的应用程序,它是用户的桌面界面,负责管理应用程序、小部件、壁纸等的展示和操作。在Android开发中,我们可以使用默认的系统Launcher也可以自定义Launcher,下面就来分析一下Android Launcher的源码。 1. Launcher的结构分析 Launcher的展示分为三个层次:桌面、工作区和屏幕。在源码中,它们分别对应着Launcher、Workspace和CellLayout。Launcher类是整个应用程序的外壳,它管理了整个应用程序的生命周期。Workspace管理着桌面上的工作区,它可以管理多个屏幕。而CellLayout则是每个工作区上的单元格容器,用于展示应用程序和小部件的图标。 2. Launcher的主界面 Launcher的主界面一般包括一个搜索栏、一个Dock栏和一个应用程序列表。其中搜索栏和Dock栏是Launcher的核心部分,它们的实现都是需要注意的: 2.1 搜索栏 Launcher的搜索栏是通过SearchView实现的,需要处理SearchView的监听事件和搜索逻辑。其中监听事件可以根据具体需求进行定制,比如支持模糊搜索、联想搜索等。 2.2 Dock栏 Dock栏是一个固定在桌面底部的横向图标栏,它可以管理常用的应用程序和快捷方式。Dock栏的实现需要考虑如下几个问题: - 图标的布局 - 图标的拖动 - 图标的排序 - 图标的管理 3. Workspace实现分析 Workspace是Launcher的核心部分,它基本实现了所有的界面交互逻辑。在Workspace上,用户可以添加、移动、删除应用程序和小部件。Workspace的实现需要考虑如下几个问题: 3.1 桌面上的应用程序和小部件布局 Workspace上的每个屏幕都是通过CellLayout实现的,它负责管理应用程序和小部件的布局和显示。 3.2 图标的拖动和排序 用户可以通过拖动来移动和排序图标,需要考虑如下几个问题: - 拖动的实现逻辑 - 滑动过程的动画效果 - 拖动时的图标缩放效果 3.3 图标的删除和添加 用户可以通过长按图标来触发删除操作,也可以通过添加菜单来添加新的应用程序和小部件。 4. 总结 以上是针对Android Launcher源码的分析,这些内容只是基础的使用方法和思路,具体的实现还需要根据自己的需求和场景进行定制和优化。在实现Launcher的时候,需要注意性能问题,尽量避免不必要的计算和操作;同时,还需要考虑到用户的交互体验,保证操作的流畅和直观。 ### 回答3: Android Launcher是Android手机主屏幕上最常见的应用,它提供了一个桌面环境和一些关键功能,如快速访问应用程序、设置壁纸、小部件和通知中心等。本文将对Android Launcher源码进行分析,重点关注Launcher的核心组件:Workspace、Folder、App Drawer和Widget。 首先,Workspace是Launcher中最核心的组件之一,它是桌面上的容器,用于展示应用程序图标和小部件。Workspace是一个基于GridView组件自定义的ViewGroup,用于支持滑动手势和快捷方式的编辑管理。Framework层通过Workspace类读取和更新数据,它通过扩展Adapter来创建UI元素,并通过GridLayoutManager装填与交换元素。 其次,Folder是Launcher用于存储和管理应用程序快捷方式的组件。Folder的UI元素扩展自ViewGroup,可以包含多个应用快捷方式,在Folder中用户可以自由地添加、删除和排序快捷方式。Folder同时提供一个搜索框,用于帮助用户快速搜索自己的应用程序。 其次,App Drawer是Launcher的另一个核心组件,它是一个垂直滚动的列表,用于展示所有的应用程序。App Drawer是按字母排序的,同时可以通过搜索框查找用户需要的特定应用程序。App Drawer实现类似于Workspace,只不过它只横向的有一个方向轴且没有操作修改排序功能。 最后,Widget是一个独特的组件,它是一些非小部件的快捷方式,用户可以将它们放置在桌面上。Widget允许用户在主屏幕上快速访问特定功能或信息,例如天气、电子邮件和日历等。Widget UI元素的设计与Folder和App Drawer不同,它们提供更多信息和控制,通常需要桌面空间大才能放下。 总体而言,Android Launcher的源码分析需要涉及多个关键组件,Workspace、Folder、App Drawer和Widget是其中最重要的几个。这些组件深度整合在Launcher中,可以帮助用户快速访问他们需要的应用程序和信息。因此,熟练掌握这些核心组件的基本实现和逻辑是Android开发者不可或缺的技能之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值